diff --git a/testdata/ignore-test-name.txt b/testdata/ignore-test-name.txt new file mode 100644 index 0000000..e69de29 diff --git a/testdata/ignore-test-name/main.go b/testdata/ignore-test-name/main.go new file mode 100644 index 0000000..13a13b4 --- /dev/null +++ b/testdata/ignore-test-name/main.go @@ -0,0 +1 @@ +package ignoretestname diff --git a/testdata/test-duplicated-paths/file-1.txt b/testdata/test-duplicated-paths/file-1.txt new file mode 100644 index 0000000..e69de29 diff --git a/testdata/test-duplicated-paths/file-2.txt b/testdata/test-duplicated-paths/file-2.txt new file mode 100644 index 0000000..e69de29 diff --git a/watcher.go b/watcher.go index 1772532..c872237 100644 --- a/watcher.go +++ b/watcher.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "regexp" "strings" "time" @@ -158,6 +159,7 @@ func (w *watcher) ignoreFile(path string, info os.FileInfo) bool { if _, ignored := w.ignoreItems[path]; ignored { return true } + return false } @@ -180,7 +182,7 @@ func resolvePaths(paths []string, extensions map[string]bool) (map[string]bool, // don't care for extension filter right now for non glob paths // since they could be a directory if isGlob { - if _, ok := extensions[filepath.Ext(path)]; !ok { + if _, ok := extensions[filepath.Ext(match)]; !ok { continue } } @@ -198,13 +200,19 @@ func resolvePaths(paths []string, extensions map[string]bool) (map[string]bool, // remove overlapped paths so it makes the scan for changes later faster and simpler func removeOverlappedPaths(mapPaths map[string]bool) { + startDot := regexp.MustCompile(`^\./`) + for p1 := range mapPaths { + p1 = startDot.ReplaceAllString(p1, "") + // skip to next item if this path has already been checked if v, ok := mapPaths[p1]; ok && !v { continue } for p2 := range mapPaths { + p2 = startDot.ReplaceAllString(p2, "") + if p1 == p2 { continue } diff --git a/watcher_test.go b/watcher_test.go index 7f42432..73f701a 100644 --- a/watcher_test.go +++ b/watcher_test.go @@ -1,6 +1,7 @@ package gaper import ( + "log" "os" "path/filepath" "runtime" @@ -60,7 +61,7 @@ func TestWatcherGlobPath(t *testing.T) { func TestWatcherRemoveOverlapdPaths(t *testing.T) { pollInterval := 0 watchItems := []string{filepath.Join("testdata", "server")} - ignoreItems := []string{"./testdata/**/*", "./testdata/server"} + ignoreItems := []string{"./testdata/server/**/*", "./testdata/server"} var extensions []string wCfg := WatcherConfig{ @@ -120,3 +121,132 @@ func TestWatcherWatchChange(t *testing.T) { assert.Nil(t, err, "wacher event error") } } + +func TestWatcherIgnoreFile(t *testing.T) { + testCases := []struct { + name, file, ignoreFile string + defaultIgnore, expectIgnore bool + }{ + { + name: "with default ignore enabled it ignores vendor folder", + file: "vendor", + defaultIgnore: true, + expectIgnore: true, + }, + { + name: "without default ignore enabled it does not ignore vendor folder", + file: "vendor", + defaultIgnore: false, + expectIgnore: false, + }, + { + name: "with default ignore enabled it ignores test file", + file: filepath.Join("testdata", "server", "main_test.go"), + defaultIgnore: true, + expectIgnore: true, + }, + { + name: "with default ignore enabled it does no ignore non test files which have test in the name", + file: filepath.Join("testdata", "ignore-test-name.txt"), + defaultIgnore: true, + expectIgnore: false, + }, + { + name: "without default ignore enabled it does not ignore test file", + file: filepath.Join("testdata", "server", "main_test.go"), + defaultIgnore: false, + expectIgnore: false, + }, + { + name: "with default ignore enabled it ignores ignored items", + file: filepath.Join("testdata", "server", "main.go"), + ignoreFile: filepath.Join("testdata", "server", "main.go"), + defaultIgnore: true, + expectIgnore: true, + }, + { + name: "without default ignore enabled it ignores ignored items", + file: filepath.Join("testdata", "server", "main.go"), + ignoreFile: filepath.Join("testdata", "server", "main.go"), + defaultIgnore: false, + expectIgnore: true, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + srvdir := "." + + watchItems := []string{srvdir} + ignoreItems := []string{} + if len(tc.ignoreFile) > 0 { + ignoreItems = append(ignoreItems, tc.ignoreFile) + } + extensions := []string{"go"} + + wCfg := WatcherConfig{ + DefaultIgnore: tc.defaultIgnore, + WatchItems: watchItems, + IgnoreItems: ignoreItems, + Extensions: extensions, + } + w, err := NewWatcher(wCfg) + assert.Nil(t, err, "wacher error") + + wt := w.(*watcher) + + filePath := tc.file + file, err := os.Open(filePath) + if err != nil { + log.Fatal(err) + } + + fileInfo, err := file.Stat() + if err != nil { + log.Fatal(err) + } + + assert.Equal(t, tc.expectIgnore, wt.ignoreFile(filePath, fileInfo)) + }) + } +} + +func TestWatcherResolvePaths(t *testing.T) { + testCases := []struct { + name string + paths []string + extensions, expectPaths map[string]bool + err error + }{ + { + name: "remove duplicated paths", + paths: []string{"testdata/test-duplicated-paths", "testdata/test-duplicated-paths"}, + extensions: map[string]bool{".txt": true}, + expectPaths: map[string]bool{"testdata/test-duplicated-paths": true}, + }, + { + name: "remove duplicated paths from glob", + paths: []string{"testdata/test-duplicated-paths", "testdata/test-duplicated-paths/**/*"}, + extensions: map[string]bool{".txt": true}, + expectPaths: map[string]bool{"testdata/test-duplicated-paths": true}, + }, + { + name: "remove duplicated paths from glob with inverse order", + paths: []string{"testdata/test-duplicated-paths/**/*", "testdata/test-duplicated-paths"}, + extensions: map[string]bool{".txt": true}, + expectPaths: map[string]bool{"testdata/test-duplicated-paths": true}, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + paths, err := resolvePaths(tc.paths, tc.extensions) + if tc.err == nil { + assert.Nil(t, err, "resolve path error") + assert.Equal(t, tc.expectPaths, paths) + } else { + assert.Equal(t, tc.err, err) + } + }) + } +}