Skip to content

Commit

Permalink
Use new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Jul 31, 2022
1 parent 3368117 commit ccfa501
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 59 deletions.
6 changes: 6 additions & 0 deletions helpers_test.go
Expand Up @@ -411,6 +411,12 @@ func newEvents(t *testing.T, s string) Events {
if e, ok := events[runtime.GOOS]; ok {
return e
}
switch runtime.GOOS {
case "freebsd", "netbsd", "openbsd", "dragonfly", "darwin":
if e, ok := events["kqueue"]; ok {
return e
}
}
return events[""]
}

Expand Down
85 changes: 30 additions & 55 deletions integration_test.go
Expand Up @@ -5,8 +5,6 @@ package fsnotify

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -166,6 +164,36 @@ func TestWatchRename(t *testing.T) {
windows:
create /renamed
`},

{"rename watched directory", func(t *testing.T, w *Watcher, tmp string) {
addWatch(t, w, tmp)

dir := filepath.Join(tmp, "dir")
mkdir(t, dir)
addWatch(t, w, dir)

mv(t, dir, tmp, "dir-renamed")
touch(t, tmp, "dir-renamed/file")
}, `
CREATE "/dir" # mkdir
RENAME "/dir" # mv
CREATE "/dir-renamed"
RENAME "/dir"
CREATE "/dir/file" # touch
windows:
CREATE "/dir" # mkdir
RENAME "/dir" # mv
CREATE "/dir-renamed"
CREATE "/dir-renamed/file" # touch
# TODO: no results for the touch; this is probably a bug; windows
# was fixed in #370.
kqueue:
CREATE "/dir" # mkdir
CREATE "/dir-renamed" # mv
REMOVE|RENAME "/dir"
`},
}

for _, tt := range tests {
Expand Down Expand Up @@ -475,56 +503,3 @@ func TestRemove(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)

dir1 := filepath.Join(testDir, "dir")
dir2 := filepath.Join(testDir, "dir2")

if err := os.Mkdir(dir1, 0o775); err != nil {
t.Fatal(err)
}
time.Sleep(10 * time.Millisecond)
addWatch(t, watcher, dir1)
if err := os.Rename(dir1, dir2); err != nil {
t.Fatal(err)
}

time.Sleep(10 * time.Millisecond)
err := ioutil.WriteFile(filepath.Join(dir2, "file.ext"), []byte(""), 0o664)
if 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)
}
}
8 changes: 4 additions & 4 deletions windows.go
Expand Up @@ -502,12 +502,12 @@ func (w *Watcher) readEvents() {
watch.rename = name
case syscall.FILE_ACTION_RENAMED_NEW_NAME:
// Update saved path of all sub-watches.
oldFullName := filepath.Join(watch.path, watch.rename)
old := filepath.Join(watch.path, watch.rename)
w.mu.Lock()
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))
for _, ww := range watchMap {
if strings.HasPrefix(ww.path, old) {
ww.path = filepath.Join(fullname, strings.TrimPrefix(ww.path, old))
}
}
}
Expand Down

0 comments on commit ccfa501

Please sign in to comment.