[test-improver] Improve tests for tty package#1742
Draft
github-actions[bot] wants to merge 1 commit intomainfrom
Draft
[test-improver] Improve tests for tty package#1742github-actions[bot] wants to merge 1 commit intomainfrom
github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
- Add tty_test.go: tests for IsStderrTerminal() which had 0% coverage - Replace manual env save/restore with t.Setenv/t.Cleanup in container_test.go - Remove __UNSET__ sentinel hack, use unset bool field in table test struct - Extract unsetEnvForTest helper to eliminate 5 copies of the same 9-line pattern - Per-subtest env isolation: each subtest now manages its own env state Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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/ttyFile Analyzed
internal/tty/container_test.go,internal/tty/tty_test.go(new)internal/ttyImprovements Made
1. New Test File:
tty_test.go— CoversIsStderrTerminal()tty.gocontained one exported functionIsStderrTerminal()with zero test coverage. Three tests were added:TestIsStderrTerminal— verifies the result matchesterm.IsTerminal(os.Stderr.Fd())directlyTestIsStderrTerminal_ConsistentResult— verifies repeated calls return the same value (determinism)TestIsStderrTerminal_NotATerminalInCI— assertsfalsein CI environments (GITHUB_ACTIONS / CI env var), skipped otherwise2. Cleaner env var management in
container_test.goBefore — five test functions each contained this 9-line manual save/restore block:
After — a single
unsetEnvForTest(t, key)helper andt.Setenv(key, value)built-in:3. Removed
"__UNSET__"sentinel hackBefore: Table-driven test used
envValue: "__UNSET__"as a magic string with special-caseiflogic:After: Explicit
unset boolfield in the struct makes intent clear:{name: "RUNNING_IN_CONTAINER not set", unset: true, expectEnvDetection: false}4. Fixed per-subtest env isolation in
TestIsRunningInContainer_EdgeCasesPreviously, each subtest called
os.Setenvwithout any cleanup, relying on the outer test function'sdeferto restore the variable at the end. This meant env state leaked between subtests. Usingt.Setenvinside each subtest now ensures the env var is properly restored after each individual subtest.5.
setupEnv funcsignature fix inTestIsRunningInContainer_AllMethodsCombinedUpdated the
setupEnvfunction in the table struct fromfunc()tofunc(t *testing.T)so it can callt.Setenv/unsetEnvForTestwith the subtest's*testing.T, ensuring cleanup is scoped to the correct subtest.Why These Changes?
The
ttypackage had a split:container.gohad extensive tests buttty.go's sole functionIsStderrTerminal()had no test file at all. Addingtty_test.gocloses that gap.container_test.gohad significant code duplication: the manual env save/restore pattern appeared 5 times, totalling ~45 lines of boilerplate. Go'st.Setenv(added in Go 1.17) handles exactly this pattern idiomatically. The refactor removes 83 lines of boilerplate while being equivalent in correctness—and actually more correct due to per-subtest isolation.Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests