From 2bdd47e6572fc9c3f4da73ef918b114e84dfd699 Mon Sep 17 00:00:00 2001 From: Martin Tournoij Date: Sat, 15 Oct 2022 00:18:41 +0200 Subject: [PATCH] filepath.Join is so much work to type --- backend_fen_test.go | 5 ++- backend_inotify_test.go | 9 +++-- backend_kqueue_test.go | 5 ++- backend_windows_test.go | 5 ++- fsnotify_test.go | 78 ++++++++++++++++++++--------------------- helpers_test.go | 56 +++++++++++++++-------------- system_darwin_test.go | 5 ++- 7 files changed, 80 insertions(+), 83 deletions(-) diff --git a/backend_fen_test.go b/backend_fen_test.go index 16df761..50a7b00 100644 --- a/backend_fen_test.go +++ b/backend_fen_test.go @@ -5,7 +5,6 @@ package fsnotify import ( "fmt" - "path/filepath" "strings" "testing" ) @@ -13,8 +12,8 @@ import ( func TestRemoveState(t *testing.T) { var ( tmp = t.TempDir() - dir = filepath.Join(tmp, "dir") - file = filepath.Join(dir, "file") + dir = join(tmp, "dir") + file = join(dir, "file") ) mkdir(t, dir) touch(t, file) diff --git a/backend_inotify_test.go b/backend_inotify_test.go index 0b6c918..f0806f0 100644 --- a/backend_inotify_test.go +++ b/backend_inotify_test.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "os" - "path/filepath" "strconv" "strings" "sync" @@ -86,7 +85,7 @@ func TestInotifyOverflow(t *testing.T) { go func(i int) { defer wg.Done() - dir := filepath.Join(tmp, strconv.Itoa(i)) + dir := join(tmp, strconv.Itoa(i)) mkdir(t, dir, noWait) addWatch(t, w, dir) @@ -133,7 +132,7 @@ func TestInotifyDeleteOpenFile(t *testing.T) { t.Parallel() tmp := t.TempDir() - file := filepath.Join(tmp, "file") + file := join(tmp, "file") touch(t, file) fp, err := os.Open(file) @@ -157,8 +156,8 @@ func TestInotifyDeleteOpenFile(t *testing.T) { func TestRemoveState(t *testing.T) { var ( tmp = t.TempDir() - dir = filepath.Join(tmp, "dir") - file = filepath.Join(dir, "file") + dir = join(tmp, "dir") + file = join(dir, "file") ) mkdir(t, dir) touch(t, file) diff --git a/backend_kqueue_test.go b/backend_kqueue_test.go index 590d985..094b1db 100644 --- a/backend_kqueue_test.go +++ b/backend_kqueue_test.go @@ -5,7 +5,6 @@ package fsnotify import ( "fmt" - "path/filepath" "strings" "testing" ) @@ -13,8 +12,8 @@ import ( func TestRemoveState(t *testing.T) { var ( tmp = t.TempDir() - dir = filepath.Join(tmp, "dir") - file = filepath.Join(dir, "file") + dir = join(tmp, "dir") + file = join(dir, "file") ) mkdir(t, dir) touch(t, file) diff --git a/backend_windows_test.go b/backend_windows_test.go index 213f14c..7dc46bb 100644 --- a/backend_windows_test.go +++ b/backend_windows_test.go @@ -5,7 +5,6 @@ package fsnotify import ( "fmt" - "path/filepath" "strings" "testing" ) @@ -16,8 +15,8 @@ func TestRemoveState(t *testing.T) { var ( tmp = t.TempDir() - dir = filepath.Join(tmp, "dir") - file = filepath.Join(dir, "file") + dir = join(tmp, "dir") + file = join(dir, "file") ) mkdir(t, dir) touch(t, file) diff --git a/fsnotify_test.go b/fsnotify_test.go index e846fd6..78ac848 100644 --- a/fsnotify_test.go +++ b/fsnotify_test.go @@ -28,7 +28,7 @@ func init() { 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) @@ -47,8 +47,8 @@ func TestWatch(t *testing.T) { `}, {"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) @@ -66,9 +66,9 @@ func TestWatch(t *testing.T) { {"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 @@ -147,7 +147,7 @@ func TestWatch(t *testing.T) { `}, {"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) @@ -167,8 +167,8 @@ func TestWatch(t *testing.T) { 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) @@ -196,8 +196,8 @@ func TestWatch(t *testing.T) { 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) @@ -248,7 +248,7 @@ func TestWatchCreate(t *testing.T) { {"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 `}, @@ -302,7 +302,7 @@ func TestWatchWrite(t *testing.T) { 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) @@ -333,7 +333,7 @@ func TestWatchWrite(t *testing.T) { `}, {"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) @@ -368,7 +368,7 @@ func TestWatchWrite(t *testing.T) { 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) @@ -383,7 +383,7 @@ func TestWatchRename(t *testing.T) { 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 `}, @@ -394,8 +394,8 @@ func TestWatchRename(t *testing.T) { } 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) @@ -419,7 +419,7 @@ func TestWatchRename(t *testing.T) { {"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) @@ -441,7 +441,7 @@ func TestWatchRename(t *testing.T) { `}, {"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) @@ -459,8 +459,8 @@ func TestWatchRename(t *testing.T) { `}, {"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) @@ -477,8 +477,8 @@ func TestWatchRename(t *testing.T) { `}, {"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) @@ -512,7 +512,7 @@ func TestWatchSymlink(t *testing.T) { {"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 @@ -564,14 +564,14 @@ func TestWatchSymlink(t *testing.T) { 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 @@ -592,7 +592,7 @@ func TestWatchSymlink(t *testing.T) { 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) @@ -605,7 +605,7 @@ func TestWatchAttrib(t *testing.T) { `}, {"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) @@ -620,7 +620,7 @@ func TestWatchAttrib(t *testing.T) { `}, {"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) @@ -646,7 +646,7 @@ func TestWatchAttrib(t *testing.T) { 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) @@ -665,7 +665,7 @@ func TestWatchRm(t *testing.T) { 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. @@ -691,7 +691,7 @@ func TestWatchRm(t *testing.T) { 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) @@ -809,7 +809,7 @@ func TestClose(t *testing.T) { 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) } @@ -899,7 +899,7 @@ func TestClose(t *testing.T) { 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) @@ -922,7 +922,7 @@ func TestAdd(t *testing.T) { 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) @@ -1215,8 +1215,8 @@ func TestWatchList(t *testing.T) { 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) diff --git a/helpers_test.go b/helpers_test.go index 0be25fb..22bb6df 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -64,16 +64,16 @@ func addWatch(t *testing.T, w *Watcher, path ...string) { 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 @@ -105,7 +105,7 @@ func createFiles(t *testing.T, dir, prefix string, n int, d time.Duration) int { 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 @@ -128,9 +128,9 @@ func mkdir(t *testing.T, path ...string) { 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() @@ -143,9 +143,9 @@ func mkdir(t *testing.T, path ...string) { // 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() @@ -158,9 +158,9 @@ func symlink(t *testing.T, target string, link ...string) { 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() @@ -173,9 +173,9 @@ func mkfifo(t *testing.T, path ...string) { 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() @@ -188,9 +188,9 @@ func mknod(t *testing.T, dev int, path ...string) { 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() @@ -205,7 +205,7 @@ func cat(t *testing.T, data string, path ...string) { } 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 } @@ -227,7 +227,7 @@ func cat(t *testing.T, data string, path ...string) { return fp.Close() }() if err != nil { - t.Fatalf("cat(%q): %s", filepath.Join(path...), err) + t.Fatalf("cat(%q): %s", join(path...), err) } } @@ -237,13 +237,13 @@ func touch(t *testing.T, path ...string) { 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() @@ -257,9 +257,9 @@ func mv(t *testing.T, src string, dst ...string) { 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() @@ -272,9 +272,9 @@ func rm(t *testing.T, path ...string) { 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() @@ -287,9 +287,9 @@ func rmAll(t *testing.T, path ...string) { 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() @@ -302,9 +302,9 @@ func chmod(t *testing.T, mode fs.FileMode, path ...string) { 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() @@ -553,6 +553,8 @@ func indent(s fmt.Stringer) string { return "\t" + strings.ReplaceAll(s.String(), "\n", "\n\t") } +var join = filepath.Join + func isCI() bool { _, ok := os.LookupEnv("CI") return ok diff --git a/system_darwin_test.go b/system_darwin_test.go index 48cabe9..75fdb65 100644 --- a/system_darwin_test.go +++ b/system_darwin_test.go @@ -5,7 +5,6 @@ package fsnotify import ( "os" - "path/filepath" "strconv" "strings" "testing" @@ -49,8 +48,8 @@ func testExchangedataForWatcher(t *testing.T, watchDir bool) { // 2. unlink intermediate // // Let's try to simulate that: - resolved := filepath.Join(testDir1, resolvedFilename) - intermediate := filepath.Join(testDir2, resolvedFilename+"~") + resolved := join(testDir1, resolvedFilename) + intermediate := join(testDir2, resolvedFilename+"~") // Make sure we create the file before we start watching createAndSyncFile(t, resolved) -- 2.50.1