forked from bittorrent/go-btfs-files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
126 lines (114 loc) · 2.33 KB
/
helpers_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package files
import (
"io"
"testing"
)
type Kind int
const (
TFile Kind = iota
TSymlink
TDirStart
TDirEnd
)
type Event struct {
kind Kind
name string
value string
}
func CheckDir(t *testing.T, dir Directory, expected []Event) {
expectedIndex := 0
expect := func() (Event, int) {
t.Helper()
if expectedIndex > len(expected) {
t.Fatal("no more expected entries")
}
i := expectedIndex
expectedIndex++
// Add an implicit "end" event at the end. It makes this
// function a bit easier to write.
next := Event{kind: TDirEnd}
if i < len(expected) {
next = expected[i]
}
return next, i
}
var check func(d Directory)
check = func(d Directory) {
it := d.Entries()
for it.Next() {
next, i := expect()
if it.Name() != next.name {
t.Fatalf("[%d] expected filename to be %q", i, next.name)
}
switch next.kind {
case TFile:
mf, ok := it.Node().(File)
if !ok {
t.Fatalf("[%d] expected file to be a normal file: %T", i, it.Node())
}
out, err := io.ReadAll(mf)
if err != nil {
t.Errorf("[%d] failed to read file", i)
continue
}
if string(out) != next.value {
t.Errorf(
"[%d] while reading %q, expected %q, got %q",
i,
it.Name(),
next.value,
string(out),
)
continue
}
case TSymlink:
mf, ok := it.Node().(*Symlink)
if !ok {
t.Errorf("[%d] expected file to be a symlink: %T", i, it.Node())
continue
}
if mf.Target != next.value {
t.Errorf(
"[%d] target of symlink %q should have been %q but was %q",
i,
it.Name(),
next.value,
mf.Target,
)
continue
}
case TDirStart:
mf, ok := it.Node().(Directory)
if !ok {
t.Fatalf(
"[%d] expected file to be a directory: %T",
i,
it.Node(),
)
}
check(mf)
case TDirEnd:
t.Errorf(
"[%d] expected end of directory, found %#v at %q",
i,
it.Node(),
it.Name(),
)
return
default:
t.Fatal("unhandled type", next.kind)
}
if err := it.Node().Close(); err != nil {
t.Fatalf("[%d] expected to be able to close node", i)
}
}
next, i := expect()
if it.Err() != nil {
t.Fatalf("[%d] got error: %s", i, it.Err())
}
if next.kind != TDirEnd {
t.Fatalf("[%d] found end of directory, expected %#v", i, next)
}
}
check(dir)
}