Skip to content

Commit 8f89011

Browse files
committed
feat: warning on unused path-except
1 parent 13cccb3 commit 8f89011

File tree

2 files changed

+66
-13
lines changed

2 files changed

+66
-13
lines changed

pkg/result/processors/exclusion_paths.go

+24-12
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ type ExclusionPaths struct {
1616
pathPatterns []*regexp.Regexp
1717
pathExceptPatterns []*regexp.Regexp
1818

19-
warnUnused bool
20-
skippedPathCounter map[*regexp.Regexp]int
19+
warnUnused bool
20+
excludedPathCounter map[*regexp.Regexp]int
21+
excludedPathExceptCounter map[*regexp.Regexp]int
2122

2223
log logutils.Log
2324
}
2425

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

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

3738
pathPatterns = append(pathPatterns, patternRe)
38-
counter[patternRe] = 0
39+
excludedPathCounter[patternRe] = 0
3940
}
4041

42+
excludedPathExceptCounter := make(map[*regexp.Regexp]int)
43+
4144
var pathExceptPatterns []*regexp.Regexp
4245
for _, p := range cfg.PathsExcept {
4346
p = fsutils.NormalizePathInRegex(p)
@@ -48,14 +51,16 @@ func NewExclusionPaths(log logutils.Log, cfg *config.LinterExclusions) (*Exclusi
4851
}
4952

5053
pathExceptPatterns = append(pathExceptPatterns, patternRe)
54+
excludedPathExceptCounter[patternRe] = 0
5155
}
5256

5357
return &ExclusionPaths{
54-
pathPatterns: pathPatterns,
55-
pathExceptPatterns: pathExceptPatterns,
56-
warnUnused: cfg.WarnUnused,
57-
skippedPathCounter: counter,
58-
log: log.Child(logutils.DebugKeyExclusionPaths),
58+
pathPatterns: pathPatterns,
59+
pathExceptPatterns: pathExceptPatterns,
60+
warnUnused: cfg.WarnUnused,
61+
excludedPathCounter: excludedPathCounter,
62+
excludedPathExceptCounter: excludedPathExceptCounter,
63+
log: log.Child(logutils.DebugKeyExclusionPaths),
5964
}, nil
6065
}
6166

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

7479
func (p *ExclusionPaths) Finish() {
75-
for pattern, count := range p.skippedPathCounter {
80+
for pattern, count := range p.excludedPathCounter {
7681
if p.warnUnused && count == 0 {
77-
p.log.Warnf("Skipped %d issues by pattern %q", count, pattern)
82+
p.log.Warnf("The pattern %q match %d issues", pattern, count)
7883
} else {
7984
p.log.Infof("Skipped %d issues by pattern %q", count, pattern)
8085
}
8186
}
87+
88+
for pattern, count := range p.excludedPathExceptCounter {
89+
if p.warnUnused && count == 0 {
90+
p.log.Warnf("The pattern %q match %d issues", pattern, count)
91+
}
92+
}
8293
}
8394

8495
func (p *ExclusionPaths) shouldPassIssue(issue *result.Issue) bool {
8596
for _, pattern := range p.pathPatterns {
8697
if pattern.MatchString(issue.RelativePath) {
87-
p.skippedPathCounter[pattern] += 1
98+
p.excludedPathCounter[pattern] += 1
8899
return false
89100
}
90101
}
@@ -99,6 +110,7 @@ func (p *ExclusionPaths) shouldPassIssue(issue *result.Issue) bool {
99110
continue
100111
}
101112

113+
p.excludedPathExceptCounter[pattern] += 1
102114
matched = true
103115
}
104116

pkg/result/processors/exclusion_paths_test.go

+42-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,23 @@ func TestExclusionPaths_Process(t *testing.T) {
9797
{RelativePath: filepath.FromSlash("a/b/c/d.go")},
9898
},
9999
},
100+
{
101+
desc: "paths: unused",
102+
cfg: &config.LinterExclusions{
103+
WarnUnused: true,
104+
Paths: []string{
105+
`^z/d.go`, // This pattern is unused.
106+
`^c/d.go`,
107+
},
108+
},
109+
issues: []result.Issue{
110+
{RelativePath: filepath.FromSlash("a/b/c/d.go")},
111+
{RelativePath: filepath.FromSlash("c/d.go")},
112+
},
113+
expected: []result.Issue{
114+
{RelativePath: filepath.FromSlash("a/b/c/d.go")},
115+
},
116+
},
100117
{
101118
desc: "pathsExcept",
102119
cfg: &config.LinterExclusions{
@@ -116,11 +133,34 @@ func TestExclusionPaths_Process(t *testing.T) {
116133
{RelativePath: filepath.FromSlash("base/d/file.go")},
117134
},
118135
},
136+
{
137+
desc: "pathsExcept: unused",
138+
cfg: &config.LinterExclusions{
139+
WarnUnused: true,
140+
PathsExcept: []string{
141+
`^base/z/.*$`, // This pattern is unused.
142+
`^base/c/.*$`,
143+
},
144+
},
145+
issues: []result.Issue{
146+
{RelativePath: filepath.FromSlash("base/a/file.go")},
147+
{RelativePath: filepath.FromSlash("base/b/file.go")},
148+
{RelativePath: filepath.FromSlash("base/c/file.go")},
149+
{RelativePath: filepath.FromSlash("base/c/a/file.go")},
150+
{RelativePath: filepath.FromSlash("base/c/b/file.go")},
151+
{RelativePath: filepath.FromSlash("base/d/file.go")},
152+
},
153+
expected: []result.Issue{
154+
{RelativePath: filepath.FromSlash("base/a/file.go")},
155+
{RelativePath: filepath.FromSlash("base/b/file.go")},
156+
{RelativePath: filepath.FromSlash("base/d/file.go")},
157+
},
158+
},
119159
{
120160
desc: "pathsExcept: multiple patterns",
121161
cfg: &config.LinterExclusions{
122162
PathsExcept: []string{
123-
`^base/z/.*$`,
163+
`^base/e/.*$`,
124164
`^base/c/.*$`,
125165
},
126166
},
@@ -131,6 +171,7 @@ func TestExclusionPaths_Process(t *testing.T) {
131171
{RelativePath: filepath.FromSlash("base/c/a/file.go")},
132172
{RelativePath: filepath.FromSlash("base/c/b/file.go")},
133173
{RelativePath: filepath.FromSlash("base/d/file.go")},
174+
{RelativePath: filepath.FromSlash("base/e/file.go")},
134175
},
135176
expected: []result.Issue{
136177
{RelativePath: filepath.FromSlash("base/a/file.go")},

0 commit comments

Comments
 (0)