From 4609806afe298803a938bf18e5d1888ed64d5b10 Mon Sep 17 00:00:00 2001 From: Tom Hayward Date: Fri, 27 Sep 2024 19:11:21 -0700 Subject: [PATCH] clean extra slashes from path (#33) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel GarcĂ­a --- 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 59b4885..5895c45 100644 --- a/fsnotify/filewatcher.go +++ b/fsnotify/filewatcher.go @@ -4,6 +4,7 @@ import ( "context" "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_")