[test-improver] Improve tests for envutil package#3566
Merged
Conversation
Replace manual os.Setenv/os.Unsetenv + defer patterns with the idiomatic t.Setenv in envutil_test.go. t.Setenv automatically restores the original env var value when the test ends, ensuring correct cleanup even if a test panics. It also prevents accidental t.Parallel() calls on env-dependent tests. - Table-driven loops: replace os.Unsetenv+defer+os.Setenv with t.Setenv for setEnv:true cases; keep os.Unsetenv for setEnv:false to distinguish "not set" from "set to empty" - RealWorld scenario tests: replace os.Unsetenv+defer+os.Setenv with t.Setenv throughout Consistent with github_test.go in the same package which already uses t.Setenv correctly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates internal/envutil/envutil_test.go to use Go’s t.Setenv for environment setup/cleanup, reducing manual os.Setenv/os.Unsetenv + defer patterns and making env restoration more reliable within subtests.
Changes:
- Replaces
os.Setenv+defer os.Unsetenvpairs witht.Setenvin table-driven tests. - Updates “real world scenario” tests to start from a “clean slate” using
t.Setenv(key, "")and then override via additionalt.Setenvcalls. - Removes now-unnecessary
defercleanup code.
Show a summary per file
| File | Description |
|---|---|
internal/envutil/envutil_test.go |
Migrates env var manipulation in tests to t.Setenv to improve cleanup reliability and consistency. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comments suppressed due to low confidence (3)
internal/envutil/envutil_test.go:176
- Same issue here:
os.Unsetenvin thesetEnv == falsebranch doesn’t restore the original environment, which can leak state across subtests/test runs. Prefert.Setenv(tt.envKey, "")(equivalent forGetEnvString) or implement a save/restore withos.LookupEnv+t.Cleanup, and handle any Unsetenv error.
if tt.setEnv {
t.Setenv(tt.envKey, tt.envValue)
} else {
os.Unsetenv(tt.envKey)
}
internal/envutil/envutil_test.go:272
os.Unsetenvin thesetEnv == falsebranch will clear any pre-existing value for the rest of the process and can interfere with later subtests. SinceGetEnvInttreats empty as unset, consider usingt.Setenv(tt.envKey, "")for these cases, or add at.Cleanupthat restores the value captured fromos.LookupEnv(and fail on Unsetenv error).
if tt.setEnv {
t.Setenv(tt.envKey, tt.envValue)
} else {
os.Unsetenv(tt.envKey)
}
internal/envutil/envutil_test.go:467
- Same pattern here: unsetting the env var without restoring it can leak state across subtests and permanently clear a user/CI-provided value. Prefer
t.Setenv(tt.envKey, "")(behaviorally equivalent forGetEnvBool) or explicitly save/restore viaos.LookupEnv+t.Cleanupand check the Unsetenv error.
if tt.setEnv {
t.Setenv(tt.envKey, tt.envValue)
} else {
os.Unsetenv(tt.envKey)
}
- Files reviewed: 1/1 changed files
- Comments generated: 1
| os.Setenv(tt.envKey, tt.envValue) | ||
| t.Setenv(tt.envKey, tt.envValue) | ||
| } else { | ||
| os.Unsetenv(tt.envKey) |
This was referenced Apr 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Test Improvements:
internal/envutil/envutil_test.goFile Analyzed
internal/envutil/envutil_test.gointernal/envutilImprovements Made
1. Idiomatic Use of
t.SetenvReplace every
os.Setenv+defer os.Unsetenvpair with Go's built-int.Setenv:Before (in all four table-driven test loops):
After:
Why this matters:
t.Setenvsaves the original env var value and restores it at test end — cleanup is guaranteed even if the test panicst.Parallel()calls after env mutation, preventing a common mistakesetEnv: falsecases still useos.Unsetenvto explicitly test "truly not set" behaviour, preserving the semantic distinction fromsetEnv: true, envValue: ""2. Same pattern in RealWorld scenario tests
All four
*RealWorldScenariostest functions (TestGetEnvDurationRealWorldScenarios,TestGetEnvStringRealWorldScenarios,TestGetEnvIntRealWorldScenarios,TestGetEnvBoolRealWorldScenarios) had the same antipattern:Replaced with
t.Setenv(key, "")as the initial "clean slate" call (empty string is treated as "not configured" by all fourGetEnv*functions), followed byt.Setenv(key, value)for each override. The manualdeferis eliminated.3. Consistency with sibling test file
internal/envutil/github_test.go(same package) already usest.Setenvcorrectly. This change makesenvutil_test.goconsistent with the established pattern.Test Execution
Tests pass (verified by the repository CI). The changes are purely mechanical — no logic, control flow, or assertions were modified.
Why These Changes?
envutil_test.gohad a consistent antipattern across all 8 test loop bodies and 4 real-world scenario sub-tests: manualos.Setenv/os.Unsetenv+deferinstead of the standard library'st.Setenv. Thet.SetenvAPI was added in Go 1.17 precisely to address this pattern. Using it is safer (panic-safe cleanup), clearer about intent, and consistent withgithub_test.goalready in the package.Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests