test(bazel): hermetic test env + inject bd binary into subprocess tests (#6843)
* test(bazel): isolate the bazel test environment like pr-core's go test Every `bazel test` process now runs under //tools/bazel:test_env (wired with `test --run_under`), a wrapper that mirrors scripts/ci/lib/test-env.sh: a private HOME/USERPROFILE, XDG_CONFIG_HOME, DOLT_ROOT_PATH and empty GIT_CONFIG_GLOBAL, GIT_CONFIG_NOSYSTEM, BEADS_TEST_IGNORE_REPO_CONFIG, and the same scrub of workspace/Dolt-server variables. Everything lives in one fresh mktemp directory under /tmp that is removed when the test exits, so TMPDIR (t.TempDir) stays short enough for unix sockets and is never shared between concurrent runs. This replaces `--test_env=HOME` (the client's HOME string entered the action key and was shipped to remote workers, where it does not exist: the embeddeddolt suite panicked on "stat /home/ubuntu") and the shared `--test_tmpdir=/tmp/bbt`. No host path or client env enters the action key. bd_test (~10 min under -race -short) and embeddeddolt_test (~6 min) get size = "large" / timeout = "long" instead of Bazel's 300 s default; pr-core gives go test -timeout=30m. rules_shell (already in the module graph) becomes a direct dep for sh_binary. MODULE.bazel.lock records the pybind11 extension that building these test targets evaluates, so `make bazel-sync` is stable. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * test(bazel): inject the bd binary into subprocess tests (slice B3) Tests that exec bd used to `go build` it themselves, which cannot work under `bazel test` (no Go toolchain or module tree in the sandbox or on a remote worker): 135 cmd/bd subtests failed with "go build failed", and protocol, doctor and uow failed the same way. - //cmd/bd:bd_for_tests: a go_binary named bd, race = "off" (the non-race gms_pure_go binary these tests build today, even under --config=prcore). - bd_test, protocol_test, doctor_test and uow_test declare it as data and set BEADS_TEST_BD_BINARY to its $(rlocationpath). - bazeltest.PrebuiltBD is the one resolver: under Bazel it resolves the rlocationpath through runfiles (via the new bazeltest.RunfileEnv) and requires it; under plain go test it keeps today's semantics (absolute form of BEADS_TEST_BD_BINARY, or "" so the caller builds bd). - cmd/bd's findPrebuiltBDBinary delegates to it, and the sites that read the variable by hand (init_embedded, shared_server_integration, cli_fast, migrate front door) now go through findPrebuiltBDBinary; uow calls it directly. - Raw `go build` sites that never honored the variable (protocol, doctor dolt_e2e, test/conformance e2e, internal/beads integration TestMain) use it only under Bazel, so go test is unchanged; protocol and doctor fail instead of skipping when the injection is miswired. tests/regression is left alone (it builds candidate and baseline binaries on purpose). - The read-only canary still forces a source build under go test; under Bazel the injected binary is already built from this tree. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * test(bazel): declare what cmd/bd and doctor tests read under bazel With bd injected, the remaining //cmd/bd:bd_test and //cmd/bd/doctor failures under `bazel test` were inputs the tests read from the checkout without declaring them (they were hidden behind the go-build failures before): - source-scanning guards (capability registry, events-journal construction, serve database source) parse cmd/bd, cmd/bd/doctor, cmd/bd/doctor/fix and internal/storage/embeddeddolt/cmd: those packages get go_srcs filegroups (tools/bazel/go_srcs.py) declared as data; serve_test resolves its package dir with bazeltest.CallerDir. - .githooks (managed-section parity) and examples/formulas/primitives are declared through //:bd_test_files. - the gofmt preflight tests exec gofmt: under Bazel they use the Go SDK's, declared as data and named by BEADS_TEST_GOFMT (bazeltest.RunfileEnv). - TestGOMODCACHENotUnderTestHome guards in-test `go build`, which does not happen under Bazel, so it skips there. - doctor's TestCheckDeletionsManifest_LegacyFile only passed because the process cwd happened to be inside a git checkout; its fixture is now a git repo and the check runs in it. go test behavior is unchanged except that the deletions-manifest test no longer depends on the caller's cwd. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(bazel): give test_env a dolt identity; never fail on cleanup beads_test_env_enter sets a dolt user.name/user.email in the fresh DOLT_ROOT_PATH; test_env.sh created it empty, so tests that shell out to dolt (remotecache, storage/dolt) failed with 'Author identity unknown'. Write config_global.json directly (no dolt exec per test). Also keep rm -rf in the EXIT trap from overriding the test's exit status when a leftover child is still writing to the tmp root. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * build(bazel): re-sync BUILD files with main Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
e30823f452
commit
4f2f8f2b79
30 changed files with 419 additions and 54 deletions
22
.bazelrc
22
.bazelrc
|
|
@ -13,23 +13,17 @@ build --@rules_go//go/config:tags=gms_pure_go
|
|||
build --incompatible_strict_action_env
|
||||
|
||||
test --test_output=errors
|
||||
# Bazel roots t.TempDir() deep under the output tree, which overflows the
|
||||
# 104-byte unix-socket sun_path limit. Root the per-test tmpdir short.
|
||||
# TODO: /tmp/bbt is shared by every checkout and user on a host (the per-target
|
||||
# subdirectory is keyed only by target), so concurrent runs can collide and a
|
||||
# second user can hit EACCES. Replace with a per-run path before CI relies on it.
|
||||
test --test_tmpdir=/tmp/bbt
|
||||
test --sandbox_writable_path=/tmp/bbt
|
||||
# Test actions do not inherit HOME by default; CI overrides with a scratch HOME.
|
||||
test --test_env=HOME
|
||||
# Test environment isolation (the pr-core `go test` environment of
|
||||
# scripts/ci/lib/test-env.sh): every test process runs under a wrapper that
|
||||
# gives it a private HOME, XDG_CONFIG_HOME, DOLT_ROOT_PATH, empty global
|
||||
# gitconfig and a short, per-process TMPDIR (unix-socket sun_path limit).
|
||||
# Client HOME/TMPDIR are never forwarded, so no host value enters the action key.
|
||||
test --run_under=//tools/bazel:test_env
|
||||
test --test_env=BD_DISABLE_METRICS=1
|
||||
test --test_env=BD_DISABLE_EVENT_FLUSH=1
|
||||
test --test_env=BEADS_TEST_IGNORE_REPO_CONFIG=1
|
||||
test --test_env=GIT_CONFIG_NOSYSTEM=1
|
||||
|
||||
# PR-core parity: the same test *selection* as scripts/ci/pr-core.sh (race,
|
||||
# -short, skips, tag filters). The test environment is not yet isolated the way
|
||||
# pr-core's is (HOME, git/XDG/Dolt config); that is follow-up work.
|
||||
# PR-core parity: the same test selection as scripts/ci/pr-core.sh (race,
|
||||
# -short, skips, tag filters); the environment comes from the wrapper above.
|
||||
test:prcore --@rules_go//go/config:race
|
||||
test:prcore --test_arg=-test.short
|
||||
test:prcore --test_arg=-test.skip=^TestEmbedded
|
||||
|
|
|
|||
11
BUILD.bazel
11
BUILD.bazel
|
|
@ -20,6 +20,17 @@ exports_files(
|
|||
visibility = ["//scripts/gitattributespolicy:__pkg__"],
|
||||
)
|
||||
|
||||
# //cmd/bd:bd_test: tracked git hooks (generator parity) and the formula
|
||||
# primitive fixtures.
|
||||
filegroup(
|
||||
name = "bd_test_files",
|
||||
srcs = glob([
|
||||
".githooks/*",
|
||||
"examples/formulas/primitives/**",
|
||||
]),
|
||||
visibility = ["//cmd/bd:__pkg__"],
|
||||
)
|
||||
|
||||
exports_files(
|
||||
[".claude-plugin/marketplace.json"],
|
||||
visibility = ["//cmd/bd/setup:__pkg__"],
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ module(
|
|||
|
||||
bazel_dep(name = "rules_go", version = "0.63.0")
|
||||
bazel_dep(name = "gazelle", version = "0.53.0")
|
||||
bazel_dep(name = "rules_shell", version = "0.6.1")
|
||||
|
||||
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
|
||||
go_sdk.download(
|
||||
|
|
|
|||
22
MODULE.bazel.lock
generated
22
MODULE.bazel.lock
generated
|
|
@ -214,6 +214,28 @@
|
|||
},
|
||||
"selectedYankedVersions": {},
|
||||
"moduleExtensions": {
|
||||
"@@pybind11_bazel+//:internal_configure.bzl%internal_configure_extension": {
|
||||
"general": {
|
||||
"bzlTransitiveDigest": "NRXra7941UfmNUyIxnLt82V5hULluVGL2nBsijTl4j4=",
|
||||
"usagesDigest": "D1r3lfzMuUBFxgG8V6o0bQTLMk3GkaGOaPzw53wrwyw=",
|
||||
"recordedInputs": [
|
||||
"REPO_MAPPING:pybind11_bazel+,bazel_tools bazel_tools",
|
||||
"FILE:@@pybind11_bazel+//MODULE.bazel e6f4c20442eaa7c90d7190d8dc539d0ab422f95c65a57cc59562170c58ae3d34"
|
||||
],
|
||||
"generatedRepoSpecs": {
|
||||
"pybind11": {
|
||||
"repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive",
|
||||
"attributes": {
|
||||
"build_file": "@@pybind11_bazel+//:pybind11-BUILD.bazel",
|
||||
"strip_prefix": "pybind11-2.12.0",
|
||||
"urls": [
|
||||
"https://github.com/pybind/pybind11/archive/v2.12.0.zip"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"@@rules_kotlin+//src/main/starlark/core/repositories:bzlmod_setup.bzl%rules_kotlin_extensions": {
|
||||
"general": {
|
||||
"bzlTransitiveDigest": "+Kp6j204mBZ3mxlIDDR0gBoP45BZ4jYRhRAcB8sU0qc=",
|
||||
|
|
|
|||
|
|
@ -297,6 +297,7 @@ go_library(
|
|||
"storage_chain.go",
|
||||
"store_factory.go",
|
||||
"store_factory_nocgo.go",
|
||||
"store_factory_server_mode.go",
|
||||
"store_reopen.go",
|
||||
"swarm.go",
|
||||
"swarm_proxied_server.go",
|
||||
|
|
@ -455,8 +456,28 @@ go_binary(
|
|||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
# The bd that subprocess tests exec, injected through BEADS_TEST_BD_BINARY
|
||||
# (bazeltest.PrebuiltBD) instead of a per-package `go build`. Never race
|
||||
# instrumented, even under --config=prcore: it matches the non-race
|
||||
# gms_pure_go binary pr-core's tests build today. Named bd, like that binary.
|
||||
go_binary(
|
||||
name = "bd_for_tests",
|
||||
out = "bd_for_tests/bd",
|
||||
embed = [":bd_lib"],
|
||||
race = "off",
|
||||
visibility = [
|
||||
"//cmd/bd:__subpackages__",
|
||||
"//internal/beads:__pkg__",
|
||||
"//internal/storage/uow:__pkg__",
|
||||
"//test/conformance:__pkg__",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "bd_test",
|
||||
# -race -short runs ~10 min (pr-core gives go test -timeout=30m).
|
||||
size = "large",
|
||||
timeout = "long",
|
||||
srcs = [
|
||||
"aaa_stdio_baseline_test.go",
|
||||
"actor_test.go",
|
||||
|
|
@ -869,6 +890,7 @@ go_test(
|
|||
"prime_gemini_hook_test.go",
|
||||
"prime_memory_caps_test.go",
|
||||
"prime_proxied_integration_test.go",
|
||||
"prime_shared_server_test.go",
|
||||
"prime_test.go",
|
||||
"promote_embedded_test.go",
|
||||
"promote_proxied_integration_test.go",
|
||||
|
|
@ -1094,8 +1116,20 @@ go_test(
|
|||
"write_verbs_parity_test.go",
|
||||
"zz_stdio_leak_guard_test.go",
|
||||
],
|
||||
data = glob(["testdata/**"]),
|
||||
data = glob(["testdata/**"]) + [
|
||||
":bd_for_tests",
|
||||
":go_srcs",
|
||||
"//:bd_test_files",
|
||||
"//cmd/bd/doctor:go_srcs",
|
||||
"//cmd/bd/doctor/fix:go_srcs",
|
||||
"//internal/storage/embeddeddolt/cmd:go_srcs",
|
||||
"@go_sdk//:bin/gofmt",
|
||||
],
|
||||
embed = [":bd_lib"],
|
||||
env = {
|
||||
"BEADS_TEST_BD_BINARY": "$(rlocationpath :bd_for_tests)",
|
||||
"BEADS_TEST_GOFMT": "$(rlocationpath @go_sdk//:bin/gofmt)",
|
||||
},
|
||||
deps = [
|
||||
"//:beads",
|
||||
"//cmd/bd/doctor",
|
||||
|
|
@ -1143,6 +1177,7 @@ go_test(
|
|||
"//internal/storage/versioncontrolops",
|
||||
"//internal/telemetry",
|
||||
"//internal/testutil",
|
||||
"//internal/testutil/bazeltest",
|
||||
"//internal/tracker",
|
||||
"//internal/types",
|
||||
"//internal/ui",
|
||||
|
|
@ -1221,3 +1256,16 @@ exports_files(
|
|||
["init_safety_help.go"],
|
||||
visibility = ["//test/docsync:__pkg__"],
|
||||
)
|
||||
|
||||
# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) ---
|
||||
|
||||
filegroup(
|
||||
name = "go_srcs",
|
||||
srcs = glob(
|
||||
["**/*.go"],
|
||||
allow_empty = True,
|
||||
),
|
||||
visibility = ["//:__subpackages__"],
|
||||
)
|
||||
|
||||
# --- end go_srcs ---
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ import (
|
|||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/testutil/bazeltest"
|
||||
)
|
||||
|
||||
// Fast CLI tests converted from scripttest suite
|
||||
|
|
@ -1016,13 +1018,11 @@ func init() {
|
|||
// Prebuilt fast path (scripts/test.sh and CI export this), else build
|
||||
// once. No repo-root ./bd reuse: a stale checkout-root binary silently
|
||||
// substitutes itself for the source under test (wy-4mtr0).
|
||||
if prebuilt := os.Getenv("BEADS_TEST_BD_BINARY"); prebuilt != "" {
|
||||
if abs, err := filepath.Abs(prebuilt); err == nil {
|
||||
if _, statErr := os.Stat(abs); statErr == nil {
|
||||
testBD = abs
|
||||
return
|
||||
}
|
||||
}
|
||||
if prebuilt, err := findPrebuiltBDBinary(); err == nil && prebuilt != "" {
|
||||
testBD = prebuilt
|
||||
return
|
||||
} else if bazeltest.IsBazel() {
|
||||
panic(fmt.Sprintf("bd binary for tests: %v", err))
|
||||
}
|
||||
bdBinary := "bd"
|
||||
if runtime.GOOS == "windows" {
|
||||
|
|
|
|||
|
|
@ -146,7 +146,9 @@ go_test(
|
|||
"validation_test.go",
|
||||
"version_test.go",
|
||||
],
|
||||
data = ["//cmd/bd:bd_for_tests"],
|
||||
embed = [":doctor"],
|
||||
env = {"BEADS_TEST_BD_BINARY": "$(rlocationpath //cmd/bd:bd_for_tests)"},
|
||||
deps = [
|
||||
"//internal/beads",
|
||||
"//internal/configfile",
|
||||
|
|
@ -157,9 +159,23 @@ go_test(
|
|||
"//internal/storage/embeddeddolt",
|
||||
"//internal/storage/issueops",
|
||||
"//internal/testutil",
|
||||
"//internal/testutil/bazeltest",
|
||||
"//internal/types",
|
||||
"//internal/utils",
|
||||
"@com_github_data_dog_go_sqlmock//:go-sqlmock",
|
||||
"@com_github_go_sql_driver_mysql//:mysql",
|
||||
],
|
||||
)
|
||||
|
||||
# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) ---
|
||||
|
||||
filegroup(
|
||||
name = "go_srcs",
|
||||
srcs = glob(
|
||||
["**/*.go"],
|
||||
allow_empty = True,
|
||||
),
|
||||
visibility = ["//:__subpackages__"],
|
||||
)
|
||||
|
||||
# --- end go_srcs ---
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ import (
|
|||
"github.com/steveyegge/beads/internal/doltserver"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
"github.com/steveyegge/beads/internal/testutil"
|
||||
"github.com/steveyegge/beads/internal/testutil/bazeltest"
|
||||
)
|
||||
|
||||
// e2eDoctorResult mirrors the JSON output struct from cmd/bd/doctor.go.
|
||||
|
|
@ -169,6 +170,12 @@ func buildTestBD(t *testing.T) string {
|
|||
t.Helper()
|
||||
|
||||
testBDOnce.Do(func() {
|
||||
// Under Bazel the binary is injected (//cmd/bd:bd_for_tests); there is
|
||||
// no toolchain or module tree to build from. Plain go test is unchanged.
|
||||
if bazeltest.IsBazel() {
|
||||
testBDPath, testBDErr = bazeltest.PrebuiltBD()
|
||||
return
|
||||
}
|
||||
bdBinary := "bd-test"
|
||||
if runtime.GOOS == "windows" {
|
||||
bdBinary = "bd-test.exe"
|
||||
|
|
@ -210,6 +217,9 @@ func buildTestBD(t *testing.T) string {
|
|||
}
|
||||
})
|
||||
|
||||
if testBDErr != nil && bazeltest.IsBazel() {
|
||||
t.Fatalf("bd binary for tests: %v", testBDErr) // a wiring bug, never a skip
|
||||
}
|
||||
if testBDErr != nil {
|
||||
t.Skipf("skipping E2E test: failed to build bd binary: %v", testBDErr)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -95,3 +95,16 @@ go_test(
|
|||
"@com_github_go_sql_driver_mysql//:mysql",
|
||||
],
|
||||
)
|
||||
|
||||
# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) ---
|
||||
|
||||
filegroup(
|
||||
name = "go_srcs",
|
||||
srcs = glob(
|
||||
["**/*.go"],
|
||||
allow_empty = True,
|
||||
),
|
||||
visibility = ["//:__subpackages__"],
|
||||
)
|
||||
|
||||
# --- end go_srcs ---
|
||||
|
|
|
|||
|
|
@ -66,7 +66,10 @@ func TestIntegrityChecks_EmptyBeadsDir(t *testing.T) {
|
|||
// TestCheckDeletionsManifest_LegacyFile tests the specific case where a legacy
|
||||
// deletions.jsonl file exists and should trigger a warning.
|
||||
func TestCheckDeletionsManifest_LegacyFile(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
// The check only warns inside a git repository, and it asks git about the
|
||||
// working directory, so the fixture is a repository and the check runs in
|
||||
// it (not in whatever checkout the test binary happens to start in).
|
||||
tmpDir := newGitRepo(t)
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.Mkdir(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
@ -78,7 +81,8 @@ func TestCheckDeletionsManifest_LegacyFile(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check := CheckDeletionsManifest(tmpDir)
|
||||
var check DoctorCheck
|
||||
runInDir(t, tmpDir, func() { check = CheckDeletionsManifest(tmpDir) })
|
||||
|
||||
// Should warn about legacy deletions file
|
||||
if check.Status != StatusWarning {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ import (
|
|||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/testutil/bazeltest"
|
||||
)
|
||||
|
||||
// TestGOMODCACHENotUnderTestHome guards the testMainInner fix that pins
|
||||
|
|
@ -19,6 +21,9 @@ import (
|
|||
//
|
||||
// So GOMODCACHE must be set and must NOT resolve under the current HOME.
|
||||
func TestGOMODCACHENotUnderTestHome(t *testing.T) {
|
||||
if bazeltest.IsBazel() {
|
||||
t.Skip("no in-test go build under bazel: bd is injected through BEADS_TEST_BD_BINARY")
|
||||
}
|
||||
home := os.Getenv("HOME")
|
||||
if home == "" {
|
||||
t.Skip("HOME not set; testMainInner did not run")
|
||||
|
|
|
|||
|
|
@ -35,11 +35,12 @@ var (
|
|||
func buildEmbeddedBD(t *testing.T) string {
|
||||
t.Helper()
|
||||
embeddedBDOnce.Do(func() {
|
||||
if prebuilt := os.Getenv("BEADS_TEST_BD_BINARY"); prebuilt != "" {
|
||||
if _, err := os.Stat(prebuilt); err != nil {
|
||||
embeddedBDErr = fmt.Errorf("BEADS_TEST_BD_BINARY=%q not found: %w", prebuilt, err)
|
||||
return
|
||||
}
|
||||
prebuilt, err := findPrebuiltBDBinary()
|
||||
if err != nil {
|
||||
embeddedBDErr = err
|
||||
return
|
||||
}
|
||||
if prebuilt != "" {
|
||||
embeddedBD = prebuilt
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -69,7 +69,9 @@ func assertDependencyPair(t *testing.T, bd, dir string, env []string, sentinelID
|
|||
}
|
||||
|
||||
func migrationFrontDoorBinary(t *testing.T) string {
|
||||
if p := os.Getenv("BEADS_TEST_BD_BINARY"); p != "" {
|
||||
if p, err := findPrebuiltBDBinary(); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if p != "" {
|
||||
return p
|
||||
}
|
||||
out := filepath.Join(t.TempDir(), "bd")
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/testutil/bazeltest"
|
||||
)
|
||||
|
||||
func TestCheckResult_Passed(t *testing.T) {
|
||||
|
|
@ -223,6 +224,21 @@ func TestRunLintCheck_SkipLintFlag(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// gofmtForTest returns the gofmt to run: the one on PATH under go test, and
|
||||
// under Bazel (no host toolchain) the Go SDK's, declared as data and named by
|
||||
// BEADS_TEST_GOFMT.
|
||||
func gofmtForTest(t *testing.T) string {
|
||||
t.Helper()
|
||||
if !bazeltest.IsBazel() {
|
||||
return "gofmt"
|
||||
}
|
||||
path, err := bazeltest.RunfileEnv("BEADS_TEST_GOFMT")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func TestRunFmtCheck_Formatted(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
// Write a properly formatted Go file
|
||||
|
|
@ -232,7 +248,7 @@ func TestRunFmtCheck_Formatted(t *testing.T) {
|
|||
}
|
||||
|
||||
// Run gofmt -l in the temp dir
|
||||
cmd := exec.Command("gofmt", "-l", ".")
|
||||
cmd := exec.Command(gofmtForTest(t), "-l", ".")
|
||||
cmd.Dir = dir
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
|
|
@ -251,7 +267,7 @@ func TestRunFmtCheck_Unformatted(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cmd := exec.Command("gofmt", "-l", ".")
|
||||
cmd := exec.Command(gofmtForTest(t), "-l", ".")
|
||||
cmd.Dir = dir
|
||||
output, _ := cmd.CombinedOutput()
|
||||
unformatted := strings.TrimSpace(string(output))
|
||||
|
|
|
|||
|
|
@ -40,10 +40,12 @@ go_test(
|
|||
"versioning_contract_test.go",
|
||||
"wp4_helpers_test.go",
|
||||
],
|
||||
data = glob(["testdata/**"]),
|
||||
data = glob(["testdata/**"]) + ["//cmd/bd:bd_for_tests"],
|
||||
embed = [":protocol"],
|
||||
env = {"BEADS_TEST_BD_BINARY": "$(rlocationpath //cmd/bd:bd_for_tests)"},
|
||||
deps = [
|
||||
"//internal/storage/schema",
|
||||
"//internal/testutil",
|
||||
"//internal/testutil/bazeltest",
|
||||
],
|
||||
)
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/testutil"
|
||||
"github.com/steveyegge/beads/internal/testutil/bazeltest"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
|
@ -100,6 +101,12 @@ func requireDoltStore(t *testing.T, what string) {
|
|||
func buildBD(t *testing.T) string {
|
||||
t.Helper()
|
||||
bdOnce.Do(func() {
|
||||
// Under Bazel the binary is injected (//cmd/bd:bd_for_tests); there is
|
||||
// no toolchain or module tree to build from. Plain go test is unchanged.
|
||||
if bazeltest.IsBazel() {
|
||||
bdPath, bdErr = bazeltest.PrebuiltBD()
|
||||
return
|
||||
}
|
||||
bin := "bd-protocol"
|
||||
if runtime.GOOS == "windows" {
|
||||
bin += ".exe"
|
||||
|
|
@ -122,6 +129,9 @@ func buildBD(t *testing.T) string {
|
|||
bdErr = fmt.Errorf("go build: %w\n%s", err, out)
|
||||
}
|
||||
})
|
||||
if bdErr != nil && bazeltest.IsBazel() {
|
||||
t.Fatalf("bd binary for tests: %v", bdErr) // a wiring bug, never a skip
|
||||
}
|
||||
if bdErr != nil {
|
||||
t.Skipf("skipping: failed to build bd: %v", bdErr)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import (
|
|||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
"github.com/steveyegge/beads/internal/storage/doltutil"
|
||||
"github.com/steveyegge/beads/internal/testutil/bazeltest"
|
||||
)
|
||||
|
||||
func TestEffectiveRootStorePolicy(t *testing.T) {
|
||||
|
|
@ -325,8 +326,12 @@ func TestConfigValidateReadOnlyIsHermetic(t *testing.T) {
|
|||
t.Fatalf("create isolated XDG home: %v", err)
|
||||
}
|
||||
// The canary must execute the current worktree source, never a caller-provided
|
||||
// prebuilt binary that may predate this candidate.
|
||||
t.Setenv("BEADS_TEST_BD_BINARY", "")
|
||||
// prebuilt binary that may predate this candidate. Under Bazel the injected
|
||||
// binary (//cmd/bd:bd_for_tests) is built from this source tree, and there is
|
||||
// no toolchain to build another one.
|
||||
if !bazeltest.IsBazel() {
|
||||
t.Setenv("BEADS_TEST_BD_BINARY", "")
|
||||
}
|
||||
bd := buildBDForTest(t)
|
||||
cmd := exec.Command(bd, "config", "validate", "--readonly")
|
||||
cmd.Dir = repoDir
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import (
|
|||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/httpapi"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/testutil/bazeltest"
|
||||
)
|
||||
|
||||
// TestServeFlags pins the flag surface. Every bound that is NOT here — the
|
||||
|
|
@ -400,7 +401,7 @@ func packageDir(t *testing.T) string {
|
|||
if !ok {
|
||||
t.Fatal("cannot resolve this test's source path")
|
||||
}
|
||||
return filepath.Dir(file)
|
||||
return bazeltest.CallerDir(file, "cmd/bd")
|
||||
}
|
||||
|
||||
// TestServeRefusesStrictReadonly.
|
||||
|
|
|
|||
|
|
@ -602,11 +602,12 @@ var (
|
|||
func buildSharedServerTestBinary(t *testing.T) string {
|
||||
t.Helper()
|
||||
sharedServerBuildOnce.Do(func() {
|
||||
if prebuilt := os.Getenv("BEADS_TEST_BD_BINARY"); prebuilt != "" {
|
||||
if _, err := os.Stat(prebuilt); err != nil {
|
||||
sharedServerBuildErr = fmt.Errorf("BEADS_TEST_BD_BINARY=%q not found: %w", prebuilt, err)
|
||||
return
|
||||
}
|
||||
prebuilt, err := findPrebuiltBDBinary()
|
||||
if err != nil {
|
||||
sharedServerBuildErr = err
|
||||
return
|
||||
}
|
||||
if prebuilt != "" {
|
||||
sharedServerBdBinary = prebuilt
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ import (
|
|||
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/testutil/bazeltest"
|
||||
)
|
||||
|
||||
const windowsOS = "windows"
|
||||
|
|
@ -315,14 +316,12 @@ var (
|
|||
initTestBDErr error
|
||||
)
|
||||
|
||||
// findPrebuiltBDBinary returns the absolute path of the BEADS_TEST_BD_BINARY
|
||||
// binary, or "" when none is configured and the caller should `go build` bd.
|
||||
// Under Bazel the binary is always injected (//cmd/bd:bd_for_tests) and is
|
||||
// resolved through runfiles; see bazeltest.PrebuiltBD.
|
||||
func findPrebuiltBDBinary() (string, error) {
|
||||
if configured := os.Getenv("BEADS_TEST_BD_BINARY"); configured != "" {
|
||||
if _, err := os.Stat(configured); err != nil {
|
||||
return "", fmt.Errorf("BEADS_TEST_BD_BINARY %q is not usable: %w", configured, err)
|
||||
}
|
||||
return filepath.Abs(configured)
|
||||
}
|
||||
return "", nil
|
||||
return bazeltest.PrebuiltBD()
|
||||
}
|
||||
|
||||
// buildBDForInitTests builds (or locates) a bd binary suitable for subprocess
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/steveyegge/beads/internal/testutil"
|
||||
"github.com/steveyegge/beads/internal/testutil/bazeltest"
|
||||
)
|
||||
|
||||
func setupIntegrationTestMain(root string) (func(), error) {
|
||||
|
|
@ -30,6 +31,22 @@ func setupIntegrationTestMain(root string) (func(), error) {
|
|||
return cleanup, nil
|
||||
}
|
||||
|
||||
// Under Bazel the binary is injected (//cmd/bd:bd_for_tests); there is no
|
||||
// toolchain or module tree to build from. Plain go test is unchanged.
|
||||
if bazeltest.IsBazel() {
|
||||
testBDBinary, err := bazeltest.PrebuiltBD()
|
||||
if err != nil {
|
||||
testutil.TerminateDoltContainer()
|
||||
cleanup()
|
||||
return nil, err
|
||||
}
|
||||
os.Setenv("BEADS_TEST_BD_BINARY", testBDBinary)
|
||||
return func() {
|
||||
testutil.TerminateDoltContainer()
|
||||
cleanup()
|
||||
}, nil
|
||||
}
|
||||
|
||||
binName := "bd"
|
||||
if runtime.GOOS == "windows" {
|
||||
binName = "bd.exe"
|
||||
|
|
|
|||
|
|
@ -90,6 +90,9 @@ go_library(
|
|||
|
||||
go_test(
|
||||
name = "embeddeddolt_test",
|
||||
# -race -short runs ~6 min (pr-core gives go test -timeout=30m).
|
||||
size = "large",
|
||||
timeout = "long",
|
||||
srcs = [
|
||||
"active_database_size_test.go",
|
||||
"batch_apply_contract_test.go",
|
||||
|
|
|
|||
|
|
@ -149,8 +149,12 @@ go_test(
|
|||
"version_reconciler_contract_test.go",
|
||||
"workspace_config_contract_test.go",
|
||||
],
|
||||
data = [":go_srcs"],
|
||||
data = [
|
||||
":go_srcs",
|
||||
"//cmd/bd:bd_for_tests",
|
||||
],
|
||||
embed = [":uow"],
|
||||
env = {"BEADS_TEST_BD_BINARY": "$(rlocationpath //cmd/bd:bd_for_tests)"},
|
||||
deps = [
|
||||
"//backend/conformance",
|
||||
"//internal/configfile",
|
||||
|
|
@ -167,6 +171,7 @@ go_test(
|
|||
"//internal/storage/issueops",
|
||||
"//internal/storage/schema",
|
||||
"//internal/testutil",
|
||||
"//internal/testutil/bazeltest",
|
||||
"//internal/types",
|
||||
"//internal/workapi",
|
||||
"//internal/workapi/storereader",
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ import (
|
|||
|
||||
"github.com/steveyegge/beads/internal/storage/dbproxy/proxy"
|
||||
"github.com/steveyegge/beads/internal/testutil"
|
||||
"github.com/steveyegge/beads/internal/testutil/bazeltest"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
|
@ -190,11 +191,12 @@ var (
|
|||
func buildBDBinary(t *testing.T) string {
|
||||
t.Helper()
|
||||
bdBinaryOnce.Do(func() {
|
||||
if prebuilt := os.Getenv("BEADS_TEST_BD_BINARY"); prebuilt != "" {
|
||||
if _, err := os.Stat(prebuilt); err != nil {
|
||||
bdBinaryErr = fmt.Errorf("BEADS_TEST_BD_BINARY=%q not found: %w", prebuilt, err)
|
||||
return
|
||||
}
|
||||
prebuilt, err := bazeltest.PrebuiltBD()
|
||||
if err != nil {
|
||||
bdBinaryErr = err
|
||||
return
|
||||
}
|
||||
if prebuilt != "" {
|
||||
bdBinary = prebuilt
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -172,3 +172,65 @@ outer:
|
|||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// BDBinaryEnv names the environment variable that points subprocess tests at a
|
||||
// prebuilt bd binary instead of a per-package `go build`. Under plain `go test`
|
||||
// it holds a filesystem path (scripts/test.sh and CI export it). Under Bazel a
|
||||
// go_test sets it from its BUILD file:
|
||||
//
|
||||
// data = ["//cmd/bd:bd_for_tests"],
|
||||
// env = {"BEADS_TEST_BD_BINARY": "$(rlocationpath //cmd/bd:bd_for_tests)"},
|
||||
const BDBinaryEnv = "BEADS_TEST_BD_BINARY"
|
||||
|
||||
// PrebuiltBD resolves BEADS_TEST_BD_BINARY to an absolute path. It is the one
|
||||
// resolver every bd-building test helper consults before running `go build`.
|
||||
//
|
||||
// Under plain `go test` it returns "" when the variable is unset (the caller
|
||||
// builds bd itself, exactly as before) and the absolute form of the value
|
||||
// otherwise. Under Bazel there is no Go toolchain or module tree to build
|
||||
// from, so the variable is required: its value is an rlocationpath resolved
|
||||
// through the runfiles tree. A set but unusable value is an error in both
|
||||
// modes.
|
||||
func PrebuiltBD() (string, error) {
|
||||
if IsBazel() {
|
||||
return RunfileEnv(BDBinaryEnv)
|
||||
}
|
||||
val := os.Getenv(BDBinaryEnv)
|
||||
if val == "" {
|
||||
return "", nil
|
||||
}
|
||||
path, err := filepath.Abs(val)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s=%q: %w", BDBinaryEnv, val, err)
|
||||
}
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
return "", fmt.Errorf("%s=%q is not usable: %w", BDBinaryEnv, val, err)
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
// RunfileEnv resolves an environment variable that a go_test sets from its
|
||||
// BUILD file to a declared data file, for example
|
||||
//
|
||||
// env = {"BEADS_TEST_GOFMT": "$(rlocationpath @go_sdk//:bin/gofmt)"},
|
||||
//
|
||||
// to an absolute path. A relative value is an rlocationpath resolved through
|
||||
// the runfiles tree; an absolute one must exist. It is an error for the
|
||||
// variable to be unset, so a BUILD file that forgot the wiring fails loudly.
|
||||
func RunfileEnv(key string) (string, error) {
|
||||
val := os.Getenv(key)
|
||||
if val == "" {
|
||||
return "", fmt.Errorf("%s is not set: declare the file as data of this go_test and set env = {%q: \"$(rlocationpath <label>)\"}", key, key)
|
||||
}
|
||||
if filepath.IsAbs(val) {
|
||||
if _, err := os.Stat(val); err != nil {
|
||||
return "", fmt.Errorf("%s=%q is not usable: %w", key, val, err)
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
path, err := Runfile(val)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s: %w", key, err)
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -117,3 +117,47 @@ func TestShardFreeEnvStripsShardingVariables(t *testing.T) {
|
|||
t.Fatalf("ShardFreeEnv() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrebuiltBD(t *testing.T) {
|
||||
probe := filepath.Join(RepoRoot(t), filepath.FromSlash(probeRel))
|
||||
|
||||
t.Setenv(BDBinaryEnv, "")
|
||||
got, err := PrebuiltBD()
|
||||
if IsBazel() {
|
||||
if err == nil {
|
||||
t.Fatalf("PrebuiltBD() with %s unset under bazel = %q, want an error", BDBinaryEnv, got)
|
||||
}
|
||||
} else if err != nil || got != "" {
|
||||
t.Fatalf("PrebuiltBD() with %s unset = %q, %v; want \"\", nil", BDBinaryEnv, got, err)
|
||||
}
|
||||
|
||||
// An absolute path is honored in both modes.
|
||||
t.Setenv(BDBinaryEnv, probe)
|
||||
if got, err := PrebuiltBD(); err != nil || got != probe {
|
||||
t.Fatalf("PrebuiltBD() = %q, %v; want %q", got, err, probe)
|
||||
}
|
||||
|
||||
// Under bazel a relative value is an rlocationpath; under go test it is
|
||||
// relative to the working directory (the package directory).
|
||||
rel := "testdata/probe.txt"
|
||||
if IsBazel() {
|
||||
ws := os.Getenv("TEST_WORKSPACE")
|
||||
if ws == "" {
|
||||
ws = "_main"
|
||||
}
|
||||
rel = ws + "/" + probeRel
|
||||
}
|
||||
t.Setenv(BDBinaryEnv, rel)
|
||||
got, err = PrebuiltBD()
|
||||
if err != nil || !filepath.IsAbs(got) {
|
||||
t.Fatalf("PrebuiltBD() with %s=%q = %q, %v; want an absolute path", BDBinaryEnv, rel, got, err)
|
||||
}
|
||||
if _, err := os.Stat(got); err != nil {
|
||||
t.Fatalf("PrebuiltBD() = %q does not exist: %v", got, err)
|
||||
}
|
||||
|
||||
t.Setenv(BDBinaryEnv, "no/such/bd")
|
||||
if got, err := PrebuiltBD(); err == nil {
|
||||
t.Fatalf("PrebuiltBD() with a missing binary = %q, want an error", got)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,8 @@ import (
|
|||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/testutil/bazeltest"
|
||||
)
|
||||
|
||||
// scenario is a named sequence of bd CLI invocations. IDs are pinned with --id so
|
||||
|
|
@ -479,6 +481,12 @@ func TestMain(m *testing.M) {
|
|||
func buildBD(t *testing.T) string {
|
||||
t.Helper()
|
||||
buildOnce.Do(func() {
|
||||
// Under Bazel the binary is injected (//cmd/bd:bd_for_tests); there is
|
||||
// no toolchain or module tree to build from. Plain go test is unchanged.
|
||||
if bazeltest.IsBazel() {
|
||||
bdBin, bdErr = bazeltest.PrebuiltBD()
|
||||
return
|
||||
}
|
||||
dir, err := os.MkdirTemp("", "bd-e2e")
|
||||
if err != nil {
|
||||
bdErr = fmt.Errorf("mkdir temp for bd binary: %v", err)
|
||||
|
|
|
|||
9
tools/bazel/BUILD.bazel
Normal file
9
tools/bazel/BUILD.bazel
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
|
||||
|
||||
# Wraps every test process (`test --run_under` in .bazelrc) with the isolated
|
||||
# HOME/XDG/Dolt/git environment that scripts/ci/lib/test-env.sh gives `go test`.
|
||||
sh_binary(
|
||||
name = "test_env",
|
||||
srcs = ["test_env.sh"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
|
@ -37,6 +37,9 @@ PACKAGES = (
|
|||
"backend", # //backend:backend_test (public alias census)
|
||||
"backend/conformance", # //backend/conformance, //internal/storage
|
||||
"beadserrors", # role facade alias targets
|
||||
"cmd/bd", # //cmd/bd:bd_test (capability registry, journal, serve scans)
|
||||
"cmd/bd/doctor", # //cmd/bd:bd_test (events-journal construction scan)
|
||||
"cmd/bd/doctor/fix", # //cmd/bd:bd_test (events-journal construction scan)
|
||||
"internal/types", # role facade alias targets
|
||||
"issueops", # //backend/conformance (role facade census)
|
||||
"journalops", # //backend/conformance (role facade census)
|
||||
|
|
|
|||
51
tools/bazel/test_env.sh
Executable file
51
tools/bazel/test_env.sh
Executable file
|
|
@ -0,0 +1,51 @@
|
|||
#!/usr/bin/env bash
|
||||
# Hermetic environment for every `bazel test` action (wired in .bazelrc with
|
||||
# `test --run_under=//tools/bazel:test_env`). It mirrors what
|
||||
# scripts/ci/lib/test-env.sh does for `go test` in pr-core: a private HOME,
|
||||
# XDG config, Dolt root and empty global gitconfig, so no test reads or writes
|
||||
# the developer's or the worker's real configuration.
|
||||
#
|
||||
# Everything lives in one fresh mktemp directory under /tmp, unique per test
|
||||
# process: HOME, config and TMPDIR must be short (t.TempDir() paths hold unix
|
||||
# sockets, whose sun_path limit is 104 bytes, and Bazel's TEST_TMPDIR is deep
|
||||
# under the output base) and must not be shared between concurrent runs of the
|
||||
# same target. The directory is removed when the test exits. Only the fixed
|
||||
# wrapper text enters the action key; no host path or client env does.
|
||||
set -euo pipefail
|
||||
|
||||
root="$(mktemp -d /tmp/bbt.XXXXXX)"
|
||||
# Cleanup must never change the test's exit status: a child the test left
|
||||
# running (a detached bd or a Dolt server shutting down) can still be writing
|
||||
# when rm runs, and Bazel only reaps it after this wrapper exits.
|
||||
trap 'chmod -R u+w "$root" 2>/dev/null || true; rm -rf "$root" 2>/dev/null || true' EXIT
|
||||
|
||||
mkdir -p "$root/home" "$root/xdg-config" "$root/dolt-root" "$root/tmp"
|
||||
: >"$root/gitconfig"
|
||||
# Dolt identity, as beads_test_env_enter sets with `dolt config --global`:
|
||||
# tests that shell out to dolt commit need an author. Written directly so the
|
||||
# wrapper does not depend on a dolt binary.
|
||||
mkdir -p "$root/dolt-root/.dolt"
|
||||
printf '%s\n' '{"user.email":"test@beads.local","user.name":"beads-test"}' \
|
||||
>"$root/dolt-root/.dolt/config_global.json"
|
||||
|
||||
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 TMPDIR="$root/tmp"
|
||||
export BEADS_TEST_IGNORE_REPO_CONFIG=1
|
||||
|
||||
# Same scrub as beads_test_env_enter; `--test_env=NAME` on a command line must
|
||||
# not be able to point a test at a live workspace or Dolt server.
|
||||
unset BEADS_DIR BEADS_DB BD_DB BD_JSON BD_NO_DB BD_NO_DAEMON BD_ACTOR \
|
||||
BEADS_ACTOR GT_ROOT BEADS_DOLT_SHARED_SERVER BEADS_DOLT_SERVER_MODE \
|
||||
BEADS_DOLT_AUTO_START BEADS_DOLT_SERVER_HOST BEADS_DOLT_SERVER_PORT \
|
||||
BEADS_DOLT_PORT BEADS_DOLT_SERVER_DATABASE BEADS_DOLT_SERVER_SOCKET \
|
||||
BEADS_DOLT_PASSWORD BEADS_TEST_REPO_ROOT
|
||||
|
||||
# Not exec: the trap must run to remove $root. The exit status is the test's.
|
||||
status=0
|
||||
"$@" || status=$?
|
||||
exit "$status"
|
||||
Loading…
Add table
Add a link
Reference in a new issue