fix: eliminate data race in ci and job tests by avoiding global os.Stdout/os.Stderr mutation
Description
Fix a data race in TestPipelineCmd and TestJobCmd that is exposed when running the test suite with the race detector (make test-race).
Root Cause
internal/commands/ci/ci_test.go and internal/commands/job/job_test.go were capturing command output by replacing the global os.Stdout and os.Stderr variables:
oldOut := os.Stdout
rOut, wOut, _ := os.Pipe()
os.Stdout = wOut // ← writes to global
// ...
go func() { // ← goroutine reads from pipe concurrently
io.Copy(&buf, rOut)
}()When the Go test runner executes tests from different packages concurrently (the default behaviour), this causes a data race: one goroutine writes to os.Stdout/os.Stderr while another goroutine in a different package reads them (e.g. via iostreams.IsTerminal(os.Stdout) or fmt.Fprintf(os.Stderr, ...)).
The race was pre-existing but is reliably surfaced by the make test-race job in the pipeline for !3112 (merged) (bumping go-isatty to v0.0.21).
Fix
- Rewrite
TestPipelineCmdandTestJobCmdto usecmdtest.TestIOStreams()and cobra'scmd.SetOut/cmd.SetErrto capture output without touching global state. - Fix
ci.goto write the deprecation warning tocmd.ErrOrStderr()instead ofos.Stderrdirectly, so the output is captured correctly in tests and respects any stderr override set on the command.
Related Issues
Related to !3112 (merged) (data race in tests:unit job).
How has this been tested?
The fix removes the global os.Stdout/os.Stderr mutation from the two test files. The tests now use the same cmdtest.TestIOStreams() pattern used throughout the rest of the test suite. The race detector should no longer flag these tests.