beads/scripts/release_script_test.go
Julian Knutsen 469462d56a
test(bazel): run every Bazel test against a hermetic, pinned dolt CLI (#6853)
* test(bazel): run every test against a hermetic, pinned dolt CLI (slice B4)

Tests that shell out to dolt ran whatever /usr/local/bin/dolt the executor
had, a version outside the action key, and skipped (a vacuous, cacheable
PASS) on an executor without one.

- tools/bazel/dolt.bzl: dolt_release repository rule fetching the upstream
  release pinned to scripts/ci/install-dolt.sh (2.2.0), sha256 for
  linux/darwin amd64/arm64; MODULE.bazel declares one repo per platform and
  //tools/bazel:dolt selects the one for the execution platform.
- //tools/bazel:test_env (the --run_under wrapper) carries the pinned dolt
  as data. Bazel merges the run_under runfiles into every test's runfiles, so
  every test action declares dolt as an input; test_env.sh puts it first on
  PATH, exports BEADS_TEST_DOLT_BINARY, and fails the test if it is missing.
  DOLT_ROOT_PATH identity seeding stays in test_env.sh.
- testutil.requireDoltBinaryPresent fails instead of skipping under Bazel.
- TestPinnedDoltCLIMatchesContainerImage also checks tools/bazel/dolt.bzl.
  scripts_test declares the pin files and resolves the repo root through
  bazeltest, so the three dolt pin tests now pass under bazel test.

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

* test(bazel): declare the Go source dolt_test's scope guards scan

TestEveryRawTxJournalScopeIsScopedOrExempt, TestEveryRawTxVersionScopeIsScopedOrExempt,
TestTxMintingWrappersScopeVersionedHistory and TestHelperSubprocessSentinelsAreComplete
parse internal/storage/dolt and internal/storage/issueops at test time; under
bazel test that source was not in the runfiles, so all four failed.

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

* fix(bazel): tie hermetic dolt download to its version; skip on unpinned platforms

- Key DOLT_SHA256 by version and pass canonical_id=url, so bumping
  DOLT_VERSION without new digests fails instead of silently reusing the
  previous release from the sha256-keyed repository cache.
- //tools/bazel:dolt_version_test runs the hermetic dolt and checks it
  reports DOLT_VERSION.
- Unpinned platforms (e.g. windows) resolve to an incompatible target, so
  bazel build //... skips the wrapper there instead of failing analysis.
- test_env.sh also unsets BEADS_DOLT_BIN so it cannot bypass the pin.

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

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 14:29:49 -07:00

421 lines
13 KiB
Go

package scripts_test
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/steveyegge/beads/internal/formula"
"github.com/steveyegge/beads/internal/testutil/bazeltest"
)
func TestReleaseScriptUsesVerifiedInstalledBDBeforeStaleRepoBD(t *testing.T) {
repo := copyReleaseScriptFixture(t)
writeExecutable(t, filepath.Join(repo, "bd"), `#!/bin/sh
echo "stale repo bd should not run" >&2
exit 42
`)
bin := filepath.Join(repo, "bin")
if err := os.MkdirAll(bin, 0o755); err != nil {
t.Fatal(err)
}
writeFakeBD(t, bin, repo)
writeExecutable(t, filepath.Join(bin, "jq"), `#!/bin/sh
echo "jq should not be called" >&2
exit 99
`)
output, err := runReleaseDryRun(t, repo, bin)
if err != nil {
t.Fatalf("release.sh failed: %v\n%s", err, output)
}
if strings.Contains(output, "stale repo bd should not run") {
t.Fatalf("release.sh used stale repo-root bd:\n%s", output)
}
if !strings.Contains(output, "bd mol wisp beads-release --var version=1.2.3") {
t.Fatalf("release.sh did not use installed bd command:\n%s", output)
}
}
func TestReleaseScriptInstalledBDSelectionDoesNotRequireJQ(t *testing.T) {
repo := copyReleaseScriptFixture(t)
bin := filepath.Join(repo, "bin")
if err := os.MkdirAll(bin, 0o755); err != nil {
t.Fatal(err)
}
writeFakeBD(t, bin, repo)
writeExecutable(t, filepath.Join(bin, "jq"), `#!/bin/sh
echo "jq should not be called" >&2
exit 99
`)
output, err := runReleaseDryRun(t, repo, bin)
if err != nil {
t.Fatalf("release.sh failed without jq: %v\n%s", err, output)
}
if strings.Contains(output, "jq should not be called") {
t.Fatalf("release.sh invoked jq during bd selection:\n%s", output)
}
}
func TestReleaseScriptRejectsExplicitBDThatDoesNotResolveRepoFormula(t *testing.T) {
repo := copyReleaseScriptFixture(t)
bin := filepath.Join(repo, "bin")
if err := os.MkdirAll(bin, 0o755); err != nil {
t.Fatal(err)
}
staleBD := filepath.Join(bin, "bd-stale")
writeExecutable(t, staleBD, `#!/bin/sh
echo '{"source":"/tmp/stale.formula.toml"}'
`)
output, err := runReleaseDryRunWithEnv(t, repo, bin, "BD="+staleBD)
if err == nil {
t.Fatalf("release.sh succeeded with stale explicit BD:\n%s", output)
}
if !strings.Contains(output, "BD is set but does not resolve the checked-in beads-release formula") {
t.Fatalf("release.sh did not explain stale explicit BD:\n%s", output)
}
}
func TestReleaseFormulaCleanupStaleDoltOrphansHandlesLocalModeWithoutJQ(t *testing.T) {
repoRoot := sourceRepoRoot(t)
formulaPath := filepath.Join(repoRoot, ".beads", "formulas", "beads-release.formula.toml")
if _, err := formula.NewParser().ParseFile(formulaPath); err != nil {
t.Fatalf("beads-release formula does not parse: %v", err)
}
data, err := os.ReadFile(formulaPath)
if err != nil {
t.Fatal(err)
}
text := string(data)
start := strings.Index(text, `id = "cleanup-stale-dolt-orphans"`)
if start < 0 {
t.Fatal("cleanup-stale-dolt-orphans step not found")
}
step := text[start:]
if next := strings.Index(step[len(`id = "cleanup-stale-dolt-orphans"`):], "\n[[steps]]"); next >= 0 {
step = step[:len(`id = "cleanup-stale-dolt-orphans"`)+next]
}
for _, unwanted := range []string{"jq", "Could not determine Dolt mode"} {
if strings.Contains(step, unwanted) {
t.Fatalf("cleanup-stale-dolt-orphans still contains %q:\n%s", unwanted, step)
}
}
for _, want := range []string{`case "$MODE" in`, "embedded)", "external)", "running|data_dir", `bd dolt killall`} {
if !strings.Contains(step, want) {
t.Fatalf("cleanup-stale-dolt-orphans missing %q:\n%s", want, step)
}
}
}
func TestReleaseFormulaHomebrewCoreProcedureCoversTemplateAndBottles(t *testing.T) {
repoRoot := sourceRepoRoot(t)
formulaPath := filepath.Join(repoRoot, ".beads", "formulas", "beads-release.formula.toml")
if _, err := formula.NewParser().ParseFile(formulaPath); err != nil {
t.Fatalf("beads-release formula does not parse: %v", err)
}
homebrewStep := releaseFormulaStep(t, formulaPath, `id = "verify-homebrew-core"`)
for _, want := range []string{
"brew bump-formula-pr",
"--url=\"https://github.com/gastownhall/beads/archive/refs/tags/v{{version}}.tar.gz\"",
"Homebrew's current PR template",
"Do not remove or hand-edit the bottle block.",
"Do not open a duplicate manual PR.",
"homepage \"https://github.com/gastownhall/beads\"",
"Homebrew/homebrew-core@main",
} {
if !strings.Contains(homebrewStep, want) {
t.Fatalf("verify-homebrew-core step missing %q:\n%s", want, homebrewStep)
}
}
localInstallStep := releaseFormulaStep(t, formulaPath, `id = "local-install"`)
for _, want := range []string{
`needs = ["verify-github", "verify-npm", "verify-pypi", "verify-homebrew-core"]`,
"brew upgrade beads",
} {
if !strings.Contains(localInstallStep, want) {
t.Fatalf("local-install step missing %q:\n%s", want, localInstallStep)
}
}
if strings.Contains(localInstallStep, "brew upgrade bd") {
t.Fatalf("local-install step still references old Homebrew formula name:\n%s", localInstallStep)
}
}
func releaseFormulaStep(t *testing.T, formulaPath, marker string) string {
t.Helper()
data, err := os.ReadFile(formulaPath)
if err != nil {
t.Fatal(err)
}
text := string(data)
start := strings.Index(text, marker)
if start < 0 {
t.Fatalf("release formula step marker not found: %s", marker)
}
step := text[start:]
if next := strings.Index(step[len(marker):], "\n[[steps]]"); next >= 0 {
step = step[:len(marker)+next]
}
return step
}
func copyReleaseScriptFixture(t *testing.T) string {
t.Helper()
src := filepath.Join(sourceRepoRoot(t), "scripts", "release.sh")
data, err := os.ReadFile(src)
if err != nil {
t.Fatal(err)
}
repo := releaseTestTempDir(t)
if err := os.MkdirAll(filepath.Join(repo, "scripts"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(repo, ".beads", "formulas"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(repo, "scripts", "release.sh"), data, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(repo, ".beads", "formulas", "beads-release.formula.toml"), []byte("formula = \"beads-release\"\n"), 0o644); err != nil {
t.Fatal(err)
}
return repo
}
func releaseTestTempDir(t *testing.T) string {
t.Helper()
// Keep fixtures out of the source tree (t.TempDir handles cleanup and
// works from read-only checkouts); shellPath converts the resulting
// host path for Bash separately at the call sites that need it.
return t.TempDir()
}
func sourceRepoRoot(t *testing.T) string {
t.Helper()
_, file, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("runtime.Caller failed")
}
// Under Bazel the caller path is workspace-relative; CallerDir rebuilds it
// under the runfiles root, which holds the files scripts_test declares.
return filepath.Dir(bazeltest.CallerDir(file, "scripts"))
}
func runReleaseDryRun(t *testing.T, repo, bin string) (string, error) {
t.Helper()
return runReleaseDryRunWithEnv(t, repo, bin, "BD=")
}
func runReleaseDryRunWithEnv(t *testing.T, repo, bin string, extraEnv ...string) (string, error) {
t.Helper()
assignments := []string{
// Fake bin first, then the caller's PATH (Nix/Guix coreutils), then a
// /usr/bin:/bin baseline: on Windows git-bash the coreutils release.sh
// needs live there but are absent from the Go process's PATH.
"PATH=" + shSingleQuote(shellPath(t, bin)+":"+bashPathList(t, os.Getenv("PATH"))+":/usr/bin:/bin"),
"BD_FAKE_FORMULA_SOURCE=" + shSingleQuote(shellPath(t, filepath.Join(repo, ".beads", "formulas", "beads-release.formula.toml"))),
}
for _, env := range extraEnv {
key, value, ok := strings.Cut(env, "=")
if !ok {
continue
}
if key == "BD" && value != "" {
value = shellPath(t, value)
}
assignments = append(assignments, key+"="+shSingleQuote(value))
}
cmd := exec.Command("bash", "-lc", strings.Join(assignments, " ")+" bash scripts/release.sh 1.2.3 --dry-run")
cmd.Dir = repo
out, err := cmd.CombinedOutput()
return string(out), err
}
func msysPath(path string) string {
path = filepath.Clean(path)
path = filepath.ToSlash(path)
if len(path) >= 3 && path[1] == ':' && path[2] == '/' {
return "/" + strings.ToLower(path[:1]) + path[2:]
}
return path
}
func shellPath(t *testing.T, path string) string {
t.Helper()
clean := filepath.Clean(path)
dir := clean
base := ""
if info, err := os.Stat(clean); err == nil && !info.IsDir() {
dir = filepath.Dir(clean)
base = filepath.Base(clean)
}
cmd := exec.Command("bash", "-lc", "pwd")
cmd.Dir = dir
if out, err := cmd.Output(); err == nil {
converted := strings.TrimSpace(string(out))
if converted != "" {
if base != "" {
return converted + "/" + filepath.ToSlash(base)
}
return converted
}
}
for _, tool := range []string{"wslpath", "cygpath"} {
out, err := exec.Command(tool, "-u", path).Output()
if err == nil {
converted := strings.TrimSpace(string(out))
if converted != "" {
return converted
}
}
}
return msysPath(path)
}
func shellPathUnderEnv(t *testing.T, bash, path string, env []string) string {
t.Helper()
clean := filepath.Clean(path)
dir := clean
base := ""
if info, err := os.Stat(clean); err == nil && !info.IsDir() {
dir = filepath.Dir(clean)
base = filepath.Base(clean)
}
cmd := exec.Command(bash, "--noprofile", "--norc", "-c", "pwd")
cmd.Dir = dir
cmd.Env = env
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("resolve shell path for %s under script environment: %v\n%s", path, err, out)
}
converted := strings.TrimSpace(string(out))
if converted == "" {
t.Fatalf("resolve shell path for %s under script environment: empty output", path)
}
if base != "" {
return converted + "/" + filepath.ToSlash(base)
}
return converted
}
// shellPathEnv keeps minimal-environment path conversion independent of
// inherited temporary-path mounts. Git for Windows can map /tmp differently
// when TEMP/TMP are omitted, so helpers that replace the child environment
// must resolve paths under that same authority.
func shellPathEnv() []string {
path := os.Getenv("PATH")
if runtime.GOOS == "windows" {
path = "/usr/bin:/bin"
}
return []string{
"PATH=" + path,
"LC_ALL=C",
"LANG=C",
"BASH_ENV=",
"ENV=",
}
}
func requireShellCommandPath(t *testing.T, bash, dir string, env []string, name, want string) {
t.Helper()
cmd := exec.Command(bash, "--noprofile", "--norc", "-c", `PATH="$BEADS_TEST_COMMAND_PATH"; export PATH; printf '%s\n' "$PATH"; command -v -- "$1"`, "shim-path-check", name)
cmd.Dir = dir
cmd.Env = env
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("resolve injected %s under script environment: %v\n%s", name, err, out)
}
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
got := lines[len(lines)-1]
if got != want {
t.Fatalf("injected %s resolved to %q, want %q; PATH=%q", name, got, want, lines[0])
}
}
func bashScriptCommand(bash, script string) *exec.Cmd {
return exec.Command(bash, "--noprofile", "--norc", "-c", `PATH="$BEADS_TEST_COMMAND_PATH"; export PATH; exec "$BASH" --noprofile --norc "$1"`, "script-test", script)
}
// bashPathList converts a host-style PATH value (entries separated by
// os.PathListSeparator) into a Bash-visible, colon-separated PATH so the
// caller's PATH is preserved (not just /usr/bin:/bin) when it is prepended
// with the fake bd directory. This matters on systems such as Nix/Guix
// where bash and core utilities live outside /usr/bin and /bin.
func bashPathList(t *testing.T, hostPath string) string {
t.Helper()
entries := filepath.SplitList(hostPath)
converted := make([]string, 0, len(entries))
for _, entry := range entries {
if entry == "" {
continue
}
converted = append(converted, shellPath(t, entry))
}
return strings.Join(converted, ":")
}
func shSingleQuote(s string) string {
return "'" + strings.ReplaceAll(s, "'", `'"'"'`) + "'"
}
func writeFakeBD(t *testing.T, bin, repo string) {
t.Helper()
// Let bash itself canonicalize the formula path. release.sh derives
// FORMULA_PATH from `pwd`, which on Windows under Git Bash/MSYS produces
// mount-aware forms. Keep the fake bd path repo-relative so bash can
// `cd` there portably, then let `pwd` resolve it to the exact form that
// release.sh will compare against.
formulaDir := ".beads/formulas"
body := fmt.Sprintf(`#!/bin/sh
if [ -n "${BD_FAKE_FORMULA_SOURCE:-}" ]; then
SOURCE="$BD_FAKE_FORMULA_SOURCE"
else
SOURCE="$(cd %q && pwd)/beads-release.formula.toml"
fi
if [ "$1 $2 $3 $4" = "formula show beads-release --json" ]; then
printf '%%s\n' "{\"source\":\"$SOURCE\"}"
exit 0
fi
if [ "$1 $2 $3" = "formula show beads-release" ]; then
printf ' ├── preflight\n'
printf ' └── release-complete\n'
exit 0
fi
echo "unexpected fake bd invocation: $*" >&2
exit 64
`, formulaDir)
writeExecutable(t, filepath.Join(bin, "bd"), body)
}
func writeExecutable(t *testing.T, path, body string) {
t.Helper()
if err := os.WriteFile(path, []byte(body), 0o755); err != nil {
t.Fatal(err)
}
if runtime.GOOS == "windows" {
chmodPath := shellPath(t, path)
if wd, err := os.Getwd(); err == nil {
if rel, relErr := filepath.Rel(wd, path); relErr == nil {
chmodPath = filepath.ToSlash(rel)
}
}
cmd := exec.Command("bash", "-lc", "chmod +x "+shSingleQuote(chmodPath))
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("chmod +x %s failed: %v\n%s", path, err, out)
}
}
}