beads/scripts/ci/lib/test-env.sh
Julian Knutsen f66375459f
test(bazel): runfiles helpers + declared inputs for scan-guard tests (#6828)
* test(bazel): add internal/testutil/bazeltest runfiles helpers

Repository-path resolution for tests that works identically under plain
go test (package cwd, full checkout), bazel test locally, and remote
execution (declared runfiles only):

- IsBazel, RepoRoot (BEADS_TEST_REPO_ROOT, then the runfiles workspace
  root, then a go.mod walk), OverrideRoot (drop-in condition for existing
  helpers; empty under plain go test), CallerDir
- Runfile for $(rlocationpath) values, GOROOT for type-checking tests
- ShardFreeEnv for helpers that re-exec a test binary

Every helper reduces to the pre-Bazel behavior under go test.

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

* test(bazel): declare source/data reads for 14 scan-guard targets

Source-scanning and repo-file tests read files their Bazel targets never
declared, so they failed under `bazel test` (and could not run on remote
workers at all). Declare narrow inputs as `data` and resolve paths through
bazeltest where the test located the tree via runtime.Caller:

- tools/bazel/go_srcs.py (run by `make bazel-sync` after gazelle) keeps a
  managed `go_srcs` filegroup in the packages whose Go source tests scan,
  plus `//internal/storage:tree_go_srcs` for the storage-tree walk. Bazel
  globs stop at package boundaries, so the aggregate needs one filegroup
  per package; the script keeps it in sync and gazelle-stable.
- Hand-written narrow inputs: //internal/storage/schema:migrations,
  //:docsync_files, //plugins/beads:plugin_files, and exports of
  .gitattributes, .claude-plugin/marketplace.json, cmd/bd list.go and
  ready.go, testutil/testdoltcommon.go, scripts README.md and the release
  catalog, each visible only to its consumer.
- runtime.Caller-based roots in backend, backend/conformance,
  internal/storage, internal/formula and test/docsync now go through
  bazeltest.CallerDir / OverrideRoot; relative-path tests need data only.
- integrations/ leaves .bazelignore (it is still gazelle-excluded) so the
  docsync link check can declare integrations/beads-mcp/PYPI.md.

Under --config=prcore every target reports the same PASS/SKIP/FAIL test
set as `go test -race -short` for its package, locally and remotely.
Plain `go test` behavior is unchanged.

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

* test(bazel): guard generated go_srcs and harden bazeltest root override

- tools/bazel/go_srcs.py: add --check (print diff, exit 1 on stale blocks);
  new `make bazel-sync-check` target runs it.
- scripts/bazel_policy_test.go: under go test, assert every tree_go_srcs
  (//internal/storage) lists exactly the BUILD.bazel packages below its
  root, run go_srcs.py --check, and forbid BUILD files under docs/ and
  engdocs/ (they would cut subtrees out of //:docsync_files).
- bazeltest: honor BEADS_TEST_REPO_ROOT only when it holds go.mod
  (RepoRoot fails loudly otherwise); drop the unused GOROOT() helper,
  whose compiled-in branch could never fire under rules_go.
- scripts/ci/lib/test-env.sh: unset BEADS_TEST_REPO_ROOT.
- Document the hand-listed out-of-tree link targets in //:docsync_files.

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

* build(bazel): re-sync BUILD files with main

Regenerated with make bazel-sync after commits that landed while the
scaffold PR was in CI (new internal/execenv, gitenv, gitignore packages
and new sources/deps).

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

* test(bazel): declare init_safety_help.go for docsync

TestInitSafetyCLIHelpCoversReCloneGotchas (#6431) reads
cmd/bd/init_safety_help.go; export it to //test/docsync only.

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 08:24:08 -07:00

101 lines
2.8 KiB
Bash

#!/usr/bin/env bash
# Shared hermetic environment setup for broad local/CI test wrappers.
if [[ -n "${BEADS_CI_TEST_ENV_SH_LOADED:-}" ]]; then
return 0
fi
BEADS_CI_TEST_ENV_SH_LOADED=1
beads_test_env_enter() {
if [[ "${BEADS_TEST_ENV_DISABLE:-0}" == "1" ]]; then
return 0
fi
if [[ "${BEADS_TEST_ENV_ACTIVE:-0}" == "1" ]]; then
return 0
fi
local root
root="$(mktemp -d "${TMPDIR:-/tmp}/beads-test-env-XXXXXX")"
export BEADS_TEST_ENV_ROOT="$root"
export BEADS_TEST_ENV_ACTIVE=1
if [[ -z "${GOCACHE:-}" ]]; then
local go_cache
go_cache="$(go env GOCACHE 2>/dev/null || true)"
if [[ -n "$go_cache" ]]; then
export GOCACHE="$go_cache"
fi
fi
if [[ -z "${GOMODCACHE:-}" ]]; then
local go_mod_cache
go_mod_cache="$(go env GOMODCACHE 2>/dev/null || true)"
if [[ -n "$go_mod_cache" ]]; then
export GOMODCACHE="$go_mod_cache"
fi
fi
mkdir -p "$root/home" "$root/xdg-config" "$root/dolt-root"
: >"$root/gitconfig"
export HOME="$root/home"
export USERPROFILE="$root/home"
export XDG_CONFIG_HOME="$root/xdg-config"
export DOLT_ROOT_PATH="$root/dolt-root"
export GIT_CONFIG_NOSYSTEM=1
export GIT_CONFIG_GLOBAL="$root/gitconfig"
export BEADS_TEST_IGNORE_REPO_CONFIG=1
if [[ "${BEADS_TEST_ENV_RUN_DOLT:-0}" != "1" ]]; then
beads_test_env_add_skip "dolt"
fi
unset BEADS_DIR
unset BEADS_DB
unset BD_DB
unset BD_JSON
unset BD_NO_DB
unset BD_NO_DAEMON
unset BD_ACTOR
unset BEADS_ACTOR
unset GT_ROOT
unset BEADS_DOLT_SHARED_SERVER
unset BEADS_DOLT_SERVER_MODE
unset BEADS_DOLT_AUTO_START
unset BEADS_DOLT_SERVER_HOST
unset BEADS_DOLT_SERVER_PORT
unset BEADS_DOLT_PORT
unset BEADS_DOLT_SERVER_DATABASE
unset BEADS_DOLT_SERVER_SOCKET
unset BEADS_DOLT_PASSWORD
# bazeltest debug override: never let an exported value point source-scan
# guards at another checkout.
unset BEADS_TEST_REPO_ROOT
if command -v dolt >/dev/null 2>&1; then
dolt config --global --add user.name "beads-test" >/dev/null 2>&1 || true
dolt config --global --add user.email "test@beads.local" >/dev/null 2>&1 || true
fi
trap beads_test_env_cleanup EXIT
}
beads_test_env_add_skip() {
local service="$1"
local current=",${BEADS_TEST_SKIP:-},"
if [[ "$current" != *",$service,"* ]]; then
if [[ -n "${BEADS_TEST_SKIP:-}" ]]; then
export BEADS_TEST_SKIP="${BEADS_TEST_SKIP},${service}"
else
export BEADS_TEST_SKIP="$service"
fi
fi
}
beads_test_env_cleanup() {
if [[ "${BEADS_TEST_ENV_KEEP:-0}" == "1" ]]; then
return 0
fi
if [[ -n "${BEADS_TEST_ENV_ROOT:-}" ]]; then
rm -rf "$BEADS_TEST_ENV_ROOT"
unset BEADS_TEST_ENV_ROOT
fi
}