Skip to content

Commit 181ab08

Browse files
committed
analyzer: skip over files under /tests
Files under /?tests/* are causing many errors but we really don't care about that code being flagged. Fixes #60
1 parent 9592313 commit 181ab08

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

analyzer.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,21 @@ func (gosec *Analyzer) Process(buildTags []string, packagePaths ...string) error
159159
return nil
160160
}
161161

162+
const sep = os.PathSeparator
163+
164+
var reTestsPath = regexp.MustCompile(fmt.Sprintf("(^\\s*tests%c?)|%c\\s*tests\\s*%c|%c\\s*tests\\s*$", sep, sep, sep, sep))
165+
166+
func allowedFiles(fullPaths ...string) (filtered []string) {
167+
for _, fullPath := range fullPaths {
168+
// Skip over "/tests/" files as they are generating lots of noise.
169+
// Please see https://github.com/cosmos/gosec/issues/60
170+
if !reTestsPath.MatchString(fullPath) {
171+
filtered = append(filtered, fullPath)
172+
}
173+
}
174+
return filterOutGeneratedGoFiles(filtered)
175+
}
176+
162177
var reGeneratedGoFile = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.`)
163178

164179
// filterOutGeneratedGoFiles parallelizes the proocess of checking the contents
@@ -344,7 +359,7 @@ func (gosec *Analyzer) Check(pkg *packages.Package) {
344359
// Only walk non-generated Go files as we definitely don't
345360
// want to report on generated code, which is out of our direct control.
346361
// Please see: https://github.com/cosmos/gosec/issues/30
347-
if filtered := filterOutGeneratedGoFiles([]string{checkedFile}); len(filtered) > 0 {
362+
if filtered := allowedFiles(checkedFile); len(filtered) > 0 {
348363
ast.Walk(gosec, file)
349364
}
350365
gosec.stats.NumFiles++

0 commit comments

Comments
 (0)