beads/cmd/bd/scripttest_test.go
Julian Knutsen 17127ee544
fix(ci-policy): close review leftovers from #7485/#7486 (#7491)
* fix(ci-policy): close review leftovers from #7485/#7486

Follow-ups from maintainer review on the host-tool exec/shell policy
tests and the bazel.yml integration gate:

- host_tool_shell_policy_test.go: recognize indented function headers
  and `function name {` forms when finding a function's closing brace,
  so scripts/test.sh's indented cleanup_shared_server() no longer
  mis-attributes its trailing lines to the wrong function; classify
  same-line `command -v gofmt && gofmt` (and `go`) as the guarded
  presence-check idiom instead of flagging it.
- host_tool_exec_policy_test.go: make condPolarity reject mixed-polarity
  && / || compounds (one recognized side, one not) instead of guessing
  at the recognized side's polarity, which was unsound in both
  directions; add pinning tests documenting the exec-before-gate and
  function-value-helper cases as known, safety-conservative limitations
  rather than bugs. Fixing this exposed a real ungated fallback in
  cmd/bd/scripttest_test.go (a `go build` that could still run under
  Bazel on a prebuilt-binary basename mismatch); fixed by symlinking the
  prebuilt binary under the expected name instead of rebuilding it.
- bazel.yml: bazel-server-storage now honors the `integration` input
  like bazel-integration/bazel-cmd-dolt do, so an explicit "off" skips
  it too (latent today: no caller passes "off"). Updated
  ci_workflow_test.go's policy tests to match.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(ci-policy): close should-fix B (go command -v guard evasion) on #7491

shellCommandVGuard previously excused go the same as gofmt, but under an
exit-127 host-tool stub `command -v` finds the stub and the guarded exec
still runs, and on a worker genuinely lacking go the exec was silently
skipped -- the worker-dependent behavior this policy exists to ban. Drop
go from the guard entirely, require the guarded exec to sit directly after
the guard's own `&&` (no intervening `;`, `||`, `|`, or leading `!`), and
require an exact gofmt match so a same-prefixed sibling like `gofmt-foo`/
`go-junit-report` can't borrow the guard. Add every should-fix B shape as
a fixture that must still be flagged, plus the generalized gofmt-side
cases (leading `!`, exact-name, post-`;` exec).

Also: correct host_tool_exec_policy_test.go's doc claim that a
function-value helper call site is "not a soundness hole" -- mixed with
one gated direct call site, the invisible function-value call site's
ungated-ness is silently lost and the combined verdict flips to gated;
pinned as review M22. And recognize a function closed by `} >&2` as
ended, so abort-style safety computed inside it does not leak into the
rest of the file (no scoped script has this shape today, but a bare `}`
equality check would silently miss it the moment one does).

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

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-11 01:22:48 +00:00

65 lines
2 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)
prebuilt, err := findPrebuiltBDBinary()
if err != nil {
t.Fatal(err)
}
if prebuilt != "" {
// Under Bazel this is always non-empty (bazeltest.PrebuiltBD), so the
// `go build` fallback below never runs there -- but its basename may
// not be literally "bd" (the injected //cmd/bd:bd_for_tests runfile),
// and the scripts below invoke plain `bd` via sh -c, so symlink it
// under the expected name rather than falling through to a host `go
// build` that would otherwise still run under Bazel on a basename
// mismatch (the exact worker-dependent failure this scan guards
// against: f4-spec.md §9).
if filepath.Base(prebuilt) == exeName {
exe = prebuilt
binDir = filepath.Dir(prebuilt)
} else if err := os.Symlink(prebuilt, exe); err != nil {
t.Fatalf("symlink prebuilt bd binary: %v", err)
}
} 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")
}