forked from josharian/txtarfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txtarfs_test.go
74 lines (68 loc) · 1.69 KB
/
txtarfs_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
package txtarfs_test
import (
"bytes"
"fmt"
"io/fs"
"sort"
"testing"
"testing/fstest"
"github.com/josharian/txtarfs"
"golang.org/x/tools/txtar"
)
func TestBasics(t *testing.T) {
tests := []map[string]string{
nil,
{"x.txt": "hi"},
{"a/x.txt": "hi"},
{"a/x.txt, b/y.txt": "hello"},
{"a/b/c/x.txt": ""},
}
for _, tt := range tests {
ar := new(txtar.Archive)
var names []string
for name, data := range tt {
ar.Files = append(ar.Files, txtar.File{Name: name, Data: []byte(data)})
names = append(names, name)
}
sort.Slice(ar.Files, func(i, j int) bool { return ar.Files[i].Name < ar.Files[j].Name })
arfs := txtarfs.As(ar)
if err := fstest.TestFS(arfs, names...); err != nil {
t.Fatal(err)
}
for name, data := range tt {
out, err := fs.ReadFile(arfs, name)
if err != nil {
t.Errorf("fs.ReadFile(%s) = _, %v", name, err)
continue
}
if !bytes.Equal([]byte(data), out) {
t.Errorf("fs.ReadFile(%s) = %s want %s", name, out, data)
}
}
err := fs.WalkDir(arfs, ".", func(path string, d fs.DirEntry, err error) error {
fi, err := d.Info()
if err != nil {
return err
}
if mode := fi.Mode().Perm(); mode&0666 != 0666 {
return fmt.Errorf("%s has mode %v", path, mode)
}
return nil
})
if err != nil {
t.Errorf("fs.WalkDir(...) = %v", err)
continue
}
ar2, err := txtarfs.From(arfs)
if err != nil {
t.Errorf("failed to write fsys for %v: %v", tt, err)
continue
}
sort.Slice(ar2.Files, func(i, j int) bool { return ar2.Files[i].Name < ar2.Files[j].Name })
in := string(txtar.Format(ar))
out := string(txtar.Format(ar2))
if in != out {
t.Errorf("As/From round trip failed: %v != %v", in, out)
}
}
}