github.com/misseven0/notify@v0.0.0-20230519123055-c1422e46da05/watcher_test.go (about) 1 // Copyright (c) 2014-2015 The Notify Authors. All rights reserved. 2 // Use of this source code is governed by the MIT license that can be 3 // found in the LICENSE file. 4 5 //go:build darwin || linux || freebsd || dragonfly || netbsd || openbsd || windows || solaris 6 // +build darwin linux freebsd dragonfly netbsd openbsd windows solaris 7 8 package notify 9 10 import ( 11 "os" 12 "testing" 13 "time" 14 ) 15 16 // NOTE Set NOTIFY_DEBUG env var or debug build tag for extra debugging info. 17 18 func TestWatcher(t *testing.T) { 19 w := NewWatcherTest(t, "testdata/vfs.txt") 20 defer w.Close() 21 22 cases := [...]WCase{ 23 create(w, "src/github.com/ppknap/link/include/coost/.link.hpp.swp"), 24 create(w, "src/github.com/rjeczalik/fs/fs_test.go"), 25 create(w, "src/github.com/rjeczalik/fs/binfs/"), 26 create(w, "src/github.com/rjeczalik/fs/binfs.go"), 27 create(w, "src/github.com/rjeczalik/fs/binfs_test.go"), 28 remove(w, "src/github.com/rjeczalik/fs/binfs/"), 29 create(w, "src/github.com/rjeczalik/fs/binfs/"), 30 create(w, "src/github.com/rjeczalik/fs/virfs"), 31 remove(w, "src/github.com/rjeczalik/fs/virfs"), 32 create(w, "file"), 33 create(w, "dir/"), 34 } 35 36 w.ExpectAny(cases[:]) 37 } 38 39 // Simulates the scenario, where outside of the programs control the base dir 40 // is removed. This is detected and the watch removed. Then the directory is 41 // restored and a new watch set up. 42 func TestStopPathNotExists(t *testing.T) { 43 w := NewWatcherTest(t, "testdata/vfs.txt") 44 defer w.Close() 45 46 if err := os.RemoveAll(w.root); err != nil { 47 panic(err) 48 } 49 Sync() 50 51 // Don't check the returned error, as the public function (notify.Stop) 52 // does not return a potential error. As long as everything later on 53 // works as inteded, that's fine 54 time.Sleep(time.Duration(100) * time.Millisecond) 55 w.Watcher.Unwatch(w.root) 56 time.Sleep(time.Duration(100) * time.Millisecond) 57 58 if err := os.Mkdir(w.root, 0777); err != nil { 59 panic(err) 60 } 61 Sync() 62 w.Watch("", All) 63 64 drainall(w.C) 65 cases := [...]WCase{ 66 create(w, "file"), 67 create(w, "dir/"), 68 } 69 w.ExpectAny(cases[:]) 70 } 71 72 func TestWatcherUnwatch(t *testing.T) { 73 w := NewWatcherTest(t, "testdata/vfs.txt") 74 defer w.Close() 75 76 remove(w, "src/github.com/ppknap/link/test/test_circular_calls.cpp").Action() 77 w.Unwatch("") 78 79 w.Watch("", All) 80 81 drainall(w.C) 82 cases := [...]WCase{ 83 create(w, "file"), 84 create(w, "dir/"), 85 } 86 w.ExpectAny(cases[:]) 87 }