beads/scripts/BUILD.bazel
Julian Knutsen b7dba4d4e4
test: ban ungated host go/gofmt execs under Bazel; pin compact_gc_fixture pure/norace (#7485)
* test: ban ungated host go/gofmt execs under Bazel; pin compact_gc_fixture pure/norace

Follow-up to #7478. Adds two policy tests on //scripts:go_sources_test:

- host_tool_exec_policy_test.go: an AST scan of every *_test.go file and
  internal/testutil's non-test .go files for exec.Command/CommandContext/
  LookPath("go"/"gofmt") that isn't gated by bazeltest.IsBazel()/TEST_SRCDIR
  in its enclosing function, with an explicit, justified allowlist for the
  handful of sites gated by caller dataflow instead (bazeltest.PrebuiltBD()
  checked before calling a separate helper). "dolt" is out of scope: the
  hermetic copy resolves first on Bazel's PATH, unlike go/gofmt.
- host_tool_shell_policy_test.go: the same policy for the shell scripts
  under scripts/ that Bazel sh_test/go_test targets actually execute
  (fmt-check.sh, the repochecks guards, gofmt-bin.sh, the migration-test
  harness), matching the gating pattern #7478 added to fmt-check.sh and
  scripts/ci/lib/test-env.sh.

Both are mutation-verified (an added ungated exec.Command("go","version")/
`go build` is detected, then reverted cleanly).

Also sets pure = "on" and race = "off" on cmd/bd/BUILD.bazel's
compact_gc_fixture go_binary, matching :bd_for_tests/:cmd_norace: it is a
testonly, stdlib-only fixture, so neither cgo nor the race detector buys it
anything, under any --config. Verified TestRunCompactDoltTargetsOnlyAuthorizedActiveDatabase
still passes via `bazel --nohome_rc test` under both --config=prcore and
--config=doltserver-cmd.

Known pre-existing issue, out of scope here: gosec's GoVersion() call still
execs a host `go` from third-party code this policy doesn't reach.

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

* fix(scripts): address review of host-tool exec/shell policy (#7485)

Fixes all 6 maintainer-city-c3 review items against the ban-ungated-go/
gofmt-exec policy, each with a mutation test proving the fix catches
the described regression:

1. Shell scan now gates per-branch/per-call-site (shellLineGating),
   not by any TEST_SRCDIR mention anywhere in the whole file.
2. Shell scanner adds gofmt and `go tool` detection, fixes the
   heredoc-start regex (no longer swallows the rest of the script on an
   arithmetic `<<` shift or a `<<<` here-string), and replaces the
   blanket printf/echo/git-grep line skip with a quoted-span/command-
   substitution-aware check that still catches a real exec joined onto
   a message line with `&&`.
3. shellPolicyScope gains pr-lint.sh, check-versions.sh,
   upgrade-smoke-test.sh and the 4 .github/scripts shard fallbacks,
   each cited against its real BUILD.bazel/.bzl wiring; corrects two
   factually wrong "not executed" doc-comment claims (pr-lint.sh and
   upgrade-smoke-test.sh ARE Bazel-executed); and adds
   TestShellPolicyScopeCoversBazelShellData, a mechanical backstop that
   fails on a Bazel-referenced .sh under scripts/ or .github/scripts/
   missing from scope.
4. hostToolExecAllowlist no longer exempts goBuildBDCommand's 9 call
   sites by helper name; helperIsGatedByAllCallers checks every real
   call site is itself gated.
5. Both allowlists are keyed by file + enclosing function + exact
   snippet, not line number.
6. gated()/shellLineGating understand condition polarity, so an
   inverted `if !IsBazel() { t.Skip() }` (or its shell equivalent) is
   correctly NOT counted as a gate.

Also extends the Go scanner's condPolarity to recognize
findPrebuiltBDBinary()/bazeltest.PrebuiltBD() result variables as a
TEST_SRCDIR-equivalent gate (proven safe by reading their
implementation), and the shell scanner's analogous shellBazelSetVars
for CANDIDATE_BIN/SOURCE_TAG_SQLITE_BIN/BEADS_TEST_*_BINARY, including
through a script's own default-value variable alias
(`VAR="${BAZEL_VAR:-default}"`).

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

* fix(scripts): give go_sources_test runfiles access to the new shell scope

The shell-policy rewrite (host_tool_shell_policy_test.go) added
shellPolicyScope entries for scripts/ci/pr-lint.sh,
scripts/check-versions.sh, scripts/upgrade-smoke-test.sh, and the four
.github/scripts/*-test-shard.sh files, plus a scope-coverage guard
(TestShellPolicyScopeCoversBazelShellData) that walks every tracked
BUILD.bazel file. None of those were runfiles of
//scripts:go_sources_test, so bazel test passed locally under plain
`go test` but failed in CI's hermetic Bazel sandbox with "no such file
or directory" for the new scope entries and "found only 0 BUILD.bazel
files" for the guard's walk.

Add //:repo_other_files (every tracked non-Go, non-Markdown file,
including every BUILD/.bzl file) to go_sources_test's data, mirroring
the same pattern already used by //scripts:scripts_test.

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

* fix(scripts): rename AST-fixture function so it isn't counted as a real test

tools/bazel/equivalence.py compares `go test`'s discovered top-level
tests against what Bazel actually ran, using a raw-text regex
(`^func\s+(Test\w*)\s*\(\s*\w+\s+\*testing\.T\s*\)`) rather than
compiling the file. The "inverted IsBazel skip" fixture in
host_tool_exec_policy_test.go embeds a column-0
`func TestSomething(t *testing.T) {` inside a backtick string used to
build an AST for findHostToolExecCalls, which the regex scan counted
as a real top-level test that Bazel's go_sources_test then "failed to
run" (CI's Bazel / test leg: EQUIVALENCE: FAIL on "scripts
TestSomething"). Rename the fixture's inner function to
bazelOnlyFixture; the test's behavior is unchanged since the name was
never semantically significant to the scan being exercised.

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

* fix(scripts): close host-go-policy gaps from PR #7485 re-review

Four fixes from the final review round:

1. test.sh/conformance.sh/release.sh ARE in scope (they run under Bazel
   via scripts/** data globs); drop the false "not in scope" claims and
   add justified fn-keyed allowlist entries for their 8 flagged execs.
2. Go AST: an exec inside the Bazel-true branch, before its own abort,
   is no longer wrongly counted as gated (M17); a gate inside a sibling
   closure no longer covers execs outside it.
3. Shell scanner: abort/skip gate state now resets at function-scope end
   (a top-level `go build` after build_candidate in
   upgrade-smoke-test.sh is correctly flagged), and `bash -c 'go build'`
   is detected again.
4. Split host_tool_shell_policy_test.go into its own go_test
   (shell_host_tool_policy_test) with narrow, curated data instead of
   riding go_sources_test's //:repo_other_files; restored
   go_sources_test's data to its pre-PR scope. Dropped
   TestShellPolicyScopeCoversBazelShellData: its regex backstop could
   not see Starlark labels/.bzl/globs and still missed real in-scope
   scripts, so it wasn't earning its cache-rerun cost. The new target
   carries no tags, so it still runs in the required "Bazel / test"
   lane.

Each of 1-3 ships with a mutation-proof fixture that fails on the prior
code and passes on the fix.

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

* fix(scripts): rename sibling-closure fixture to avoid false test-equivalence hit

tools/bazel/equivalence.py line-scans *_test.go files for a column-0
`func Test\w*(t *testing.T)` to build its "what go test runs" list; it
reads raw text, not compiled Go, so the fixture's embedded raw-string
Go source (used to feed findHostToolExecCalls a parseable AST) was
mistaken for a real top-level test the package defines, and the
equivalence check failed because no go_test target "ran" it. Rename
the fixture function from TestX to siblingClosureFixture, matching the
existing convention a few lines down (bazelOnlyFixture) that documents
the same pitfall.

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

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-10 23:51:34 +00:00

442 lines
16 KiB
Python

load("@rules_go//go:def.bzl", "go_test")
# CI and repo-policy tests: they parse the workflows, run the CI shell scripts
# against fixtures, and check repository-wide rules (BUILD tag taxonomy, skip
# allowlist, managed BUILD blocks, shard manifests, tracked-file scans).
#
# They are one Go package but several go_test targets, each compiling only
# the files whose tests it runs and declaring only what those tests read, so
# that a change re-runs only the targets that read it: a Go-only change runs
# no workflow policy test, a docs-only or workflow-only change no Go-source
# scan. The data are the partitions of the checkout that tools/bazel/go_srcs.py
# generates (//:repo_go_srcs, //:repo_go_test_srcs, //:repo_doc_files,
# //:repo_other_files, and per package) or single files; only
# :tracked_files_test declares //:repo_files, every tracked file.
#
# Every *_test.go here is in exactly one of these targets, except the
# helpers-only files (no Test functions) that several compile; srcs are kept
# by hand (`# keep` stops gazelle from putting every file back into
# scripts_test), and TestScriptsTestTargetsPartitionTheTests
# (scripts_targets_test.go) fails on a file in no target or a test in two.
#
# No tags: they run remotely like any other target. The scripts and fixtures
# run bash, git, python3, jq, yq, curl, openssl, unzip and coreutils from the
# executor (the rbe-west worker image, or the runner in fork-cache mode); a
# missing tool fails the test under Bazel instead of skipping it
# (host_tools_test.go), so a cached pass never stands for an executor that
# could not run it. Go and the release formula come in hermetically.
# The CI and Bazel policy tests: workflows, the setup-bazel action, .bazelrc,
# MODULE.bazel, every BUILD and .bzl file, tools/bazel and the CI scripts,
# all in //:repo_other_files (every tracked file but Go and Markdown source),
# plus the one Go file whose dolt image pin they compare.
go_test(
name = "scripts_test",
srcs = [
"bazel_autofix_push_test.go",
"bazel_cache_mode_test.go",
"bazel_dolt_race_shadow_test.go",
"bazel_farm_workflow_test.go",
"bazel_fork_mode_test.go",
"bazel_go_repository_tools_test.go",
"bazel_hermetic_cc_test.go",
"bazel_invocations_test.go",
"bazel_policy_test.go",
"bazel_rbe_prewarm_test.go",
"bazel_rrc_test.go",
"ci_actions_policy_test.go",
"ci_analytics_workflow_test.go",
"ci_blacksmith_runner_test.go",
"ci_f7b_review_test.go",
"ci_f7c_advisory_test.go",
"ci_merge_queue_test.go",
"ci_workflow_apt_get_timeout_test.go",
"ci_workflow_core_test.go",
"ci_workflow_test.go",
"embedded_shard_count_test.go",
"host_tools_test.go",
"makefile_bazel_primary_test.go",
"migration_corpus_bazel_test.go",
"nogo_integration_step_script_test.go",
"nogo_lint_policy_test.go",
"pinned_shards_helpers_test.go",
"pinned_shards_test.go",
"pr_lanes_bazel_coverage_test.go",
"pr_risk_bazel_coverage_test.go",
"rbe_worker_env_test.go",
"repo_helpers_test.go",
"worker_env_autosync_test.go",
], # keep
data = [
"//:repo_other_files",
"//internal/testutil:testdoltcommon.go",
"@go_sdk//:bin/go",
"@go_sdk//:files",
],
# The Go SDK is go.mod's toolchain, whatever go the executor has on PATH.
# No //:repo_go_srcs here (F5 S2, cost fix, independent review of #7486):
# T4 (TestNogoIntegrationStepCoversItsConfiguration) and the
# unvalidated_integration_tests.txt exactness check moved to their own
# :nogo_integration_scan_test target below, which alone depends on
# //:repo_go_srcs and //:repo_go_test_srcs, so a Go-only change no longer
# re-runs the rest of this suite too.
env = {"BEADS_TEST_GO": "$(rlocationpath @go_sdk//:bin/go)"},
deps = [
"//internal/formula",
"//internal/testutil/bazeltest",
"//scripts/internal/testbash",
"@in_gopkg_yaml_v3//:yaml_v3",
],
)
# T4 (TestNogoIntegrationStepCoversItsConfiguration) and T6
# (TestUnvalidatedIntegrationTestsFileIsExact, independent review of #7486
# must-fix 6): both source-scan every tracked .go file (test and non-test)
# for the "integration" build tag, so they alone depend on //:repo_go_srcs
# and //:repo_go_test_srcs. Carved out of scripts_test (cost fix, same
# review): with this data in scripts_test, any Go-only change re-ran the
# whole nogo-policy suite, raising scripts_test's rerun rate from 73% to 91%
# of main commits. bazel_invocations_test.go and ci_workflow_core_test.go are
# helper-only files (no Test functions) shared with scripts_test, so neither
# copy duplicates the other's parsing logic.
go_test(
name = "nogo_integration_scan_test",
srcs = [
"bazel_invocations_test.go",
"ci_workflow_core_test.go",
"nogo_integration_scan_test.go",
"repo_helpers_test.go",
], # keep
data = [
"//:repo_go_srcs",
"//:repo_go_test_srcs",
"//:repo_other_files",
],
deps = [
"//internal/testutil/bazeltest",
"@in_gopkg_yaml_v3//:yaml_v3",
],
)
# The shell scripts' own behavior, run against fixtures: this package's
# scripts and their testdata, the few root files they read, and the release
# formula.
go_test(
name = "shell_scripts_test",
srcs = [
"check_build_tags_test.go",
"check_doc_freshness_test.go",
"check_release_cross_compile_test.go",
"check_testing_short_test.go",
"check_winget_portable_alias_test.go",
"ci_analytics_extract_test.go",
"complexity_script_test.go",
"conformance_script_test.go",
"host_tools_test.go",
"install_dolt_test.go",
"pr_preflight_test.go",
"pre_push_suite_test.go",
"pull_dolt_image_test.go",
"release_script_test.go",
"repo_helpers_test.go",
"required_suite_test.go",
"test_script_test.go",
"upgrade_smoke_loop_test.go",
],
data = [
":repo_other_files",
"//:.buildflags",
"//:.githooks/pre-push",
"//:.test-skip",
"//internal/httpclient:repo_other_files",
"//test/conformance:repo_go_test_srcs",
"//test/conformance:repo_other_files",
"//tools/bazel:repo_other_files",
"@beads_formulas//:beads-release.formula.toml",
],
# The release formula lives in .beads/ (in .bazelignore: it also holds
# live bd data), so it comes in through its own local repository.
env = {"BEADS_TEST_RELEASE_FORMULA": "$(rlocationpath @beads_formulas//:beads-release.formula.toml)"},
deps = [
"//internal/formula",
"//internal/testutil/bazeltest",
"//scripts/internal/testbash",
],
)
# Scans of the Go test files: the Bazel-skip allowlist, the managed local
# lane, and the shard manifests and scripts against the tests they shard.
# Every _test.go file (//:repo_go_test_srcs) but no non-test Go source, so a
# non-test Go change leaves it cached.
go_test(
name = "go_test_sources_test",
srcs = [
"bazel_skip_allowlist_test.go",
"embedded_shard_count_test.go",
"host_tools_test.go",
"managed_local_lane_test.go",
"pinned_shards_helpers_test.go",
"pinned_shards_sources_test.go",
"proxied_lane_coverage_test.go",
"repo_helpers_test.go",
"scripts_targets_test.go",
"shard_manifest_test.go",
],
data = [
"BUILD.bazel",
"ci/_embedded_shard_manifest_lib.py",
"ci/embedded_cmd_test_durations.json",
"ci/embedded_storage_test_durations.json",
"ci/gen_embedded_cmd_shard_manifest.py",
"ci/gen_embedded_storage_shard_manifest.py",
"ci/gen_proxied_shard_manifest.py",
"ci/proxied_test_durations.json",
"//:.github/scripts/embedded-cmd-test-shards.txt",
"//:.github/scripts/embedded-storage-test-shard.sh",
"//:.github/scripts/embedded-storage-test-shards.txt",
"//:.github/scripts/embedded-test-shard.sh",
"//:.github/scripts/proxied-cmd-test-shards.txt",
"//:.github/scripts/proxied-test-shard.sh",
"//:.github/scripts/server-storage-test-shard.sh",
"//:.github/scripts/server-storage-test-shards.txt",
"//:nested_go_modules",
"//:repo_go_test_srcs",
"//cmd/bd:repo_other_files",
"//internal/httpclient:repo_other_files",
"//internal/storage/dolt:repo_other_files",
"//internal/storage/embeddeddolt:repo_other_files",
"//tests/regression:repo_other_files",
"//tools/bazel:check_shard_coverage.py",
"//tools/bazel:equivalence_allowlist.txt",
],
deps = ["//internal/testutil/bazeltest"],
)
# check-testing-short.sh over every Go file, test and non-test, plus the
# Go-exec half of the host-go-under-Bazel policy (host_tool_exec_policy_test.go
# scans //:repo_go_srcs and //:repo_go_test_srcs for a literal go/gofmt exec).
# The shell-script half (host_tool_shell_policy_test.go) is its own
# :shell_host_tool_policy_test below: it reads a fixed, narrow list of shell
# scripts, not every Go source file, so it would otherwise pay this target's
# full re-run cost for files it never looks at.
go_test(
name = "go_sources_test",
srcs = [
"host_tool_exec_policy_test.go",
"repo_helpers_test.go",
"testing_short_tree_test.go",
],
data = [
"check-testing-short.sh",
"//:repo_go_srcs",
"//:repo_go_test_srcs",
],
deps = ["//internal/testutil/bazeltest"],
)
# The shell-script half of the host-go-under-Bazel policy
# (host_tool_shell_policy_test.go: see its package comment for shellPolicyScope
# and the detection tradeoffs). Split out of go_sources_test (which otherwise
# re-ran on every Go-source change to serve only this file) with data limited
# to exactly the shell scripts shellPolicyScope names -- no BUILD.bazel/.bzl
# walk, and so no //:repo_other_files: TestShellPolicyScopeCoversBazelShellData
# (dropped; see that test's old doc comment in version control) could not
# parse a `//pkg:file.sh` label or a glob() either, so paying 758 extra input
# files and ~4 points of cache-rerun rate across the whole repo for a walk
# that demonstrably still missed real in-scope scripts was not worth it.
go_test(
name = "shell_host_tool_policy_test",
srcs = [
"host_tool_shell_policy_test.go",
"repo_helpers_test.go",
],
data = [
"check-build-tags.sh",
"check-doc-freshness.sh",
"check-go-install-guidance.sh",
"check-migration-hygiene.sh",
"check-testing-short.sh",
"check-versions.sh",
"check-winget-portable-alias.sh",
"check-workapi-frontend-boundary.sh",
"ci/fmt-check.sh",
"ci/gofmt-bin.sh",
"ci/pr-lint.sh",
"conformance.sh",
"migrate-legacy-to-current.sh",
"migration-test/historical-dolt-upgrade-test.sh",
"migration-test/legacy-bridge-test.sh",
"migration-test/lib/binary.sh",
"release.sh",
"test.sh",
"upgrade-smoke-test.sh",
"//:.github/scripts/embedded-storage-test-shard.sh",
"//:.github/scripts/embedded-test-shard.sh",
"//:.github/scripts/proxied-test-shard.sh",
"//:.github/scripts/server-storage-test-shard.sh",
"//scripts/repochecks:run_check.sh",
"//scripts/repochecks:types_gen_drift_test.sh",
],
deps = ["//internal/testutil/bazeltest"],
)
# Scans of every tracked file (//:repo_files): any change re-runs them, so
# keep them few and fast.
go_test(
name = "tracked_files_test",
srcs = [
"repo_helpers_test.go",
"tracked_files_test.go",
],
data = ["//:repo_files"],
deps = ["//internal/testutil/bazeltest"],
)
# pr.yml's check-doc-freshness-platforms Linux leg: `go test
# -tags integration ./scripts -count=1 -run '^(TestDocFreshness.*|
# TestRequiredSuiteContract)$' -args -required-suite=doc-freshness`, whose
# TestMain fails unless exactly the doc-freshness inventory is compiled and
# selected. shell_scripts_test runs those tests too (in the integration lane);
# this target adds the inventory and selection contract, over only the files
# the suite reads (its fixtures copy check-doc-freshness.sh). The macOS and
# Windows legs stay in pr.yml.
# Tags:
# integration-only: every TestDocFreshness test is //go:build integration,
# so --config=integration's lane runs it.
go_test(
name = "doc_freshness_required_test",
srcs = [
"check_doc_freshness_test.go",
"repo_helpers_test.go",
"required_suite_test.go",
],
args = [
"-test.count=1",
"-test.run=^(TestDocFreshness.*|TestRequiredSuiteContract)$$",
"-required-suite=doc-freshness",
],
data = ["check-doc-freshness.sh"],
tags = ["integration-only"],
deps = [
"//internal/testutil/bazeltest",
"//scripts/internal/testbash",
],
)
exports_files(
["migration-test/release-catalog.json"],
visibility = ["//scripts/migration-test/catalog:__pkg__"],
)
# The historical-upgrade harness (scripts/migration-test) and the public
# SQLite bridge it drives, for //tests/migration's per-release targets.
filegroup(
name = "migration_harness",
srcs = [
"migrate-legacy-to-current.sh",
"migration-test/historical-dolt-upgrade-test.sh",
"migration-test/legacy-bridge-test.sh",
"migration-test/lib/binary.sh",
"migration-test/lib/versions.sh",
],
visibility = ["//tests/migration:__pkg__"],
)
exports_files(
["upgrade-smoke-test.sh"],
visibility = ["//tests/upgrade_smoke:__pkg__"],
)
exports_files(
[
"ci/fmt-check.sh",
"ci/lib/timing.sh",
"ci/pr-lint.sh",
],
visibility = ["//scripts/prlintmake:__pkg__"],
)
exports_files(
["ci/gofmt-bin.sh"],
visibility = ["//scripts/prlintmake:__pkg__"],
)
# The compatibility entrypoint whose exit-status contract
# //scripts/check-versions:check-versions_test drives.
exports_files(
["check-versions.sh"],
visibility = ["//scripts/check-versions:__pkg__"],
)
# Scripts the docs and agent instructions link to by path, which
# //test/docsync checks resolve.
exports_files(
[
"bump-version.sh",
"check-cli-docs-drift.sh",
"generate-cli-docs.sh",
"test.sh",
],
visibility = ["//test/docsync:__pkg__"],
)
# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) ---
filegroup(
name = "repo_doc_files",
srcs = glob(
[
"**/*.md",
"**/*.mdx",
],
allow_empty = True,
exclude = ["**/node_modules/**"],
),
visibility = ["//:__subpackages__"],
)
filegroup(
name = "repo_go_srcs",
srcs = glob(
["**/*.go"],
allow_empty = True,
exclude = [
"**/*_test.go",
"**/node_modules/**",
],
),
visibility = ["//:__subpackages__"],
)
filegroup(
name = "repo_go_test_srcs",
srcs = glob(
["**/*_test.go"],
allow_empty = True,
exclude = ["**/node_modules/**"],
),
visibility = ["//:__subpackages__"],
)
filegroup(
name = "repo_other_files",
srcs = glob(
["**"],
exclude = [
"**/*.db",
"**/*.exe",
"**/*.go",
"**/*.md",
"**/*.mdx",
"**/*.out",
"**/*.prof",
"**/*.pyc",
"**/*.test",
"**/__pycache__/**",
"**/node_modules/**",
],
),
visibility = ["//:__subpackages__"],
)
# --- end repo_files ---