Skip to content

Commit

Permalink
Added test and implemented working fix
Browse files Browse the repository at this point in the history
  • Loading branch information
arnegroskurth authored and arp242 committed Jul 22, 2022
1 parent ec2ac5c commit 61d73cc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
50 changes: 50 additions & 0 deletions integration_test.go
Expand Up @@ -14,6 +14,7 @@ import (
"path"
"path/filepath"
"runtime"
"strings"
"sync"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -1244,6 +1245,55 @@ func TestRemoveWithClose(t *testing.T) {
}
}

func TestMoveWatchedDirectory(t *testing.T) {

testDir := tempMkdir(t)
defer os.RemoveAll(testDir)

watcher := newWatcher(t)

// event recording
var events []Event
go func() {
for {
event, ok := <-watcher.Events
if !ok {
return
}
events = append(events, event)
}
}()

addWatch(t, watcher, testDir)

if err := os.Mkdir(testDir+"/dir", 0o775); err != nil {
t.Fatal(err)
}
time.Sleep(10 * time.Millisecond)
addWatch(t, watcher, testDir+"/dir")
if err := os.Rename(testDir+"/dir", testDir+"/dir2"); err != nil {
t.Fatal(err)
}
time.Sleep(10 * time.Millisecond)
if err := ioutil.WriteFile(testDir+"/dir2/file.ext", []byte(""), 0o664); err != nil {
t.Fatal(err)
}

if err := watcher.Close(); err != nil {
t.Fatal(err)
}
time.Sleep(10 * time.Millisecond)

if len(events) != 4 {
t.Fatalf("Expected 4 events. Got: %d", len(events))
}

expectedSuffix := filepath.Join("dir2", "file.ext")
if !strings.HasSuffix(events[3].Name, expectedSuffix) {
t.Fatalf("Expected suffix %s, Got: %s", expectedSuffix, events[3].Name)
}
}

func testRename(file1, file2 string) error {
switch runtime.GOOS {
case "windows", "plan9":
Expand Down
8 changes: 5 additions & 3 deletions windows.go
Expand Up @@ -504,9 +504,11 @@ func (w *Watcher) readEvents() {

// update saved path of all sub-watches
oldFullName := filepath.Join(watch.path, watch.rename)
for _, otherWatch := range w.watches {
if strings.HasPrefix(otherWatch.path, oldFullName) {
otherWatch.path = filepath.Join(watch.path, strings.TrimPrefix(otherWatch.path, oldFullName))
for _, watchMap := range w.watches {
for _, otherWatch := range watchMap {
if strings.HasPrefix(otherWatch.path, oldFullName) {
otherWatch.path = filepath.Join(fullname, strings.TrimPrefix(otherWatch.path, oldFullName))
}
}
}

Expand Down

0 comments on commit 61d73cc

Please sign in to comment.