github.com/misseven0/notify@v0.0.0-20230519123055-c1422e46da05/watcher_recursive_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 && !kqueue && cgo) || windows 6 // +build darwin,!kqueue,cgo windows 7 8 package notify 9 10 import ( 11 "fmt" 12 "testing" 13 ) 14 15 // noevent stripts test-case from expected event list, used when action is not 16 // expected to trigger any events. 17 func noevent(cas WCase) WCase { 18 return WCase{Action: cas.Action} 19 } 20 21 func TestWatcherRecursiveRewatch(t *testing.T) { 22 w := newWatcherTest(t, "testdata/vfs.txt") 23 defer w.Close() 24 25 cases := []WCase{ 26 create(w, "src/github.com/rjeczalik/file"), 27 create(w, "src/github.com/rjeczalik/dir/"), 28 noevent(create(w, "src/github.com/rjeczalik/fs/dir/")), 29 noevent(create(w, "src/github.com/dir/")), 30 noevent(write(w, "src/github.com/rjeczalik/file", []byte("XD"))), 31 noevent(rename(w, "src/github.com/rjeczalik/fs/LICENSE", "src/LICENSE")), 32 } 33 34 w.Watch("src/github.com/rjeczalik", Create) 35 w.ExpectAny(cases) 36 37 cases = []WCase{ 38 create(w, "src/github.com/rjeczalik/fs/file"), 39 create(w, "src/github.com/rjeczalik/fs/cmd/gotree/file"), 40 create(w, "src/github.com/rjeczalik/fs/cmd/dir/"), 41 create(w, "src/github.com/rjeczalik/fs/cmd/gotree/dir/"), 42 noevent(write(w, "src/github.com/rjeczalik/fs/file", []byte("XD"))), 43 noevent(create(w, "src/github.com/anotherdir/")), 44 } 45 46 w.RecursiveRewatch("src/github.com/rjeczalik", "src/github.com/rjeczalik", Create, Create) 47 w.ExpectAny(cases) 48 49 cases = []WCase{ 50 create(w, "src/github.com/rjeczalik/1"), 51 create(w, "src/github.com/rjeczalik/2/"), 52 noevent(create(w, "src/github.com/rjeczalik/fs/cmd/1")), 53 noevent(create(w, "src/github.com/rjeczalik/fs/1/")), 54 noevent(write(w, "src/github.com/rjeczalik/fs/file", []byte("XD"))), 55 } 56 57 w.Rewatch("src/github.com/rjeczalik", Create, Create) 58 w.ExpectAny(cases) 59 } 60 61 // TODO(rjeczalik): move to watcher_test.go after #5 62 func TestIsDirCreateEvent(t *testing.T) { 63 w := NewWatcherTest(t, "testdata/vfs.txt") 64 defer w.Close() 65 66 cases := [...]WCase{ 67 // i=0 68 create(w, "src/github.com/jszwec/"), 69 // i=1 70 create(w, "src/github.com/jszwec/gojunitxml/"), 71 // i=2 72 create(w, "src/github.com/jszwec/gojunitxml/README.md"), 73 // i=3 74 create(w, "src/github.com/jszwec/gojunitxml/LICENSE"), 75 // i=4 76 create(w, "src/github.com/jszwec/gojunitxml/cmd/"), 77 } 78 79 dirs := [...]bool{ 80 true, // i=0 81 true, // i=1 82 false, // i=2 83 false, // i=3 84 true, // i=4 85 } 86 87 fn := func(i int, _ WCase, ei EventInfo) error { 88 d, ok := ei.(isDirer) 89 if !ok { 90 return fmt.Errorf("received EventInfo does not implement isDirer") 91 } 92 switch ok, err := d.isDir(); { 93 case err != nil: 94 return err 95 case ok != dirs[i]: 96 return fmt.Errorf("want ok=%v; got %v", dirs[i], ok) 97 default: 98 return nil 99 } 100 } 101 102 w.ExpectAnyFunc(cases[:], fn) 103 }