Adding case-sensitive exclude processor
Signed-off-by: Maciej "Iwan" Iwanowski <maciej.iwanowski@critical.today>
This commit is contained in:
parent
4958e50dfe
commit
a68b411e4a
@ -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"))
|
||||
|
@ -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"`
|
||||
|
@ -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),
|
||||
|
||||
|
@ -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"
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user