Skip to content

Commit e8ccb74

Browse files
committed
feat(exclusionfilter): add addenda arg to ShouldExclude method
1 parent 512e694 commit e8ccb74

2 files changed

Lines changed: 40 additions & 34 deletions

File tree

exclusion_filter.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ type ExclusionFilter struct {
3232
rules []compiledExcludeRule
3333
}
3434

35+
type (
36+
Keyer interface{ Key() string }
37+
Valuer interface{ Value() string }
38+
)
39+
3540
// CompileRegexes returns a slice of compiled regular expressions.
3641
// Returns nil if an empty patterns slice is provided.
3742
// Returns an error if any pattern is empty or failed to compile.
@@ -119,7 +124,7 @@ func NewExclusionFilter(rules []ExcludeRule) (*ExclusionFilter, error) {
119124

120125
// ShouldExclude returns true if the given issue should be excluded based on
121126
// its file path and rule ID
122-
func (f *ExclusionFilter) ShouldExclude(filePath, ruleID string) bool {
127+
func (f *ExclusionFilter) ShouldExclude(filePath, ruleID string, addenda any) bool {
123128
if f == nil || len(f.rules) == 0 {
124129
return false
125130
}
@@ -152,7 +157,7 @@ func (f *ExclusionFilter) FilterIssues(issues []*issue.Issue) ([]*issue.Issue, i
152157
excluded := 0
153158

154159
for _, iss := range issues {
155-
if f.ShouldExclude(iss.File, iss.RuleID) {
160+
if f.ShouldExclude(iss.File, iss.RuleID, iss.Addenda) {
156161
excluded++
157162
continue
158163
}

exclusion_filter_test.go

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,24 @@ var _ = Describe("ExclusionFilter", func() {
7676
})
7777

7878
It("should exclude matching path and rule", func() {
79-
Expect(filter.ShouldExclude("cmd/mytool/main.go", "G204")).To(BeTrue())
80-
Expect(filter.ShouldExclude("cmd/another/file.go", "G304")).To(BeTrue())
79+
Expect(filter.ShouldExclude("cmd/mytool/main.go", "G204", nil)).To(BeTrue())
80+
Expect(filter.ShouldExclude("cmd/another/file.go", "G304", nil)).To(BeTrue())
8181
})
8282

8383
It("should not exclude matching path with non-matching rule", func() {
84-
Expect(filter.ShouldExclude("cmd/mytool/main.go", "G101")).To(BeFalse())
85-
Expect(filter.ShouldExclude("cmd/mytool/main.go", "G401")).To(BeFalse())
84+
Expect(filter.ShouldExclude("cmd/mytool/main.go", "G101", nil)).To(BeFalse())
85+
Expect(filter.ShouldExclude("cmd/mytool/main.go", "G401", nil)).To(BeFalse())
8686
})
8787

8888
It("should not exclude non-matching path", func() {
89-
Expect(filter.ShouldExclude("pkg/server/main.go", "G204")).To(BeFalse())
90-
Expect(filter.ShouldExclude("internal/api/handler.go", "G304")).To(BeFalse())
89+
Expect(filter.ShouldExclude("pkg/server/main.go", "G204", nil)).To(BeFalse())
90+
Expect(filter.ShouldExclude("internal/api/handler.go", "G304", nil)).To(BeFalse())
9191
})
9292

9393
It("should handle nested paths correctly", func() {
94-
Expect(filter.ShouldExclude("internal/testutil/helper.go", "G101")).To(BeTrue())
95-
Expect(filter.ShouldExclude("internal/testutil/sub/file.go", "G101")).To(BeTrue())
96-
Expect(filter.ShouldExclude("internal/other/file.go", "G101")).To(BeFalse())
94+
Expect(filter.ShouldExclude("internal/testutil/helper.go", "G101", nil)).To(BeTrue())
95+
Expect(filter.ShouldExclude("internal/testutil/sub/file.go", "G101", nil)).To(BeTrue())
96+
Expect(filter.ShouldExclude("internal/other/file.go", "G101", nil)).To(BeFalse())
9797
})
9898
})
9999

@@ -109,14 +109,14 @@ var _ = Describe("ExclusionFilter", func() {
109109
})
110110

111111
It("should exclude any rule for matching path", func() {
112-
Expect(filter.ShouldExclude("scripts/build.go", "G101")).To(BeTrue())
113-
Expect(filter.ShouldExclude("scripts/build.go", "G204")).To(BeTrue())
114-
Expect(filter.ShouldExclude("scripts/build.go", "G304")).To(BeTrue())
115-
Expect(filter.ShouldExclude("vendor/lib/file.go", "G401")).To(BeTrue())
112+
Expect(filter.ShouldExclude("scripts/build.go", "G101", nil)).To(BeTrue())
113+
Expect(filter.ShouldExclude("scripts/build.go", "G204", nil)).To(BeTrue())
114+
Expect(filter.ShouldExclude("scripts/build.go", "G304", nil)).To(BeTrue())
115+
Expect(filter.ShouldExclude("vendor/lib/file.go", "G401", nil)).To(BeTrue())
116116
})
117117

118118
It("should not exclude non-matching paths", func() {
119-
Expect(filter.ShouldExclude("cmd/main.go", "G101")).To(BeFalse())
119+
Expect(filter.ShouldExclude("cmd/main.go", "G101", nil)).To(BeFalse())
120120
})
121121
})
122122

@@ -131,20 +131,20 @@ var _ = Describe("ExclusionFilter", func() {
131131
})
132132

133133
It("should normalize backslashes to forward slashes", func() {
134-
Expect(filter.ShouldExclude("cmd\\mytool\\main.go", "G204")).To(BeTrue())
135-
Expect(filter.ShouldExclude("cmd\\nested\\deep\\file.go", "G204")).To(BeTrue())
134+
Expect(filter.ShouldExclude("cmd\\mytool\\main.go", "G204", nil)).To(BeTrue())
135+
Expect(filter.ShouldExclude("cmd\\nested\\deep\\file.go", "G204", nil)).To(BeTrue())
136136
})
137137
})
138138

139139
Context("with nil or empty filter", func() {
140140
It("should not exclude anything with nil filter", func() {
141141
var nilFilter *gosec.ExclusionFilter
142-
Expect(nilFilter.ShouldExclude("any/path.go", "G101")).To(BeFalse())
142+
Expect(nilFilter.ShouldExclude("any/path.go", "G101", nil)).To(BeFalse())
143143
})
144144

145145
It("should not exclude anything with empty rules", func() {
146146
filter, _ := gosec.NewExclusionFilter(nil)
147-
Expect(filter.ShouldExclude("any/path.go", "G101")).To(BeFalse())
147+
Expect(filter.ShouldExclude("any/path.go", "G101", nil)).To(BeFalse())
148148
})
149149
})
150150

@@ -161,23 +161,23 @@ var _ = Describe("ExclusionFilter", func() {
161161
})
162162

163163
It("should match test files", func() {
164-
Expect(filter.ShouldExclude("pkg/auth/auth_test.go", "G101")).To(BeTrue())
165-
Expect(filter.ShouldExclude("internal/handler_test.go", "G101")).To(BeTrue())
166-
Expect(filter.ShouldExclude("pkg/auth/auth.go", "G101")).To(BeFalse())
164+
Expect(filter.ShouldExclude("pkg/auth/auth_test.go", "G101", nil)).To(BeTrue())
165+
Expect(filter.ShouldExclude("internal/handler_test.go", "G101", nil)).To(BeTrue())
166+
Expect(filter.ShouldExclude("pkg/auth/auth.go", "G101", nil)).To(BeFalse())
167167
})
168168

169169
It("should match cmd or tools prefix", func() {
170-
Expect(filter.ShouldExclude("cmd/server/main.go", "G204")).To(BeTrue())
171-
Expect(filter.ShouldExclude("tools/generator/gen.go", "G204")).To(BeTrue())
172-
Expect(filter.ShouldExclude("pkg/cmd/helper.go", "G204")).To(BeFalse())
170+
Expect(filter.ShouldExclude("cmd/server/main.go", "G204", nil)).To(BeTrue())
171+
Expect(filter.ShouldExclude("tools/generator/gen.go", "G204", nil)).To(BeTrue())
172+
Expect(filter.ShouldExclude("pkg/cmd/helper.go", "G204", nil)).To(BeFalse())
173173
})
174174

175175
It("should match mock/fake/stub directories", func() {
176-
Expect(filter.ShouldExclude("internal/mocks/service.go", "G401")).To(BeTrue())
177-
Expect(filter.ShouldExclude("internal/mock/client.go", "G304")).To(BeTrue())
178-
Expect(filter.ShouldExclude("internal/fakes/repo.go", "G101")).To(BeTrue())
179-
Expect(filter.ShouldExclude("internal/stub/handler.go", "G204")).To(BeTrue())
180-
Expect(filter.ShouldExclude("internal/real/service.go", "G401")).To(BeFalse())
176+
Expect(filter.ShouldExclude("internal/mocks/service.go", "G401", nil)).To(BeTrue())
177+
Expect(filter.ShouldExclude("internal/mock/client.go", "G304", nil)).To(BeTrue())
178+
Expect(filter.ShouldExclude("internal/fakes/repo.go", "G101", nil)).To(BeTrue())
179+
Expect(filter.ShouldExclude("internal/stub/handler.go", "G204", nil)).To(BeTrue())
180+
Expect(filter.ShouldExclude("internal/real/service.go", "G401", nil)).To(BeFalse())
181181
})
182182
})
183183
})
@@ -336,6 +336,7 @@ func TestShouldExclude(t *testing.T) {
336336
rules []gosec.ExcludeRule
337337
filePath string
338338
ruleID string
339+
addenda any
339340
want bool
340341
}{
341342
{
@@ -392,10 +393,10 @@ func TestShouldExclude(t *testing.T) {
392393
t.Fatalf("NewExclusionFilter() error = %v", err)
393394
}
394395

395-
got := filter.ShouldExclude(tt.filePath, tt.ruleID)
396+
got := filter.ShouldExclude(tt.filePath, tt.ruleID, tt.addenda)
396397
if got != tt.want {
397-
t.Errorf("ShouldExclude(%q, %q) = %v, want %v",
398-
tt.filePath, tt.ruleID, got, tt.want)
398+
t.Errorf("ShouldExclude(%q, %q, %#v) = %v, want %v",
399+
tt.filePath, tt.ruleID, tt.addenda, got, tt.want)
399400
}
400401
})
401402
}

0 commit comments

Comments
 (0)