gotest.tools/gotestsum@v1.11.0/internal/filewatcher/watch_test.go (about) 1 package filewatcher 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "testing" 7 "time" 8 9 "github.com/fsnotify/fsnotify" 10 "gotest.tools/v3/assert" 11 "gotest.tools/v3/env" 12 "gotest.tools/v3/fs" 13 ) 14 15 func TestFSEventHandler_HandleEvent(t *testing.T) { 16 type testCase struct { 17 name string 18 last time.Time 19 expectedRun bool 20 event fsnotify.Event 21 } 22 23 fn := func(t *testing.T, tc testCase) { 24 var ran bool 25 run := func(opts Event) error { 26 ran = true 27 return nil 28 } 29 30 h := fsEventHandler{last: tc.last, fn: run} 31 err := h.handleEvent(tc.event) 32 assert.NilError(t, err) 33 assert.Equal(t, ran, tc.expectedRun) 34 if tc.expectedRun { 35 assert.Assert(t, !h.last.IsZero()) 36 } 37 } 38 39 var testCases = []testCase{ 40 { 41 name: "Op is rename", 42 event: fsnotify.Event{Op: fsnotify.Rename, Name: "file_test.go"}, 43 expectedRun: true, 44 }, 45 { 46 name: "Op is remove", 47 event: fsnotify.Event{Op: fsnotify.Remove, Name: "file_test.go"}, 48 }, 49 { 50 name: "Op is chmod", 51 event: fsnotify.Event{Op: fsnotify.Chmod, Name: "file_test.go"}, 52 }, 53 { 54 name: "Op is write+chmod", 55 event: fsnotify.Event{Op: fsnotify.Write | fsnotify.Chmod, Name: "file_test.go"}, 56 expectedRun: true, 57 }, 58 { 59 name: "Op is write", 60 event: fsnotify.Event{Op: fsnotify.Write, Name: "file_test.go"}, 61 expectedRun: true, 62 }, 63 { 64 name: "Op is create", 65 event: fsnotify.Event{Op: fsnotify.Create, Name: "file_test.go"}, 66 expectedRun: true, 67 }, 68 { 69 name: "file is not a go file", 70 event: fsnotify.Event{Op: fsnotify.Write, Name: "readme.md"}, 71 }, 72 { 73 name: "under flood threshold", 74 event: fsnotify.Event{Op: fsnotify.Create, Name: "file_test.go"}, 75 last: time.Now(), 76 }, 77 } 78 for _, tc := range testCases { 79 t.Run(tc.name, func(t *testing.T) { 80 fn(t, tc) 81 }) 82 } 83 } 84 85 func TestHasGoFiles(t *testing.T) { 86 t.Run("none", func(t *testing.T) { 87 tmpDir := fs.NewDir(t, t.Name(), fs.WithFile("readme.md", "")) 88 defer tmpDir.Remove() 89 assert.Assert(t, !hasGoFiles(tmpDir.Path())) 90 }) 91 t.Run("empty", func(t *testing.T) { 92 tmpDir := fs.NewDir(t, t.Name()) 93 defer tmpDir.Remove() 94 assert.Assert(t, !hasGoFiles(tmpDir.Path())) 95 }) 96 t.Run("some go files", func(t *testing.T) { 97 tmpDir := fs.NewDir(t, t.Name(), fs.WithFile("main.go", "")) 98 defer tmpDir.Remove() 99 assert.Assert(t, hasGoFiles(tmpDir.Path())) 100 }) 101 t.Run("many go files", func(t *testing.T) { 102 tmpDir := fs.NewDir(t, t.Name()) 103 for i := 0; i < 47; i++ { 104 fs.Apply(t, tmpDir, fs.WithFile(fmt.Sprintf("file%d.go", i), "")) 105 } 106 defer tmpDir.Remove() 107 assert.Assert(t, hasGoFiles(tmpDir.Path())) 108 }) 109 } 110 111 func TestFindAllDirs(t *testing.T) { 112 goFile := fs.WithFile("file.go", "") 113 dirOne := fs.NewDir(t, t.Name(), 114 goFile, 115 fs.WithFile("not-a-dir", ""), 116 fs.WithDir("no-go-files"), 117 fs.WithDir(".starts-with-dot", goFile)) 118 defer dirOne.Remove() 119 var path string 120 for i := 1; i <= 10; i++ { 121 path = filepath.Join(path, fmt.Sprintf("%d", i)) 122 var ops []fs.PathOp 123 if i != 4 && i != 5 { 124 ops = []fs.PathOp{goFile} 125 } 126 fs.Apply(t, dirOne, fs.WithDir(path, ops...)) 127 } 128 129 dirTwo := fs.NewDir(t, t.Name(), 130 goFile, 131 // subdir should be ignored, dirTwo is used without /... suffix 132 fs.WithDir("subdir", goFile)) 133 defer dirTwo.Remove() 134 135 dirs := findAllDirs([]string{dirOne.Path() + "/...", dirTwo.Path()}, maxDepth) 136 expected := []string{ 137 dirOne.Path(), 138 dirOne.Join("1"), 139 dirOne.Join("1/2"), 140 dirOne.Join("1/2/3"), 141 dirOne.Join("1/2/3/4/5/6"), 142 dirOne.Join("1/2/3/4/5/6/7"), 143 dirTwo.Path(), 144 } 145 assert.DeepEqual(t, dirs, expected) 146 } 147 148 func TestFindAllDirs_DefaultPath(t *testing.T) { 149 goFile := fs.WithFile("file.go", "") 150 dirOne := fs.NewDir(t, t.Name(), 151 goFile, 152 fs.WithDir("a", goFile), 153 fs.WithDir("b", goFile)) 154 defer dirOne.Remove() 155 156 defer env.ChangeWorkingDir(t, dirOne.Path())() 157 dirs := findAllDirs([]string{}, maxDepth) 158 expected := []string{".", "a", "b"} 159 assert.DeepEqual(t, dirs, expected) 160 }