diff --git a/README.md b/README.md index b3a52ba..af4dc7a 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,8 @@ err = http.ListenAndServe(":3000", web.ErrorChecker(m)) # The root of your application relative to your configuration file. app_root: . # List of folders you don't want to watch. The more folders you ignore, the -# faster things will be. +# faster things will be. For cross-platform compatibility, use forward slashes +# as the path separator (i.e. 'cmd/web/client', not 'cmd\\web\\client'). ignored_folders: - vendor - log diff --git a/refresh/watcher.go b/refresh/watcher.go index aeb6426..cd39f1a 100644 --- a/refresh/watcher.go +++ b/refresh/watcher.go @@ -61,15 +61,20 @@ func (w *Watcher) Start() { } func (w Watcher) isIgnoredFolder(path string) bool { - paths := strings.Split(path, "/") - if len(paths) <= 0 { - return false - } - for _, e := range w.IgnoredFolders { - if strings.TrimSpace(e) == paths[0] { - return true + rel, err := filepath.Rel(e, path) + if err != nil { + // unable to construct relative path, not an ignored folder + continue } + + if strings.Contains(rel, "..") { + // to construct a relative path requires going up the directory tree, not + // an ignored folder + continue + } + + return true } return false } diff --git a/refresh/watcher_posix_test.go b/refresh/watcher_posix_test.go new file mode 100644 index 0000000..112050e --- /dev/null +++ b/refresh/watcher_posix_test.go @@ -0,0 +1,45 @@ +// +build linux darwin + +package refresh + +import ( + "testing" +) + +func TestIsIgnoredFolder(t *testing.T) { + ignoredFolders := []string{ + "cmd/web/client", + "vendor", + } + + isIgnoredFolderTests := []struct { + path string + ignoredFolder bool + }{ + {"cmd/web", false}, + {"cmd/web/main.go", false}, + {"cmd/web/client", true}, + {"cmd/web/client/src", true}, + {"pkg", false}, + {"pkg/cmd/web/client", false}, + {".", false}, + } + + watcher := Watcher{ + Manager: &Manager{ + Configuration: &Configuration{ + IgnoredFolders: ignoredFolders, + }, + }, + } + + for _, tc := range isIgnoredFolderTests { + if watcher.isIgnoredFolder(tc.path) != tc.ignoredFolder { + if tc.ignoredFolder { + t.Errorf("expected path '%s' to be ignored", tc.path) + } else { + t.Errorf("expected path '%s' not to be ignored", tc.path) + } + } + } +} diff --git a/refresh/watcher_windows_test.go b/refresh/watcher_windows_test.go new file mode 100644 index 0000000..bd9b679 --- /dev/null +++ b/refresh/watcher_windows_test.go @@ -0,0 +1,45 @@ +// +build windows + +package refresh + +import ( + "testing" +) + +func TestIsIgnoredFolder(t *testing.T) { + ignoredFolders := []string{ + "cmd/web/client", + "vendor", + } + + isIgnoredFolderTests := []struct { + path string + ignoredFolder bool + }{ + {"cmd\\web", false}, + {"cmd\\web\\main.go", false}, + {"cmd\\web\\client", true}, + {"cmd\\web\\client\\src", true}, + {"pkg", false}, + {"pkg\\cmd\\web\\client", false}, + {".", false}, + } + + watcher := Watcher{ + Manager: &Manager{ + Configuration: &Configuration{ + IgnoredFolders: ignoredFolders, + }, + }, + } + + for _, tc := range isIgnoredFolderTests { + if watcher.isIgnoredFolder(tc.path) != tc.ignoredFolder { + if tc.ignoredFolder { + t.Errorf("expected path '%s' to be ignored", tc.path) + } else { + t.Errorf("expected path '%s' not to be ignored", tc.path) + } + } + } +}