1
- // +build !windows
2
-
3
1
package watch
4
2
5
3
import (
@@ -14,9 +12,8 @@ import (
14
12
"testing"
15
13
"time"
16
14
17
- "github.com/stretchr/testify/require"
18
-
19
15
"github.com/stretchr/testify/assert"
16
+ "github.com/stretchr/testify/require"
20
17
21
18
"github.com/windmilleng/tilt/internal/dockerignore"
22
19
"github.com/windmilleng/tilt/internal/testutils/tempdir"
@@ -41,6 +38,11 @@ func TestNoWatches(t *testing.T) {
41
38
}
42
39
43
40
func TestEventOrdering (t * testing.T ) {
41
+ if runtime .GOOS == "windows" {
42
+ // https://qualapps.blogspot.com/2010/05/understanding-readdirectorychangesw_19.html
43
+ t .Skip ("Windows doesn't make great guarantees about duplicate/out-of-order events" )
44
+ return
45
+ }
44
46
f := newNotifyFixture (t )
45
47
defer f .tearDown ()
46
48
@@ -143,10 +145,7 @@ func TestWatchesAreRecursive(t *testing.T) {
143
145
f .events = nil
144
146
// change sub directory
145
147
changeFilePath := filepath .Join (subPath , "change" )
146
- _ , err := os .OpenFile (changeFilePath , os .O_RDONLY | os .O_CREATE , 0666 )
147
- if err != nil {
148
- t .Fatal (err )
149
- }
148
+ f .WriteFile (changeFilePath , "change" )
150
149
151
150
f .assertEvents (changeFilePath )
152
151
}
@@ -278,6 +277,9 @@ func TestSingleFile(t *testing.T) {
278
277
}
279
278
280
279
func TestWriteBrokenLink (t * testing.T ) {
280
+ if runtime .GOOS == "windows" {
281
+ t .Skip ("no user-space symlinks on windows" )
282
+ }
281
283
f := newNotifyFixture (t )
282
284
defer f .tearDown ()
283
285
@@ -292,6 +294,9 @@ func TestWriteBrokenLink(t *testing.T) {
292
294
}
293
295
294
296
func TestWriteGoodLink (t * testing.T ) {
297
+ if runtime .GOOS == "windows" {
298
+ t .Skip ("no user-space symlinks on windows" )
299
+ }
295
300
f := newNotifyFixture (t )
296
301
defer f .tearDown ()
297
302
@@ -311,6 +316,9 @@ func TestWriteGoodLink(t *testing.T) {
311
316
}
312
317
313
318
func TestWatchBrokenLink (t * testing.T ) {
319
+ if runtime .GOOS == "windows" {
320
+ t .Skip ("no user-space symlinks on windows" )
321
+ }
314
322
f := newNotifyFixture (t )
315
323
defer f .tearDown ()
316
324
@@ -399,6 +407,9 @@ func TestWatchNonexistentDirectory(t *testing.T) {
399
407
f := newNotifyFixture (t )
400
408
defer f .tearDown ()
401
409
410
+ ignore , _ := dockerignore .NewDockerPatternMatcher (f .paths [0 ], []string {"./" })
411
+ f .setIgnore (ignore )
412
+
402
413
root := f .JoinPath ("root" )
403
414
err := os .Mkdir (root , 0777 )
404
415
if err != nil {
@@ -422,20 +433,19 @@ func TestWatchNonexistentDirectory(t *testing.T) {
422
433
} else {
423
434
f .assertEvents (parent )
424
435
}
436
+ f .events = nil
425
437
f .WriteFile (file , "hello" )
426
438
427
- if runtime .GOOS == "darwin" {
428
- // mac doesn't return the dir change as part of file creation
429
- f .assertEvents (file )
430
- } else {
431
- f .assertEvents (parent , file )
432
- }
439
+ f .assertEvents (file )
433
440
}
434
441
435
442
func TestWatchNonexistentFileInNonexistentDirectory (t * testing.T ) {
436
443
f := newNotifyFixture (t )
437
444
defer f .tearDown ()
438
445
446
+ ignore , _ := dockerignore .NewDockerPatternMatcher (f .paths [0 ], []string {"./" })
447
+ f .setIgnore (ignore )
448
+
439
449
root := f .JoinPath ("root" )
440
450
err := os .Mkdir (root , 0777 )
441
451
if err != nil {
@@ -469,7 +479,7 @@ func TestWatchCountInnerFile(t *testing.T) {
469
479
f .assertEvents (a , b , file )
470
480
471
481
expectedWatches := 3
472
- if runtime . GOOS == "darwin" {
482
+ if isRecursiveWatcher () {
473
483
expectedWatches = 1
474
484
}
475
485
assert .Equal (t , expectedWatches , int (numberOfWatches .Value ()))
@@ -493,7 +503,7 @@ func TestWatchCountInnerFileWithIgnore(t *testing.T) {
493
503
f .assertEvents (b , file )
494
504
495
505
expectedWatches := 3
496
- if runtime . GOOS == "darwin" {
506
+ if isRecursiveWatcher () {
497
507
expectedWatches = 1
498
508
}
499
509
assert .Equal (t , expectedWatches , int (numberOfWatches .Value ()))
@@ -514,7 +524,7 @@ func TestIgnoreCreatedDir(t *testing.T) {
514
524
f .assertEvents (a )
515
525
516
526
expectedWatches := 2
517
- if runtime . GOOS == "darwin" {
527
+ if isRecursiveWatcher () {
518
528
expectedWatches = 1
519
529
}
520
530
assert .Equal (t , expectedWatches , int (numberOfWatches .Value ()))
@@ -540,7 +550,7 @@ func TestIgnoreCreatedDirWithExclusions(t *testing.T) {
540
550
f .assertEvents (a )
541
551
542
552
expectedWatches := 2
543
- if runtime . GOOS == "darwin" {
553
+ if isRecursiveWatcher () {
544
554
expectedWatches = 1
545
555
}
546
556
assert .Equal (t , expectedWatches , int (numberOfWatches .Value ()))
@@ -563,14 +573,20 @@ func TestIgnoreInitialDir(t *testing.T) {
563
573
f .assertEvents ()
564
574
565
575
expectedWatches := 3
566
- if runtime . GOOS == "darwin" {
576
+ if isRecursiveWatcher () {
567
577
expectedWatches = 2
568
578
}
569
579
assert .Equal (t , expectedWatches , int (numberOfWatches .Value ()))
570
580
}
571
581
582
+ func isRecursiveWatcher () bool {
583
+ return runtime .GOOS == "darwin" || runtime .GOOS == "windows"
584
+ }
585
+
572
586
type notifyFixture struct {
573
- out * bytes.Buffer
587
+ ctx context.Context
588
+ cancel func ()
589
+ out * bytes.Buffer
574
590
* tempdir.TempDirFixture
575
591
notify Notify
576
592
ignore PathMatcher
@@ -580,7 +596,10 @@ type notifyFixture struct {
580
596
581
597
func newNotifyFixture (t * testing.T ) * notifyFixture {
582
598
out := bytes .NewBuffer (nil )
599
+ ctx , cancel := context .WithCancel (context .Background ())
583
600
nf := & notifyFixture {
601
+ ctx : ctx ,
602
+ cancel : cancel ,
584
603
TempDirFixture : tempdir .NewTempDirFixture (t ),
585
604
paths : []string {},
586
605
ignore : EmptyMatcher {},
@@ -621,6 +640,11 @@ func (f *notifyFixture) rebuildWatcher() {
621
640
622
641
func (f * notifyFixture ) assertEvents (expected ... string ) {
623
642
f .fsync ()
643
+ if runtime .GOOS == "windows" {
644
+ // NOTE(nick): It's unclear to me why an extra fsync() helps
645
+ // here, but it makes the I/O way more predictable.
646
+ f .fsync ()
647
+ }
624
648
625
649
if len (f .events ) != len (expected ) {
626
650
f .T ().Fatalf ("Got %d events (expected %d): %v %v" , len (f .events ), len (expected ), f .events , expected )
@@ -639,6 +663,9 @@ func (f *notifyFixture) consumeEventsInBackground(ctx context.Context) chan erro
639
663
go func () {
640
664
for {
641
665
select {
666
+ case <- f .ctx .Done ():
667
+ close (done )
668
+ return
642
669
case <- ctx .Done ():
643
670
close (done )
644
671
return
@@ -672,6 +699,8 @@ func (f *notifyFixture) fsyncWithRetryCount(retryCount int) {
672
699
F:
673
700
for {
674
701
select {
702
+ case <- f .ctx .Done ():
703
+ return
675
704
case err := <- f .notify .Errors ():
676
705
f .T ().Fatal (err )
677
706
@@ -714,13 +743,15 @@ func (f *notifyFixture) closeWatcher() {
714
743
for range notify .Events () {
715
744
}
716
745
}()
746
+
717
747
go func () {
718
748
for range notify .Errors () {
719
749
}
720
750
}()
721
751
}
722
752
723
753
func (f * notifyFixture ) tearDown () {
754
+ f .cancel ()
724
755
f .closeWatcher ()
725
756
f .TempDirFixture .TearDown ()
726
757
numberOfWatches .Set (0 )
0 commit comments