Skip to content

Commit 644fbb6

Browse files
committed
Add test to ensure Add()/Remove() works when not reading events
Taken from a branch I worked on some time ago where this was a bug. Also clarify that WatchList() order is undefined.
1 parent e7ec5f7 commit 644fbb6

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

fsnotify.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ func (w *Watcher) Close() error { return w.b.Close() }
338338
// WatchList returns all paths explicitly added with [Watcher.Add] (and are not
339339
// yet removed).
340340
//
341-
// Returns nil if [Watcher.Close] was called.
341+
// The order is undefined, and may differ per call. Returns nil if
342+
// [Watcher.Close] was called.
342343
func (w *Watcher) WatchList() []string { return w.b.WatchList() }
343344

344345
// Supports reports if all the listed operations are supported by this platform.

fsnotify_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,37 @@ func TestAdd(t *testing.T) {
562562
remove /file
563563
`))
564564
})
565+
566+
t.Run("not reading events", func(t *testing.T) {
567+
t.Parallel()
568+
569+
w := newWatcher(t)
570+
defer w.Close()
571+
572+
tmp := t.TempDir()
573+
mkdir(t, tmp, "/dir1")
574+
mkdir(t, tmp, "/dir2")
575+
addWatch(t, w, tmp, "/dir1")
576+
addWatch(t, w, tmp, "/dir2")
577+
578+
{
579+
have, want := w.WatchList(), []string{join(tmp, "/dir1"), join(tmp, "/dir2")}
580+
sort.Strings(have)
581+
if !reflect.DeepEqual(have, want) {
582+
t.Errorf("\nhave: %s\nwant: %s", have, want)
583+
}
584+
}
585+
if err := w.Remove(join(tmp, "/dir1")); err != nil {
586+
t.Fatal(err)
587+
}
588+
{
589+
have, want := w.WatchList(), []string{join(tmp, "/dir2")}
590+
sort.Strings(have)
591+
if !reflect.DeepEqual(have, want) {
592+
t.Errorf("\nhave: %s\nwant: %s", have, want)
593+
}
594+
}
595+
})
565596
}
566597

567598
func TestRemove(t *testing.T) {

0 commit comments

Comments
 (0)