-
Notifications
You must be signed in to change notification settings - Fork 16
fix: isolate test environment from external variables #613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
33e5f85
e0d260b
32b3921
30ab48f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package configtest | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| // snykAPIKey is the AutomaticEnv name for configuration.API_URL (viper key "snyk_api"). | ||
| // Keep aligned with pkg/configuration/constants.go. | ||
| const snykAPIKey = "SNYK_API" | ||
|
|
||
| // KnownLeakEnvironmentKeys is cleared when [IsolateEnvironmentForTest] is called with no arguments. | ||
| var KnownLeakEnvironmentKeys = []string{ | ||
| snykAPIKey, | ||
| } | ||
|
|
||
| // IsolateEnvironmentForTest clears environment variables for a test using t.Setenv(k, ""). | ||
| // If no keys are provided, it clears the variables listed in [KnownLeakEnvironmentKeys]. | ||
| // If explicit keys are provided, they OVERRIDE the default behavior: only the specified keys are cleared, | ||
| // and they are NOT merged with [KnownLeakEnvironmentKeys]. Empty keys are skipped. | ||
| func IsolateEnvironmentForTest(t *testing.T, keys ...string) { | ||
| t.Helper() | ||
| toClear := keys | ||
| if len(toClear) == 0 { | ||
| toClear = KnownLeakEnvironmentKeys | ||
| } | ||
|
Comment on lines
+20
to
+25
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we ALWAYS consider ignoring the KnownLeakEnvironmentKeys values?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this behavior has been documented in my last commit |
||
| for _, k := range toClear { | ||
| if k == "" { | ||
| continue | ||
| } | ||
| t.Setenv(k, "") | ||
| } | ||
|
robertolopezlopez marked this conversation as resolved.
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.