beads/cmd/bd/zz_stdio_leak_guard_test.go
Julian Knutsen 0500fc6f40
ci(bazel): run the scripts and source-walking tests remotely; retire their go test legs (#7320)
//scripts:scripts_test and //scripts/check-versions:check-versions_test
drop their host-tools/no-remote-exec/no-remote-cache tags and run on the
rbe-west workers like every other target (the worker image ships bash, git,
python3, jq, yq, curl, openssl, unzip and coreutils; a missing tool fails the
test under Bazel rather than skipping it). Go comes in hermetically as the
registered SDK; check-versions.sh's `go build` is stood in for by the
Bazel-built checker.

The tests that walked the source checkout now get it as data:
tools/bazel/go_srcs.py keeps a managed `repo_files` filegroup in every
package and aggregates them into //:repo_files (exactly the tracked files
Bazel can see; make bazel-sync-check fails on a package without one).
scripts_test and docsync_test take it instead of hand-listed filegroups,
and every TEST_SRCDIR/IsBazel skip and go-test-only block in them is gone.
The release formula under .bazelignore'd .beads/ comes in through a
@beads_formulas local repository; npm-package/ and .claude/ are visible to
Bazel again (only their node_modules/worktrees stay ignored), so the npm
manifest release check and the AGENTS.md skill links run under Bazel too.
TestZZStdioNotLeaked falls back to TestMain's pre-run streams instead of
skipping in a shard without its baseline.

tools/bazel/equivalence_allowlist.txt shrinks from 20 skip entries to the
two cmd/bd tests of plain go test's own bd build fallback, which Bazel never
takes; pr-preflight-platforms now runs them on Linux, macOS and Windows. The
scripts-go-checks scripts-test and allowlisted legs, their scripts
(scripts-go-test.sh, allowlisted-go-tests.sh, run_allowlisted_go_tests.py)
and main.yml's --compile-only warm for them are removed. The vet leg stays
(ga-96smfk.18).

Refs: ga-96smfk.16

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-06 22:20:16 +00:00

75 lines
3.1 KiB
Go

package main
import (
"fmt"
"os"
"testing"
)
// TestZZStdioNotLeaked fails if any test in this package reassigned os.Stdout
// or os.Stderr and did not restore it. A capture helper that restores in a
// defer cannot trip this; one that restores on the happy path only will trip
// it as soon as its callback calls t.Fatal. See be-gh02.
//
// The baseline is what the FIRST test saw (aaa_stdio_baseline_test.go), not
// the var-init streams: under `go test -json` the testing framework swaps
// os.Stderr to os.Stdout inside M.Run (go.dev/issue/33419), after var-init
// and before any test, and that framework swap is not a leak (#5881).
//
// It only sees tests that ran before it in this process. TestMain repeats the
// check after m.Run (checkStdioAfterRun), which covers every test in the
// process, including each Bazel shard.
//
// Where TestAAAStdioBaseline did not run in this process (a -run filter, or a
// Bazel shard that holds this test but not that one), the baseline is the
// streams TestMain saw before m.Run, with checkStdioAfterRun's one allowance:
// the framework's own -json swap, os.Stderr = os.Stdout.
func TestZZStdioNotLeaked(t *testing.T) {
wantStdout, wantStderr := baselineStdout, baselineStderr
frameworkSwapAllowed := false
if wantStdout == nil || wantStderr == nil {
if preRunStdout == nil || preRunStderr == nil {
t.Fatal("neither TestAAAStdioBaseline nor TestMain recorded the streams; TestMain must run the suite through runTestsAndSweep")
}
wantStdout, wantStderr = preRunStdout, preRunStderr
frameworkSwapAllowed = true
}
if os.Stdout != wantStdout {
t.Errorf("os.Stdout was leaked by an earlier test (now fd=%d name=%q); "+
"a capture helper restored it on the happy path only - move the restore into a defer",
os.Stdout.Fd(), os.Stdout.Name())
os.Stdout = wantStdout
}
if os.Stderr != wantStderr && (!frameworkSwapAllowed || os.Stderr != wantStdout) {
t.Errorf("os.Stderr was leaked by an earlier test (now fd=%d name=%q); "+
"a capture helper restored it on the happy path only - move the restore into a defer",
os.Stderr.Fd(), os.Stderr.Name())
os.Stderr = wantStderr
}
}
// checkStdioAfterRun fails the process (returning a non-zero code) when a test
// left os.Stdout or os.Stderr reassigned. stdout/stderr are the streams from
// before m.Run. The one reassignment allowed is the framework's own under
// -json, os.Stderr = os.Stdout (see TestZZStdioNotLeaked).
func checkStdioAfterRun(code int, stdout, stderr *os.File) int {
var leaked []string
if os.Stdout != stdout {
leaked = append(leaked, fmt.Sprintf("os.Stdout (now fd=%d name=%q)", os.Stdout.Fd(), os.Stdout.Name()))
}
if os.Stderr != stderr && os.Stderr != stdout {
leaked = append(leaked, fmt.Sprintf("os.Stderr (now fd=%d name=%q)", os.Stderr.Fd(), os.Stderr.Name()))
}
if len(leaked) == 0 {
return code
}
os.Stdout, os.Stderr = stdout, stderr
for _, l := range leaked {
fmt.Fprintf(stderr, "FAIL: %s was leaked by a test in this process; "+
"a capture helper restored it on the happy path only - move the restore into a defer\n", l)
}
if code == 0 {
code = 1
}
return code
}