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 4da4dfe..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() @@ -657,7 +671,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. 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()