From 05138497f2fbef10c298c77fbff957ee8829e88c Mon Sep 17 00:00:00 2001 From: "Maciej \"Iwan\" Iwanowski" Date: Thu, 23 Apr 2020 09:23:37 +0200 Subject: [PATCH] Adding case sensitive exclude rules support Signed-off-by: Maciej "Iwan" Iwanowski --- pkg/result/processors/exclude_rules.go | 33 +++++ pkg/result/processors/exclude_rules_test.go | 127 +++++++++++++++--- .../testdata/exclude_rules_case_sensitive.go | 3 + 3 files changed, 147 insertions(+), 16 deletions(-) create mode 100644 pkg/result/processors/testdata/exclude_rules_case_sensitive.go diff --git a/pkg/result/processors/exclude_rules.go b/pkg/result/processors/exclude_rules.go index 6b16fdc5..f577af82 100644 --- a/pkg/result/processors/exclude_rules.go +++ b/pkg/result/processors/exclude_rules.go @@ -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{} diff --git a/pkg/result/processors/exclude_rules_test.go b/pkg/result/processors/exclude_rules_test.go index ee2f15e1..80759d4f 100644 --- a/pkg/result/processors/exclude_rules_test.go +++ b/pkg/result/processors/exclude_rules_test.go @@ -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")) +} diff --git a/pkg/result/processors/testdata/exclude_rules_case_sensitive.go b/pkg/result/processors/testdata/exclude_rules_case_sensitive.go new file mode 100644 index 00000000..fe2cabdf --- /dev/null +++ b/pkg/result/processors/testdata/exclude_rules_case_sensitive.go @@ -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