beads/scripts/pre_push_suite_test.go
Julian Knutsen 368b225eef
docs(bazel): make bazel test the primary build and test path (ga-96smfk.23) (#7348)
Bazel is beads' build and test system: bazel.yml's lanes gate every PR,
and nogo (lint + vet), gofmt and the repository guards exist only as
Bazel targets. The docs, the Makefile and the hooks now say so.

Makefile: `make test` runs bazel.yml's test lane (`bazel test //...
--config=ci`), `make check` adds the testing.Short policy and the
`make ci-pr-lint` nogo gate, and `make check-docs` runs the docsync and
doc-freshness Bazel targets before the pinned-release CLI flag check.
BAZEL_FLAGS passes --config=fork-cache or --config=remote-exec through.
The plain-Go recipes stay as explicit twins: test-go, check-go,
check-docs-go. No workflow called the primary names, and
TestWorkflowsNameTheirTestEngine keeps it that way.

Hooks: pre-push runs scripts/pre-push-suite.sh for branch pushes whose
commits change Go or Bazel inputs: the test lane with
--config=remote-exec when some rc names an executor, else
--config=fork-cache. Without bazel, or with BD_PREPUSH_SUITE=go, it runs
make test-go under a banner saying that is not what CI gates on.

Docs: AGENTS.md, CONTRIBUTING.md, engdocs/TESTING.md (a per-lane command
table and the fork-cache/remote-exec setup), README, RELEASING, the PR
template and the .bazelrc header. Bazelisk is a required prerequisite;
go test is an inner-loop convenience CI does not enforce.

Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-07 07:46:02 +00:00

157 lines
5.8 KiB
Go

package scripts_test
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// scripts/pre-push-suite.sh is the push-time suite .githooks/pre-push runs
// when a pushed branch changes Go or Bazel inputs: bazel.yml's test lane
// (`bazel test //... --config=ci`), remotely when some rc names an executor,
// else through the read-only fork cache. Plain go test runs only when bazel
// cannot (or BD_PREPUSH_SUITE=go asks for it), and then under a banner, so
// a green push is never read as CI parity.
type prePushSuiteRun struct {
out string
bazel string
make string
exitErr error
}
// runPrePushSuite runs the suite with fake bazel and make that log their
// arguments. announce is what the fake `bazel info --announce_rc` prints to
// stderr; bazel "missing" points BAZEL at a path that does not exist.
func runPrePushSuite(t *testing.T, bazel, announce string, env ...string) prePushSuiteRun {
t.Helper()
bash := requireHostTool(t, "bash")
dir := t.TempDir()
bazelLog := filepath.Join(dir, "bazel.log")
makeLog := filepath.Join(dir, "make.log")
writeExecutable := func(name, body string) string {
path := filepath.Join(dir, name)
if err := os.WriteFile(path, []byte("#!/usr/bin/env bash\n"+body), 0o755); err != nil {
t.Fatal(err)
}
return path
}
fakeBazel := writeExecutable("bazel", `if [ "$1" = info ]; then printf '%s\n' "$FAKE_ANNOUNCE" >&2; echo release 9; exit 0; fi
printf '%s\n' "$*" >>"`+bazelLog+`"
`)
fakeMake := writeExecutable("make", `printf '%s\n' "$*" >>"`+makeLog+`"
`)
if bazel == "missing" {
fakeBazel = filepath.Join(dir, "no-such-bazel")
}
cmd := exec.Command(bash, filepath.Join(sourceRepoRoot(t), "scripts", "pre-push-suite.sh"))
for _, entry := range os.Environ() {
if name, _, _ := strings.Cut(entry, "="); name == "BD_PREPUSH_SUITE" {
continue
}
cmd.Env = append(cmd.Env, entry)
}
cmd.Env = append(cmd.Env, "BAZEL="+fakeBazel, "MAKE="+fakeMake, "FAKE_ANNOUNCE="+announce)
cmd.Env = append(cmd.Env, env...)
out, err := cmd.CombinedOutput()
read := func(path string) string {
data, _ := os.ReadFile(path)
return strings.TrimSpace(string(data))
}
return prePushSuiteRun{out: string(out), bazel: read(bazelLog), make: read(makeLog), exitErr: err}
}
const announceWithExecutor = `INFO: Reading rc options for 'info' from /home/u/.bazelrc:
Inherited 'build' options: --remote_executor=grpcs://rbe.example:443 --jobs=64`
const announceWithoutExecutor = `INFO: Reading rc options for 'info' from /w/.bazelrc:
Inherited 'common' options: --enable_bzlmod`
func TestPrePushSuiteRunsBazelTestLane(t *testing.T) {
for _, tc := range []struct {
name, announce string
env []string
want string
}{
{"auto with an executor", announceWithExecutor, nil, "test //... --config=ci --config=remote-exec"},
{"auto without an executor", announceWithoutExecutor, nil, "test //... --config=ci --config=fork-cache"},
{"explicit cache", announceWithExecutor, []string{"BD_PREPUSH_SUITE=cache"}, "test //... --config=ci --config=fork-cache"},
{"explicit rbe", announceWithExecutor, []string{"BD_PREPUSH_SUITE=rbe"}, "test //... --config=ci --config=remote-exec"},
} {
t.Run(tc.name, func(t *testing.T) {
run := runPrePushSuite(t, "present", tc.announce, tc.env...)
if run.exitErr != nil {
t.Fatalf("suite failed: %v\n%s", run.exitErr, run.out)
}
if run.bazel != tc.want {
t.Errorf("bazel ran %q, want %q\n%s", run.bazel, tc.want, run.out)
}
if run.make != "" {
t.Errorf("make ran %q; the bazel suite needs no go fallback\n%s", run.make, run.out)
}
})
}
}
func TestPrePushSuiteRbeWithoutExecutorFails(t *testing.T) {
run := runPrePushSuite(t, "present", announceWithoutExecutor, "BD_PREPUSH_SUITE=rbe")
if run.exitErr == nil || run.bazel != "" {
t.Fatalf("BD_PREPUSH_SUITE=rbe without an executor: err %v, bazel ran %q\n%s", run.exitErr, run.bazel, run.out)
}
}
func TestPrePushSuiteGoFallbackIsAnnounced(t *testing.T) {
for _, tc := range []struct {
name, bazel, why string
env []string
}{
{"bazel missing", "missing", "bazel is not installed", nil},
{"explicit go", "present", "BD_PREPUSH_SUITE=go", []string{"BD_PREPUSH_SUITE=go"}},
} {
t.Run(tc.name, func(t *testing.T) {
run := runPrePushSuite(t, tc.bazel, announceWithExecutor, tc.env...)
if run.exitErr != nil {
t.Fatalf("suite failed: %v\n%s", run.exitErr, run.out)
}
if run.make != "test-go" || run.bazel != "" {
t.Errorf("ran make %q, bazel %q; want make test-go only\n%s", run.make, run.bazel, run.out)
}
for _, want := range []string{"NOT the bazel suite CI gates on", "why: " + tc.why, "bazel test //... --config=ci"} {
if !strings.Contains(run.out, want) {
t.Errorf("go fallback banner lacks %q:\n%s", want, run.out)
}
}
})
}
}
func TestPrePushSuiteRejectsUnknownMode(t *testing.T) {
run := runPrePushSuite(t, "present", announceWithExecutor, "BD_PREPUSH_SUITE=sometimes")
if run.exitErr == nil || run.bazel != "" || run.make != "" {
t.Fatalf("unknown mode: err %v, bazel %q, make %q\n%s", run.exitErr, run.bazel, run.make, run.out)
}
}
// The hook runs the suite only for branch pushes whose commits change Go or
// Bazel inputs; a tag push, a deletion, or a docs-only push skips it.
func TestPrePushHookRunsSuiteOnlyForGoChanges(t *testing.T) {
body := readPolicyFile(t, sourceRepoRoot(t), ".githooks/pre-push")
for _, want := range []string{
`"$repo_root/scripts/pre-push-suite.sh" </dev/null`,
"refs/heads/*)",
"'*.go'",
"'*.bazel'",
} {
if !strings.Contains(body, want) {
t.Errorf(".githooks/pre-push lacks %q", want)
}
}
managed := strings.Index(body, "# --- BEGIN BEADS INTEGRATION")
suite := strings.Index(body, "pre-push-suite.sh")
if managed < 0 || suite < 0 || suite > managed {
t.Errorf(".githooks/pre-push must run the suite before the managed beads section")
}
}