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 f9eedb9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 55 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", "macos":
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
kqueue:
CREATE "/dir" # mkdir
CREATE "/dir-renamed" # mv
REMOVE|RENAME "/dir"
# TODO: no results for the touch; this is probably a bug.
windows:
CREATE "/dir" # mkdir
RENAME "/dir" # mv
CREATE "/dir-renamed"
CREATE "/dir-renamed/file" # touch
`},
}

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

0 comments on commit f9eedb9

Please sign in to comment.