func TestWatch(t *testing.T) {
tests := []testCase{
{"multiple creates", func(t *testing.T, w *Watcher, tmp string) {
- file := filepath.Join(tmp, "file")
+ file := join(tmp, "file")
addWatch(t, w, tmp)
cat(t, "data", file)
`},
{"dir only", func(t *testing.T, w *Watcher, tmp string) {
- beforeWatch := filepath.Join(tmp, "beforewatch")
- file := filepath.Join(tmp, "file")
+ beforeWatch := join(tmp, "beforewatch")
+ file := join(tmp, "file")
touch(t, beforeWatch)
addWatch(t, w, tmp)
{"subdir", func(t *testing.T, w *Watcher, tmp string) {
addWatch(t, w, tmp)
- file := filepath.Join(tmp, "file")
- dir := filepath.Join(tmp, "sub")
- dirfile := filepath.Join(tmp, "sub/file2")
+ file := join(tmp, "file")
+ dir := join(tmp, "sub")
+ dirfile := join(tmp, "sub/file2")
mkdir(t, dir) // Create sub-directory
touch(t, file) // Create a file
`},
{"watch same file twice", func(t *testing.T, w *Watcher, tmp string) {
- file := filepath.Join(tmp, "file")
+ file := join(tmp, "file")
touch(t, file)
addWatch(t, w, file)
t.Skip("broken on macOS")
}
- file := filepath.Join(tmp, "file")
- link := filepath.Join(tmp, "link")
+ file := join(tmp, "file")
+ link := join(tmp, "link")
touch(t, file)
symlink(t, file, link)
addWatch(t, w, link)
t.Skip("broken on macOS")
}
- dir := filepath.Join(tmp, "dir")
- link := filepath.Join(tmp, "link")
+ dir := join(tmp, "dir")
+ link := join(tmp, "link")
mkdir(t, dir)
symlink(t, dir, link)
addWatch(t, w, link)
{"create new symlink to file", func(t *testing.T, w *Watcher, tmp string) {
touch(t, tmp, "file")
addWatch(t, w, tmp)
- symlink(t, filepath.Join(tmp, "file"), tmp, "link")
+ symlink(t, join(tmp, "file"), tmp, "link")
}, `
create /link
`},
tests := []testCase{
// Files
{"truncate file", func(t *testing.T, w *Watcher, tmp string) {
- file := filepath.Join(tmp, "file")
+ file := join(tmp, "file")
cat(t, "data", file)
addWatch(t, w, tmp)
`},
{"multiple writes to a file", func(t *testing.T, w *Watcher, tmp string) {
- file := filepath.Join(tmp, "file")
+ file := join(tmp, "file")
cat(t, "data", file)
addWatch(t, w, tmp)
func TestWatchRename(t *testing.T) {
tests := []testCase{
{"rename file in watched dir", func(t *testing.T, w *Watcher, tmp string) {
- file := filepath.Join(tmp, "file")
+ file := join(tmp, "file")
cat(t, "asd", file)
addWatch(t, w, tmp)
addWatch(t, w, tmp)
touch(t, unwatched, "file")
- mv(t, filepath.Join(unwatched, "file"), tmp, "file")
+ mv(t, join(unwatched, "file"), tmp, "file")
}, `
create /file
`},
}
unwatched := t.TempDir()
- file := filepath.Join(tmp, "file")
- renamed := filepath.Join(unwatched, "renamed")
+ file := join(tmp, "file")
+ renamed := join(unwatched, "renamed")
addWatch(t, w, tmp)
{"rename overwriting existing file", func(t *testing.T, w *Watcher, tmp string) {
unwatched := t.TempDir()
- file := filepath.Join(unwatched, "file")
+ file := join(unwatched, "file")
touch(t, tmp, "renamed")
touch(t, file)
`},
{"rename watched directory", func(t *testing.T, w *Watcher, tmp string) {
- dir := filepath.Join(tmp, "dir")
+ dir := join(tmp, "dir")
mkdir(t, dir)
addWatch(t, w, dir)
`},
{"rename watched file", func(t *testing.T, w *Watcher, tmp string) {
- file := filepath.Join(tmp, "file")
- rename := filepath.Join(tmp, "rename-one")
+ file := join(tmp, "file")
+ rename := join(tmp, "rename-one")
touch(t, file)
addWatch(t, w, file)
`},
{"re-add renamed file", func(t *testing.T, w *Watcher, tmp string) {
- file := filepath.Join(tmp, "file")
- rename := filepath.Join(tmp, "rename")
+ file := join(tmp, "file")
+ rename := join(tmp, "rename")
touch(t, file)
addWatch(t, w, file)
{"create unresolvable symlink", func(t *testing.T, w *Watcher, tmp string) {
addWatch(t, w, tmp)
- symlink(t, filepath.Join(tmp, "target"), tmp, "link")
+ symlink(t, join(tmp, "target"), tmp, "link")
}, `
create /link
touch(t, tmp, "file1")
touch(t, tmp, "file2")
- symlink(t, filepath.Join(tmp, "file1"), tmp, "link1")
- symlink(t, filepath.Join(tmp, "file2"), tmp, "link2")
+ symlink(t, join(tmp, "file1"), tmp, "link1")
+ symlink(t, join(tmp, "file2"), tmp, "link2")
addWatch(t, w, tmp)
touch(t, tmp, "foo")
rm(t, tmp, "foo")
mkdir(t, tmp, "apple")
- mv(t, filepath.Join(tmp, "apple"), tmp, "pear")
+ mv(t, join(tmp, "apple"), tmp, "pear")
rmAll(t, tmp, "pear")
}, `
create /foo # touch foo
func TestWatchAttrib(t *testing.T) {
tests := []testCase{
{"chmod", func(t *testing.T, w *Watcher, tmp string) {
- file := filepath.Join(tmp, "file")
+ file := join(tmp, "file")
cat(t, "data", file)
addWatch(t, w, file)
`},
{"write does not trigger CHMOD", func(t *testing.T, w *Watcher, tmp string) {
- file := filepath.Join(tmp, "file")
+ file := join(tmp, "file")
cat(t, "data", file)
addWatch(t, w, file)
`},
{"chmod after write", func(t *testing.T, w *Watcher, tmp string) {
- file := filepath.Join(tmp, "file")
+ file := join(tmp, "file")
cat(t, "data", file)
addWatch(t, w, file)
func TestWatchRm(t *testing.T) {
tests := []testCase{
{"remove watched file", func(t *testing.T, w *Watcher, tmp string) {
- file := filepath.Join(tmp, "file")
+ file := join(tmp, "file")
touch(t, file)
addWatch(t, w, file)
t.Skip("Windows hard-locks open files so this will never work")
}
- file := filepath.Join(tmp, "file")
+ file := join(tmp, "file")
touch(t, file)
// Intentionally don't close the descriptor here so it stays around.
t.Skip("behaviour is inconsistent on OpenBSD and NetBSD, and this test is flaky")
}
- file := filepath.Join(tmp, "file")
+ file := join(tmp, "file")
touch(t, file)
addWatch(t, w, tmp)
files := make([]string, 0, 200)
for i := 0; i < 200; i++ {
- f := filepath.Join(tmp, fmt.Sprintf("file-%03d", i))
+ f := join(tmp, fmt.Sprintf("file-%03d", i))
touch(t, f, noWait)
files = append(files, f)
}
t.Fatal(err)
}
- file := filepath.Join(tmp, "file")
+ file := join(tmp, "file")
touch(t, file)
if err := w.Add(file); !errors.Is(err, ErrClosed) {
t.Fatalf("wrong error for Add: %#v", err)
t.Parallel()
tmp := t.TempDir()
- dir := filepath.Join(tmp, "dir-unreadable")
+ dir := join(tmp, "dir-unreadable")
mkdir(t, dir)
touch(t, dir, "/file")
chmod(t, 0, dir)
t.Parallel()
tmp := t.TempDir()
- file := filepath.Join(tmp, "file")
- other := filepath.Join(tmp, "other")
+ file := join(tmp, "file")
+ other := join(tmp, "other")
touch(t, file)
touch(t, other)
if len(path) < 1 {
t.Fatalf("addWatch: path must have at least one element: %s", path)
}
- err := w.Add(filepath.Join(path...))
+ err := w.Add(join(path...))
if err != nil {
- t.Fatalf("addWatch(%q): %s", filepath.Join(path...), err)
+ t.Fatalf("addWatch(%q): %s", join(path...), err)
}
}
const noWait = ""
func shouldWait(path ...string) bool {
- // Take advantage of the fact that filepath.Join skips empty parameters.
+ // Take advantage of the fact that join skips empty parameters.
for _, p := range path {
if p == "" {
return false
t.Logf("createFiles: stopped at %s files because it took longer than %s", fmtNum(created), d)
return created
default:
- fp, err := os.Create(filepath.Join(dir, prefix+fmtNum(i)))
+ fp, err := os.Create(join(dir, prefix+fmtNum(i)))
if err != nil {
t.Errorf("create failed for %s: %s", fmtNum(i), err)
continue
if len(path) < 1 {
t.Fatalf("mkdir: path must have at least one element: %s", path)
}
- err := os.Mkdir(filepath.Join(path...), 0o0755)
+ err := os.Mkdir(join(path...), 0o0755)
if err != nil {
- t.Fatalf("mkdir(%q): %s", filepath.Join(path...), err)
+ t.Fatalf("mkdir(%q): %s", join(path...), err)
}
if shouldWait(path...) {
eventSeparator()
// if len(path) < 1 {
// t.Fatalf("mkdirAll: path must have at least one element: %s", path)
// }
-// err := os.MkdirAll(filepath.Join(path...), 0o0755)
+// err := os.MkdirAll(join(path...), 0o0755)
// if err != nil {
-// t.Fatalf("mkdirAll(%q): %s", filepath.Join(path...), err)
+// t.Fatalf("mkdirAll(%q): %s", join(path...), err)
// }
// if shouldWait(path...) {
// eventSeparator()
if len(link) < 1 {
t.Fatalf("symlink: link must have at least one element: %s", link)
}
- err := os.Symlink(target, filepath.Join(link...))
+ err := os.Symlink(target, join(link...))
if err != nil {
- t.Fatalf("symlink(%q, %q): %s", target, filepath.Join(link...), err)
+ t.Fatalf("symlink(%q, %q): %s", target, join(link...), err)
}
if shouldWait(link...) {
eventSeparator()
if len(path) < 1 {
t.Fatalf("mkfifo: path must have at least one element: %s", path)
}
- err := internal.Mkfifo(filepath.Join(path...), 0o644)
+ err := internal.Mkfifo(join(path...), 0o644)
if err != nil {
- t.Fatalf("mkfifo(%q): %s", filepath.Join(path...), err)
+ t.Fatalf("mkfifo(%q): %s", join(path...), err)
}
if shouldWait(path...) {
eventSeparator()
if len(path) < 1 {
t.Fatalf("mknod: path must have at least one element: %s", path)
}
- err := internal.Mknod(filepath.Join(path...), 0o644, dev)
+ err := internal.Mknod(join(path...), 0o644, dev)
if err != nil {
- t.Fatalf("mknod(%d, %q): %s", dev, filepath.Join(path...), err)
+ t.Fatalf("mknod(%d, %q): %s", dev, join(path...), err)
}
if shouldWait(path...) {
eventSeparator()
}
err := func() error {
- fp, err := os.OpenFile(filepath.Join(path...), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
+ fp, err := os.OpenFile(join(path...), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
return fp.Close()
}()
if err != nil {
- t.Fatalf("cat(%q): %s", filepath.Join(path...), err)
+ t.Fatalf("cat(%q): %s", join(path...), err)
}
}
if len(path) < 1 {
t.Fatalf("touch: path must have at least one element: %s", path)
}
- fp, err := os.Create(filepath.Join(path...))
+ fp, err := os.Create(join(path...))
if err != nil {
- t.Fatalf("touch(%q): %s", filepath.Join(path...), err)
+ t.Fatalf("touch(%q): %s", join(path...), err)
}
err = fp.Close()
if err != nil {
- t.Fatalf("touch(%q): %s", filepath.Join(path...), err)
+ t.Fatalf("touch(%q): %s", join(path...), err)
}
if shouldWait(path...) {
eventSeparator()
t.Fatalf("mv: dst must have at least one element: %s", dst)
}
- err := os.Rename(src, filepath.Join(dst...))
+ err := os.Rename(src, join(dst...))
if err != nil {
- t.Fatalf("mv(%q, %q): %s", src, filepath.Join(dst...), err)
+ t.Fatalf("mv(%q, %q): %s", src, join(dst...), err)
}
if shouldWait(dst...) {
eventSeparator()
if len(path) < 1 {
t.Fatalf("rm: path must have at least one element: %s", path)
}
- err := os.Remove(filepath.Join(path...))
+ err := os.Remove(join(path...))
if err != nil {
- t.Fatalf("rm(%q): %s", filepath.Join(path...), err)
+ t.Fatalf("rm(%q): %s", join(path...), err)
}
if shouldWait(path...) {
eventSeparator()
if len(path) < 1 {
t.Fatalf("rmAll: path must have at least one element: %s", path)
}
- err := os.RemoveAll(filepath.Join(path...))
+ err := os.RemoveAll(join(path...))
if err != nil {
- t.Fatalf("rmAll(%q): %s", filepath.Join(path...), err)
+ t.Fatalf("rmAll(%q): %s", join(path...), err)
}
if shouldWait(path...) {
eventSeparator()
if len(path) < 1 {
t.Fatalf("chmod: path must have at least one element: %s", path)
}
- err := os.Chmod(filepath.Join(path...), mode)
+ err := os.Chmod(join(path...), mode)
if err != nil {
- t.Fatalf("chmod(%q): %s", filepath.Join(path...), err)
+ t.Fatalf("chmod(%q): %s", join(path...), err)
}
if shouldWait(path...) {
eventSeparator()
return "\t" + strings.ReplaceAll(s.String(), "\n", "\n\t")
}
+var join = filepath.Join
+
func isCI() bool {
_, ok := os.LookupEnv("CI")
return ok