Skip to content

Commit

Permalink
Fixed word boundary tests on paths (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
cognitivegears authored Jul 7, 2021
1 parent f86ffb4 commit 4c7f232
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/result/pathresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func MatchPath(r *rule.Rule, path string) (rs []PathResult) {

for _, p := range dirParts {
p = filepath.ToSlash(p)
if r.MatchString(p, false) {
if len(r.FindMatchIndexes(p)) > 0 {
rs = append(rs, PathResult{LineResult: NewLineResult(r, p, path, 1, 1, 1)})
}
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/result/pathresult_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,33 @@ func TestMatchPathRules(t *testing.T) {
})
}
}

func TestMatchPathRulesBoundary(t *testing.T) {
tt := []struct {
path string
matches int
}{
{path: "/whitelist/list/test.go", matches: 1},
{path: "/testwhitelist/list/test.go", matches: 0},
{path: "/whitetest/list/test.go", matches: 0},
{path: "/whitelist.test/list/test.go", matches: 1},
{path: "/whitelist.test/blacklist/test.go", matches: 2},
{path: "/whitelisttest/blacklist/test.go", matches: 1},
{path: "/foo/bar/path_test.go", matches: 0},
{path: "/foo/bar/whitelistblacklist-new.go", matches: 0},
{path: "/foo/bar/whitelist-new.go", matches: 1},
}
for _, test := range tt {
t.Run(test.path, func(t *testing.T) {
defaultRules := rule.DefaultRules
for i := range defaultRules {
defaultRules[i].Options.WordBoundary = true
}
pr := MatchPathRules(defaultRules, test.path)
assert.Len(t, pr, test.matches)
for _, p := range pr {
assert.Equal(t, "Filename violation: "+p.Rule.Reason(p.LineResult.Violation), p.Reason())
}
})
}
}

0 comments on commit 4c7f232

Please sign in to comment.