From bfe94149837e2bad7fd1aa5756cc4755d065404e Mon Sep 17 00:00:00 2001 From: Tom Hayward Date: Thu, 26 Sep 2024 10:12:18 -0700 Subject: [PATCH] clean extra slashes from path --- fsnotify/filewatcher.go | 2 ++ fsnotify/filewatcher_test.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/fsnotify/filewatcher.go b/fsnotify/filewatcher.go index f68fead..1f9377d 100644 --- a/fsnotify/filewatcher.go +++ b/fsnotify/filewatcher.go @@ -5,6 +5,7 @@ import ( "log" "net/url" "os" + "path" "strings" "sync" "time" @@ -116,6 +117,7 @@ func (s *Strategy) setVal(pth string, val string) { // Watch implements the hotload.Strategy interface. func (s *Strategy) Watch(ctx context.Context, pth string, options url.Values) (value string, values <-chan string, err error) { + pth = path.Clean(pth) s.mu.Lock() defer s.mu.Unlock() // if this is the first time this strategy is called, initialize ourselves diff --git a/fsnotify/filewatcher_test.go b/fsnotify/filewatcher_test.go index 1b67aa4..de99018 100644 --- a/fsnotify/filewatcher_test.go +++ b/fsnotify/filewatcher_test.go @@ -167,6 +167,26 @@ var _ = Describe("FileWatcher", func() { os.Remove(args.pth) }, }), + Entry("extra slash in path", test{ + setup: func(args *args) { + f, _ := os.CreateTemp("", "unittest_") + f.Write([]byte("a")) + args.pth = "/" + f.Name() + f.Close() + }, + wantErr: false, + post: func(args *args, value string, values <-chan string) error { + if value != "a" { + return fmt.Errorf("expected 'a' got %v", value) + } + os.WriteFile(args.pth, []byte("b"), 0660) + assertStringFromChannel("wating for update b", "b", values) + return nil + }, + tearDown: func(args *args) { + os.Remove(args.pth) + }, + }), Entry("a, rm a, create b", test{ setup: func(args *args) { f, _ := os.CreateTemp("", "unittest_")