Adding case sensitive exclude rules support

Signed-off-by: Maciej "Iwan" Iwanowski <maciej.iwanowski@critical.today>
This commit is contained in:
Maciej "Iwan" Iwanowski 2020-04-23 09:23:37 +02:00
parent d9970e4862
commit 05138497f2
No known key found for this signature in database
GPG Key ID: 2484258A4DD3EE84
3 changed files with 147 additions and 16 deletions

View File

@ -118,3 +118,36 @@ func (ExcludeRules) Name() string { return "exclude-rules" }
func (ExcludeRules) Finish() {}
var _ Processor = ExcludeRules{}
type ExcludeRulesCaseSensitive struct {
*ExcludeRules
}
func NewExcludeRulesCaseSensitive(rules []ExcludeRule, lineCache *fsutils.LineCache, log logutils.Log) *ExcludeRulesCaseSensitive {
r := &ExcludeRules{
lineCache: lineCache,
log: log,
}
for _, rule := range rules {
parsedRule := excludeRule{
linters: rule.Linters,
}
if rule.Text != "" {
parsedRule.text = regexp.MustCompile(rule.Text)
}
if rule.Source != "" {
parsedRule.source = regexp.MustCompile(rule.Source)
}
if rule.Path != "" {
parsedRule.path = regexp.MustCompile(rule.Path)
}
r.rules = append(r.rules, parsedRule)
}
return &ExcludeRulesCaseSensitive{r}
}
func (ExcludeRulesCaseSensitive) Name() string { return "exclude-rules-case-sensitive" }
var _ Processor = ExcludeCaseSensitive{}

View File

@ -31,26 +31,12 @@ func TestExcludeRulesMultiple(t *testing.T) {
Linters: []string{"lll"},
},
}, lineCache, nil)
type issueCase struct {
Path string
Line int
Text string
Linter string
}
var newIssueCase = func(c issueCase) result.Issue {
return result.Issue{
Text: c.Text,
FromLinter: c.Linter,
Pos: token.Position{
Filename: c.Path,
Line: c.Line,
},
}
}
cases := []issueCase{
{Path: "e.go", Text: "exclude", Linter: "linter"},
{Path: "e.go", Text: "some", Linter: "linter"},
{Path: "e_test.go", Text: "normal", Linter: "testlinter"},
{Path: "e_Test.go", Text: "normal", Linter: "testlinter"},
{Path: "e_test.go", Text: "another", Linter: "linter"},
{Path: "e_test.go", Text: "testonly", Linter: "linter"},
{Path: filepath.Join("testdata", "exclude_rules.go"), Line: 3, Linter: "lll"},
@ -71,11 +57,30 @@ func TestExcludeRulesMultiple(t *testing.T) {
}
expectedCases := []issueCase{
{Path: "e.go", Text: "some", Linter: "linter"},
{Path: "e_Test.go", Text: "normal", Linter: "testlinter"},
{Path: "e_test.go", Text: "another", Linter: "linter"},
}
assert.Equal(t, expectedCases, resultingCases)
}
type issueCase struct {
Path string
Line int
Text string
Linter string
}
func newIssueCase(c issueCase) result.Issue {
return result.Issue{
Text: c.Text,
FromLinter: c.Linter,
Pos: token.Position{
Filename: c.Path,
Line: c.Line,
},
}
}
func TestExcludeRulesText(t *testing.T) {
p := NewExcludeRules([]ExcludeRule{
{
@ -103,6 +108,96 @@ func TestExcludeRulesText(t *testing.T) {
}
assert.Equal(t, texts[1:], processedTexts)
}
func TestExcludeRulesEmpty(t *testing.T) {
processAssertSame(t, NewExcludeRules(nil, nil, nil), newTextIssue("test"))
}
func TestExcludeRulesCaseSensitiveMultiple(t *testing.T) {
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
p := NewExcludeRulesCaseSensitive([]ExcludeRule{
{
Text: "^exclude$",
Linters: []string{"linter"},
},
{
Linters: []string{"testlinter"},
Path: `_test\.go`,
},
{
Text: "^testonly$",
Path: `_test\.go`,
},
{
Source: "^//go:generate ",
Linters: []string{"lll"},
},
}, lineCache, nil)
cases := []issueCase{
{Path: "e.go", Text: "exclude", Linter: "linter"},
{Path: "e.go", Text: "excLude", Linter: "linter"},
{Path: "e.go", Text: "some", Linter: "linter"},
{Path: "e_test.go", Text: "normal", Linter: "testlinter"},
{Path: "e_Test.go", Text: "normal", Linter: "testlinter"},
{Path: "e_test.go", Text: "another", Linter: "linter"},
{Path: "e_test.go", Text: "testonly", Linter: "linter"},
{Path: "e_test.go", Text: "testOnly", Linter: "linter"},
{Path: filepath.Join("testdata", "exclude_rules_case_sensitive.go"), Line: 3, Linter: "lll"},
}
var issues []result.Issue
for _, c := range cases {
issues = append(issues, newIssueCase(c))
}
processedIssues := process(t, p, issues...)
var resultingCases []issueCase
for _, i := range processedIssues {
resultingCases = append(resultingCases, issueCase{
Path: i.FilePath(),
Linter: i.FromLinter,
Text: i.Text,
Line: i.Line(),
})
}
expectedCases := []issueCase{
{Path: "e.go", Text: "excLude", Linter: "linter"},
{Path: "e.go", Text: "some", Linter: "linter"},
{Path: "e_Test.go", Text: "normal", Linter: "testlinter"},
{Path: "e_test.go", Text: "another", Linter: "linter"},
{Path: "e_test.go", Text: "testOnly", Linter: "linter"},
{Path: filepath.Join("testdata", "exclude_rules_case_sensitive.go"), Line: 3, Linter: "lll"},
}
assert.Equal(t, expectedCases, resultingCases)
}
func TestExcludeRulesCaseSensitiveText(t *testing.T) {
p := NewExcludeRulesCaseSensitive([]ExcludeRule{
{
Text: "^exclude$",
Linters: []string{
"linter",
},
},
}, nil, nil)
texts := []string{"exclude", "excLude", "1", "", "exclud", "notexclude"}
var issues []result.Issue
for _, t := range texts {
issues = append(issues, result.Issue{
Text: t,
FromLinter: "linter",
})
}
processedIssues := process(t, p, issues...)
assert.Len(t, processedIssues, len(issues)-1)
var processedTexts []string
for _, i := range processedIssues {
processedTexts = append(processedTexts, i.Text)
}
assert.Equal(t, texts[1:], processedTexts)
}
func TestExcludeRulesCaseSensitiveEmpty(t *testing.T) {
processAssertSame(t, NewExcludeRulesCaseSensitive(nil, nil, nil), newTextIssue("test"))
}

View File

@ -0,0 +1,3 @@
package testdata
//GO:generate long line that will be excluded by default processor but will not be affected by case-sensitive one because of capital GO