diff options
author | Martin Polden <mpolden@mpolden.no> | 2018-05-11 17:30:03 +0200 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2018-05-11 17:30:03 +0200 |
commit | 824b9143087ce8feccf66ad8c6687d9fe1b36578 (patch) | |
tree | 9b5554b58d50a7fa3be530dde206e78f83532f98 | |
parent | fb3d4cfece80379c2150c79c467d741a5e54d612 (diff) |
Simplify
-rw-r--r-- | pathutil/pathutil.go | 18 | ||||
-rw-r--r-- | pathutil/pathutil_test.go | 27 | ||||
-rw-r--r-- | watcher/watcher.go | 2 |
3 files changed, 11 insertions, 36 deletions
diff --git a/pathutil/pathutil.go b/pathutil/pathutil.go index e59b56a..e4b8edb 100644 --- a/pathutil/pathutil.go +++ b/pathutil/pathutil.go @@ -11,20 +11,10 @@ func Depth(name string) int { return strings.Count(name, string(os.PathSeparator)) } -func IsHidden(name string) bool { - if strings.ContainsRune(name, os.PathSeparator) { - name = filepath.Base(name) - } - return strings.HasPrefix(name, ".") -} - -func IsParentHidden(name string) bool { - if !strings.ContainsRune(name, os.PathSeparator) { - return false - } - components := strings.Split(filepath.Dir(name), string(os.PathSeparator)) - for _, c := range components { - if IsHidden(c) { +func ContainsHidden(path string) bool { + names := strings.Split(path, string(os.PathSeparator)) + for _, name := range names { + if strings.HasPrefix(name, ".") { return true } } diff --git a/pathutil/pathutil_test.go b/pathutil/pathutil_test.go index 08334b1..8f1189e 100644 --- a/pathutil/pathutil_test.go +++ b/pathutil/pathutil_test.go @@ -19,35 +19,20 @@ func TestDepth(t *testing.T) { } } -func TestIsHidden(t *testing.T) { +func TestContainsHidden(t *testing.T) { var tests = []struct { in string out bool }{ - {"/foo/.bar", true}, + {"foo", false}, {"/foo/bar", false}, - {"/foo/.bar/baz", false}, - } - for _, tt := range tests { - if got := IsHidden(tt.in); got != tt.out { - t.Errorf("want %t, got %t for %s", tt.out, got, tt.in) - } - } -} - -func TestIsParentHidden(t *testing.T) { - var tests = []struct { - in string - out bool - }{ - {".bar", false}, - {"/foo/.bar", false}, + {".bar", true}, + {"/foo/.bar", true}, {"/foo/.bar/baz", true}, - {"/foo/.bar/baz/foo", true}, - {"/foo/.bar/baz/foo/bar", true}, + {"/.foo/bar/baz", true}, } for _, tt := range tests { - if got := IsParentHidden(tt.in); got != tt.out { + if got := ContainsHidden(tt.in); got != tt.out { t.Errorf("want %t, got %t for %s", tt.out, got, tt.in) } } diff --git a/watcher/watcher.go b/watcher/watcher.go index d96e10e..973b0a1 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -33,7 +33,7 @@ func (w *watcher) handle(name string) error { if !ok { return errors.Errorf("no configured path found: %s", name) } - if p.SkipHidden && (pathutil.IsHidden(name) || pathutil.IsParentHidden(name)) { + if p.SkipHidden && pathutil.ContainsHidden(name) { return errors.Errorf("hidden parent dir or file: %s", name) } depth := pathutil.Depth(name) |