forked from fsnotify/fsnotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsnotify_test.go
1739 lines (1514 loc) · 38.3 KB
/
fsnotify_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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package fsnotify
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"reflect"
"runtime"
"sort"
"strings"
"sync"
"sync/atomic"
"syscall"
"testing"
"time"
"github.com/fsnotify/fsnotify/internal"
)
// Set soft open file limit to the maximum; on e.g. OpenBSD it's 512/1024.
//
// Go 1.19 will always do this when the os package is imported.
//
// https://go-review.googlesource.com/c/go/+/393354/
func init() {
internal.SetRlimit()
}
func TestWatch(t *testing.T) {
tests := []testCase{
{"multiple creates", func(t *testing.T, w *Watcher, tmp string) {
file := join(tmp, "file")
addWatch(t, w, tmp)
cat(t, "data", file)
rm(t, file)
touch(t, file) // Recreate the file
cat(t, "data", file) // Modify
cat(t, "data", file) // Modify
}, `
create /file
write /file
remove /file
create /file
write /file
write /file
`},
{"dir only", func(t *testing.T, w *Watcher, tmp string) {
beforeWatch := join(tmp, "beforewatch")
file := join(tmp, "file")
touch(t, beforeWatch)
addWatch(t, w, tmp)
cat(t, "data", file)
rm(t, file)
rm(t, beforeWatch)
}, `
create /file
write /file
remove /file
remove /beforewatch
`},
{"subdir", func(t *testing.T, w *Watcher, tmp string) {
addWatch(t, w, tmp)
file := join(tmp, "file")
dir := join(tmp, "sub")
dirfile := join(tmp, "sub/file2")
mkdir(t, dir) // Create sub-directory
touch(t, file) // Create a file
touch(t, dirfile) // Create a file (Should not see this! we are not watching subdir)
time.Sleep(200 * time.Millisecond)
rmAll(t, dir) // Make sure receive deletes for both file and sub-directory
rm(t, file)
}, `
create /sub
create /file
remove /sub
remove /file
# TODO: not sure why the REMOVE /sub is dropped.
dragonfly:
create /sub
create /file
remove /file
fen:
create /sub
create /file
write /sub
remove /sub
remove /file
# Windows includes a write for the /sub dir too, two of them even(?)
windows:
create /sub
create /file
write /sub
write /sub
remove /sub
remove /file
`},
{"file in directory is not readable", func(t *testing.T, w *Watcher, tmp string) {
if runtime.GOOS == "windows" {
t.Skip("attributes don't work on Windows") // TODO: figure out how to make a file unreadable
}
touch(t, tmp, "file-unreadable")
chmod(t, 0, tmp, "file-unreadable")
touch(t, tmp, "file")
addWatch(t, w, tmp)
cat(t, "hello", tmp, "file")
rm(t, tmp, "file")
rm(t, tmp, "file-unreadable")
}, `
WRITE "/file"
REMOVE "/file"
REMOVE "/file-unreadable"
# We never set up a watcher on the unreadable file, so we don't get
# the REMOVE.
kqueue:
WRITE "/file"
REMOVE "/file"
windows:
empty
`},
{"watch same dir twice", func(t *testing.T, w *Watcher, tmp string) {
addWatch(t, w, tmp)
addWatch(t, w, tmp)
touch(t, tmp, "file")
cat(t, "hello", tmp, "file")
rm(t, tmp, "file")
mkdir(t, tmp, "dir")
}, `
create /file
write /file
remove /file
create /dir
`},
{"watch same file twice", func(t *testing.T, w *Watcher, tmp string) {
file := join(tmp, "file")
touch(t, file)
addWatch(t, w, file)
addWatch(t, w, file)
cat(t, "hello", tmp, "file")
}, `
write /file
`},
}
for _, tt := range tests {
tt := tt
tt.run(t)
}
}
func TestWatchCreate(t *testing.T) {
tests := []testCase{
// Files
{"create empty file", func(t *testing.T, w *Watcher, tmp string) {
addWatch(t, w, tmp)
touch(t, tmp, "file")
}, `
create /file
`},
{"create file with data", func(t *testing.T, w *Watcher, tmp string) {
addWatch(t, w, tmp)
cat(t, "data", tmp, "file")
}, `
create /file
write /file
`},
// Directories
{"create new directory", func(t *testing.T, w *Watcher, tmp string) {
addWatch(t, w, tmp)
mkdir(t, tmp, "dir")
}, `
create /dir
`},
// Links
{"create new symlink to file", func(t *testing.T, w *Watcher, tmp string) {
if !internal.HasPrivilegesForSymlink() {
t.Skip("does not have privileges for symlink on this OS")
}
touch(t, tmp, "file")
addWatch(t, w, tmp)
symlink(t, join(tmp, "file"), tmp, "link")
}, `
create /link
`},
{"create new symlink to directory", func(t *testing.T, w *Watcher, tmp string) {
if !internal.HasPrivilegesForSymlink() {
t.Skip("does not have privileges for symlink on this OS")
}
addWatch(t, w, tmp)
symlink(t, tmp, tmp, "link")
}, `
create /link
`},
// FIFO
{"create new named pipe", func(t *testing.T, w *Watcher, tmp string) {
if runtime.GOOS == "windows" {
t.Skip("No named pipes on Windows")
}
touch(t, tmp, "file")
addWatch(t, w, tmp)
mkfifo(t, tmp, "fifo")
}, `
create /fifo
`},
// Device node
{"create new device node pipe", func(t *testing.T, w *Watcher, tmp string) {
if runtime.GOOS == "windows" {
t.Skip("No device nodes on Windows")
}
if isKqueue() {
// Don't want to use os/user to check uid, since that pulls in
// cgo by default and stuff that uses fsnotify won't be
// statically linked by default.
t.Skip("needs root on BSD")
}
if isSolaris() {
t.Skip(`"mknod fails with "not owner"`)
}
touch(t, tmp, "file")
addWatch(t, w, tmp)
mknod(t, 0, tmp, "dev")
}, `
create /dev
`},
}
for _, tt := range tests {
tt := tt
tt.run(t)
}
}
func TestWatchWrite(t *testing.T) {
tests := []testCase{
// Files
{"truncate file", func(t *testing.T, w *Watcher, tmp string) {
file := join(tmp, "file")
cat(t, "data", file)
addWatch(t, w, tmp)
fp, err := os.Create(file)
if err != nil {
t.Fatal(err)
}
if err := fp.Sync(); err != nil {
t.Fatal(err)
}
eventSeparator()
if _, err := fp.Write([]byte("X")); err != nil {
t.Fatal(err)
}
if err := fp.Close(); err != nil {
t.Fatal(err)
}
}, `
write /file # truncate
write /file # write
# Truncate is chmod on kqueue, except NetBSD
netbsd:
write /file
kqueue:
chmod /file
write /file
`},
{"multiple writes to a file", func(t *testing.T, w *Watcher, tmp string) {
file := join(tmp, "file")
cat(t, "data", file)
addWatch(t, w, tmp)
fp, err := os.OpenFile(file, os.O_RDWR, 0)
if err != nil {
t.Fatal(err)
}
if _, err := fp.Write([]byte("X")); err != nil {
t.Fatal(err)
}
if err := fp.Sync(); err != nil {
t.Fatal(err)
}
eventSeparator()
if _, err := fp.Write([]byte("Y")); err != nil {
t.Fatal(err)
}
if err := fp.Close(); err != nil {
t.Fatal(err)
}
}, `
write /file # write X
write /file # write Y
`},
}
for _, tt := range tests {
tt := tt
tt.run(t)
}
}
func TestWatchRename(t *testing.T) {
tests := []testCase{
{"rename file in watched dir", func(t *testing.T, w *Watcher, tmp string) {
file := join(tmp, "file")
cat(t, "asd", file)
addWatch(t, w, tmp)
mv(t, file, tmp, "renamed")
}, `
rename /file
create /renamed
`},
{"rename from unwatched dir", func(t *testing.T, w *Watcher, tmp string) {
unwatched := t.TempDir()
addWatch(t, w, tmp)
touch(t, unwatched, "file")
mv(t, join(unwatched, "file"), tmp, "file")
}, `
create /file
`},
{"rename to unwatched dir", func(t *testing.T, w *Watcher, tmp string) {
if runtime.GOOS == "netbsd" && isCI() {
t.Skip("fails in CI; see #488") // TODO
}
unwatched := t.TempDir()
file := join(tmp, "file")
renamed := join(unwatched, "renamed")
addWatch(t, w, tmp)
cat(t, "data", file)
mv(t, file, renamed)
cat(t, "data", renamed) // Modify the file outside of the watched dir
touch(t, file) // Recreate the file that was moved
}, `
create /file # cat data >file
write /file # ^
rename /file # mv file ../renamed
create /file # touch file
# Windows has REMOVE /file, rather than CREATE /file
windows:
create /file
write /file
remove /file
create /file
`},
{"rename overwriting existing file", func(t *testing.T, w *Watcher, tmp string) {
unwatched := t.TempDir()
file := join(unwatched, "file")
touch(t, tmp, "renamed")
touch(t, file)
addWatch(t, w, tmp)
mv(t, file, tmp, "renamed")
}, `
# TODO: this should really be RENAME.
remove /renamed
create /renamed
# No remove event for inotify; inotify just sends MOVE_SELF.
linux:
create /renamed
# TODO: this is broken.
dragonfly:
REMOVE "/"
`},
{"rename watched directory", func(t *testing.T, w *Watcher, tmp string) {
dir := join(tmp, "dir")
mkdir(t, dir)
addWatch(t, w, dir)
mv(t, dir, tmp, "dir-renamed")
touch(t, tmp, "dir-renamed/file")
}, `
rename /dir
# TODO(v2): Windows should behave the same by default. See #518
windows:
create /dir/file
`},
{"rename watched file", func(t *testing.T, w *Watcher, tmp string) {
file := join(tmp, "file")
rename := join(tmp, "rename-one")
touch(t, file)
addWatch(t, w, file)
mv(t, file, rename)
mv(t, rename, tmp, "rename-two")
}, `
rename /file
# TODO(v2): Windows should behave the same by default. See #518
windows:
rename /file
rename /rename-one
`},
{"re-add renamed file", func(t *testing.T, w *Watcher, tmp string) {
file := join(tmp, "file")
rename := join(tmp, "rename")
touch(t, file)
addWatch(t, w, file)
mv(t, file, rename)
touch(t, file)
addWatch(t, w, file)
cat(t, "hello", rename)
cat(t, "hello", file)
}, `
rename /file # mv file rename
# Watcher gets removed on rename, so no write for /rename
write /file # cat hello >file
# TODO(v2): Windows should behave the same by default. See #518
windows:
rename /file
write /rename
write /file
`},
}
for _, tt := range tests {
tt := tt
tt.run(t)
}
}
func TestWatchSymlink(t *testing.T) {
if !internal.HasPrivilegesForSymlink() {
t.Skip("does not have privileges for symlink on this OS")
}
tests := []testCase{
{"watch a symlink to a file", func(t *testing.T, w *Watcher, tmp string) {
file := join(tmp, "file")
link := join(tmp, "link")
touch(t, file)
symlink(t, file, link)
addWatch(t, w, link)
cat(t, "hello", file)
}, `
write /link
# TODO: Symlinks followed on kqueue; it shouldn't do this, but I'm
# afraid changing it will break stuff. See #227, #390
kqueue:
write /file
# TODO: see if we can fix this.
windows:
empty
`},
{"watch a symlink to a dir", func(t *testing.T, w *Watcher, tmp string) {
dir := join(tmp, "dir")
link := join(tmp, "link")
mkdir(t, dir)
symlink(t, dir, link)
addWatch(t, w, link)
touch(t, dir, "file")
}, `
create /link/file
# TODO: Symlinks followed on kqueue; it shouldn't do this, but I'm
# afraid changing it will break stuff. See #227, #390
kqueue:
create /dir/file
`},
{"create unresolvable symlink", func(t *testing.T, w *Watcher, tmp string) {
addWatch(t, w, tmp)
symlink(t, join(tmp, "target"), tmp, "link")
}, `
create /link
# No events at all on Dragonfly
# TODO: should fix this.
dragonfly:
empty
`},
{"cyclic symlink", func(t *testing.T, w *Watcher, tmp string) {
symlink(t, ".", tmp, "link")
addWatch(t, w, tmp)
rm(t, tmp, "link")
cat(t, "foo", tmp, "link")
}, `
write /link
create /link
linux, windows, fen:
remove /link
create /link
write /link
`},
// Bug #277
{"277", func(t *testing.T, w *Watcher, tmp string) {
if runtime.GOOS == "netbsd" && isCI() {
t.Skip("fails in CI") // TODO
}
// TODO: there is some strange fuckery going on if I use go test
// -count=2; the second test run has unix.Kqueue() in newKqueue()
// return 0, which is a very odd fd number, but the first event does
// work (create /foo). After that we get EBADF (Bad file
// descriptor).
//
// This is *only* for this test, and *only* if we have the symlinks
// below. kqueue(2) doesn't document returning fd 0.
//
// This happens on all the BSDs, but *not* macOS.
touch(t, tmp, "file1")
touch(t, tmp, "file2")
symlink(t, join(tmp, "file1"), tmp, "link1")
symlink(t, join(tmp, "file2"), tmp, "link2")
addWatch(t, w, tmp)
touch(t, tmp, "foo")
rm(t, tmp, "foo")
mkdir(t, tmp, "apple")
mv(t, join(tmp, "apple"), tmp, "pear")
rmAll(t, tmp, "pear")
}, `
create /foo # touch foo
remove /foo # rm foo
create /apple # mkdir apple
rename /apple # mv apple pear
create /pear
remove /pear # rm -r pear
# TODO: the CREATE/REMOVE for /pear don't show; I'm not entirely
# sure why; sendDirectoryChangeEvents() doesn't pick it up. It does
# seem consistent though, both locally and in CI.
freebsd, netbsd, dragonfly:
create /foo # touch foo
remove /foo # rm foo
create /apple # mkdir apple
rename /apple # mv apple pear
`},
}
for _, tt := range tests {
tt := tt
tt.run(t)
}
}
func TestWatchAttrib(t *testing.T) {
tests := []testCase{
{"chmod", func(t *testing.T, w *Watcher, tmp string) {
file := join(tmp, "file")
cat(t, "data", file)
addWatch(t, w, file)
chmod(t, 0o700, file)
}, `
CHMOD "/file"
windows:
empty
`},
{"write does not trigger CHMOD", func(t *testing.T, w *Watcher, tmp string) {
file := join(tmp, "file")
cat(t, "data", file)
addWatch(t, w, file)
chmod(t, 0o700, file)
cat(t, "more data", file)
}, `
CHMOD "/file"
WRITE "/file"
windows:
write /file
`},
{"chmod after write", func(t *testing.T, w *Watcher, tmp string) {
file := join(tmp, "file")
cat(t, "data", file)
addWatch(t, w, file)
chmod(t, 0o700, file)
cat(t, "more data", file)
chmod(t, 0o600, file)
}, `
CHMOD "/file"
WRITE "/file"
CHMOD "/file"
windows:
write /file
`},
}
for _, tt := range tests {
tt := tt
tt.run(t)
}
}
func TestWatchRemove(t *testing.T) {
tests := []testCase{
{"remove watched file", func(t *testing.T, w *Watcher, tmp string) {
file := join(tmp, "file")
touch(t, file)
addWatch(t, w, file)
rm(t, file)
}, `
REMOVE "/file"
# unlink always emits a CHMOD on Linux.
linux:
CHMOD "/file"
REMOVE "/file"
`},
{"remove watched file with open fd", func(t *testing.T, w *Watcher, tmp string) {
if runtime.GOOS == "windows" {
t.Skip("Windows hard-locks open files so this will never work")
}
file := join(tmp, "file")
touch(t, file)
// Intentionally don't close the descriptor here so it stays around.
_, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
addWatch(t, w, file)
rm(t, file)
}, `
REMOVE "/file"
# inotify will just emit a CHMOD for the unlink, but won't actually
# emit a REMOVE until the descriptor is closed. Bit odd, but not much
# we can do about it. The REMOVE is tested in TestInotifyDeleteOpenFile()
linux:
CHMOD "/file"
`},
{"remove recursive", func(t *testing.T, w *Watcher, tmp string) {
recurseOnly(t)
mkdirAll(t, tmp, "dir1", "subdir")
mkdirAll(t, tmp, "dir2", "subdir")
touch(t, tmp, "dir1", "subdir", "file")
touch(t, tmp, "dir2", "subdir", "file")
addWatch(t, w, tmp, "dir1", "...")
addWatch(t, w, tmp, "dir2", "...")
cat(t, "asd", tmp, "dir1", "subdir", "file")
cat(t, "asd", tmp, "dir2", "subdir", "file")
if err := w.Remove(join(tmp, "dir1")); err != nil {
t.Fatal(err)
}
if err := w.Remove(join(tmp, "dir2", "...")); err != nil {
t.Fatal(err)
}
if w := w.WatchList(); len(w) != 0 {
t.Errorf("WatchList not empty: %s", w)
}
cat(t, "asd", tmp, "dir1", "subdir", "file")
cat(t, "asd", tmp, "dir2", "subdir", "file")
}, `
write /dir1/subdir
write /dir1/subdir/file
write /dir2/subdir
write /dir2/subdir/file
`},
}
for _, tt := range tests {
tt := tt
tt.run(t)
}
t.Run("remove watched directory", func(t *testing.T) {
t.Parallel()
tmp := t.TempDir()
w := newCollector(t)
w.collect(t)
touch(t, tmp, "a")
touch(t, tmp, "b")
touch(t, tmp, "c")
touch(t, tmp, "d")
touch(t, tmp, "e")
touch(t, tmp, "f")
touch(t, tmp, "g")
mkdir(t, tmp, "h")
mkdir(t, tmp, "h", "a")
mkdir(t, tmp, "i")
mkdir(t, tmp, "i", "a")
mkdir(t, tmp, "j")
mkdir(t, tmp, "j", "a")
addWatch(t, w.w, tmp)
rmAll(t, tmp)
if runtime.GOOS != "windows" {
cmpEvents(t, tmp, w.stop(t), newEvents(t, `
remove /
remove /a
remove /b
remove /c
remove /d
remove /e
remove /f
remove /g
remove /h
remove /i
remove /j`))
return
}
// ReadDirectoryChangesW gives undefined results: not all files are
// always present. So test only that 1) we got the directory itself, and
// 2) we don't get events for unspected files.
var (
events = w.stop(t)
found bool
)
for _, e := range events {
if e.Name == tmp && e.Has(Remove) {
found = true
continue
}
if filepath.Dir(e.Name) != tmp {
t.Errorf("unexpected event: %s", e)
}
}
if !found {
t.Fatalf("didn't see directory in:\n%s", events)
}
})
}
func TestWatchRecursive(t *testing.T) {
recurseOnly(t)
tests := []testCase{
// Make a nested directory tree, then write some files there.
{"basic", func(t *testing.T, w *Watcher, tmp string) {
mkdirAll(t, tmp, "/one/two/three/four")
addWatch(t, w, tmp, "/...")
cat(t, "asd", tmp, "/file.txt")
cat(t, "asd", tmp, "/one/two/three/file.txt")
}, `
create /file.txt # cat asd >file.txt
write /file.txt
write /one/two/three # cat asd >one/two/three/file.txt
create /one/two/three/file.txt
write /one/two/three/file.txt
`},
// Create a new directory tree and then some files under that.
{"add directory", func(t *testing.T, w *Watcher, tmp string) {
mkdirAll(t, tmp, "/one/two/three/four")
addWatch(t, w, tmp, "/...")
mkdirAll(t, tmp, "/one/two/new/dir")
touch(t, tmp, "/one/two/new/file")
touch(t, tmp, "/one/two/new/dir/file")
}, `
write /one/two # mkdir -p one/two/new/dir
create /one/two/new
create /one/two/new/dir
write /one/two/new # touch one/two/new/file
create /one/two/new/file
create /one/two/new/dir/file # touch one/two/new/dir/file
`},
// Remove nested directory
{"remove directory", func(t *testing.T, w *Watcher, tmp string) {
mkdirAll(t, tmp, "one/two/three/four")
addWatch(t, w, tmp, "...")
cat(t, "asd", tmp, "one/two/three/file.txt")
rmAll(t, tmp, "one/two")
}, `
write /one/two/three # cat asd >one/two/three/file.txt
create /one/two/three/file.txt
write /one/two/three/file.txt
write /one/two # rm -r one/two
write /one/two/three
remove /one/two/three/file.txt
remove /one/two/three/four
write /one/two/three
remove /one/two/three
write /one/two
remove /one/two
`},
// Rename nested directory
{"rename directory", func(t *testing.T, w *Watcher, tmp string) {
mkdirAll(t, tmp, "/one/two/three/four")
addWatch(t, w, tmp, "...")
mv(t, join(tmp, "one"), tmp, "one-rename")
touch(t, tmp, "one-rename/file")
touch(t, tmp, "one-rename/two/three/file")
}, `
rename "/one" # mv one one-rename
create "/one-rename"
write "/one-rename" # touch one-rename/file
create "/one-rename/file"
write "/one-rename/two/three" # touch one-rename/two/three/file
create "/one-rename/two/three/file"
`},
{"remove watched directory", func(t *testing.T, w *Watcher, tmp string) {
mk := func(r string) {
touch(t, r, "a")
touch(t, r, "b")
touch(t, r, "c")
touch(t, r, "d")
touch(t, r, "e")
touch(t, r, "f")
touch(t, r, "g")
mkdir(t, r, "h")
mkdir(t, r, "h", "a")
mkdir(t, r, "i")
mkdir(t, r, "i", "a")
mkdir(t, r, "j")
mkdir(t, r, "j", "a")
}
mk(tmp)
mkdir(t, tmp, "sub")
mk(join(tmp, "sub"))
addWatch(t, w, tmp, "...")
rmAll(t, tmp)
}, `
remove "/a"
remove "/b"
remove "/c"
remove "/d"
remove "/e"
remove "/f"
remove "/g"
write "/h"
remove "/h/a"
write "/h"
remove "/h"
write "/i"
remove "/i/a"
write "/i"
remove "/i"
write "/j"
remove "/j/a"
write "/j"
remove "/j"
write "/sub"
remove "/sub/a"
remove "/sub/b"
remove "/sub/c"
remove "/sub/d"
remove "/sub/e"
remove "/sub/f"
remove "/sub/g"
write "/sub/h"
remove "/sub/h/a"
write "/sub/h"
remove "/sub/h"
write "/sub/i"
remove "/sub/i/a"
write "/sub/i"
remove "/sub/i"
write "/sub/j"
remove "/sub/j/a"
write "/sub/j"
remove "/sub/j"
write "/sub"
remove "/sub"
remove "/"
`},
}
for _, tt := range tests {
tt := tt
tt.run(t)
}
}
// TODO: this fails reguarly in the CI; not sure if it's a bug with the test or
// code; need to look in to it.
func TestClose(t *testing.T) {
chanClosed := func(t *testing.T, w *Watcher) {
t.Helper()
// Need a small sleep as Close() on kqueue does all sorts of things,
// which may take a little bit.
switch runtime.GOOS {
case "freebsd", "openbsd", "netbsd", "dragonfly", "darwin", "solaris", "illumos":
time.Sleep(50 * time.Millisecond)
}
tim := time.NewTimer(50 * time.Millisecond)
loop:
for {
select {
default:
t.Fatal("blocking on Events")
case <-tim.C:
t.Fatalf("Events not closed")
case _, ok := <-w.Events:
if !ok {
break loop
}
}
}
select {
default:
t.Fatal("blocking on Errors")
case err, ok := <-w.Errors:
if ok {
t.Fatalf("Errors not closed; read:\n\t%s", err)
}
}
}
t.Run("close", func(t *testing.T) {
t.Parallel()
w := newWatcher(t)
if err := w.Close(); err != nil {
t.Fatal(err)
}
chanClosed(t, w)
var done int32
go func() {
w.Close()
atomic.StoreInt32(&done, 1)
}()
eventSeparator()
if atomic.LoadInt32(&done) == 0 {
t.Fatal("double Close() test failed: second Close() call didn't return")
}
if err := w.Add(t.TempDir()); err == nil {
t.Fatal("expected error on Watch() after Close(), got nil")
}
})