Skip to content

[test-improver] Improve tests for tty package#1742

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
test-improver/tty-package-tests-745f2bf065d31046
Draft

[test-improver] Improve tests for tty package#1742
github-actions[bot] wants to merge 1 commit intomainfrom
test-improver/tty-package-tests-745f2bf065d31046

Conversation

@github-actions
Copy link
Contributor

Test Improvements: internal/tty

File Analyzed

  • Test Files: internal/tty/container_test.go, internal/tty/tty_test.go (new)
  • Package: internal/tty
  • Net change: 74 insertions, 83 deletions (net reduction despite adding a new file)

Improvements Made

1. New Test File: tty_test.go — Covers IsStderrTerminal()

tty.go contained one exported function IsStderrTerminal() with zero test coverage. Three tests were added:

  • TestIsStderrTerminal — verifies the result matches term.IsTerminal(os.Stderr.Fd()) directly
  • TestIsStderrTerminal_ConsistentResult — verifies repeated calls return the same value (determinism)
  • TestIsStderrTerminal_NotATerminalInCI — asserts false in CI environments (GITHUB_ACTIONS / CI env var), skipped otherwise

2. Cleaner env var management in container_test.go

Before — five test functions each contained this 9-line manual save/restore block:

originalValue, originalExists := os.LookupEnv("RUNNING_IN_CONTAINER")
defer func() {
    if originalExists {
        os.Setenv("RUNNING_IN_CONTAINER", originalValue)
    } else {
        os.Unsetenv("RUNNING_IN_CONTAINER")
    }
}()
os.Unsetenv("RUNNING_IN_CONTAINER")

After — a single unsetEnvForTest(t, key) helper and t.Setenv(key, value) built-in:

unsetEnvForTest(t, "RUNNING_IN_CONTAINER")   // for the "not set" case
t.Setenv("RUNNING_IN_CONTAINER", "true")      // for the "set to value" case

3. Removed "__UNSET__" sentinel hack

Before: Table-driven test used envValue: "__UNSET__" as a magic string with special-case if logic:

if tt.envValue == "__UNSET__" {
    os.Unsetenv("RUNNING_IN_CONTAINER")
} else {
    os.Setenv("RUNNING_IN_CONTAINER", tt.envValue)
}

After: Explicit unset bool field in the struct makes intent clear:

{name: "RUNNING_IN_CONTAINER not set", unset: true, expectEnvDetection: false}

4. Fixed per-subtest env isolation in TestIsRunningInContainer_EdgeCases

Previously, each subtest called os.Setenv without any cleanup, relying on the outer test function's defer to restore the variable at the end. This meant env state leaked between subtests. Using t.Setenv inside each subtest now ensures the env var is properly restored after each individual subtest.

5. setupEnv func signature fix in TestIsRunningInContainer_AllMethodsCombined

Updated the setupEnv function in the table struct from func() to func(t *testing.T) so it can call t.Setenv/unsetEnvForTest with the subtest's *testing.T, ensuring cleanup is scoped to the correct subtest.

Why These Changes?

The tty package had a split: container.go had extensive tests but tty.go's sole function IsStderrTerminal() had no test file at all. Adding tty_test.go closes that gap.

container_test.go had significant code duplication: the manual env save/restore pattern appeared 5 times, totalling ~45 lines of boilerplate. Go's t.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

Generated by Test Improver ·

- 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants