-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatcher_test.go
More file actions
290 lines (245 loc) · 7.26 KB
/
watcher_test.go
File metadata and controls
290 lines (245 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//go:build darwin
package main
import (
"os"
"path/filepath"
"sync"
"testing"
"time"
"github.com/rjeczalik/notify"
)
func TestNativeWatcherDetectsFileChanges(t *testing.T) {
// Create a temporary directory for testing
tmpDir, err := os.MkdirTemp("", "watcher-test-*")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
// Create subdirectory
subDir := filepath.Join(tmpDir, "subdir")
if err := os.MkdirAll(subDir, 0755); err != nil {
t.Fatalf("failed to create subdir: %v", err)
}
// Track detected events
var mu sync.Mutex
detectedFiles := make(map[string]bool)
// Create event channel
c := make(chan notify.EventInfo, 100)
// Watch the temp directory recursively
if err := notify.Watch(tmpDir+"/...", c, notify.All); err != nil {
t.Fatalf("failed to start watcher: %v", err)
}
defer notify.Stop(c)
// Start event listener in background
done := make(chan bool)
go func() {
timeout := time.After(5 * time.Second)
for {
select {
case ei := <-c:
event := ei.Event()
if event¬ify.Write != 0 || event¬ify.Create != 0 {
mu.Lock()
relPath, _ := filepath.Rel(tmpDir, ei.Path())
detectedFiles[relPath] = true
t.Logf("Detected: %s (event: %s)", relPath, event.String())
mu.Unlock()
}
case <-timeout:
close(done)
return
}
}
}()
// Give watcher time to start
time.Sleep(100 * time.Millisecond)
// Test 1: Create a new file in root
testFile1 := filepath.Join(tmpDir, "test1.go")
if err := os.WriteFile(testFile1, []byte("package main"), 0644); err != nil {
t.Fatalf("failed to create test file 1: %v", err)
}
t.Log("Created test1.go")
// Test 2: Create a file in subdirectory
testFile2 := filepath.Join(subDir, "test2.sql")
if err := os.WriteFile(testFile2, []byte("SELECT 1;"), 0644); err != nil {
t.Fatalf("failed to create test file 2: %v", err)
}
t.Log("Created subdir/test2.sql")
// Test 3: Modify existing file
time.Sleep(100 * time.Millisecond)
if err := os.WriteFile(testFile1, []byte("package main\n// modified"), 0644); err != nil {
t.Fatalf("failed to modify test file 1: %v", err)
}
t.Log("Modified test1.go")
// Test 4: Create a new subdirectory and file in it
newSubDir := filepath.Join(tmpDir, "newsubdir")
if err := os.MkdirAll(newSubDir, 0755); err != nil {
t.Fatalf("failed to create new subdir: %v", err)
}
testFile3 := filepath.Join(newSubDir, "test3.graphql")
if err := os.WriteFile(testFile3, []byte("type Query { test: String }"), 0644); err != nil {
t.Fatalf("failed to create test file 3: %v", err)
}
t.Log("Created newsubdir/test3.graphql")
// Wait for events to be processed
<-done
// Verify detections
mu.Lock()
defer mu.Unlock()
t.Logf("\nDetected files: %v", detectedFiles)
// Check that files were detected by looking for the filename in the paths
expectedSuffixes := []string{
"test1.go",
"test2.sql",
"test3.graphql",
}
for _, suffix := range expectedSuffixes {
found := false
for path := range detectedFiles {
if filepath.Base(path) == suffix || filepath.Base(path) == filepath.Base(suffix) {
found = true
break
}
}
if !found {
t.Errorf("Expected to detect a file ending with %s but didn't", suffix)
}
}
if len(detectedFiles) == 0 {
t.Error("No files were detected at all - watcher may not be working")
}
}
func TestNativeWatcherPerformance(t *testing.T) {
// Create a temporary directory with many subdirectories
tmpDir, err := os.MkdirTemp("", "watcher-perf-*")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
// Create 100 subdirectories (simulating a medium-sized project)
for i := 0; i < 100; i++ {
subDir := filepath.Join(tmpDir, "dir"+string(rune('a'+i%26))+string(rune('0'+i/26)))
if err := os.MkdirAll(subDir, 0755); err != nil {
t.Fatalf("failed to create subdir %d: %v", i, err)
}
// Create a file in each directory
testFile := filepath.Join(subDir, "file.go")
if err := os.WriteFile(testFile, []byte("package test"), 0644); err != nil {
t.Fatalf("failed to create test file in subdir %d: %v", i, err)
}
}
// Measure time to set up watcher
c := make(chan notify.EventInfo, 100)
start := time.Now()
if err := notify.Watch(tmpDir+"/...", c, notify.All); err != nil {
t.Fatalf("failed to start watcher: %v", err)
}
setupTime := time.Since(start)
defer notify.Stop(c)
t.Logf("FSEvents watcher setup time for 100 directories: %v", setupTime)
// FSEvents should set up very quickly (typically < 10ms)
if setupTime > 500*time.Millisecond {
t.Errorf("Watcher setup took too long: %v (expected < 500ms)", setupTime)
}
// Verify it can detect changes
detectedCount := 0
done := make(chan bool)
go func() {
timeout := time.After(3 * time.Second)
for {
select {
case ei := <-c:
event := ei.Event()
if event¬ify.Write != 0 || event¬ify.Create != 0 {
detectedCount++
}
case <-timeout:
close(done)
return
}
}
}()
// Give watcher time to start
time.Sleep(50 * time.Millisecond)
// Create 10 files rapidly
for i := 0; i < 10; i++ {
testFile := filepath.Join(tmpDir, "rapid"+string(rune('0'+i))+".go")
if err := os.WriteFile(testFile, []byte("package rapid"), 0644); err != nil {
t.Fatalf("failed to create rapid test file %d: %v", i, err)
}
}
<-done
t.Logf("Detected %d file events", detectedCount)
if detectedCount < 5 {
t.Errorf("Expected to detect at least 5 rapid file changes, got %d", detectedCount)
}
}
func TestWatcherFiltersByExtension(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "watcher-filter-*")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
var mu sync.Mutex
detectedFiles := make(map[string]string) // path -> extension
c := make(chan notify.EventInfo, 100)
if err := notify.Watch(tmpDir+"/...", c, notify.All); err != nil {
t.Fatalf("failed to start watcher: %v", err)
}
defer notify.Stop(c)
done := make(chan bool)
go func() {
timeout := time.After(3 * time.Second)
for {
select {
case ei := <-c:
event := ei.Event()
if event¬ify.Write != 0 || event¬ify.Create != 0 {
mu.Lock()
ext := filepath.Ext(ei.Path())
relPath, _ := filepath.Rel(tmpDir, ei.Path())
detectedFiles[relPath] = ext
mu.Unlock()
}
case <-timeout:
close(done)
return
}
}
}()
time.Sleep(100 * time.Millisecond)
// Create files with different extensions
testCases := map[string]string{
"app.go": ".go",
"schema.graphql": ".graphql",
"migration.sql": ".sql",
"template.gohtml": ".gohtml",
"config.env": ".env",
"readme.md": ".md",
"image.png": ".png",
}
for name := range testCases {
path := filepath.Join(tmpDir, name)
if err := os.WriteFile(path, []byte("test content"), 0644); err != nil {
t.Fatalf("failed to create %s: %v", name, err)
}
}
<-done
mu.Lock()
defer mu.Unlock()
t.Logf("Detected files with extensions: %v", detectedFiles)
// Check that we detected the important file types
importantExts := []string{".go", ".graphql", ".sql", ".gohtml", ".env"}
for _, ext := range importantExts {
found := false
for _, detectedExt := range detectedFiles {
if detectedExt == ext {
found = true
break
}
}
if !found {
t.Errorf("Expected to detect a %s file", ext)
}
}
}