Skip to content

Commit 602fbdd

Browse files
committed
Fix tests
1 parent f5f446d commit 602fbdd

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

internal/scan/apikeys_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ func clearHighRiskEnv(t *testing.T) {
1818
}
1919
}
2020

21+
// clearAllEnv sets every environment variable to empty for the duration of the test.
22+
// Use this in tests that assert 0 findings, since nameRegex patterns (e.g. (?i)GITHUB)
23+
// can match CI variables like GITHUB_WORKSPACE that aren't credentials.
24+
// t.Setenv restores original values after the test.
25+
func clearAllEnv(t *testing.T) {
26+
t.Helper()
27+
for _, entry := range os.Environ() {
28+
if idx := strings.IndexByte(entry, '='); idx >= 0 {
29+
t.Setenv(entry[:idx], "")
30+
}
31+
}
32+
}
33+
2134
// newScannerWithHome creates an APIKeyScanner with HomeDir set to home and no extras.
2235
func newScannerWithHome(home string) *scan.APIKeyScanner {
2336
s := scan.NewAPIKeyScanner()
@@ -66,7 +79,7 @@ func TestAPIKeyScanner_NeverStoresSecretValue(t *testing.T) {
6679
}
6780

6881
func TestAPIKeyScanner_EmptyEnvNoFindings(t *testing.T) {
69-
clearHighRiskEnv(t)
82+
clearAllEnv(t)
7083

7184
s := newScannerWithHome(t.TempDir())
7285
result := s.Scan()
@@ -131,7 +144,7 @@ func TestAPIKeyScanner_CredentialFileContentNotInFindings(t *testing.T) {
131144
}
132145

133146
func TestAPIKeyScanner_NoCredentialFileNoFinding(t *testing.T) {
134-
clearHighRiskEnv(t)
147+
clearAllEnv(t)
135148

136149
s := newScannerWithHome(t.TempDir())
137150
result := s.Scan()

0 commit comments

Comments
 (0)