]> go.fuhry.dev Git - fsnotify.git/commitdiff
Add test for remWatch (#418)
authormattn <mattn.jp@gmail.com>
Wed, 12 Jul 2023 12:47:58 +0000 (21:47 +0900)
committerGitHub <noreply@github.com>
Wed, 12 Jul 2023 12:47:58 +0000 (14:47 +0200)
Add test for remWatch, which #288 fixed.

Co-authored-by: jie <jie@tt.com>
Co-authored-by: Nahum Shalman <nahamu@gmail.com>
Co-authored-by: Martin Tournoij <martin@arp242.net>
backend_windows_test.go
fsnotify_test.go
helpers_test.go

index 7dc46bb102c4e82c514cf2ebd91dd16426a4f8ff..ee59b27d9994c04abf75e66efff34f851eee6a5a 100644 (file)
@@ -49,3 +49,20 @@ func TestRemoveState(t *testing.T) {
        }
        check(0)
 }
+
+func TestWindowsRemWatch(t *testing.T) {
+       tmp := t.TempDir()
+
+       touch(t, tmp, "file")
+
+       w := newWatcher(t)
+       defer w.Close()
+
+       addWatch(t, w, tmp)
+       if err := w.Remove(tmp); err != nil {
+               t.Fatalf("Could not remove the watch: %v\n", err)
+       }
+       if err := w.remWatch(tmp); err == nil {
+               t.Fatal("Should be fail with closed handle\n")
+       }
+}
index b86d54d5a4306dd4c5eb6bdfbfdbd283e2076918..61ded522ab7ada930c9d040e7c9d6c3b4235a05b 100644 (file)
@@ -1302,6 +1302,53 @@ func TestRemove(t *testing.T) {
                }
        })
 
+       // Make sure file handles are correctly released.
+       //
+       // regression test for #42 see https://gist.github.com/timshannon/603f92824c5294269797
+       t.Run("", func(t *testing.T) {
+               w := newWatcher(t)
+               defer w.Close()
+
+               // consume the events
+               var werr error
+               var wg sync.WaitGroup
+               wg.Add(1)
+               go func() {
+                       defer wg.Done()
+                       for {
+                               select {
+                               case werr = <-w.Errors:
+                                       return
+                               case <-w.Events:
+                               }
+                       }
+               }()
+
+               tmp := t.TempDir()
+               dir := join(tmp, "child")
+               addWatch(t, w, tmp)
+               mkdir(t, dir)
+               addWatch(t, w, dir) // start watching child
+               rmWatch(t, w, dir)  // stop watching child
+               rmAll(t, dir)       // delete child dir
+
+               // Child dir should no longer exist
+               _, err := os.Stat(dir)
+               if err == nil {
+                       t.Fatalf("dir %q should no longer exist!", dir)
+               }
+               if _, ok := err.(*os.PathError); err != nil && !ok {
+                       t.Errorf("Expected a PathError, got %v", err)
+               }
+
+               w.Close()
+               wg.Wait()
+
+               if werr != nil {
+                       t.Fatal(werr)
+               }
+       })
+
        t.Run("remove with ... when non-recursive", func(t *testing.T) {
                recurseOnly(t)
                t.Parallel()
@@ -1317,7 +1364,6 @@ func TestRemove(t *testing.T) {
                        t.Fatal(err)
                }
        })
-
 }
 
 func TestEventString(t *testing.T) {
index 6ebf2ea142718709f0af639aa6fa1615b0585dc5..2051a94f3f86f07f8a9db81bea692727c746437a 100644 (file)
@@ -70,6 +70,18 @@ func addWatch(t *testing.T, w *Watcher, path ...string) {
        }
 }
 
+// rmWatch removes a watch.
+func rmWatch(t *testing.T, watcher *Watcher, path ...string) {
+       t.Helper()
+       if len(path) < 1 {
+               t.Fatalf("rmWatch: path must have at least one element: %s", path)
+       }
+       err := watcher.Remove(join(path...))
+       if err != nil {
+               t.Fatalf("rmWatch(%q): %s", join(path...), err)
+       }
+}
+
 const noWait = ""
 
 func shouldWait(path ...string) bool {