beads/cmd/bd/scripttest_test.go
Julian Knutsen cdda0dffa5
test(cmd/bd): drop dead unsupported-storage test, CWD-independent bd fallback build, stop BEADS_DIR leaking between in-process runs (#7171)
* test(delete): drop stale TestDeleteIssueUnsupportedStorage

The test asserted that deleteIssue returns "delete operation not supported
by this storage backend" when the global store is nil. That error came
from a type assertion to a deleter interface that a5da57e39 (Phase 7:
remove storage factory/memory backend/provider abstraction) removed:
store is now storage.DoltStorage, whose method set always includes
DeleteIssue, so there is no unsupported backend left to report. With the
assertion gone, the test's nil store is just a nil-interface method call
and panics at deleteIssue, killing the whole cmd/bd test binary.

The nil store is not reachable in production: deleteIssue's only caller
(bd doctor --check=pollution --clean) has already queried the store to
find the issues it deletes. So the test constructs an impossible state
for a removed behavior; delete it rather than reintroduce a dead branch.

No CI lane ran it: it needs the integration tag and a Dolt test server
(testDoltServerPort != 0), and every integration lane that builds
cmd/bd (main.yml integration cmd/bd shards, nightly Full Test Suite,
bazel --config=integration) sets BEADS_TEST_SKIP=dolt, while the
proxied/dolt-server lanes run only their TestProxiedServer*/
TestServerMode* manifests. It only ran in local integration runs with
Docker or a matching dolt.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* test(cmd/bd): build the fallback bd by package path, independent of CWD

When BEADS_TEST_BD_BINARY is unset, buildEmbeddedBD and its sibling
helpers fall back to `go build -tags gms_pure_go -o X .`, which builds
whatever package is in the process's working directory. That is cmd/bd
only if the first caller of the sync.Once has not chdir'd into a fixture
and the test binary was started in cmd/bd. A precompiled test binary run
from the repo root (the pr-risk pattern) builds the root library package
instead: go writes a non-executable archive to X and the test dies with
"fork/exec .../bd: permission denied" (reproduced with TestEmbeddedCount).

Add goBuildBDCommand: the same non-race -tags gms_pure_go build CI
prebuilds BEADS_TEST_BD_BINARY with, naming
github.com/steveyegge/beads/cmd/bd explicitly and running in cmd/bd's
source directory, resolved at package init (this file's compiled-in path,
else the start-up working directory) before any test can chdir. Use it in
buildEmbeddedBD and the other copies of the pattern (cli_fast,
doctor_repair, explicit_db_nodb, init_permissions, scripttest,
shared_server, buildBDForInitTests); migrationFrontDoorBinary keeps its
untagged cgo build but gets the same package path and directory.
TestGoBuildBDCommandIsCWDIndependent checks it from a chdir'd temp dir
with go list (no link).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* test(cmd/bd): stop in-process bd from leaking BEADS_DIR into later tests

TestResolvedConfigRepoRoot's two CWD-discovery subtests ("uses local
workspace from subdirectory", "uses worktree fallback when local .beads
is absent") failed in full cmd/bd runs with "no active beads workspace
found" but passed alone. Cause: runBDInProcess / runBDInProcessAllowError
run rootCmd.Execute in the test process, and PersistentPreRun exports the
selected workspace with a raw os.Setenv("BEADS_DIR", ...) that nothing
restores. After any TestCLI_* test, BEADS_DIR still names that test's
since-deleted bd-cli-test-* fixture; FindBeadsDir honors an explicit
BEADS_DIR before walking from CWD, finds nothing there, and reports no
workspace. Minimal repro: -run '^(TestCLI_Ready|TestResolvedConfigRepoRoot)$'.

The leak also broke later TestCLI_* tests themselves: with
-run '^(TestCLI_.*|TestResolvedConfigRepoRoot)$' against a Dolt test
server, failures drop from 47 to 20 with this change and no new ones (the
remaining 20 are leftover cobra flag values between in-process runs, a
separate issue).

- runBDInProcess(AllowError): snapshot os.Environ() around Execute and
  restore exactly the keys it changed (restoreEnvSnapshot).
- TestResolvedConfigRepoRoot: clear BEADS_DIR (t.Setenv) in the two
  subtests that test CWD discovery, so neither a leak nor a developer's
  exported BEADS_DIR decides them.

Not host-specific (HOME, global git config and ceilings play no part),
and CI never saw it: the TestCLI_* leakers need a Dolt test server, and
every CI lane that runs cmd/bd's integration tests sets
BEADS_TEST_SKIP=dolt, so they skip and BEADS_DIR is never set.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* test(delete): expect storage.ErrNotFound after delete in TestDeleteIssueWrapper

"successful issue deletion" asserted that GetIssue returns (nil, nil)
for a deleted issue; the Dolt store reports a missing issue as
storage.ErrNotFound, so the subtest failed with "GetIssue failed: not
found: issue test-xxx" whenever it ran. Like TestDeleteIssueUnsupportedStorage
it needs the integration tag and a Dolt test server, which no CI lane
provides to cmd/bd (BEADS_TEST_SKIP=dolt), so it rotted unseen.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-03 23:44:37 -07:00

49 lines
1.4 KiB
Go

//go:build scripttests
// +build scripttests
package main
import (
"context"
"os"
"path/filepath"
"runtime"
"testing"
"time"
"rsc.io/script"
"rsc.io/script/scripttest"
)
func TestScripts(t *testing.T) {
// Skip on Windows - test scripts use sh -c which requires Unix shell
if runtime.GOOS == "windows" {
t.Skip("scripttest uses Unix shell commands (sh -c), skipping on Windows")
}
// Locate or build the bd binary. Prebuilt fast path (scripts/test.sh and
// CI export BEADS_TEST_BD_BINARY, wy-4mtr0); the scripts invoke plain
// `bd` via `sh -c`, so the prebuilt is only usable when its basename is
// exactly the expected executable name.
exeName := "bd"
binDir := t.TempDir()
exe := filepath.Join(binDir, exeName)
if prebuilt, err := findPrebuiltBDBinary(); err == nil && prebuilt != "" && filepath.Base(prebuilt) == exeName {
exe = prebuilt
binDir = filepath.Dir(prebuilt)
} else if err := goBuildBDCommand(exe).Run(); err != nil {
t.Fatal(err)
}
// Create minimal engine with default commands plus bd
timeout := 2 * time.Second
engine := script.NewEngine()
engine.Cmds["bd"] = script.Program(exe, nil, timeout)
// Add binDir to PATH so 'sh -c bd ...' works in test scripts
currentPath := os.Getenv("PATH")
env := []string{"PATH=" + binDir + ":" + currentPath}
// Run all tests
scripttest.Test(t, context.Background(), engine, env, "testdata/*.txt")
}