github.com/misseven0/notify@v0.0.0-20230519123055-c1422e46da05/watcher_kqueue_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) || (darwin && !cgo) || dragonfly || freebsd || netbsd || openbsd 6 // +build darwin,kqueue darwin,!cgo dragonfly freebsd netbsd openbsd 7 8 package notify 9 10 import ( 11 "os" 12 "path/filepath" 13 "testing" 14 ) 15 16 func kqremove(w *W, path string, files []string) WCase { 17 cas := remove(w, path) 18 cas.Events[0] = &Call{P: path, E: NoteDelete} 19 for _, f := range files { 20 cas.Events = append(cas.Events, &Call{P: f, E: NoteDelete}) 21 } 22 return cas 23 } 24 25 func kqwrite(w *W, path string, p []byte) WCase { 26 cas := write(w, path, p) 27 path = cas.Events[0].Path() 28 cas.Events[0] = &Call{P: path, E: NoteExtend | NoteWrite} 29 return cas 30 } 31 32 func kqrename(w *W, path string, files []string) WCase { 33 const ext = ".notify" 34 cas := WCase{ 35 Action: func() { 36 file := filepath.Join(w.root, path) 37 if err := os.Rename(file, file+ext); err != nil { 38 w.Fatalf("Rename(%q, %q)=%v", path, path+ext, err) 39 } 40 }, 41 Events: []EventInfo{ 42 &Call{P: path + ext, E: osSpecificCreate}, 43 &Call{P: path, E: NoteRename}, 44 }, 45 } 46 for _, f := range files { 47 cas.Events = append(cas.Events, &Call{P: f, E: NoteRename}) 48 } 49 return cas 50 } 51 52 func kqlink(w *W, path string) WCase { 53 const ext = ".notify" 54 return WCase{ 55 Action: func() { 56 file := filepath.Join(w.root, path) 57 if err := os.Link(file, file+ext); err != nil { 58 w.Fatalf("Link(%q, %q)=%v", path, path+ext, err) 59 } 60 }, 61 Events: []EventInfo{ 62 &Call{P: path, E: NoteLink}, 63 &Call{P: path + ext, E: osSpecificCreate}, 64 }, 65 } 66 } 67 68 var events = []Event{ 69 NoteWrite, 70 NoteAttrib, 71 NoteRename, 72 osSpecificCreate, 73 NoteDelete, 74 NoteExtend, 75 NoteLink, 76 } 77 78 func TestWatcherKqueue(t *testing.T) { 79 w := NewWatcherTest(t, "testdata/vfs.txt", events...) 80 defer w.Close() 81 82 cases := [...]WCase{ 83 kqremove(w, "src/github.com/ppknap/link/include/coost/link", []string{ 84 "src/github.com/ppknap/link/include/coost/link/definitions.hpp", 85 "src/github.com/ppknap/link/include/coost/link/detail/bundle.hpp", 86 "src/github.com/ppknap/link/include/coost/link/detail/container_invoker.hpp", 87 "src/github.com/ppknap/link/include/coost/link/detail/container_value_trait.hpp", 88 "src/github.com/ppknap/link/include/coost/link/detail/dummy_type.hpp", 89 "src/github.com/ppknap/link/include/coost/link/detail/function_trait.hpp", 90 "src/github.com/ppknap/link/include/coost/link/detail/immediate_invoker.hpp", 91 "src/github.com/ppknap/link/include/coost/link/detail/stdhelpers/always_same.hpp", 92 "src/github.com/ppknap/link/include/coost/link/detail/stdhelpers/make_unique.hpp", 93 "src/github.com/ppknap/link/include/coost/link/detail/stdhelpers", 94 "src/github.com/ppknap/link/include/coost/link/detail/vertex.hpp", 95 "src/github.com/ppknap/link/include/coost/link/detail/wire.hpp", 96 "src/github.com/ppknap/link/include/coost/link/detail", 97 "src/github.com/ppknap/link/include/coost/link/link.hpp", 98 }, 99 ), 100 kqwrite(w, "src/github.com/rjeczalik/fs/fs.go", []byte("XD")), 101 kqremove(w, "src/github.com/ppknap/link/README.md", nil), 102 kqlink(w, "src/github.com/rjeczalik/fs/LICENSE"), 103 kqrename(w, "src/github.com/rjeczalik/fs/fs.go", nil), 104 kqrename(w, "src/github.com/rjeczalik/fs/cmd/gotree", []string{ 105 "src/github.com/rjeczalik/fs/cmd/gotree/go.go", 106 "src/github.com/rjeczalik/fs/cmd/gotree/main.go", 107 }, 108 ), 109 } 110 111 w.ExpectAll(cases[:]) 112 }