Explorar el Código

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

Nick Santos hace 7 años
padre
commit
9e261c18b3
Se han modificado 2 ficheros con 29 adiciones y 19 borrados
  1. 26 0
      pkg/watch/notify_test.go
  2. 3 19
      pkg/watch/ospath.go

+ 26 - 0
pkg/watch/notify_test.go

@@ -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()
 

+ 3 - 19
pkg/watch/ospath.go

@@ -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
 }