watch: fix a bug when a file and its ancestor both have direct watches (#863)

This commit is contained in:
Nick Santos 2018-12-20 12:20:34 -05:00 committed by Nicolas De loof
parent 1fd7ca5440
commit 9e261c18b3
2 changed files with 29 additions and 19 deletions

View File

@ -290,6 +290,25 @@ func TestMoveAndReplace(t *testing.T) {
f.assertEvents(file)
}
func TestWatchBothDirAndFile(t *testing.T) {
f := newNotifyFixture(t)
defer f.tearDown()
dir := f.JoinPath("foo")
fileA := f.JoinPath("foo", "a")
fileB := f.JoinPath("foo", "b")
f.WriteFile(fileA, "a")
f.WriteFile(fileB, "b")
f.watch(fileA)
f.watch(dir)
f.fsync()
f.events = nil
f.WriteFile(fileB, "b-new")
f.assertEvents(fileB)
}
type notifyFixture struct {
*tempdir.TempDirFixture
notify Notify
@ -318,6 +337,13 @@ func newNotifyFixture(t *testing.T) *notifyFixture {
}
}
func (f *notifyFixture) watch(path string) {
err := f.notify.Add(path)
if err != nil {
f.T().Fatalf("notify.Add: %s", path)
}
}
func (f *notifyFixture) assertEvents(expected ...string) {
f.fsync()

View File

@ -1,24 +1,8 @@
package watch
import (
"os"
"path/filepath"
"strings"
)
import "github.com/windmilleng/tilt/internal/ospath"
func pathIsChildOf(path string, parent string) bool {
relPath, err := filepath.Rel(parent, path)
if err != nil {
return true
}
if relPath == "." {
return true
}
if filepath.IsAbs(relPath) || strings.HasPrefix(relPath, ".."+string(os.PathSeparator)) {
return false
}
return true
_, isChild := ospath.Child(parent, path)
return isChild
}