-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwalk_test.go
203 lines (166 loc) · 3.35 KB
/
walk_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// walk_test.go -- test harness for walk.go
package walk
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
)
func newAsserter(t *testing.T) func(cond bool, msg string, args ...interface{}) {
return func(cond bool, msg string, args ...interface{}) {
if cond {
return
}
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "???"
line = 0
}
s := fmt.Sprintf(msg, args...)
t.Fatalf("%s: %d: Assertion failed: %s\n", file, line, s)
}
}
type test struct {
dir string
typ Type
}
var tests = []test{
{"$HOME/.config", FILE | SYMLINK},
}
var linuxTests = []test{
{"/dev", ALL},
{"/dev", DEVICE},
{"/dev", SPECIAL},
{"/dev", DIR | SYMLINK},
{"/lib", FILE | SYMLINK},
{"/lib", SYMLINK},
{"/bin", FILE},
}
var macOSTests = []test{
{"/etc", ALL},
{"/bin", FILE},
{"$HOME/Library", ALL},
{"$HOME/Library", FILE | SYMLINK},
}
func newWalk(tx *test) (map[string]fs.FileInfo, error) {
nm := os.ExpandEnv(tx.dir)
names := [...]string{nm}
opt := &Options{
FollowSymlinks: false,
OneFS: false,
Type: tx.typ,
}
res := make(map[string]fs.FileInfo)
och, ech := Walk(names[:], opt)
var wg sync.WaitGroup
wg.Add(1)
var errs []error
go func() {
for e := range ech {
errs = append(errs, e)
}
wg.Done()
}()
for o := range och {
res[o.Path] = o.Stat
}
wg.Wait()
if len(errs) > 0 {
return res, errors.Join(errs...)
}
return res, nil
}
func oldWalk(tx *test) (map[string]fs.FileInfo, error) {
var m os.FileMode
ty := tx.typ
for k, v := range typMap {
if (k & ty) > 0 {
m |= v
}
}
predicate := func(mode fs.FileMode) bool {
if (m&mode) > 0 || ((ty&FILE) > 0 && mode.IsRegular()) {
return true
}
return false
}
res := make(map[string]fs.FileInfo)
var errs []error
nm := os.ExpandEnv(tx.dir)
err := filepath.WalkDir(nm, func(p string, di fs.DirEntry, e error) error {
if e != nil {
errs = append(errs, e)
return nil
}
if !predicate(di.Type()) {
return nil
}
// we're interested in this entry
fi, err := di.Info()
if err != nil {
errs = append(errs, err)
} else {
res[p] = fi
}
return nil
})
if err != nil {
errs = append(errs, err)
}
if len(errs) > 0 {
return res, errors.Join(errs...)
}
return res, nil
}
func TestWalk(t *testing.T) {
switch runtime.GOOS {
case "linux":
tests = append(tests, linuxTests...)
case "darwin":
tests = append(tests, macOSTests...)
}
for i := range tests {
tx := &tests[i]
t.Run(tx.dir, func(t *testing.T) {
t.Parallel()
assert := newAsserter(t)
var wg sync.WaitGroup
var r1, r2 map[string]fs.FileInfo
var e1, e2 error
wg.Add(2)
go func(tx *test) {
r2, e2 = newWalk(tx)
wg.Done()
}(tx)
go func(tx *test) {
r1, e1 = oldWalk(tx)
wg.Done()
}(tx)
wg.Wait()
assert(e2 == nil, "%d: Errors new-walk %s:\n%s\n",
i, tx.dir, e2)
assert(e1 == nil, "%d: Errors old-walk %s:\n%s\n",
i, tx.dir, e1)
for k := range r1 {
_, ok := r2[k]
assert(ok, "%d %s: can't find %s in new walk", i, tx.dir, k)
delete(r2, k)
}
// now we know that everything the stdlib.Walk found is also present
// in our concurrent-walker.
if len(r2) > 0 {
var rem []string
for k := range r2 {
rem = append(rem, k)
}
t.Fatalf("new walk has extra entries:\n%s\n",
strings.Join(rem, "\n"))
}
})
}
}