Adding case-sensitive exclude processor

Signed-off-by: Maciej "Iwan" Iwanowski <maciej.iwanowski@critical.today>
This commit is contained in:
Maciej "Iwan" Iwanowski 2020-04-22 18:40:10 +02:00
parent 4958e50dfe
commit a68b411e4a
No known key found for this signature in database
GPG Key ID: 2484258A4DD3EE84
5 changed files with 49 additions and 4 deletions

View File

@ -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"))

View File

@ -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"`

View File

@ -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
@ -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),

View File

@ -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"
}

View File

@ -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)
}