Skip to content

Commit

Permalink
feat: warning on unused path-except
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jan 23, 2025
1 parent a2516ac commit 0a6243b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
36 changes: 24 additions & 12 deletions pkg/result/processors/exclusion_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ type ExclusionPaths struct {
pathPatterns []*regexp.Regexp
pathExceptPatterns []*regexp.Regexp

warnUnused bool
skippedPathCounter map[*regexp.Regexp]int
warnUnused bool
excludedPathCounter map[*regexp.Regexp]int
excludedPathExceptCounter map[*regexp.Regexp]int

log logutils.Log
}

func NewExclusionPaths(log logutils.Log, cfg *config.LinterExclusions) (*ExclusionPaths, error) {
var counter = make(map[*regexp.Regexp]int)
excludedPathCounter := make(map[*regexp.Regexp]int)

var pathPatterns []*regexp.Regexp
for _, p := range cfg.Paths {
Expand All @@ -35,9 +36,11 @@ func NewExclusionPaths(log logutils.Log, cfg *config.LinterExclusions) (*Exclusi
}

pathPatterns = append(pathPatterns, patternRe)
counter[patternRe] = 0
excludedPathCounter[patternRe] = 0
}

excludedPathExceptCounter := make(map[*regexp.Regexp]int)

var pathExceptPatterns []*regexp.Regexp
for _, p := range cfg.PathsExcept {
p = fsutils.NormalizePathInRegex(p)
Expand All @@ -48,14 +51,16 @@ func NewExclusionPaths(log logutils.Log, cfg *config.LinterExclusions) (*Exclusi
}

pathExceptPatterns = append(pathExceptPatterns, patternRe)
excludedPathExceptCounter[patternRe] = 0
}

return &ExclusionPaths{
pathPatterns: pathPatterns,
pathExceptPatterns: pathExceptPatterns,
warnUnused: cfg.WarnUnused,
skippedPathCounter: counter,
log: log.Child(logutils.DebugKeyExclusionPaths),
pathPatterns: pathPatterns,
pathExceptPatterns: pathExceptPatterns,
warnUnused: cfg.WarnUnused,
excludedPathCounter: excludedPathCounter,
excludedPathExceptCounter: excludedPathExceptCounter,
log: log.Child(logutils.DebugKeyExclusionPaths),
}, nil
}

Expand All @@ -72,19 +77,25 @@ func (p *ExclusionPaths) Process(issues []result.Issue) ([]result.Issue, error)
}

func (p *ExclusionPaths) Finish() {
for pattern, count := range p.skippedPathCounter {
for pattern, count := range p.excludedPathCounter {
if p.warnUnused && count == 0 {
p.log.Warnf("Skipped %d issues by pattern %q", count, pattern)
p.log.Warnf("The pattern %q match %d issues", pattern, count)
} else {
p.log.Infof("Skipped %d issues by pattern %q", count, pattern)
}
}

for pattern, count := range p.excludedPathExceptCounter {
if p.warnUnused && count == 0 {
p.log.Warnf("The pattern %q match %d issues", pattern, count)
}
}
}

func (p *ExclusionPaths) shouldPassIssue(issue *result.Issue) bool {
for _, pattern := range p.pathPatterns {
if pattern.MatchString(issue.RelativePath) {
p.skippedPathCounter[pattern] += 1
p.excludedPathCounter[pattern] += 1
return false
}
}
Expand All @@ -99,6 +110,7 @@ func (p *ExclusionPaths) shouldPassIssue(issue *result.Issue) bool {
continue
}

p.excludedPathExceptCounter[pattern] += 1
matched = true
}

Expand Down
43 changes: 42 additions & 1 deletion pkg/result/processors/exclusion_paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ func TestExclusionPaths_Process(t *testing.T) {
{RelativePath: filepath.FromSlash("a/b/c/d.go")},
},
},
{
desc: "paths: unused",
cfg: &config.LinterExclusions{
WarnUnused: true,
Paths: []string{
`^z/d.go`, // This pattern is unused.
`^c/d.go`,
},
},
issues: []result.Issue{
{RelativePath: filepath.FromSlash("a/b/c/d.go")},
{RelativePath: filepath.FromSlash("c/d.go")},
},
expected: []result.Issue{
{RelativePath: filepath.FromSlash("a/b/c/d.go")},
},
},
{
desc: "pathsExcept",
cfg: &config.LinterExclusions{
Expand All @@ -116,11 +133,34 @@ func TestExclusionPaths_Process(t *testing.T) {
{RelativePath: filepath.FromSlash("base/d/file.go")},
},
},
{
desc: "pathsExcept: unused",
cfg: &config.LinterExclusions{
WarnUnused: true,
PathsExcept: []string{
`^base/z/.*$`, // This pattern is unused.
`^base/c/.*$`,
},
},
issues: []result.Issue{
{RelativePath: filepath.FromSlash("base/a/file.go")},
{RelativePath: filepath.FromSlash("base/b/file.go")},
{RelativePath: filepath.FromSlash("base/c/file.go")},
{RelativePath: filepath.FromSlash("base/c/a/file.go")},
{RelativePath: filepath.FromSlash("base/c/b/file.go")},
{RelativePath: filepath.FromSlash("base/d/file.go")},
},
expected: []result.Issue{
{RelativePath: filepath.FromSlash("base/a/file.go")},
{RelativePath: filepath.FromSlash("base/b/file.go")},
{RelativePath: filepath.FromSlash("base/d/file.go")},
},
},
{
desc: "pathsExcept: multiple patterns",
cfg: &config.LinterExclusions{
PathsExcept: []string{
`^base/z/.*$`,
`^base/e/.*$`,
`^base/c/.*$`,
},
},
Expand All @@ -131,6 +171,7 @@ func TestExclusionPaths_Process(t *testing.T) {
{RelativePath: filepath.FromSlash("base/c/a/file.go")},
{RelativePath: filepath.FromSlash("base/c/b/file.go")},
{RelativePath: filepath.FromSlash("base/d/file.go")},
{RelativePath: filepath.FromSlash("base/e/file.go")},
},
expected: []result.Issue{
{RelativePath: filepath.FromSlash("base/a/file.go")},
Expand Down

0 comments on commit 0a6243b

Please sign in to comment.