diff --git a/.bazelrc b/.bazelrc index 1344a57fe..df220ae3c 100644 --- a/.bazelrc +++ b/.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 diff --git a/BUILD.bazel b/BUILD.bazel index b753a44b3..89e40eb6f 100644 --- a/BUILD.bazel +++ b/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__"], diff --git a/MODULE.bazel b/MODULE.bazel index 07024e105..c0dcf1db9 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -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( diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 4f5bca374..d4d434b48 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -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=", diff --git a/cmd/bd/BUILD.bazel b/cmd/bd/BUILD.bazel index 7fb04f0c7..f1e8faa6f 100644 --- a/cmd/bd/BUILD.bazel +++ b/cmd/bd/BUILD.bazel @@ -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 --- diff --git a/cmd/bd/cli_fast_test.go b/cmd/bd/cli_fast_test.go index c5897d06b..92d96cfcb 100644 --- a/cmd/bd/cli_fast_test.go +++ b/cmd/bd/cli_fast_test.go @@ -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" { diff --git a/cmd/bd/doctor/BUILD.bazel b/cmd/bd/doctor/BUILD.bazel index 8bae81016..c3d3a4194 100644 --- a/cmd/bd/doctor/BUILD.bazel +++ b/cmd/bd/doctor/BUILD.bazel @@ -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 --- diff --git a/cmd/bd/doctor/dolt_e2e_test.go b/cmd/bd/doctor/dolt_e2e_test.go index 575514bf1..2fc70e8a4 100644 --- a/cmd/bd/doctor/dolt_e2e_test.go +++ b/cmd/bd/doctor/dolt_e2e_test.go @@ -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) } diff --git a/cmd/bd/doctor/fix/BUILD.bazel b/cmd/bd/doctor/fix/BUILD.bazel index 9d7c15dc8..63a6738f6 100644 --- a/cmd/bd/doctor/fix/BUILD.bazel +++ b/cmd/bd/doctor/fix/BUILD.bazel @@ -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 --- diff --git a/cmd/bd/doctor/integrity_test.go b/cmd/bd/doctor/integrity_test.go index b8450d2c0..359946519 100644 --- a/cmd/bd/doctor/integrity_test.go +++ b/cmd/bd/doctor/integrity_test.go @@ -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 { diff --git a/cmd/bd/gomodcache_rehome_test.go b/cmd/bd/gomodcache_rehome_test.go index c54916978..96d9d054c 100644 --- a/cmd/bd/gomodcache_rehome_test.go +++ b/cmd/bd/gomodcache_rehome_test.go @@ -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") diff --git a/cmd/bd/init_embedded_test.go b/cmd/bd/init_embedded_test.go index 942926755..a07d3e542 100644 --- a/cmd/bd/init_embedded_test.go +++ b/cmd/bd/init_embedded_test.go @@ -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 } diff --git a/cmd/bd/migrate_dolt_mode_frontdoor_integration_test.go b/cmd/bd/migrate_dolt_mode_frontdoor_integration_test.go index 85f730364..51d8f6f51 100644 --- a/cmd/bd/migrate_dolt_mode_frontdoor_integration_test.go +++ b/cmd/bd/migrate_dolt_mode_frontdoor_integration_test.go @@ -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") diff --git a/cmd/bd/preflight_test.go b/cmd/bd/preflight_test.go index 53a00d47e..19d72ba6a 100644 --- a/cmd/bd/preflight_test.go +++ b/cmd/bd/preflight_test.go @@ -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)) diff --git a/cmd/bd/protocol/BUILD.bazel b/cmd/bd/protocol/BUILD.bazel index 321f32fab..f6a03b25f 100644 --- a/cmd/bd/protocol/BUILD.bazel +++ b/cmd/bd/protocol/BUILD.bazel @@ -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", ], ) diff --git a/cmd/bd/protocol/helpers_test.go b/cmd/bd/protocol/helpers_test.go index e69c4b704..1f254ce4d 100644 --- a/cmd/bd/protocol/helpers_test.go +++ b/cmd/bd/protocol/helpers_test.go @@ -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) } diff --git a/cmd/bd/readonly_policy_test.go b/cmd/bd/readonly_policy_test.go index afa6329c2..8a4017535 100644 --- a/cmd/bd/readonly_policy_test.go +++ b/cmd/bd/readonly_policy_test.go @@ -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 diff --git a/cmd/bd/serve_test.go b/cmd/bd/serve_test.go index b06dc45bc..fe64500f6 100644 --- a/cmd/bd/serve_test.go +++ b/cmd/bd/serve_test.go @@ -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. diff --git a/cmd/bd/shared_server_integration_test.go b/cmd/bd/shared_server_integration_test.go index 46d194c96..f5d02d392 100644 --- a/cmd/bd/shared_server_integration_test.go +++ b/cmd/bd/shared_server_integration_test.go @@ -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 } diff --git a/cmd/bd/test_helpers_pure_test.go b/cmd/bd/test_helpers_pure_test.go index 8375edf1d..49e46fdb2 100644 --- a/cmd/bd/test_helpers_pure_test.go +++ b/cmd/bd/test_helpers_pure_test.go @@ -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 diff --git a/internal/beads/testmain_integration_test.go b/internal/beads/testmain_integration_test.go index 83559ac84..1fca8dca5 100644 --- a/internal/beads/testmain_integration_test.go +++ b/internal/beads/testmain_integration_test.go @@ -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" diff --git a/internal/storage/embeddeddolt/BUILD.bazel b/internal/storage/embeddeddolt/BUILD.bazel index a9e1cabd5..ee79d758a 100644 --- a/internal/storage/embeddeddolt/BUILD.bazel +++ b/internal/storage/embeddeddolt/BUILD.bazel @@ -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", diff --git a/internal/storage/uow/BUILD.bazel b/internal/storage/uow/BUILD.bazel index 4084bd162..f248905e7 100644 --- a/internal/storage/uow/BUILD.bazel +++ b/internal/storage/uow/BUILD.bazel @@ -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", diff --git a/internal/storage/uow/doltserver_provider_test.go b/internal/storage/uow/doltserver_provider_test.go index 3fa47c201..b6b03f7d0 100644 --- a/internal/storage/uow/doltserver_provider_test.go +++ b/internal/storage/uow/doltserver_provider_test.go @@ -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 } diff --git a/internal/testutil/bazeltest/bazeltest.go b/internal/testutil/bazeltest/bazeltest.go index 8ec8c45d8..a22e9d2d1 100644 --- a/internal/testutil/bazeltest/bazeltest.go +++ b/internal/testutil/bazeltest/bazeltest.go @@ -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