beads/cmd/bd/main_errors_test.go
Jim Wordelman 571ab8afe6
fix(cmd/bd): restore os.Stdout via deferred idempotent restore so capture helpers survive t.Fatal (#5803)
* test(feat): red — cmd/bd capture helpers leak os.Stdout on t.Fatal (refs be-leuf)

Add TestCaptureCycleStdoutRestoresOnFatal (dep_cycles_test.go) and the
package-wide TestZZStdioNotLeaked guard (zz_stdio_leak_guard_test.go).
Both express the be-gh02 acceptance criteria before any fix: a capture
helper that restores os.Stdout/os.Stderr on the happy path only leaks
the global when its callback exits via runtime.Goexit (t.Fatal/panic).

RED confirmed: TestCaptureCycleStdoutRestoresOnFatal fails with
"captureCycleStdout leaked os.Stdout after a Goexit"; go vet clean;
0 broken-pipe collateral (the new test restores os.Stdout itself
before failing, so it doesn't poison later tests).

* fix(feat): green — cmd/bd capture helpers leak os.Stdout on t.Fatal (refs be-leuf)

Move every bare stdio-restore in cmd/bd's capture helpers into a defer
registered immediately after assignment, so a callback that exits via
runtime.Goexit (t.Fatal) or panic can no longer leave os.Stdout/os.Stderr
pointed at an orphaned pipe.

- captureCycleStdout (dep_cycles_test.go): restore moved into a defer,
  drain moved to a goroutine so a >64KiB write can't deadlock.
- Both inline os.Stderr captures in dep_cycles_test.go: deferred.
- captureStdout/captureStderr (test_helpers_pure_test.go): deferred,
  stdioMutex discipline unchanged.
- TestCaptureCycleStdoutRestoresOnFatal: drives the leaking callback via
  a goroutine + runtime.Goexit so the suite stays fully green (the fix
  spec's stated alternative to the failing-subtest shape).
- 19-site mechanical sweep across 11 more files: same bare-restore ->
  defer transform, no other restructuring, per spec item 6.

GREEN confirmed: go vet ./cmd/bd/... clean. go test -v -count=1
./cmd/bd/... via scripts/test.sh (beads_test_env_enter,
BEADS_TEST_SKIP=dolt) — EXIT_CODE=0, 0 'broken pipe' occurrences (was
3), 0 FAIL lines, all packages ok. TestRunSyncCommandExitCodeMapping
passes (all 3 subtests), TestZZStdioNotLeaked passes,
TestCaptureCycleStdoutRestoresOnFatal passes. The two dep-cycles tests
(TestDepCyclesNamesTheMembersItCannotDescribe,
TestDepCyclesSaysNothingIsWrongForACleanWorkspace) also pass in this
run — their arming precondition is order/environment-dependent per the
fix spec, so this is not required and does not indicate the fix works
only by coincidentally fixing them; the decoupling is structural.

* docs(release-gates): add be-sr7a gate record — PASS

Review be-1kxq passed; independently re-verified diff-owned tests
(TestCaptureCycleStdoutRestoresOnFatal, TestZZStdioNotLeaked,
TestRunSyncCommandExitCodeMapping) and full ./cmd/bd/... suite
(2389 PASS, 0 FAIL, 0 broken pipe/EPIPE occurrences), plus the
ci-pr-policy lane. .githooks/commit-msg false-failure root-caused
as a local gc-rig worktree shim (same finding as be-3b4e) and
remediated reversibly rather than waived.
2026-08-19 21:01:06 -07:00

45 lines
1 KiB
Go

package main
import (
"bytes"
"errors"
"io"
"os"
"strings"
"testing"
)
func TestHandleFreshCloneError_UsesBootstrapFirstGuidance(t *testing.T) {
err := errors.New("post-migration validation failed: required config key missing: issue_prefix")
origStderr := os.Stderr
r, w, pipeErr := os.Pipe()
if pipeErr != nil {
t.Fatal(pipeErr)
}
os.Stderr = w
defer func() { os.Stderr = origStderr }()
handled := handleFreshCloneError(err)
_ = w.Close()
var buf bytes.Buffer
if _, copyErr := io.Copy(&buf, r); copyErr != nil {
t.Fatal(copyErr)
}
_ = r.Close()
if !handled {
t.Fatal("expected fresh clone error to be handled")
}
msg := buf.String()
if !strings.Contains(msg, "bd bootstrap") {
t.Fatalf("expected bootstrap guidance, got:\n%s", msg)
}
if strings.Contains(msg, "To initialize a new database: bd init") {
t.Fatalf("did not expect init-first guidance for fresh clone recovery, got:\n%s", msg)
}
if !strings.Contains(msg, "brand-new database from scratch") {
t.Fatalf("expected brand-new project fallback note, got:\n%s", msg)
}
}