Skip to content

Commit

Permalink
Adding case-sensitive exclude processor
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej "Iwan" Iwanowski <[email protected]>
  • Loading branch information
iwankgb committed Apr 22, 2020
1 parent 4958e50 commit a68b411
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
ic := &cfg.Issues
fs.StringSliceVarP(&ic.ExcludePatterns, "exclude", "e", nil, wh("Exclude issue by regexp"))
fs.BoolVar(&ic.UseDefaultExcludes, "exclude-use-default", true, getDefaultIssueExcludeHelp())
fs.BoolVar(&ic.ExcludeCaseSensitive, "exclude-case-sensitive", false, wh("If set to true exclude rules are case sensitive"))

fs.IntVar(&ic.MaxIssuesPerLinter, "max-issues-per-linter", 50,
wh("Maximum issues count per one linter. Set to 0 to disable"))
Expand Down
7 changes: 4 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,10 @@ func (e ExcludeRule) Validate() error {
}

type Issues struct {
ExcludePatterns []string `mapstructure:"exclude"`
ExcludeRules []ExcludeRule `mapstructure:"exclude-rules"`
UseDefaultExcludes bool `mapstructure:"exclude-use-default"`
ExcludeCaseSensitive bool `mapstructure:"exclude-case-sensitive"`
ExcludePatterns []string `mapstructure:"exclude"`
ExcludeRules []ExcludeRule `mapstructure:"exclude-rules"`
UseDefaultExcludes bool `mapstructure:"exclude-use-default"`

MaxIssuesPerLinter int `mapstructure:"max-issues-per-linter"`
MaxSameIssues int `mapstructure:"max-same-issues"`
Expand Down
9 changes: 8 additions & 1 deletion pkg/lint/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env,
excludeTotalPattern = fmt.Sprintf("(%s)", strings.Join(excludePatterns, "|"))
}

var excludeProcessor processors.Processor
if cfg.Issues.ExcludeCaseSensitive {
excludeProcessor = processors.NewExcludeCaseSensitive(excludeTotalPattern)
} else {
excludeProcessor = processors.NewExclude(excludeTotalPattern)
}

skipFilesProcessor, err := processors.NewSkipFiles(cfg.Run.SkipFiles)
if err != nil {
return nil, err
Expand Down Expand Up @@ -81,7 +88,7 @@ func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env,
// Must be before exclude because users see already marked output and configure excluding by it.
processors.NewIdentifierMarker(),

processors.NewExclude(excludeTotalPattern),
excludeProcessor,
processors.NewExcludeRules(excludeRules, lineCache, log.Child("exclude_rules")),
processors.NewNolint(log.Child("nolint"), dbManager),

Expand Down
18 changes: 18 additions & 0 deletions pkg/result/processors/exclude.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,21 @@ func (p Exclude) Process(issues []result.Issue) ([]result.Issue, error) {
}

func (p Exclude) Finish() {}

type ExcludeCaseSensitive struct {
*Exclude
}

func NewExcludeCaseSensitive(pattern string) *ExcludeCaseSensitive {
var patternRe *regexp.Regexp
if pattern != "" {
patternRe = regexp.MustCompile(pattern)
}
return &ExcludeCaseSensitive{
&Exclude{pattern: patternRe},
}
}

func (p ExcludeCaseSensitive) Name() string {
return "exclude-case-sensitive"
}
18 changes: 18 additions & 0 deletions pkg/result/processors/exclude_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,21 @@ func TestExclude(t *testing.T) {
func TestNoExclude(t *testing.T) {
processAssertSame(t, NewExclude(""), newTextIssue("test"))
}

func TestExcludeCaseSensitive(t *testing.T) {
p := NewExcludeCaseSensitive("^exclude$")
texts := []string{"excLude", "1", "", "exclud", "exclude"}
var issues []result.Issue
for _, t := range texts {
issues = append(issues, newTextIssue(t))
}

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[:len(texts)-1], processedTexts)
}

0 comments on commit a68b411

Please sign in to comment.