@@ -14,6 +14,7 @@ import (
14
14
15
15
"github.com/stretchr/testify/assert"
16
16
17
+ "github.com/windmilleng/tilt/internal/dockerignore"
17
18
"github.com/windmilleng/tilt/internal/logger"
18
19
"github.com/windmilleng/tilt/internal/testutils/tempdir"
19
20
)
@@ -412,36 +413,99 @@ func TestWatchNonexistentDirectory(t *testing.T) {
412
413
}
413
414
}
414
415
415
- // doesn't work on linux
416
- // func TestWatchNonexistentFileInNonexistentDirectory(t *testing.T) {
417
- // f := newNotifyFixture(t)
418
- // defer f.tearDown()
416
+ func TestWatchNonexistentFileInNonexistentDirectory (t * testing.T ) {
417
+ f := newNotifyFixture (t )
418
+ defer f .tearDown ()
419
+
420
+ root := f .JoinPath ("root" )
421
+ err := os .Mkdir (root , 0777 )
422
+ if err != nil {
423
+ t .Fatal (err )
424
+ }
425
+ parent := f .JoinPath ("parent" )
426
+ file := f .JoinPath ("parent" , "a" )
427
+
428
+ f .watch (file )
429
+ f .assertEvents ()
419
430
420
- // root := f.JoinPath("root")
421
- // err := os.Mkdir(root, 0777)
422
- // if err != nil {
423
- // t.Fatal(err)
424
- // }
425
- // parent := f.JoinPath("parent")
426
- // file := f.JoinPath("parent", "a")
431
+ err = os .Mkdir (parent , 0777 )
432
+ if err != nil {
433
+ t .Fatal (err )
434
+ }
427
435
428
- // f.watch(file)
429
- // f.assertEvents()
436
+ f .assertEvents ()
437
+ f .WriteFile (file , "hello" )
438
+ f .assertEvents (file )
439
+ }
430
440
431
- // err = os.Mkdir(parent, 0777)
432
- // if err != nil {
433
- // t.Fatal(err)
434
- // }
441
+ func TestWatchCountInnerFile (t * testing.T ) {
442
+ f := newNotifyFixture (t )
443
+ defer f .tearDown ()
435
444
436
- // f.assertEvents()
437
- // f.WriteFile(file, "hello")
438
- // f.assertEvents(file)
439
- // }
445
+ root := f .paths [0 ]
446
+ a := f .JoinPath (root , "a" )
447
+ b := f .JoinPath (a , "b" )
448
+ file := f .JoinPath (b , "bigFile" )
449
+ f .WriteFile (file , "hello" )
450
+ f .assertEvents (a , b , file )
451
+
452
+ expectedWatches := 3
453
+ if runtime .GOOS == "darwin" {
454
+ expectedWatches = 1
455
+ }
456
+ assert .Equal (t , expectedWatches , int (numberOfWatches .Value ()))
457
+ }
458
+
459
+ func TestWatchCountInnerFileWithIgnore (t * testing.T ) {
460
+ f := newNotifyFixture (t )
461
+ defer f .tearDown ()
462
+
463
+ root := f .paths [0 ]
464
+ ignore , _ := dockerignore .NewDockerPatternMatcher (root , []string {
465
+ "a" ,
466
+ "!a/b" ,
467
+ })
468
+ f .setIgnore (ignore )
469
+
470
+ a := f .JoinPath (root , "a" )
471
+ b := f .JoinPath (a , "b" )
472
+ file := f .JoinPath (b , "bigFile" )
473
+ f .WriteFile (file , "hello" )
474
+ f .assertEvents (b , file )
475
+
476
+ expectedWatches := 3
477
+ if runtime .GOOS == "darwin" {
478
+ expectedWatches = 1
479
+ }
480
+ assert .Equal (t , expectedWatches , int (numberOfWatches .Value ()))
481
+ }
482
+
483
+ func TestIgnore (t * testing.T ) {
484
+ f := newNotifyFixture (t )
485
+ defer f .tearDown ()
486
+
487
+ root := f .paths [0 ]
488
+ ignore , _ := dockerignore .NewDockerPatternMatcher (root , []string {"a/b" })
489
+ f .setIgnore (ignore )
490
+
491
+ a := f .JoinPath (root , "a" )
492
+ b := f .JoinPath (a , "b" )
493
+ file := f .JoinPath (b , "bigFile" )
494
+ f .WriteFile (file , "hello" )
495
+ f .assertEvents (a )
496
+
497
+ expectedWatches := 3
498
+ if runtime .GOOS == "darwin" {
499
+ expectedWatches = 1
500
+ }
501
+ assert .Equal (t , expectedWatches , int (numberOfWatches .Value ()))
502
+ }
440
503
441
504
type notifyFixture struct {
442
505
out * bytes.Buffer
443
506
* tempdir.TempDirFixture
444
507
notify Notify
508
+ ignore PathMatcher
445
509
paths []string
446
510
events []FileEvent
447
511
}
@@ -451,23 +515,32 @@ func newNotifyFixture(t *testing.T) *notifyFixture {
451
515
nf := & notifyFixture {
452
516
TempDirFixture : tempdir .NewTempDirFixture (t ),
453
517
paths : []string {},
518
+ ignore : EmptyMatcher {},
454
519
out : out ,
455
520
}
456
521
nf .watch (nf .TempDir ("watched" ))
457
522
return nf
458
523
}
459
524
525
+ func (f * notifyFixture ) setIgnore (ignore PathMatcher ) {
526
+ f .ignore = ignore
527
+ f .rebuildWatcher ()
528
+ }
529
+
460
530
func (f * notifyFixture ) watch (path string ) {
461
531
f .paths = append (f .paths , path )
532
+ f .rebuildWatcher ()
533
+ }
462
534
535
+ func (f * notifyFixture ) rebuildWatcher () {
463
536
// sync any outstanding events and close the old watcher
464
537
if f .notify != nil {
465
538
f .fsync ()
466
539
f .closeWatcher ()
467
540
}
468
541
469
542
// create a new watcher
470
- notify , err := NewWatcher (f .paths , EmptyMatcher {} , logger .NewLogger (logger .DebugLvl , f .out ))
543
+ notify , err := NewWatcher (f .paths , f . ignore , logger .NewLogger (logger .DebugLvl , f .out ))
471
544
if err != nil {
472
545
f .T ().Fatal (err )
473
546
}
@@ -569,4 +642,5 @@ func (f *notifyFixture) closeWatcher() {
569
642
func (f * notifyFixture ) tearDown () {
570
643
f .closeWatcher ()
571
644
f .TempDirFixture .TearDown ()
645
+ numberOfWatches .Set (0 )
572
646
}
0 commit comments