internal/codescan/tests.go

545ba0bcdea3b534903bd78c918c0c025a97eda7
gitbay/internal/codescan/tests.go

35 lines · 1144 bytes

2 symbols in this file
 1package codescan
 2
 3import (
 4	"path"
 5	"strings"
 6)
 7
 8// insecureRandomRule flags random number generators that are not
 9// cryptographically secure. In a test the numbers protect nothing.
10const insecureRandomRule = "insecure-random"
11
12// testPath reports whether a path is a test by the usual layouts: a tests
13// directory, or a file named as one.
14func testPath(p string) bool {
15	dirs := strings.Split(path.Dir(p), "/")
16	for _, d := range dirs {
17		switch {
18		case d == "test", d == "tests", d == "__tests__", d == "spec", d == "testdata", strings.HasSuffix(d, "Tests"):
19			return true
20		}
21	}
22	base := path.Base(p)
23	stem := strings.TrimSuffix(base, path.Ext(base))
24	switch {
25	case strings.HasSuffix(base, "_test.go"),
26		strings.HasPrefix(base, "test_") && strings.HasSuffix(base, ".py"),
27		strings.HasSuffix(base, "_test.py"),
28		base == "conftest.py",
29		strings.HasSuffix(stem, ".test"), strings.HasSuffix(stem, ".spec"),
30		path.Ext(base) == ".swift" && (strings.HasSuffix(stem, "Tests") || strings.HasSuffix(stem, "Test")),
31		(path.Ext(base) == ".java" || path.Ext(base) == ".kt") && strings.HasSuffix(stem, "Test"):
32		return true
33	}
34	return false
35}