diff --git a/http/data.go b/http/data.go index 10f136b6..1bdafe3f 100644 --- a/http/data.go +++ b/http/data.go @@ -27,7 +27,7 @@ type data struct { // Check implements rules.Checker. func (d *data) Check(path string) bool { - if d.user.HideDotfiles && rules.MatchHidden.Matches(path) { + if d.user.HideDotfiles && rules.MatchHidden(path) { return false } diff --git a/rules/rules.go b/rules/rules.go index f58b1766..5fe2fb8a 100644 --- a/rules/rules.go +++ b/rules/rules.go @@ -1,6 +1,7 @@ package rules import ( + "path/filepath" "regexp" "strings" ) @@ -18,9 +19,10 @@ type Rule struct { Regexp *Regexp `json:"regexp"` } -var MatchHidden = Rule{ - Regex: true, - Regexp: &Regexp{Raw: `\/\..*$`}, +// MatchHidden matches paths with a basename +// that begins with a dot. +func MatchHidden(path string) bool { + return filepath.Base(path)[0] == '.' } // Matches matches a path against a rule. diff --git a/rules/rules_test.go b/rules/rules_test.go new file mode 100644 index 00000000..c9613481 --- /dev/null +++ b/rules/rules_test.go @@ -0,0 +1,23 @@ +package rules + +import "testing" + +func TestMatchHidden(t *testing.T) { + cases := map[string]bool{ + "/": false, + "/src": false, + "/src/": false, + "/.circleci": true, + "/a/b/c/.docker.json": true, + ".docker.json": true, + "Dockerfile": false, + "/Dockerfile": false, + } + + for path, truth := range cases { + got := MatchHidden(path) + if got != truth { + t.Errorf("MatchHidden(%s)=%v; want %v", path, got, truth) + } + } +}