From 79dd19459c0cf2058a70513fa1f8a26cb15e979b Mon Sep 17 00:00:00 2001 From: Akuma Isaac Akuma Date: Mon, 9 Sep 2019 11:12:53 +0100 Subject: [PATCH 1/2] Update watcher.go fileInfo is returning the wrong instance for rename and move --- watcher.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/watcher.go b/watcher.go index 4da4dfe..e185e1c 100644 --- a/watcher.go +++ b/watcher.go @@ -657,7 +657,7 @@ func (w *Watcher) pollEvents(files map[string]os.FileInfo, evt chan Event, Op: Move, Path: path2, OldPath: path1, - FileInfo: info1, + FileInfo: info2, } // If they are from the same directory, it's a rename // instead of a move event. From 2365d4c40c45040a3d157480a7c022695ea8d82c Mon Sep 17 00:00:00 2001 From: Akuma Isaac Akuma Date: Mon, 30 Dec 2019 19:02:47 +0100 Subject: [PATCH 2/2] added support for removing paths from ignored paths --- go.mod | 3 +++ watcher.go | 14 ++++++++++++++ watcher_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 go.mod diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ac238c1 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/Akumzy/watcher + +go 1.13 diff --git a/watcher.go b/watcher.go index e185e1c..0106cfd 100644 --- a/watcher.go +++ b/watcher.go @@ -428,6 +428,20 @@ func (w *Watcher) Ignore(paths ...string) (err error) { return nil } +// UnIgnore removes paths from ignored paths. +func (w *Watcher) UnIgnore(paths ...string) (err error) { + for _, path := range paths { + path, err = filepath.Abs(path) + if err != nil { + return err + } + w.mu.Lock() + delete(w.ignored, path) + w.mu.Unlock() + } + return nil +} + // WatchedFiles returns a map of files added to a Watcher. func (w *Watcher) WatchedFiles() map[string]os.FileInfo { w.mu.Lock() diff --git a/watcher_test.go b/watcher_test.go index 4453b99..cf495fb 100644 --- a/watcher_test.go +++ b/watcher_test.go @@ -240,6 +240,44 @@ func TestIgnore(t *testing.T) { } } +func TestUnIgnore(t *testing.T) { + testDir, teardown := setup(t) + defer teardown() + + w := New() + + err := w.Add(testDir) + if err != nil { + t.Errorf("expected error to be nil, got %s", err) + } + if len(w.files) != 7 { + t.Errorf("expected len(w.files) to be 7, got %d", len(w.files)) + } + + err = w.Ignore(testDir) + if err != nil { + t.Errorf("expected error to be nil, got %s", err) + } + if len(w.files) != 0 { + t.Errorf("expected len(w.files) to be 0, got %d", len(w.files)) + } + + err = w.UnIgnore(testDir) + if err != nil { + t.Errorf("expected error to be nil, got %s", err) + } + + + // Now try to add the ignored directory. + err = w.Add(testDir) + if err != nil { + t.Errorf("expected error to be nil, got %s", err) + } + if len(w.files) < 0 { + t.Errorf("expected len(w.files) to be greater than 0, got %d", len(w.files)) + } +} + func TestRemove(t *testing.T) { testDir, teardown := setup(t) defer teardown()