* 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.
122 lines
4.1 KiB
Go
122 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/config"
|
|
"github.com/steveyegge/beads/internal/storage/schema"
|
|
)
|
|
|
|
// TestHandleSchemaSkewJSON_Shape verifies the JSON written to stderr by
|
|
// handleSchemaSkewJSON has the expected shape: error (terse one-liner),
|
|
// hint (escape-hatch string), schema_skew subobject, and schema_version.
|
|
func TestHandleSchemaSkewJSON_Shape(t *testing.T) {
|
|
skew := &schema.SchemaSkewError{DBVersion: 45, BinaryVersion: 42}
|
|
|
|
origStderr := os.Stderr
|
|
r, w, pipeErr := os.Pipe()
|
|
if pipeErr != nil {
|
|
t.Fatal(pipeErr)
|
|
}
|
|
os.Stderr = w
|
|
defer func() { os.Stderr = origStderr }()
|
|
|
|
handleSchemaSkewJSON(skew)
|
|
|
|
_ = w.Close()
|
|
|
|
var buf bytes.Buffer
|
|
if _, err := io.Copy(&buf, r); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_ = r.Close()
|
|
|
|
var parsed map[string]interface{}
|
|
if err := json.Unmarshal(buf.Bytes(), &parsed); err != nil {
|
|
t.Fatalf("json.Unmarshal stderr: %v\nstderr was: %s", err, buf.String())
|
|
}
|
|
|
|
// "error" key must contain the terse Error() string.
|
|
wantError := skew.Error()
|
|
if got, ok := parsed["error"].(string); !ok || got != wantError {
|
|
t.Errorf("error = %v, want %q", parsed["error"], wantError)
|
|
}
|
|
|
|
// "hint" key must contain EscapeHint().
|
|
wantHint := skew.EscapeHint()
|
|
if got, ok := parsed["hint"].(string); !ok || got != wantHint {
|
|
t.Errorf("hint = %v, want %q", parsed["hint"], wantHint)
|
|
}
|
|
|
|
// "schema_version" key must be JSONSchemaVersion.
|
|
if got, ok := parsed["schema_version"].(float64); !ok || int(got) != JSONSchemaVersion {
|
|
t.Errorf("schema_version = %v, want %d", parsed["schema_version"], JSONSchemaVersion)
|
|
}
|
|
|
|
// "schema_skew" subobject must be present with current_version, required_version, delta.
|
|
skewObj, ok := parsed["schema_skew"].(map[string]interface{})
|
|
if !ok {
|
|
t.Fatalf("schema_skew key missing or wrong type: %T", parsed["schema_skew"])
|
|
}
|
|
if got, ok := skewObj["current_version"].(float64); !ok || int(got) != 45 {
|
|
t.Errorf("schema_skew.current_version = %v, want 45", skewObj["current_version"])
|
|
}
|
|
if got, ok := skewObj["required_version"].(float64); !ok || int(got) != 42 {
|
|
t.Errorf("schema_skew.required_version = %v, want 42", skewObj["required_version"])
|
|
}
|
|
if got, ok := skewObj["delta"].(float64); !ok || int(got) != 3 {
|
|
t.Errorf("schema_skew.delta = %v, want 3", skewObj["delta"])
|
|
}
|
|
}
|
|
|
|
// TestIgnoreSchemaSkewFlagRegistered verifies that --ignore-schema-skew is
|
|
// registered as a persistent flag on the root command.
|
|
func TestIgnoreSchemaSkewFlagRegistered(t *testing.T) {
|
|
f := rootCmd.PersistentFlags().Lookup("ignore-schema-skew")
|
|
if f == nil {
|
|
t.Fatal("--ignore-schema-skew persistent flag is not registered")
|
|
}
|
|
if f.Value.Type() != "bool" {
|
|
t.Errorf("--ignore-schema-skew flag type = %q, want bool", f.Value.Type())
|
|
}
|
|
}
|
|
|
|
// TestIgnoreSchemaSkewFlagPropagatesEnvVar verifies that PersistentPreRun
|
|
// sets BD_IGNORE_SCHEMA_SKEW=1 when --ignore-schema-skew is true, so all
|
|
// store-open paths see the escape hatch uniformly (not just checkSchemaSkew).
|
|
func TestIgnoreSchemaSkewFlagPropagatesEnvVar(t *testing.T) {
|
|
// Isolate CWD and BEADS_DIR before invoking PersistentPreRunE below.
|
|
// Without this, selectedNoDBBeadsDir falls through to FindBeadsDir(),
|
|
// which auto-detects a real .beads dir from the ambient working
|
|
// directory (e.g. the shared .beads of a git worktree's main
|
|
// checkout) and leaks it into BEADS_DIR for the rest of the test
|
|
// binary — corrupting later tests that assume BEADS_DIR is unset.
|
|
t.Chdir(t.TempDir())
|
|
t.Setenv("BEADS_DIR", "")
|
|
t.Setenv("BD_IGNORE_SCHEMA_SKEW", "")
|
|
t.Setenv("BEADS_DOLT_SERVER_DATABASE", "")
|
|
t.Setenv("BEADS_DOLT_SERVER_PORT", "")
|
|
|
|
config.ResetForTesting()
|
|
t.Cleanup(config.ResetForTesting)
|
|
|
|
savePersistentPreRunState(t)
|
|
old := ignoreSchemaSkew
|
|
ignoreSchemaSkew = true
|
|
t.Cleanup(func() { ignoreSchemaSkew = old })
|
|
|
|
if rootCmd.PersistentPreRunE == nil {
|
|
t.Fatal("rootCmd.PersistentPreRunE must be set")
|
|
}
|
|
if err := rootCmd.PersistentPreRunE(versionCmd, nil); err != nil {
|
|
t.Fatalf("PersistentPreRunE: %v", err)
|
|
}
|
|
|
|
if got := os.Getenv("BD_IGNORE_SCHEMA_SKEW"); got != "1" {
|
|
t.Errorf("BD_IGNORE_SCHEMA_SKEW = %q after PersistentPreRun with --ignore-schema-skew; want 1", got)
|
|
}
|
|
}
|