github.com/misseven0/notify@v0.0.0-20230519123055-c1422e46da05/watcher_inotify_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 linux 6 // +build linux 7 8 package notify 9 10 import ( 11 "os" 12 "path/filepath" 13 "testing" 14 ) 15 16 func icreate(w *W, path string) WCase { 17 cas := create(w, path) 18 cas.Events = append(cas.Events, 19 &Call{P: path, E: InCreate}, 20 ) 21 return cas 22 } 23 24 func iremove(w *W, path string) WCase { 25 cas := remove(w, path) 26 cas.Events = append(cas.Events, 27 &Call{P: path, E: InDelete}, 28 ) 29 return cas 30 } 31 32 func iopen(w *W, path string) WCase { 33 return WCase{ 34 Action: func() { 35 f, err := os.OpenFile(filepath.Join(w.root, path), os.O_RDWR, 0644) 36 if err != nil { 37 w.Fatalf("OpenFile(%q)=%v", path, err) 38 } 39 if err := f.Close(); err != nil { 40 w.Fatalf("Close(%q)=%v", path, err) 41 } 42 }, 43 Events: []EventInfo{ 44 &Call{P: path, E: InAccess}, 45 &Call{P: path, E: InOpen}, 46 &Call{P: path, E: InCloseNowrite}, 47 }, 48 } 49 } 50 51 func iread(w *W, path string, p []byte) WCase { 52 return WCase{ 53 Action: func() { 54 f, err := os.OpenFile(filepath.Join(w.root, path), os.O_RDWR, 0644) 55 if err != nil { 56 w.Fatalf("OpenFile(%q)=%v", path, err) 57 } 58 if _, err := f.Read(p); err != nil { 59 w.Fatalf("Read(%q)=%v", path, err) 60 } 61 if err := f.Close(); err != nil { 62 w.Fatalf("Close(%q)=%v", path, err) 63 } 64 }, 65 Events: []EventInfo{ 66 &Call{P: path, E: InAccess}, 67 &Call{P: path, E: InOpen}, 68 &Call{P: path, E: InModify}, 69 &Call{P: path, E: InCloseNowrite}, 70 }, 71 } 72 } 73 74 func iwrite(w *W, path string, p []byte) WCase { 75 cas := write(w, path, p) 76 path = cas.Events[0].Path() 77 cas.Events = append(cas.Events, 78 &Call{P: path, E: InAccess}, 79 &Call{P: path, E: InOpen}, 80 &Call{P: path, E: InModify}, 81 &Call{P: path, E: InCloseWrite}, 82 ) 83 return cas 84 } 85 86 func irename(w *W, path string) WCase { 87 const ext = ".notify" 88 return WCase{ 89 Action: func() { 90 file := filepath.Join(w.root, path) 91 if err := os.Rename(file, file+ext); err != nil { 92 w.Fatalf("Rename(%q, %q)=%v", path, path+ext, err) 93 } 94 }, 95 Events: []EventInfo{ 96 &Call{P: path, E: InMovedFrom}, 97 &Call{P: path + ext, E: InMovedTo}, 98 &Call{P: path, E: InOpen}, 99 &Call{P: path, E: InAccess}, 100 &Call{P: path, E: InCreate}, 101 }, 102 } 103 } 104 105 var events = []Event{ 106 InAccess, 107 InModify, 108 InAttrib, 109 InCloseWrite, 110 InCloseNowrite, 111 InOpen, 112 InMovedFrom, 113 InMovedTo, 114 InCreate, 115 InDelete, 116 InDeleteSelf, 117 InMoveSelf, 118 } 119 120 func TestWatcherInotify(t *testing.T) { 121 w := NewWatcherTest(t, "testdata/vfs.txt", events...) 122 defer w.Close() 123 124 cases := [...]WCase{ 125 iopen(w, "src/github.com/rjeczalik/fs/fs.go"), 126 iwrite(w, "src/github.com/rjeczalik/fs/fs.go", []byte("XD")), 127 iread(w, "src/github.com/rjeczalik/fs/fs.go", []byte("XD")), 128 iremove(w, "src/github.com/ppknap/link/README.md"), 129 irename(w, "src/github.com/rjeczalik/fs/LICENSE"), 130 } 131 132 w.ExpectAny(cases[:]) 133 }