@@ -36,10 +36,7 @@ func TestEventOrdering(t *testing.T) {
36
36
for i , _ := range dirs {
37
37
dir := f .TempDir ("watched" )
38
38
dirs [i ] = dir
39
- err := f .notify .Add (dir )
40
- if err != nil {
41
- t .Fatal (err )
42
- }
39
+ f .watch (dir )
43
40
}
44
41
45
42
f .fsync ()
@@ -71,10 +68,7 @@ func TestGitBranchSwitch(t *testing.T) {
71
68
for i , _ := range dirs {
72
69
dir := f .TempDir ("watched" )
73
70
dirs [i ] = dir
74
- err := f .notify .Add (dir )
75
- if err != nil {
76
- t .Fatal (err )
77
- }
71
+ f .watch (dir )
78
72
}
79
73
80
74
f .fsync ()
@@ -129,16 +123,13 @@ func TestWatchesAreRecursive(t *testing.T) {
129
123
f .MkdirAll (subPath )
130
124
131
125
// watch parent
132
- err := f .notify .Add (root )
133
- if err != nil {
134
- t .Fatal (err )
135
- }
126
+ f .watch (root )
136
127
137
128
f .fsync ()
138
129
f .events = nil
139
130
// change sub directory
140
131
changeFilePath := filepath .Join (subPath , "change" )
141
- _ , err = os .OpenFile (changeFilePath , os .O_RDONLY | os .O_CREATE , 0666 )
132
+ _ , err : = os .OpenFile (changeFilePath , os .O_RDONLY | os .O_CREATE , 0666 )
142
133
if err != nil {
143
134
t .Fatal (err )
144
135
}
@@ -153,10 +144,7 @@ func TestNewDirectoriesAreRecursivelyWatched(t *testing.T) {
153
144
root := f .TempDir ("root" )
154
145
155
146
// watch parent
156
- err := f .notify .Add (root )
157
- if err != nil {
158
- t .Fatal (err )
159
- }
147
+ f .watch (root )
160
148
f .fsync ()
161
149
f .events = nil
162
150
@@ -166,7 +154,7 @@ func TestNewDirectoriesAreRecursivelyWatched(t *testing.T) {
166
154
167
155
// change something inside sub directory
168
156
changeFilePath := filepath .Join (subPath , "change" )
169
- _ , err = os .OpenFile (changeFilePath , os .O_RDONLY | os .O_CREATE , 0666 )
157
+ _ , err : = os .OpenFile (changeFilePath , os .O_RDONLY | os .O_CREATE , 0666 )
170
158
if err != nil {
171
159
t .Fatal (err )
172
160
}
@@ -180,11 +168,7 @@ func TestWatchNonExistentPath(t *testing.T) {
180
168
root := f .TempDir ("root" )
181
169
path := filepath .Join (root , "change" )
182
170
183
- err := f .notify .Add (path )
184
- if err != nil {
185
- t .Fatal (err )
186
- }
187
-
171
+ f .watch (path )
188
172
f .fsync ()
189
173
190
174
d1 := "hello\n go\n "
@@ -200,11 +184,7 @@ func TestWatchNonExistentPathDoesNotFireSiblingEvent(t *testing.T) {
200
184
watchedFile := filepath .Join (root , "a.txt" )
201
185
unwatchedSibling := filepath .Join (root , "b.txt" )
202
186
203
- err := f .notify .Add (watchedFile )
204
- if err != nil {
205
- t .Fatal (err )
206
- }
207
-
187
+ f .watch (watchedFile )
208
188
f .fsync ()
209
189
210
190
d1 := "hello\n go\n "
@@ -222,13 +202,10 @@ func TestRemove(t *testing.T) {
222
202
d1 := "hello\n go\n "
223
203
f .WriteFile (path , d1 )
224
204
225
- err := f .notify .Add (path )
226
- if err != nil {
227
- t .Fatal (err )
228
- }
205
+ f .watch (path )
229
206
f .fsync ()
230
207
f .events = nil
231
- err = os .Remove (path )
208
+ err : = os .Remove (path )
232
209
if err != nil {
233
210
t .Fatal (err )
234
211
}
@@ -239,17 +216,14 @@ func TestRemoveAndAddBack(t *testing.T) {
239
216
f := newNotifyFixture (t )
240
217
defer f .tearDown ()
241
218
242
- path := filepath .Join (f .watched , "change" )
219
+ path := filepath .Join (f .paths [ 0 ] , "change" )
243
220
244
221
d1 := []byte ("hello\n go\n " )
245
222
err := ioutil .WriteFile (path , d1 , 0644 )
246
223
if err != nil {
247
224
t .Fatal (err )
248
225
}
249
- err = f .notify .Add (path )
250
- if err != nil {
251
- t .Fatal (err )
252
- }
226
+ f .watch (path )
253
227
f .assertEvents (path )
254
228
255
229
err = os .Remove (path )
@@ -278,14 +252,11 @@ func TestSingleFile(t *testing.T) {
278
252
d1 := "hello\n go\n "
279
253
f .WriteFile (path , d1 )
280
254
281
- err := f .notify .Add (path )
282
- if err != nil {
283
- t .Fatal (err )
284
- }
255
+ f .watch (path )
285
256
f .fsync ()
286
257
287
258
d2 := []byte ("hello\n world\n " )
288
- err = ioutil .WriteFile (path , d2 , 0644 )
259
+ err : = ioutil .WriteFile (path , d2 , 0644 )
289
260
if err != nil {
290
261
t .Fatal (err )
291
262
}
@@ -296,8 +267,8 @@ func TestWriteBrokenLink(t *testing.T) {
296
267
f := newNotifyFixture (t )
297
268
defer f .tearDown ()
298
269
299
- link := filepath .Join (f .watched , "brokenLink" )
300
- missingFile := filepath .Join (f .watched , "missingFile" )
270
+ link := filepath .Join (f .paths [ 0 ] , "brokenLink" )
271
+ missingFile := filepath .Join (f .paths [ 0 ] , "missingFile" )
301
272
err := os .Symlink (missingFile , link )
302
273
if err != nil {
303
274
t .Fatal (err )
@@ -310,13 +281,13 @@ func TestWriteGoodLink(t *testing.T) {
310
281
f := newNotifyFixture (t )
311
282
defer f .tearDown ()
312
283
313
- goodFile := filepath .Join (f .watched , "goodFile" )
284
+ goodFile := filepath .Join (f .paths [ 0 ] , "goodFile" )
314
285
err := ioutil .WriteFile (goodFile , []byte ("hello" ), 0644 )
315
286
if err != nil {
316
287
t .Fatal (err )
317
288
}
318
289
319
- link := filepath .Join (f .watched , "goodFileSymlink" )
290
+ link := filepath .Join (f .paths [ 0 ] , "goodFileSymlink" )
320
291
err = os .Symlink (goodFile , link )
321
292
if err != nil {
322
293
t .Fatal (err )
@@ -342,11 +313,7 @@ func TestWatchBrokenLink(t *testing.T) {
342
313
t .Fatal (err )
343
314
}
344
315
345
- err = f .notify .Add (newRoot .Path ())
346
- if err != nil {
347
- t .Fatal (err )
348
- }
349
-
316
+ f .watch (newRoot .Path ())
350
317
os .Remove (link )
351
318
f .assertEvents (link )
352
319
}
@@ -359,15 +326,11 @@ func TestMoveAndReplace(t *testing.T) {
359
326
file := filepath .Join (root , "myfile" )
360
327
f .WriteFile (file , "hello" )
361
328
362
- err := f .notify .Add (file )
363
- if err != nil {
364
- t .Fatal (err )
365
- }
366
-
329
+ f .watch (file )
367
330
tmpFile := filepath .Join (root , ".myfile.swp" )
368
331
f .WriteFile (tmpFile , "world" )
369
332
370
- err = os .Rename (tmpFile , file )
333
+ err : = os .Rename (tmpFile , file )
371
334
if err != nil {
372
335
t .Fatal (err )
373
336
}
@@ -478,37 +441,40 @@ func TestWatchNonexistentDirectory(t *testing.T) {
478
441
type notifyFixture struct {
479
442
out * bytes.Buffer
480
443
* tempdir.TempDirFixture
481
- notify Notify
482
- watched string
483
- events []FileEvent
444
+ notify Notify
445
+ paths [] string
446
+ events []FileEvent
484
447
}
485
448
486
449
func newNotifyFixture (t * testing.T ) * notifyFixture {
487
450
out := bytes .NewBuffer (nil )
488
- notify , err := NewWatcher (logger .NewLogger (logger .DebugLvl , out ))
489
- if err != nil {
490
- t .Fatal (err )
491
- }
492
-
493
- f := tempdir .NewTempDirFixture (t )
494
- watched := f .TempDir ("watched" )
495
-
496
- err = notify .Add (watched )
497
- if err != nil {
498
- t .Fatal (err )
499
- }
500
- return & notifyFixture {
501
- TempDirFixture : f ,
502
- watched : watched ,
503
- notify : notify ,
451
+ nf := & notifyFixture {
452
+ TempDirFixture : tempdir .NewTempDirFixture (t ),
453
+ paths : []string {},
504
454
out : out ,
505
455
}
456
+ nf .watch (nf .TempDir ("watched" ))
457
+ return nf
506
458
}
507
459
508
460
func (f * notifyFixture ) watch (path string ) {
509
- err := f .notify .Add (path )
461
+ f .paths = append (f .paths , path )
462
+
463
+ // sync any outstanding events and close the old watcher
464
+ if f .notify != nil {
465
+ f .fsync ()
466
+ f .closeWatcher ()
467
+ }
468
+
469
+ // create a new watcher
470
+ notify , err := NewWatcher (f .paths , EmptyMatcher {}, logger .NewLogger (logger .DebugLvl , f .out ))
510
471
if err != nil {
511
- f .T ().Fatalf ("notify.Add: %s" , path )
472
+ f .T ().Fatal (err )
473
+ }
474
+ f .notify = notify
475
+ err = f .notify .Start ()
476
+ if err != nil {
477
+ f .T ().Fatal (err )
512
478
}
513
479
}
514
480
@@ -548,8 +514,8 @@ func (f *notifyFixture) consumeEventsInBackground(ctx context.Context) chan erro
548
514
549
515
func (f * notifyFixture ) fsync () {
550
516
syncPathBase := fmt .Sprintf ("sync-%d.txt" , time .Now ().UnixNano ())
551
- syncPath := filepath .Join (f .watched , syncPathBase )
552
- anySyncPath := filepath .Join (f .watched , "sync-" )
517
+ syncPath := filepath .Join (f .paths [ 0 ] , syncPathBase )
518
+ anySyncPath := filepath .Join (f .paths [ 0 ] , "sync-" )
553
519
timeout := time .After (time .Second )
554
520
555
521
f .WriteFile (syncPath , fmt .Sprintf ("%s" , time .Now ()))
582
548
}
583
549
}
584
550
585
- func (f * notifyFixture ) tearDown () {
586
- err := f .notify .Close ()
551
+ func (f * notifyFixture ) closeWatcher () {
552
+ notify := f .notify
553
+ err := notify .Close ()
587
554
if err != nil {
588
555
f .T ().Fatal (err )
589
556
}
590
557
591
558
// drain channels from watcher
592
559
go func () {
593
- for _ = range f . notify .Events () {
560
+ for _ = range notify .Events () {
594
561
}
595
562
}()
596
563
go func () {
597
- for _ = range f . notify .Errors () {
564
+ for _ = range notify .Errors () {
598
565
}
599
566
}()
567
+ }
600
568
569
+ func (f * notifyFixture ) tearDown () {
570
+ f .closeWatcher ()
601
571
f .TempDirFixture .TearDown ()
602
572
}
0 commit comments