/
helpers_test.go
437 lines (393 loc) · 8.86 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package fsnotify
import (
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
"sync"
"testing"
"time"
)
type testCase struct {
name string
ops func(t *testing.T, w *Watcher, tempDir string)
want string
}
func (tt *testCase) run(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
tmp := t.TempDir()
w := newCollector(t)
w.collect(t)
tt.ops(t, w.w, tmp)
cmpEvents(t, tmp, w.stop(t), newEvents(t, tt.want))
})
}
// We wait a little bit after most commands; gives the system some time to sync
// things and makes things more consistent across platforms.
func eventSeparator() { time.Sleep(50 * time.Millisecond) }
func waitForEvents() { time.Sleep(500 * time.Millisecond) }
// newWatcher initializes an fsnotify Watcher instance.
func newWatcher(t *testing.T, add ...string) *Watcher {
t.Helper()
w, err := NewWatcher()
if err != nil {
t.Fatalf("newWatcher: %s", err)
}
for _, a := range add {
err := w.Add(a)
if err != nil {
t.Fatalf("newWatcher: add %q: %s", a, err)
}
}
return w
}
// addWatch adds a watch for a directory
func addWatch(t *testing.T, watcher *Watcher, path ...string) {
t.Helper()
if len(path) < 1 {
t.Fatalf("addWatch: path must have at least one element: %s", path)
}
err := watcher.Add(filepath.Join(path...))
if err != nil {
t.Fatalf("addWatch(%q): %s", filepath.Join(path...), err)
}
}
const noWait = ""
func shouldWait(path ...string) bool {
// Take advantage of the fact that filepath.Join skips empty parameters.
for _, p := range path {
if p == "" {
return false
}
}
return true
}
// mkdir
func mkdir(t *testing.T, path ...string) {
t.Helper()
if len(path) < 1 {
t.Fatalf("mkdir: path must have at least one element: %s", path)
}
err := os.Mkdir(filepath.Join(path...), 0o0755)
if err != nil {
t.Fatalf("mkdir(%q): %s", filepath.Join(path...), err)
}
if shouldWait(path...) {
eventSeparator()
}
}
// mkdir -p
// func mkdirAll(t *testing.T, path ...string) {
// t.Helper()
// if len(path) < 1 {
// t.Fatalf("mkdirAll: path must have at least one element: %s", path)
// }
// err := os.MkdirAll(filepath.Join(path...), 0o0755)
// if err != nil {
// t.Fatalf("mkdirAll(%q): %s", filepath.Join(path...), err)
// }
// if shouldWait(path...) {
// eventSeparator()
// }
// }
// ln -s
func symlink(t *testing.T, target string, link ...string) {
t.Helper()
if len(link) < 1 {
t.Fatalf("symlink: link must have at least one element: %s", link)
}
err := os.Symlink(target, filepath.Join(link...))
if err != nil {
t.Fatalf("symlink(%q, %q): %s", target, filepath.Join(link...), err)
}
if shouldWait(link...) {
eventSeparator()
}
}
// cat
func cat(t *testing.T, data string, path ...string) {
t.Helper()
if len(path) < 1 {
t.Fatalf("cat: path must have at least one element: %s", path)
}
err := func() error {
fp, err := os.OpenFile(filepath.Join(path...), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
if err := fp.Sync(); err != nil {
return err
}
if shouldWait(path...) {
eventSeparator()
}
if _, err := fp.WriteString(data); err != nil {
return err
}
if err := fp.Sync(); err != nil {
return err
}
if shouldWait(path...) {
eventSeparator()
}
return fp.Close()
}()
if err != nil {
t.Fatalf("cat(%q): %s", filepath.Join(path...), err)
}
}
// touch
func touch(t *testing.T, path ...string) {
t.Helper()
if len(path) < 1 {
t.Fatalf("touch: path must have at least one element: %s", path)
}
fp, err := os.Create(filepath.Join(path...))
if err != nil {
t.Fatalf("touch(%q): %s", filepath.Join(path...), err)
}
err = fp.Close()
if err != nil {
t.Fatalf("touch(%q): %s", filepath.Join(path...), err)
}
if shouldWait(path...) {
eventSeparator()
}
}
// mv
func mv(t *testing.T, src string, dst ...string) {
t.Helper()
if len(dst) < 1 {
t.Fatalf("mv: dst must have at least one element: %s", dst)
}
var err error
switch runtime.GOOS {
case "windows", "plan9":
err = os.Rename(src, filepath.Join(dst...))
default:
err = exec.Command("mv", src, filepath.Join(dst...)).Run()
}
if err != nil {
t.Fatalf("mv(%q, %q): %s", src, filepath.Join(dst...), err)
}
if shouldWait(dst...) {
eventSeparator()
}
}
// rm
func rm(t *testing.T, path ...string) {
t.Helper()
if len(path) < 1 {
t.Fatalf("rm: path must have at least one element: %s", path)
}
err := os.Remove(filepath.Join(path...))
if err != nil {
t.Fatalf("rm(%q): %s", filepath.Join(path...), err)
}
if shouldWait(path...) {
eventSeparator()
}
}
// rm -r
func rmAll(t *testing.T, path ...string) {
t.Helper()
if len(path) < 1 {
t.Fatalf("rmAll: path must have at least one element: %s", path)
}
err := os.RemoveAll(filepath.Join(path...))
if err != nil {
t.Fatalf("rmAll(%q): %s", filepath.Join(path...), err)
}
if shouldWait(path...) {
eventSeparator()
}
}
// chmod
func chmod(t *testing.T, mode fs.FileMode, path ...string) {
t.Helper()
if len(path) < 1 {
t.Fatalf("chmod: path must have at least one element: %s", path)
}
err := os.Chmod(filepath.Join(path...), mode)
if err != nil {
t.Fatalf("chmod(%q): %s", filepath.Join(path...), err)
}
if shouldWait(path...) {
eventSeparator()
}
}
// Collect all events in an array.
//
// w := newCollector(t)
// w.collect(r)
//
// .. do stuff ..
//
// events := w.stop(t)
type eventCollector struct {
w *Watcher
events Events
mu sync.Mutex
done chan struct{}
}
func newCollector(t *testing.T) *eventCollector {
return &eventCollector{w: newWatcher(t), done: make(chan struct{})}
}
func (w *eventCollector) stop(t *testing.T) Events {
waitForEvents()
go func() {
err := w.w.Close()
if err != nil {
t.Error(err)
}
}()
select {
case <-time.After(1 * time.Second):
t.Fatal("event stream was not closed after 1 second")
case <-w.done:
}
w.mu.Lock()
defer w.mu.Unlock()
return w.events
}
func (w *eventCollector) collect(t *testing.T) {
go func() {
for {
select {
case e, ok := <-w.w.Errors:
if !ok {
w.done <- struct{}{}
return
}
t.Error(e)
return
case e, ok := <-w.w.Events:
if !ok {
w.done <- struct{}{}
return
}
w.mu.Lock()
w.events = append(w.events, e)
w.mu.Unlock()
}
}
}()
}
type Events []Event
func (e Events) String() string {
b := new(strings.Builder)
for i, ee := range e {
if i > 0 {
b.WriteString("\n")
}
fmt.Fprintf(b, "%-20s %q", ee.Op.String(), filepath.ToSlash(ee.Name))
}
return b.String()
}
func (e Events) TrimPrefix(prefix string) Events {
for i := range e {
if e[i].Name == prefix {
e[i].Name = "/"
} else {
e[i].Name = strings.TrimPrefix(e[i].Name, prefix)
}
}
return e
}
func (e Events) copy() Events {
cp := make(Events, len(e))
copy(cp, e)
return cp
}
// Create a new Events list from a string; for example:
//
// CREATE path
// CREATE|WRITE path
//
// Every event is one line, and any whitespace between the event and path are
// ignored. The path can optionally be surrounded in ". Anything after a "#" is
// ignored.
//
// Platform-specific tests can be added after GOOS:
//
// # Tested if nothing else matches
// CREATE path
//
// # Windows-specific test.
// windows:
// WRITE path
func newEvents(t *testing.T, s string) Events {
t.Helper()
var (
lines = strings.Split(s, "\n")
group string
events = make(map[string]Events)
)
for no, line := range lines {
if i := strings.IndexByte(line, '#'); i > -1 {
line = line[:i]
}
line = strings.TrimSpace(line)
if line == "" {
continue
}
if strings.HasSuffix(line, ":") {
group = strings.TrimRight(line, ":")
continue
}
fields := strings.Fields(line)
if len(fields) < 2 {
t.Fatalf("newEvents: line %d has less than 2 fields: %s", no, line)
}
path := strings.Trim(fields[len(fields)-1], `"`)
var op Op
for _, e := range fields[:len(fields)-1] {
if e == "|" {
continue
}
for _, ee := range strings.Split(e, "|") {
switch strings.ToUpper(ee) {
case "CREATE":
op |= Create
case "WRITE":
op |= Write
case "REMOVE":
op |= Remove
case "RENAME":
op |= Rename
case "CHMOD":
op |= Chmod
default:
t.Fatalf("newEvents: line %d has unknown event %q: %s", no, ee, line)
}
}
}
events[group] = append(events[group], Event{Name: path, Op: op})
}
if e, ok := events[runtime.GOOS]; ok {
return e
}
return events[""]
}
func cmpEvents(t *testing.T, tmp string, have, want Events) {
t.Helper()
have = have.TrimPrefix(tmp)
haveSort, wantSort := have.copy(), want.copy()
sort.Slice(haveSort, func(i, j int) bool {
return haveSort[i].String() > haveSort[j].String()
})
sort.Slice(wantSort, func(i, j int) bool {
return wantSort[i].String() > wantSort[j].String()
})
if haveSort.String() != wantSort.String() {
t.Errorf("\nhave:\n%s\nwant:\n%s", indent(have), indent(want))
}
}
func indent(s fmt.Stringer) string {
return "\t" + strings.ReplaceAll(s.String(), "\n", "\n\t")
}