-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrite_test.go
101 lines (91 loc) · 2.02 KB
/
write_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package mylogin_test
import (
"bytes"
"fmt"
"io"
"os"
"sort"
"strings"
"testing"
"github.com/dolmen-go/mylogin"
)
type fileInfoByName []os.FileInfo
func (s fileInfoByName) Len() int { return len(s) }
func (s fileInfoByName) Less(i, j int) bool { return s[i].Name() < s[j].Name() }
func (s fileInfoByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func iterDir(path string, filter func(os.FileInfo) bool) (chan string, error) {
if filter == nil {
filter = func(os.FileInfo) bool { return true }
}
dir, err := os.Open(path)
if err != nil {
return nil, err
}
defer dir.Close()
files, err := dir.Readdir(-1)
if err != nil {
return nil, err
}
c := make(chan string)
go func() {
sort.Sort(fileInfoByName(files))
// Ignore write on closed channel
defer func() {
// FIXME
_ = recover()
}()
for _, fileinfo := range files {
if !filter(fileinfo) {
continue
}
c <- fmt.Sprintf("%s%c%s", path, os.PathSeparator, fileinfo.Name())
}
close(c)
}()
return c, nil
}
func TestReadWrite(t *testing.T) {
files, err := iterDir("testdata", func(f os.FileInfo) bool {
return f.Mode().IsRegular() && strings.HasSuffix(f.Name(), ".cnf")
})
if err != nil {
t.Fatal(err)
}
var orig bytes.Buffer
var out bytes.Buffer
for path := range files {
t.Logf(path)
f, err := os.Open(path)
if err != nil {
t.Errorf("%s: %s", path, err)
continue
}
func() {
defer f.Close()
orig.Reset()
out.Reset()
io.Copy(&orig, f)
origBytes := orig.Bytes()
content, err := mylogin.Decode(bytes.NewBuffer(orig.Bytes()))
if err != nil {
t.Errorf("%s: %s", path, err)
return
}
err = mylogin.Encode(&out, content)
if err != nil {
t.Errorf("%s: %s", path, err)
return
}
outBytes := out.Bytes()
if bytes.Equal(origBytes, outBytes) {
t.Logf("%s: OK", path)
return
}
t.Errorf("%s: content differ", path)
if len(outBytes) != len(origBytes) {
t.Logf("orig: %d bytes", len(origBytes))
t.Logf("out: %d bytes", len(outBytes))
}
}()
}
}