Skip to content

Commit 2626995

Browse files
committed
add sub-sub-directory support for ignored_folder
This commit also "fixes" a subtle "bug" with the previous implementation of isIgnoredFolder. The previous implementation tested whether a folder is ignored by (1) splitting the path to be tested using strings.Split with the seperator "/" and (2) testing whether the first part of the path exists in w.IgnoredFolders. However, this does not work on architectures that use a different path seperator. In practice, this "bug" does not present an issue as an ignored sub-directory is skipped entirely by filepath.Walk.
1 parent c22826c commit 2626995

File tree

4 files changed

+104
-8
lines changed

4 files changed

+104
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ err = http.ListenAndServe(":3000", web.ErrorChecker(m))
5656
# The root of your application relative to your configuration file.
5757
app_root: .
5858
# List of folders you don't want to watch. The more folders you ignore, the
59-
# faster things will be.
59+
# faster things will be. For cross-platform compatibility, use forward slashes
60+
# as the path separator (i.e. 'cmd/web/client', not 'cmd\\web\\client').
6061
ignored_folders:
6162
- vendor
6263
- log

refresh/watcher.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,20 @@ func (w *Watcher) Start() {
5858
}
5959

6060
func (w Watcher) isIgnoredFolder(path string) bool {
61-
paths := strings.Split(path, "/")
62-
if len(paths) <= 0 {
63-
return false
64-
}
65-
6661
for _, e := range w.IgnoredFolders {
67-
if strings.TrimSpace(e) == paths[0] {
68-
return true
62+
rel, err := filepath.Rel(e, path)
63+
if err != nil {
64+
// unable to construct relative path, not an ignored folder
65+
continue
6966
}
67+
68+
if strings.Contains(rel, "..") {
69+
// to construct a relative path requires going up the directory tree, not
70+
// an ignored folder
71+
continue
72+
}
73+
74+
return true
7075
}
7176
return false
7277
}

refresh/watcher_posix_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// +build linux darwin
2+
3+
package refresh
4+
5+
import (
6+
"testing"
7+
)
8+
9+
func TestIsIgnoredFolder(t *testing.T) {
10+
ignoredFolders := []string{
11+
"cmd/web/client",
12+
"vendor",
13+
}
14+
15+
isIgnoredFolderTests := []struct {
16+
path string
17+
ignoredFolder bool
18+
}{
19+
{"cmd/web", false},
20+
{"cmd/web/main.go", false},
21+
{"cmd/web/client", true},
22+
{"cmd/web/client/src", true},
23+
{"pkg", false},
24+
{"pkg/cmd/web/client", false},
25+
{".", false},
26+
}
27+
28+
watcher := Watcher{
29+
Manager: &Manager{
30+
Configuration: &Configuration{
31+
IgnoredFolders: ignoredFolders,
32+
},
33+
},
34+
}
35+
36+
for _, tc := range isIgnoredFolderTests {
37+
if watcher.isIgnoredFolder(tc.path) != tc.ignoredFolder {
38+
if tc.ignoredFolder {
39+
t.Errorf("expected path '%s' to be ignored", tc.path)
40+
} else {
41+
t.Errorf("expected path '%s' not to be ignored", tc.path)
42+
}
43+
}
44+
}
45+
}

refresh/watcher_windows_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// +build windows
2+
3+
package refresh
4+
5+
import (
6+
"testing"
7+
)
8+
9+
func TestIsIgnoredFolder(t *testing.T) {
10+
ignoredFolders := []string{
11+
"cmd/web/client",
12+
"vendor",
13+
}
14+
15+
isIgnoredFolderTests := []struct {
16+
path string
17+
ignoredFolder bool
18+
}{
19+
{"cmd\\web", false},
20+
{"cmd\\web\\main.go", false},
21+
{"cmd\\web\\client", true},
22+
{"cmd\\web\\client\\src", true},
23+
{"pkg", false},
24+
{"pkg\\cmd\\web\\client", false},
25+
{".", false},
26+
}
27+
28+
watcher := Watcher{
29+
Manager: &Manager{
30+
Configuration: &Configuration{
31+
IgnoredFolders: ignoredFolders,
32+
},
33+
},
34+
}
35+
36+
for _, tc := range isIgnoredFolderTests {
37+
if watcher.isIgnoredFolder(tc.path) != tc.ignoredFolder {
38+
if tc.ignoredFolder {
39+
t.Errorf("expected path '%s' to be ignored", tc.path)
40+
} else {
41+
t.Errorf("expected path '%s' not to be ignored", tc.path)
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)