From ebe3b6bcb0b30bb588a06bf62e55cbf2370ebbf6 Mon Sep 17 00:00:00 2001 From: Julian Knutsen Date: Sun, 4 Oct 2026 03:06:54 -0700 Subject: [PATCH] test,ci(cmd/bd): run the Dolt-gated cmd/bd suite (advisory Bazel lane), fix in-process runner leaks and rotted tests; init --quiet stays quiet (#7185) * test(cmd/bd): reset cobra flags between in-process runs; keep them out of parallel tests runBDInProcess and runBDInProcessAllowError execute the package-level rootCmd tree again and again in one process, and cobra never resets a flag between Execute calls: a value one invocation parsed (create's --title, --labels, list's --status, ...) and its Changed bit stayed on the FlagSet, so the next invocation saw it, and a slice flag that had been set appended to the stale value instead of replacing it. With a Dolt test server (the only setup in which these tests run) that failed about 20 TestCLI_* tests depending on suite order. TestMain now snapshots every flag of the tree (snapshotCommandFlags, after all init()s registered them, before any test runs) and both runners put the tree back before and after each run (resetCommandFlags): the pointee of each flag.Value is restored (for pflag's scalar types that is the bound variable; for slice types it also clears the internal "changed" bit, so the next Set replaces), slice contents via SliceValue.Replace, and Changed=false. TestResetCommandFlagsRestoresEveryFlag sets every flag of the real tree and checks the reset. The ad-hoc --title reset in TestCLI_CreateRejectsEmptyTitle_ProxiedServerMode is no longer needed. Seven TestCLI_Comment* tests called t.Parallel() around the runners. inProcessMutex serializes the runners only against each other, so their chdir, stdio swap, env and config re-initialization raced every other parallel test (data races on config globals; TestCobraParallelPolicyGuard failing to read its own source files after a concurrent chdir). They run serially now, and the guard treats the two runners like rootCmd.Execute. Also two stale TestCLI_Create* cases: DryRunWithRigPrefix exercised a create --prefix flag that no longer exists, and the flag-like-title cases passed "--help" or "--foo-bar" where cobra parses them as flags; they now pass the title after "--", the only way it reaches create's own guard. Co-Authored-By: Claude Opus 5.5 (1M context) * test(cmd/bd): fix the ADO round-trip and bulk-delete tests that rotted unseen Both need the integration tag and a Dolt test server, which no CI lane gave cmd/bd (BEADS_TEST_SKIP=dolt everywhere), so they failed unnoticed: - TestADORoundTrip* stored ado.pat in the database. ado.pat is a secret read only from config.yaml or AZURE_DEVOPS_PAT (config.IsYamlOnlyKey), so the tracker found no PAT. The fixture sets AZURE_DEVOPS_PAT. - TestBulkDeleteNoResurrection expected GetIssue to return (nil, nil) for a deleted issue; the Dolt store reports storage.ErrNotFound. Co-Authored-By: Claude Opus 5.5 (1M context) * test(cmd/bd): fix parity, completion and notion tests that only fail with a Dolt server These are Dolt-gated (integration tag + test server), so no CI lane ran them and they rotted: - TestParityCreate*/TestParityReopenErrorExits1Silently: bd create auto-routes via routing.DetectUserRole("."), which warns "beads.role not configured (GH#2950)" on stderr when the working directory's git config has no beads.role. The suite pins the configured case, so the parity env now sets beads.role=maintainer in a test-owned global git config ($XDG_CONFIG_HOME/git/config; the role lookup scrubs every GIT_CONFIG_* variable, so those cannot be used). - TestIssueIDCompletion: issueIDCompletion resolves a database path before looking at the open store and returns nothing when there is none; the test now sets dbPath to its store's path. - TestIssueIDCompletion_UsesWorktreeFallbackWhenStoreNil and TestGetNotionConfigReadsDBPathWhenStoreUnset: the code under test reopens the store from metadata.json, which names a database but not the per-test branch of the shared database that newTestStore uses, so the reopened store saw none of the test's rows. They now use their own database (newTestStoreIsolatedDB). Co-Authored-By: Claude Opus 5.5 (1M context) * test(cmd/bd): fix stale doctor/e2e/autocommit/readonly Dolt-gated tests These need the integration tag and a Dolt test server, which no CI lane gave cmd/bd, so they rotted unseen: - TestE2E_DoctorFixMetadataRoundtrip, TestE2E_MigrateDoltMetadata, TestDoctorCheckHealthReportsVersionMismatchOnRepoLocalPort: bd init now defaults to embedded mode, where `bd sql` and `bd dolt start` are refused. Init with --server on a repo-local server of their own (repoLocalServerEnv: the suite's shared-server port vars cleared, server stopped at cleanup) and start it explicitly, since BEADS_TEST_MODE=1 never auto-starts one. MigrateDoltMetadata's DELETE also needed `key` quoted. - TestDoctor{Conventions,Pollution}MaxRows_EnvOnly_Exits2: bdRunRaw drops every BEADS_* variable, so the subprocess hit dolt.New's test-database firewall (exit 1) before the row cap; pass BEADS_TEST_SERVER=1. - TestDoltAutoCommit_On_WritesAdvanceHead: the embedded engine keeps its data under .beads/embeddeddolt, not .beads/dolt; findDoltRepoDir looks in both. - TestValidateCheck_AllClean: bd doctor validate runs five checks since the cross-table duplicates check was added. - TestConfigValidateReadOnlyIsHermetic: readonly commands take a shared workspace gate, whose lock file (.beads/dolt.gate.lock) workspacegate creates on first use by design; compare the .beads tree without gate lock files. Co-Authored-By: Claude Opus 5.5 (1M context) * test(cmd/bd): undo each in-process execution's env and storage-mode leaks Every in-process run of the bd command tree (runBDInProcess and the many tests that call rootCmd.Execute directly) leaves process state behind: PersistentPreRun exports the selected workspace as BEADS_DIR, loads its .beads/.env, and records the workspace's mode in serverMode / proxiedServerMode. With a Dolt test server, later tests in the same shard then resolved a deleted fixture ("no active beads workspace found", "migration sidecar is missing"), ran subprocess bd against the wrong workspace ("recorded as proxied-server in .beads/metadata.json"), or took the proxied path in RunE-level tests ("proxied-server UOW provider not initialized", a nil-UOW panic in reopenProxiedResolve that killed a shard). TestMain now registers a cobra initializer/finalizer pair (installExecuteIsolation) that snapshots the environment and the two mode globals when an execution starts and restores them when it ends; the runners' restoreEnvSnapshot shares the helper. TestDoltConfigEnvironmentOverrides used os.Setenv + defer os.Unsetenv, which deleted the suite's BEADS_DOLT_SERVER_PORT (the test server's port) for every later test; it uses t.Setenv now. Full integration-tagged cmd/bd suite against a hermetic dolt (16 shards): 18 order-dependent failures and the shard panic gone; the 27 remaining failures all fail alone too. Co-Authored-By: Claude Opus 5.5 (1M context) * fix(init): keep --quiet quiet through the Claude/Codex/Cursor auto-setup `bd init --quiet` ("Suppress output") still printed the project integration installers' progress to stdout ("Installing Claude hooks for this project...", "Claude Code integration installed", ...): the installers write to os.Stdout unconditionally. Point stdout at the null device around them while quiet; their failures keep going to stderr as before. Found by TestInitCommand's quiet case, which asserts no output and had not run in CI (Dolt-gated). Co-Authored-By: Claude Opus 5.5 (1M context) * test(init): bring the Dolt-gated init tests up to the current init contract These tests need a Dolt test server, so no CI lane ran them and they drifted from the product: - Embedded is init's default mode, so the server-mode assertions (.beads/dolt, opening the database on the test server, dolt_mode "server" in metadata.json) now pass --server (TestInitCommand, TestInitAlreadyInitialized, TestInitRedirect, TestInitBEADS_DIR, TestInitDoltMetadata, TestInitDoltMetadataNoGit, TestInitDatabaseFlag's metadata subtests). The in-process ones reset rootCmd's flags around themselves so --server cannot leak into later in-process inits. - Each server init gets its own --database: the suite shares one test server, and a "test" database another test created belongs to another project (init refuses: PROJECT IDENTITY MISMATCH). - The marker tests seed a current-era version witness: the legacy-upgrade guard refuses a non-empty .beads/dolt without one. - The redirect test provisions the canonical target with config.yaml: bd ignores a redirect to a directory with no project files (#4692). - Shared-server inits get a free port of their own (sharedServerTestEnv): the inherited BEADS_DOLT_SERVER_PORT is the suite's test server, which a shared-server start refuses as another project's, and the default shared port is 3308, a developer's live server. - --shared-server's config.yaml is now nested YAML (dolt: shared-server: true); parse it instead of matching the old flat line. Co-Authored-By: Claude Opus 5.5 (1M context) * test(cmd/bd): run the Dolt-gated tests that skipped on a bare dolt.New setupTestDB (config, kv and Linear round-trip tests), setupGatedTestDB (mol ready --gated) and the rename-prefix and bd sql tests opened stores with dolt.New(&dolt.Config{Path: ...}): no server port, no CreateIfMissing, so against a running test server they got "database ... not found" and skipped as "Dolt server not available". They now use the suite's newTestStore helpers (skip only when no test server runs), and what that exposed is fixed test-side: - TestCustomStatusConfig: custom_statuses is read back ORDER BY name; look statuses up by name / expect name order. - TestLinearRoundTripCoreFields, TestLinearPullMilestonesCreatesEpicHierarchy: linear.api_key is a yaml-only secret (LINEAR_API_KEY), push now needs an explicit linear.state_map, and batch push uses TeamLabels and issueBatchCreate, which the mock server now answers. - TestSqlCommand: bd sql refuses in the default embedded mode; set serverMode for the server-backed store, and stop discarding RunE errors. - TestE2E_AutoStartedRepoLocalServerPersistsAcrossCommands skipped on this process's embedded mode although every bd it runs is a subprocess on a --server workspace; gate on the dolt binary instead and stop the auto-started server at cleanup. Co-Authored-By: Claude Opus 5.5 (1M context) * test(cmd/bd): no fixed "unused" port, no swallowed JSON, no leaked front-door server Three more things the Dolt-backed cmd/bd run exposed: - TestTestServerConnection, TestSharedServerCfgNilHonorsSharedServer and the two TestLegacyGuard* e2e tests pointed bd at 127.0.0.1:59999 as a port nothing listens on. Any process on the host may hold it; a listener that accepts and resets turns the expected "connection refused" / "Dolt server unreachable" into another error. They take a just-freed port from closedLoopbackPort (testutil.FindFreePort) now. - TestDoltSetConfigJSONOutput and TestDoltSetConfigWithUpdateConfig created .beads 0755, so bd's permissions warning landed in the JSON under test and the tests skipped ("output not pure JSON") every run. The fixture uses 0700, and non-JSON output fails instead of skipping. - TestMigrateDoltModeFrontDoor's closing show/list auto-start a repo-local server after the reverse migration; nothing stopped it, so the suite's leak check failed (when the opt-in BEADS_TEST_MIGRATION_FRONTDOOR suite runs). It stops it at cleanup. Co-Authored-By: Claude Opus 5.5 (1M context) * ci(bazel): advisory cmd/bd Dolt-server tier (bazel-cmd-dolt) No CI lane ran cmd/bd's Dolt-gated tests (TestCLI_*, the init and store-backed suites, the doctor/e2e tests): main.yml's integration shards, nightly's Full Test Suite, and Bazel's --config=prcore/ci and --config=integration all pass BEADS_TEST_SKIP=dolt, so cmd/bd's TestMain never starts a test server and those tests skip; the proxied and embedded tiers run only their manifests' tests. They rotted unseen. //cmd/bd:bd_dolt_server_test (tag dolt-server-cmd) runs the whole integration-tagged cmd/bd suite, the integration build's bd_test, against a hermetic dolt sql-server: BEADS_TEST_DOLT_SERVER=local starts it from the pinned dolt in the test wrapper's runfiles, and BEADS_TEST_REQUIRE_DOLT_CONTAINER=1 fails the target instead of skipping into a cached pass. No test selection: the Go binary shards itself over every top-level test (32 shards), so each runs exactly once, and check_testcases.py fails a shard that ran none. --config=doltserver-cmd has exactly --config=integration's build flags, so the two lanes share bd_test's compile; prcore/ci and integration exclude the tag. bazel.yml's new bazel-cmd-dolt job runs it exactly where bazel-integration runs (same if: remotely on the farm for same-repo PRs, fork-ro/fork-rw, and locally with the read-only cache in mode cache for forks). It is advisory: job-level continue-on-error keeps a failure out of the call's aggregate (BAZEL), and pr.yml's ci-gate reads its result (BAZEL_CMD_DOLT) but requires it only while the committed top-level flag BAZEL_CMD_DOLT_REQUIRED is "true" (now "false"; flipping it is the one change needed). bazel-gate.sh accepts its skip in modes skip and local. Policy tests: a third lane category in TestBazelLaneIsGatedAlongsideLegacy (bazelFlagGatedLanes: gate env wired, never statically required, added by the run script exactly when the pinned flag is "true"), the gate simulation over every mode with the flag off (advisory: nothing it reports reddens the gate) and on (exactly a gated lane), the cache-mode and execution-mode checks, the tag taxonomy and prcore/integration exclusions, and TestBazelCmdDoltJob (job steps, timeouts, .bazelrc config equal to integration's build with no dolt skip or test selection, the target's dolt-server rules, and bd_test's env repeated). Measured locally (all fixes in this series, 32 shards, 16 at a time on a shared host): 3267 top-level tests, all shards pass, max shard 313s (one 5001-issue export test, 214s, sets it), mean 100s. Co-Authored-By: Claude Opus 5.5 (1M context) * fix(init): hand the project installers a writer instead of swapping os.Stdout The --quiet fix earlier in this series pointed the process-global os.Stdout at the null device around the Claude/Codex/Cursor installers. That works, but it mutates a global from inside a cobra Run: anything else writing to stdout meanwhile (a goroutine, a future concurrent installer) would vanish, and a concurrent read of os.Stdout is a data race. The installers already write through env structs with stdout/stderr fields. setup.ProjectInstallOutput carries the two writers, and InstallClaudeProjectTo / InstallCodexProjectTo / InstallCursorProjectTo use them (the old entry points keep the process streams). init passes QuietProjectInstallOutput() under --quiet: progress discarded, error lines still on stderr. The proxied-server init path (domain adapter InstallClaudeProject now takes quiet) does the same, so its "Installing Claude hooks..." is quiet too. Consistent error policy under --quiet: errors still reach stderr. init's own "Warning: failed to setup " lines were suppressed under --quiet while the installers' "Error:" lines were not, and some installer failures (Cursor rules) print nothing themselves; the warnings now print whether or not quiet, like the installers' errors. Coverage in required lanes: TestProjectInstallersReportToTheGivenOutput (cmd/bd/setup) and TestEmbeddedInitA/basic, which now asserts that `bd init --quiet` with agents enabled installs the Claude hooks and prints none of the installers' progress (verified to fail with the quiet output disabled). Co-Authored-By: Claude Opus 5.5 (1M context) * test(export): exercise the incremental-export fallback with a small threshold TestTryIncrementalExport_ThresholdExceededFallsBack seeded incrementalExportThreshold+1 = 5001 issues to cross the threshold. Batch create is one transaction but still several statements per issue, so under -race against the shared test server it took ~214 s, the longest test in cmd/bd's Dolt-backed suite by far. The behaviour under test is only "more changed issues than the threshold -> full export fallback". incrementalExportThreshold becomes a var (same value), and the test, which is not parallel, sets it to 20 and seeds 21 issues: 214 s -> 0.6 s. Co-Authored-By: Claude Opus 5.5 (1M context) * test(cmd/bd): tighten the flag-reset self-test, guards and canaries Review follow-ups: - TestResetCommandFlagsRestoresEveryFlag no longer skips a flag it cannot set: a flag type without a sample value, a Set error, or a non-pointer Value (which resetCommandFlags could not restore) is an error, so a new flag type cannot go untested. resetCommandFlags documents that it also resets flag-bound package globals. - TestCobraParallelPolicyGuard treats .ExecuteC/.ExecuteContext/ .ExecuteContextC like .Execute (the per-execution isolation writes the mode globals; a parallel execution would race). - TestConfigValidateReadOnlyIsHermetic ignores only the gate a readonly command takes (.beads/dolt.gate.lock and its .info sidecar), not any "*.gate.lock*" file, so a new unexpected gate still trips the canary. - TestCLI_CreateRejectsFlagLikeTitles: a bare `bd create --foo-bar` must fail and create no issue (bd-2c0's intent), next to the "--" cases. - TestCustomStatusConfig cites why statuses come back in name order: the normalized custom_statuses table (#2961, 8537d943b) has no ordinal and is read ORDER BY name, like custom_types; the test predates it. Co-Authored-By: Claude Opus 5.5 (1M context) * ci(bazel): run the cmd/bd Dolt-server tier in 16 shards Each shard pays ~30-40 s of fixed cost (TestMain plus its own dolt sql-server), so 32 shards spent roughly 40% of the lane's executor time on setup and added 32 farm actions to every PR touching cmd/bd. With the 5001-issue export test cut to under a second, 16 shards measure (locally, --nocache_test_results): max 186 s, mean 137 s, 2,187 executor-seconds (vs ~2,800-3,200 at 32), all 3,267 top-level tests, every shard passing. TestBazelCmdDoltJob pins the count; bazel.yml's timing comment is restated (cache mode ~20-30 minutes). Co-Authored-By: Claude Opus 5.5 (1M context) --------- Co-authored-by: Gas City Adopt-PR Co-authored-by: Claude Opus 5.5 (1M context) --- .bazelrc | 32 ++- .github/scripts/bazel-gate.sh | 9 +- .github/workflows/bazel.yml | 107 +++++++ .github/workflows/pr.yml | 18 ++ cmd/bd/BUILD.bazel | 54 ++++ cmd/bd/ado_roundtrip_test.go | 18 +- cmd/bd/cli_fast_test.go | 134 ++++----- cmd/bd/cobra_flag_reset_test.go | 270 ++++++++++++++++++ cmd/bd/completions_test.go | 11 +- cmd/bd/config_test.go | 52 ++-- cmd/bd/delete_test.go | 8 +- cmd/bd/doctor_health_integration_test.go | 7 +- cmd/bd/doctor_max_rows_test.go | 14 +- cmd/bd/doctor_validate_test.go | 6 +- cmd/bd/dolt_autocommit_integration_test.go | 47 +-- ...lt_autostart_lifecycle_integration_test.go | 12 +- cmd/bd/dolt_metadata_e2e_test.go | 46 ++- cmd/bd/dolt_test.go | 28 +- cmd/bd/export_auto.go | 5 +- cmd/bd/export_auto_test.go | 8 + cmd/bd/fs_adapters.go | 10 +- cmd/bd/init.go | 25 +- cmd/bd/init_embedded_test.go | 8 + cmd/bd/init_proxied_server.go | 4 +- cmd/bd/init_test.go | 114 ++++++-- cmd/bd/legacy_upgrade_guard_e2e_test.go | 4 +- cmd/bd/linear_roundtrip_test.go | 81 +++++- ...te_dolt_mode_frontdoor_integration_test.go | 4 + cmd/bd/mol_ready_gated_test.go | 41 +-- cmd/bd/notion_store_test.go | 5 +- cmd/bd/readonly_policy_test.go | 24 +- cmd/bd/rename_prefix_test.go | 28 +- cmd/bd/setup/BUILD.bazel | 2 + cmd/bd/setup/claude.go | 6 + cmd/bd/setup/codex.go | 6 + cmd/bd/setup/cursor.go | 8 +- cmd/bd/setup/project_output.go | 35 +++ cmd/bd/setup/project_output_test.go | 58 ++++ cmd/bd/shared_server_cfgnil_test.go | 3 +- cmd/bd/shared_server_cleanup_test.go | 15 + cmd/bd/sql_test.go | 27 +- cmd/bd/stdio_race_guard_test.go | 16 +- cmd/bd/test_repo_beads_guard_test.go | 8 + cmd/bd/write_verbs_parity_test.go | 17 ++ engdocs/TESTING.md | 11 + internal/storage/domain/beads.go | 8 +- scripts/bazel_cache_mode_test.go | 8 +- scripts/bazel_policy_test.go | 8 +- scripts/ci_workflow_test.go | 260 +++++++++++++++-- scripts/pr_lanes_bazel_coverage_test.go | 2 +- 50 files changed, 1392 insertions(+), 340 deletions(-) create mode 100644 cmd/bd/cobra_flag_reset_test.go create mode 100644 cmd/bd/setup/project_output.go create mode 100644 cmd/bd/setup/project_output_test.go diff --git a/.bazelrc b/.bazelrc index 4ee0cfa28..9d514ce3e 100644 --- a/.bazelrc +++ b/.bazelrc @@ -37,7 +37,7 @@ test:prcore --test_env=BEADS_TEST_REQUIRE_EXCLUDE_PERMISSION=1 # Every tag that belongs to another lane (scripts/bazel_policy_test.go # bazelPRCoreExcludedTags). integration-only targets run zero tests outside # --config=integration. -test:prcore --test_tag_filters=-requires-docker,-dolt-server,-dolt-server-proxied,-dolt-server-integration,-embedded,-manual,-integration-only +test:prcore --test_tag_filters=-requires-docker,-dolt-server,-dolt-server-proxied,-dolt-server-integration,-dolt-server-cmd,-embedded,-manual,-integration-only test:ci --config=prcore test:ci --keep_going --test_summary=terse @@ -206,6 +206,34 @@ test:doltserver-integration --nocache_test_results # visible "Re-run failed jobs" retries it. test:doltserver-integration --experimental_remote_cache_eviction_retries=0 +# cmd/bd Dolt-server tier: //cmd/bd:bd_dolt_server_test (dolt-server-cmd +# tag), the whole integration-tagged cmd/bd suite against a hermetic dolt +# sql-server, i.e. the Dolt-gated cmd/bd tests every other lane skips +# (BEADS_TEST_SKIP=dolt in --config=prcore/ci and --config=integration, and +# in main.yml's and nightly.yml's go test jobs). The build flags are exactly +# --config=integration's, so it compiles nothing bazel-integration does not +# (shared through the remote cache). bazel.yml runs it where it runs the +# integration lane (remote, or with the read-only cache in mode cache), in a +# job of its own (bazel-cmd-dolt), advisory until pr.yml's +# BAZEL_CMD_DOLT_REQUIRED says otherwise. No BEADS_TEST_SKIP, no -short. +# Results are cacheable, as in --config=doltserver: every input is in the +# action key, and a server that cannot start fails the target. +build:doltserver-cmd --@rules_go//go/config:tags=gms_pure_go,integration +test:doltserver-cmd --@rules_go//go/config:race +test:doltserver-cmd --test_tag_filters=dolt-server-cmd +test:doltserver-cmd --build_tests_only +test:doltserver-cmd --keep_going +test:doltserver-cmd --test_summary=terse +test:doltserver-cmd --test_timeout=-1,-1,-1,1200 +# As in --config=integration: Go's own timeout fires first and dumps every +# goroutine to test.log, which Bazel's kill at 1200s does not. +test:doltserver-cmd --test_arg=-test.timeout=19m +test:doltserver-cmd --test_arg=-test.parallel=4 +test:doltserver-cmd --test_env=BEADS_TEST_REQUIRE_EXCLUDE_PERMISSION=1 +test:doltserver-cmd --test_env=GO_TEST_WRAP_TESTV=1 +test:doltserver-cmd --local_test_jobs=4 +test:doltserver-cmd --remote_download_regex=.*/test\.(log|xml)$ + # Integration lane: main.yml's "Main Linux integration packages (N/6)" and # "Main Linux integration cmd/bd (N/8)" jobs (and nightly.yml's Full Test # Suite, which runs the same `go test` over ./... in one job): every package @@ -224,7 +252,7 @@ test:integration --test_env=BEADS_TEST_SKIP=dolt # keeps every container- or server-backed test skipped, which is what the # untagged base targets already do. scripts/bazel_policy_test.go derives the # required exclusions from the PR-core list, so a new lane tag lands here too. -test:integration --test_tag_filters=-requires-docker,-dolt-server,-dolt-server-proxied,-dolt-server-integration,-embedded,-manual +test:integration --test_tag_filters=-requires-docker,-dolt-server,-dolt-server-proxied,-dolt-server-integration,-dolt-server-cmd,-embedded,-manual test:integration --build_tests_only test:integration --keep_going test:integration --test_summary=terse diff --git a/.github/scripts/bazel-gate.sh b/.github/scripts/bazel-gate.sh index 40f58be47..07bed3644 100755 --- a/.github/scripts/bazel-gate.sh +++ b/.github/scripts/bazel-gate.sh @@ -9,10 +9,11 @@ # skip: every lane and the aggregate (BAZEL) # local: the remote-only BAZEL_EMBEDDED, # BAZEL_PROXIED, BAZEL_SERVER_STORAGE, -# and BAZEL_INTEGRATION +# BAZEL_INTEGRATION and BAZEL_CMD_DOLT # cache: BAZEL_EMBEDDED, BAZEL_PROXIED, # BAZEL_SERVER_STORAGE (integration -# runs with the read-only cache) +# and cmd-dolt run with the read-only +# cache) # remote, fork-ro, fork-rw: none # bazel-gate.sh aggregate the value to gate on for BAZEL: the call's # aggregate result, or, when the mode is missing @@ -40,8 +41,8 @@ case "${1:-}" in skips=() if [[ "$valid" == true ]]; then case "$mode" in - skip) skips+=(BAZEL BAZEL_TEST BAZEL_PURE BAZEL_EMBEDDED BAZEL_INTEGRATION BAZEL_DOLTSERVER BAZEL_PROXIED BAZEL_SERVER_STORAGE) ;; - local) skips+=(BAZEL_EMBEDDED BAZEL_INTEGRATION BAZEL_PROXIED BAZEL_SERVER_STORAGE) ;; + skip) skips+=(BAZEL BAZEL_TEST BAZEL_PURE BAZEL_EMBEDDED BAZEL_INTEGRATION BAZEL_DOLTSERVER BAZEL_PROXIED BAZEL_SERVER_STORAGE BAZEL_CMD_DOLT) ;; + local) skips+=(BAZEL_EMBEDDED BAZEL_INTEGRATION BAZEL_PROXIED BAZEL_SERVER_STORAGE BAZEL_CMD_DOLT) ;; cache) skips+=(BAZEL_EMBEDDED BAZEL_PROXIED BAZEL_SERVER_STORAGE) ;; esac fi diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml index e785ddce7..c0162bcdf 100644 --- a/.github/workflows/bazel.yml +++ b/.github/workflows/bazel.yml @@ -25,6 +25,9 @@ # bazel-embedded, bazel-integration, bazel-doltserver, bazel-proxied and # bazel-server-storage (BAZEL_TEST, BAZEL_PURE, BAZEL_EMBEDDED, # BAZEL_INTEGRATION, BAZEL_DOLTSERVER, BAZEL_PROXIED, BAZEL_SERVER_STORAGE); +# bazel-cmd-dolt (the cmd/bd Dolt-server tier, BAZEL_CMD_DOLT) is advisory: +# job-level continue-on-error, and pr.yml requires it only while its +# BAZEL_CMD_DOLT_REQUIRED flag is "true"; # .github/scripts/bazel-gate.sh turns the rbe job's rbe-mode output into the # skips it accepts, so a lane skipped by its `if:` below passes and one that # should have run and failed, was cancelled or reported nothing fails the @@ -306,6 +309,9 @@ on: bazel-server-storage: description: "bazel-server-storage's job.status" value: ${{ jobs.bazel-server-storage.outputs.result }} + bazel-cmd-dolt: + description: "bazel-cmd-dolt's job.status (advisory unless pr.yml's BAZEL_CMD_DOLT_REQUIRED is \"true\")" + value: ${{ jobs.bazel-cmd-dolt.outputs.result }} package-mcp: description: "package-mcp's job.status (empty unless package-gates is 'on')" value: ${{ jobs.package-mcp.outputs.result }} @@ -1609,6 +1615,107 @@ jobs: JOB_STATUS: ${{ job.status }} run: echo "result=$JOB_STATUS" >> "$GITHUB_OUTPUT" + # The cmd/bd Dolt-server tier, as --config=doltserver-cmd: the whole + # integration-tagged cmd/bd suite (//cmd/bd:bd_dolt_server_test, 16 + # shards) against a hermetic dolt sql-server, i.e. the Dolt-gated cmd/bd + # tests (TestCLI_*, TestInit*, the store-backed suites) that every other + # lane and legacy job skips with BEADS_TEST_SKIP=dolt or leaves out of its + # manifest. --config=doltserver-cmd has exactly --config=integration's + # build flags, so it shares bazel-integration's compile through the + # remote cache. Runs exactly where bazel-integration runs (the same if): + # remotely (modes remote, fork-ro, fork-rw), or locally with the + # read-only cache (mode cache: fork and Dependabot PRs while rbe-fork is + # closed), never plain local (rbe=off), and not when a caller passes + # integration: "off". + # Advisory: job-level continue-on-error keeps a failure out of this + # call's aggregate result (BAZEL), and pr.yml's ci-gate requires the + # lane's own result (BAZEL_CMD_DOLT) only while pr.yml's committed + # BAZEL_CMD_DOLT_REQUIRED flag is "true" (scripts/ci_workflow_test.go, + # TestBazelCmdDoltJob). + bazel-cmd-dolt: + name: cmd/bd Dolt-server tier + needs: rbe + runs-on: ${{ needs.rbe.outputs.mode == 'remote' && 'blacksmith-2vcpu-ubuntu-2404' || 'ubuntu-latest' }} + if: ${{ (needs.rbe.outputs.enabled == 'true' || needs.rbe.outputs.mode == 'cache') && inputs.integration != 'off' }} + timeout-minutes: 85 + continue-on-error: true + outputs: + result: ${{ steps.result.outputs.result }} + steps: + # inputs.checkout-sha: see the header (empty: the event's default ref). + # checkout v7 refuses a fork head on pull_request_target unless opted + # in; only bazel-farm.yml's authorized call opts in (false elsewhere). + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ inputs.checkout-sha }} + persist-credentials: false + allow-unsafe-pr-checkout: ${{ inputs.fork-farm == 'authorized' && github.event_name == 'pull_request_target' }} + + - name: Set up Bazel + id: bazel + uses: ./.github/actions/setup-bazel + env: + BAZEL_REMOTE_EXECUTOR: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_WEST_EXECUTOR || '' }} + RBE_TLS_CERT: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CERT || '' }} + RBE_TLS_KEY: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_KEY || '' }} + RBE_TLS_CA: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CA || '' }} + RBE_INSTANCE: ${{ needs.rbe.outputs.mode == 'remote' && 'oss' || '' }} + BAZEL_FORK_CACHE: ${{ needs.rbe.outputs.mode == 'cache' && 'true' || '' }} + BAZEL_FORK_REMOTE: ${{ startsWith(needs.rbe.outputs.mode, 'fork-') && 'true' || '' }} + RBE_FORK_TIER: ${{ needs.rbe.outputs.tier }} + RBE_FORK_PR: ${{ github.event.pull_request.number }} + + # Remote: a compile shared with bazel-integration plus the longest + # of 16 shards (~3 minutes measured; each shard pays ~30-40 s of + # TestMain and its own dolt sql-server; Go's -test.timeout=19m, + # Bazel's 1200s cap). Mode cache on a 4-CPU runner: the compile hits + # the read-only cache, and a PR that changes cmd/bd or what it imports + # runs every shard locally, four at a time (~2,200 test-seconds + # measured unloaded, slower four-up: ~20-30 minutes); with the cache + # closed, add the cold race build (~30). + - name: bazel test //... --config=doltserver-cmd + id: test + timeout-minutes: 75 + run: | + set -o pipefail + start=$(date +%s) + rc=0 + bazel test //... --config=doltserver-cmd \ + --build_event_json_file="$RUNNER_TEMP/bazel-bep.json" \ + 2>&1 | tee "$RUNNER_TEMP/bazel-test.log" || rc=$? + echo "bazel test --config=doltserver-cmd: exit $rc, $(( $(date +%s) - start ))s wall" | tee -a "$GITHUB_STEP_SUMMARY" + exit "$rc" + + # As in bazel-embedded: every shard of the target ran tests (the Go + # binary shards itself over every top-level test, so together the + # shards run each exactly once). + - name: Every target and shard ran tests + if: ${{ always() && steps.test.outcome != 'skipped' }} + run: python3 tools/bazel/check_testcases.py --bep "$RUNNER_TEMP/bazel-bep.json" + + - name: Upload test logs + if: ${{ failure() && steps.test.outcome != 'skipped' }} + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + overwrite: true # a job re-run is the same run: replace, not 409 + name: bazel-cmd-dolt-testlogs + path: | + bazel-testlogs/**/test.log + bazel-testlogs/**/test.xml + if-no-files-found: ignore + retention-days: 7 + + # Last step: the job's result as a workflow_call output for pr.yml's + # ci-gate. always() runs it after a failure, a timeout or a + # cancellation too; job.status is the steps' status, unaffected by the + # job-level continue-on-error. + - name: Record job result + id: result + if: ${{ always() }} + env: + JOB_STATUS: ${{ job.status }} + run: echo "result=$JOB_STATUS" >> "$GITHUB_OUTPUT" + # F3: moved here from pr.yml so the gate depends on nothing but the rbe # job's decision (not detect-package-gates, build-artifacts, bazel-coverage # or the whole bazel call). Only pr.yml opts in (package-gates: "on"); diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index adf785b44..54fa45d04 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -51,6 +51,15 @@ env: # lanes run remotely beside them whenever rbe-fork is open (bazel.yml # modes fork-ro/fork-rw), else locally with the read-only cache as before. BAZEL_COVERS_FORKS: "false" + # bazel.yml's bazel-cmd-dolt lane (the cmd/bd Dolt-server tier: the + # Dolt-gated cmd/bd tests no other lane runs) is advisory while this is + # "false": it runs and reports in the bazel call (continue-on-error, so a + # failure never turns the call's aggregate red), and ci-gate ignores its + # BAZEL_CMD_DOLT result. "true" adds BAZEL_CMD_DOLT to CI_GATE_REQUIRED in + # the "Evaluate CI gate" step's run script; bazel-gate.sh still accepts + # its skip in modes skip and local. Committed, not a repo variable, for + # the same reason as the flags above. + BAZEL_CMD_DOLT_REQUIRED: "false" jobs: @@ -2485,6 +2494,9 @@ jobs: BAZEL_DOLTSERVER: ${{ needs.bazel.outputs.bazel-doltserver || 'skipped' }} BAZEL_PROXIED: ${{ needs.bazel.outputs.bazel-proxied || 'skipped' }} BAZEL_SERVER_STORAGE: ${{ needs.bazel.outputs.bazel-server-storage || 'skipped' }} + # Advisory unless BAZEL_CMD_DOLT_REQUIRED (top of this file) is + # "true": see the run script. + BAZEL_CMD_DOLT: ${{ needs.bazel.outputs.bazel-cmd-dolt || 'skipped' }} # Inputs of bazel-gate.sh, which sets BAZEL (the call's aggregate, # red on a missing or inconsistent mode) and the expected skips # from bazel.yml's own rbe job decision. @@ -2518,6 +2530,12 @@ jobs: CI_GATE_REQUIRED="$CI_GATE_REQUIRED TEST_WINDOWS_LIVENESS WORKTREE_REMOVE_WINDOWS" skipped_ok="$skipped_ok TEST_WINDOWS_LIVENESS_PREBUILT WORKTREE_REMOVE_WINDOWS_PREBUILT" fi + # The cmd/bd Dolt-server tier (bazel.yml's bazel-cmd-dolt) is + # advisory until BAZEL_CMD_DOLT_REQUIRED (top of this file) is + # "true"; it is never in the static list above. + if [[ "$BAZEL_CMD_DOLT_REQUIRED" == "true" ]]; then + CI_GATE_REQUIRED="$CI_GATE_REQUIRED BAZEL_CMD_DOLT" + fi export CI_GATE_REQUIRED # Bazel lanes skipped by design in the call's execution mode (all # of them in mode skip, the remote-only embedded, proxied and diff --git a/cmd/bd/BUILD.bazel b/cmd/bd/BUILD.bazel index 437b7411b..3c1dfb49c 100644 --- a/cmd/bd/BUILD.bazel +++ b/cmd/bd/BUILD.bazel @@ -557,6 +557,7 @@ go_test( "close_proxied_integration_test.go", "close_reason_file_test.go", "close_routing_test.go", + "cobra_flag_reset_test.go", "codex_hook_test.go", "command_telemetry_test.go", "comment_promote_embedded_test.go", @@ -1314,6 +1315,7 @@ go_test( "@com_github_spf13_pflag//:pflag", "@com_github_stretchr_testify//assert", "@com_github_stretchr_testify//require", + "@in_gopkg_yaml_v3//:yaml_v3", "@land_charm_lipgloss_v2//:lipgloss", "@org_golang_x_sync//errgroup", "@org_golang_x_term//:term", @@ -1470,6 +1472,58 @@ sh_test( tags = ["dolt-server-proxied"], ) +# The whole cmd/bd suite, as --config=doltserver-cmd builds it (exactly +# --config=integration's build: the integration and gms_pure_go build tags, +# race, so the two lanes share bd_test's compile), against a hermetic dolt sql-server: +# TestMain starts the shared test server from the pinned dolt in the test +# wrapper's runfiles (BEADS_TEST_DOLT_SERVER=local), and a server that +# cannot start fails the target (BEADS_TEST_REQUIRE_DOLT_CONTAINER=1) +# instead of skipping every Dolt-gated test into a cached pass. These are +# the cmd/bd tests no other lane runs: every other configuration of bd_test +# passes BEADS_TEST_SKIP=dolt (prcore/ci, integration) or runs only its +# manifest (bd_embedded_test, bd_proxied_test), and so do main.yml's and +# nightly.yml's go test jobs. No -test.run/-test.skip: the Go binary shards +# itself over every top-level test (rules_go's TEST_SHARD_INDEX), so each +# runs in exactly one shard, and check_testcases.py fails a shard that ran +# none. It repeats bd_test's `env` (the wrapper cannot inherit it). +# +# Tags: +# dolt-server-cmd: the cmd/bd Dolt-server tier; starts hermetic dolt +# sql-servers and needs the integration build tag; excluded from +# --config=prcore/ci and --config=integration, selected by +# --config=doltserver-cmd. +sh_test( + name = "bd_dolt_server_test", + size = "large", + # --config=doltserver-cmd's -test.timeout=19m fires (with a goroutine + # dump) before Bazel's 1200s action limit (eternal under the lane's + # --test_timeout), which kills without one. + timeout = "eternal", + srcs = ["//tools/bazel:go_test_variant.sh"], + args = ["$(rootpath :bd_test)"], + data = [ + ":bd_for_tests", + ":bd_test", + ":preflight_go_fixture", + "@go_sdk//:bin/gofmt", + ], + env = { + "BEADS_TEST_BD_BINARY": "$(rlocationpath :bd_for_tests)", + "BEADS_TEST_DOLT_SERVER": "local", + # The integration jobs' global git identity (tools/bazel/test_env.sh). + "BEADS_TEST_GIT_IDENTITY": "1", + "BEADS_TEST_GOFMT": "$(rlocationpath @go_sdk//:bin/gofmt)", + "BEADS_TEST_PREFLIGHT_GO": "$(rlocationpath :preflight_go_fixture)", + "BEADS_TEST_REQUIRE_DOLT_CONTAINER": "1", + # The 4-vCPU runner shape, as in bd_proxied_test: -test.parallel + # defaults to GOMAXPROCS, and every parallel test opens stores + # against the one shared server. + "GOMAXPROCS": "4", + }, + shard_count = 16, + tags = ["dolt-server-cmd"], +) + # CLI flag registrations pinned by //internal/httpapi spec parity tests. exports_files( [ diff --git a/cmd/bd/ado_roundtrip_test.go b/cmd/bd/ado_roundtrip_test.go index 38b134e4d..acedbf703 100644 --- a/cmd/bd/ado_roundtrip_test.go +++ b/cmd/bd/ado_roundtrip_test.go @@ -259,6 +259,10 @@ func (m *mockADOServer) itemCount() int { // container infrastructure. Uses newTestStoreIsolatedDB for per-test DB isolation. func adoTestStore(t *testing.T, prefix string) *dolt.DoltStore { t.Helper() + // ado.pat is a secret, read from config.yaml or AZURE_DEVOPS_PAT and + // never from the database (config.IsYamlOnlyKey), so a SetConfig of it + // on the store would leave the tracker without a PAT. + t.Setenv("AZURE_DEVOPS_PAT", "test-pat") dbPath := filepath.Join(t.TempDir(), ".beads", "dolt") return newTestStoreIsolatedDB(t, dbPath, prefix) } @@ -280,7 +284,6 @@ func TestADORoundTripCoreFields(t *testing.T) { // Configure ADO settings in source store for k, v := range map[string]string{ - "ado.pat": "test-pat", "ado.url": server.URL, "ado.project": project, } { @@ -359,7 +362,6 @@ func TestADORoundTripCoreFields(t *testing.T) { targetStore := adoTestStore(t, "bd") for k, v := range map[string]string{ - "ado.pat": "test-pat", "ado.url": server.URL, "ado.project": project, } { @@ -457,7 +459,7 @@ func TestADORoundTripBlockedStatus(t *testing.T) { defer server.Close() for k, v := range map[string]string{ - "ado.pat": "test-pat", "ado.url": server.URL, + "ado.url": server.URL, "ado.project": project, } { if err := sourceStore.SetConfig(ctx, k, v); err != nil { @@ -490,7 +492,7 @@ func TestADORoundTripBlockedStatus(t *testing.T) { // Pull into fresh DB targetStore := adoTestStore(t, "bd") for k, v := range map[string]string{ - "ado.pat": "test-pat", "ado.url": server.URL, + "ado.url": server.URL, "ado.project": project, } { if err := targetStore.SetConfig(ctx, k, v); err != nil { @@ -538,7 +540,7 @@ func TestADORoundTripLossyPriority(t *testing.T) { defer server.Close() for k, v := range map[string]string{ - "ado.pat": "test-pat", "ado.url": server.URL, + "ado.url": server.URL, "ado.project": project, } { if err := sourceStore.SetConfig(ctx, k, v); err != nil { @@ -573,7 +575,7 @@ func TestADORoundTripLossyPriority(t *testing.T) { // Pull into fresh DB (simulating a different machine) targetStore := adoTestStore(t, "bd") for k, v := range map[string]string{ - "ado.pat": "test-pat", "ado.url": server.URL, + "ado.url": server.URL, "ado.project": project, } { if err := targetStore.SetConfig(ctx, k, v); err != nil { @@ -632,7 +634,7 @@ func TestADORoundTripLabels(t *testing.T) { defer server.Close() for k, v := range map[string]string{ - "ado.pat": "test-pat", "ado.url": server.URL, + "ado.url": server.URL, "ado.project": project, } { if err := sourceStore.SetConfig(ctx, k, v); err != nil { @@ -665,7 +667,7 @@ func TestADORoundTripLabels(t *testing.T) { // Pull into fresh DB targetStore := adoTestStore(t, "bd") for k, v := range map[string]string{ - "ado.pat": "test-pat", "ado.url": server.URL, + "ado.url": server.URL, "ado.project": project, } { if err := targetStore.SetConfig(ctx, k, v); err != nil { diff --git a/cmd/bd/cli_fast_test.go b/cmd/bd/cli_fast_test.go index 31e3576b1..47c82ced1 100644 --- a/cmd/bd/cli_fast_test.go +++ b/cmd/bd/cli_fast_test.go @@ -145,36 +145,11 @@ func createTempDirWithCleanup(t *testing.T) string { } // restoreEnvSnapshot puts the process environment back to before, a prior -// os.Environ(), touching only the keys that differ. An in-process -// rootCmd.Execute mutates the environment with raw os.Setenv and restores -// nothing (PersistentPreRun exports the selected workspace as BEADS_DIR, -// loads the workspace's .beads/.env, ...); left in place, the BEADS_DIR of a -// since-deleted fixture makes every later test's workspace discovery report -// "no active beads workspace found" (e.g. TestResolvedConfigRepoRoot). -func restoreEnvSnapshot(before []string) { - want := make(map[string]string, len(before)) - for _, kv := range before { - if k, v, ok := strings.Cut(kv, "="); ok && k != "" { - want[k] = v - } - } - for _, kv := range os.Environ() { - k, v, ok := strings.Cut(kv, "=") - if !ok || k == "" { - continue - } - if old, had := want[k]; !had { - _ = os.Unsetenv(k) - } else if old != v { - _ = os.Setenv(k, old) - } - } - for k, v := range want { - if _, ok := os.LookupEnv(k); !ok { - _ = os.Setenv(k, v) - } - } -} +// os.Environ(): an in-process rootCmd.Execute mutates the environment with +// raw os.Setenv and restores nothing (see installExecuteIsolation, which now +// does this for every execution; kept here so the runners do not depend on +// TestMain having installed it). +func restoreEnvSnapshot(before []string) { restoreProcessEnv(before) } // runBDInProcess runs bd commands in-process by calling rootCmd.Execute // This is ~10-20x faster than exec.Command because it avoids process spawn overhead @@ -207,6 +182,11 @@ func runBDInProcess(t *testing.T, dir string, args ...string) string { os.Stderr = oldStderr }() + // Start from the registered flag defaults: cobra keeps every parsed + // value (and Changed) on the shared command tree across Execute calls. + resetCommandFlags(rootCmd) + defer resetCommandFlags(rootCmd) + // Set args for rootCmd rootCmd.SetArgs(args) os.Args = append([]string{"bd"}, args...) @@ -1265,6 +1245,8 @@ func runBDInProcessAllowError(t *testing.T, dir string, args ...string) (string, os.Stderr = oldStderr }() + resetCommandFlags(rootCmd) + defer resetCommandFlags(rootCmd) rootCmd.SetArgs(args) os.Args = append([]string{"bd"}, args...) @@ -1411,25 +1393,6 @@ func TestCLI_CreateDryRun(t *testing.T) { } }) - t.Run("DryRunWithRigPrefix", func(t *testing.T) { - tmpDir := setupCLITestDB(t) - - // Run create with --dry-run and --prefix (simulates cross-rig creation) - // Note: This won't actually route to another rig since we don't have one, - // but it should show the target rig in the preview - out := runBDInProcess(t, tmpDir, "create", "Cross-rig issue", "-p", "1", - "--prefix", "other-rig", - "--dry-run") - - // Verify target rig is shown in preview - if !strings.Contains(out, "Target rig:") { - t.Errorf("Expected 'Target rig:' in output, got: %s", out) - } - if !strings.Contains(out, "other-rig") { - t.Errorf("Expected 'other-rig' in output, got: %s", out) - } - }) - t.Run("DryRunWithFileReturnsError", func(t *testing.T) { tmpDir := createTempDirWithCleanup(t) @@ -1499,8 +1462,6 @@ func TestCLI_CreateDryRun(t *testing.T) { // TestCLI_CommentsListMisplacedSyntax ensures "bd comments list" gets a helpful error (GH#3542). func TestCLI_CommentsListMisplacedSyntax(t *testing.T) { - t.Parallel() - tmpDir := setupCLITestDB(t) stdout, stderr, err := runBDInProcessAllowError(t, tmpDir, "comments", "list") if err == nil { @@ -1740,8 +1701,6 @@ func TestCLI_CommentsAddShortID(t *testing.T) { // the plural sibling), AND — the part that actually matters for this // specific bug — no comment silently landed on an unrelated real issue. func TestCLI_CommentListMisplacedSyntax(t *testing.T) { - t.Parallel() - tmpDir := setupCLITestDB(t) out := runBDInProcess(t, tmpDir, "create", "Bystander issue", "-p", "1", "--json") @@ -1781,8 +1740,6 @@ func TestCLI_CommentListMisplacedSyntax(t *testing.T) { // "bd comment add ", which the same unguarded id-resolution bug // would parse as id="add", text=[, ...]. func TestCLI_CommentAddMisplacedSyntax(t *testing.T) { - t.Parallel() - tmpDir := setupCLITestDB(t) out := runBDInProcess(t, tmpDir, "create", "Bystander issue for add-typo", "-p", "1", "--json") @@ -1865,8 +1822,6 @@ func TestCLI_CommentMisplacedSyntaxRejectedInProxiedServerMode(t *testing.T) { // TEXT that happens to start with "list" or "add" must still work exactly // as before, since args[0] (the id slot) is what's checked, never the text. func TestCLI_CommentTextStartingWithReservedWordStillWorks(t *testing.T) { - t.Parallel() - tmpDir := setupCLITestDB(t) out := runBDInProcess(t, tmpDir, "create", "Issue for reserved-word-text test", "-p", "1", "--json") @@ -1905,11 +1860,8 @@ func TestCLI_CommentTextStartingWithReservedWordStillWorks(t *testing.T) { // This pins both halves: the command is rejected, AND — the part that // actually matters — no comment silently lands on an unrelated real issue. func TestCLI_CommentRmDeleteReservedWordsRejected(t *testing.T) { - t.Parallel() - for _, word := range []string{"rm", "delete"} { t.Run(word, func(t *testing.T) { - t.Parallel() tmpDir := setupCLITestDB(t) out := runBDInProcess(t, tmpDir, "create", "Bystander issue for "+word+"-typo", "-p", "1", "--json") @@ -1961,11 +1913,8 @@ func TestCLI_CommentRmDeleteReservedWordsRejected(t *testing.T) { // special-casing, unlike "comment"'s validateCommentArgs, so all four take // the same generic-message path and are equally worth covering here). func TestCLI_CommentsAddReservedWordRejectedThroughCobraDispatch(t *testing.T) { - t.Parallel() - for _, word := range []string{"list", "add", "rm", "delete"} { t.Run(word, func(t *testing.T) { - t.Parallel() tmpDir := setupCLITestDB(t) out := runBDInProcess(t, tmpDir, "create", "Bystander issue for comments-add "+word+"-typo", "-p", "1", "--json") @@ -2011,8 +1960,6 @@ func TestCLI_CommentsAddReservedWordRejectedThroughCobraDispatch(t *testing.T) { // abbreviation, so a truthful, distinguishing message matters here more than // most refusals. func TestCLI_CommentAbbreviatedIDRejectedWithTruthfulMessage(t *testing.T) { - t.Parallel() - tmpDir := setupCLITestDB(t) out := runBDInProcess(t, tmpDir, "create", "Needs an exact id on comment", "-p", "1", "--json") @@ -2094,7 +2041,11 @@ func TestCLI_CreateRejectsFlagLikeTitles(t *testing.T) { } // Attempt to create with a flag-like positional title - cmd := exec.Command(testBD, "create", tc.title) + // "--" ends flag parsing, so the title reaches create's own + // positional-title guard instead of cobra's flag parser + // (which answers --help with help and --foo-bar with + // "unknown flag"). + cmd := exec.Command(testBD, "create", "--", tc.title) cmd.Dir = tmpDir cmd.Env = os.Environ() out, err := cmd.CombinedOutput() @@ -2109,6 +2060,38 @@ func TestCLI_CreateRejectsFlagLikeTitles(t *testing.T) { }) } + // Without "--" cobra rejects an unknown flag-like word before create's + // guard runs; either way bd-2c0's point holds: no garbage issue. + t.Run("BareUnknownFlagCreatesNothing", func(t *testing.T) { + tmpDir := createTempDirWithCleanup(t) + initCmd := exec.Command(testBD, "init", "--prefix", "test", "--quiet") + initCmd.Dir = tmpDir + initCmd.Env = os.Environ() + if out, err := initCmd.CombinedOutput(); err != nil { + t.Fatalf("init failed: %v\n%s", err, out) + } + cmd := exec.Command(testBD, "create", "--foo-bar") + cmd.Dir = tmpDir + cmd.Env = os.Environ() + if out, err := cmd.CombinedOutput(); err == nil { + t.Fatalf("bd create --foo-bar succeeded:\n%s", out) + } + list := exec.Command(testBD, "list", "--json", "--all") + list.Dir = tmpDir + list.Env = os.Environ() + out, err := list.Output() + if err != nil { + t.Fatalf("bd list: %v\n%s", err, out) + } + var issues []map[string]any + if err := json.Unmarshal(out, &issues); err != nil { + t.Fatalf("bd list --json: %v\n%s", err, out) + } + if len(issues) != 0 { + t.Fatalf("bd create --foo-bar created %d issue(s): %s", len(issues), out) + } + }) + // Verify that --title flag with dash-prefixed value is still allowed t.Run("TitleFlagAllowsDashes", func(t *testing.T) { tmpDir := setupCLITestDB(t) @@ -2163,22 +2146,9 @@ func TestCLI_CreateRejectsEmptyTitle_ProxiedServerMode(t *testing.T) { t.Cleanup(func() { proxiedServerMode = origProxied }) proxiedServerMode = true - // createCmd is a shared package-level *cobra.Command, so a --title value - // set by an earlier in-process test invocation (e.g. TestCLI_CreateRejectsEmptyTitle's - // own FlagTab case) survives on the FlagSet across rootCmd.Execute() calls. - // Reset it explicitly so this test's outcome doesn't depend on suite - // ordering — a pre-existing gap, not something this test should also fall - // victim to. - titleFlag := createCmd.Flags().Lookup("title") - origTitleValue := titleFlag.Value.String() - origTitleChanged := titleFlag.Changed - t.Cleanup(func() { - _ = titleFlag.Value.Set(origTitleValue) - titleFlag.Changed = origTitleChanged - }) - _ = titleFlag.Value.Set("") - titleFlag.Changed = false - + // A --title left on createCmd by an earlier in-process run cannot leak + // in: runBDInProcessAllowError resets every flag first + // (resetCommandFlags). tmpDir := setupCLITestDB(t) stdout, stderr, err := runBDInProcessAllowError(t, tmpDir, "create", " ", "-p", "2") if err == nil { diff --git a/cmd/bd/cobra_flag_reset_test.go b/cmd/bd/cobra_flag_reset_test.go new file mode 100644 index 000000000..1c53dbbc3 --- /dev/null +++ b/cmd/bd/cobra_flag_reset_test.go @@ -0,0 +1,270 @@ +package main + +import ( + "os" + "reflect" + "strings" + "sync" + "testing" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +// In-process runners (runBDInProcess, runBDInProcessAllowError) execute the +// package-level rootCmd tree again and again in one process. cobra never +// resets a flag between Execute calls: a value parsed by one invocation +// (create's --title, --labels, list's --status, ...) and its Changed bit +// stay on the FlagSet, so the next invocation that does not pass the flag +// still sees it, and a slice flag that was set appends to the stale value +// instead of replacing it. resetCommandFlags puts every flag of the tree +// back to the state it had before any test ran. +// +// The baseline is captured in TestMain (snapshotCommandFlags), after every +// init() registered its flags and before any test executed a command, so it +// is the registered default even for custom flag.Value types whose DefValue +// string cannot be parsed back (closeReasonFlagValue, onceStringFlag, ...). + +type flagBaseline struct { + // value is a shallow copy of *flag.Value's pointee. For pflag's scalar + // types (type boolValue bool, ...) the pointee IS the bound variable, so + // copying it back restores the variable; for the slice types it restores + // the unexported "changed" bit that makes the next Set replace instead + // of append; for this package's custom values it restores their fields. + value reflect.Value + // slice holds a pflag.SliceValue's elements: the struct copy above only + // restores the pointer to the bound []string, not its contents. + slice []string + isSlice bool +} + +var ( + flagBaselineMu sync.Mutex + flagBaselines map[*pflag.Flag]flagBaseline +) + +// visitCommandFlags calls fn once for every flag defined anywhere in cmd's +// tree (local and persistent flag sets; a flag shared by both is visited +// once). +func visitCommandFlags(cmd *cobra.Command, fn func(*pflag.Flag)) { + seen := map[*pflag.Flag]bool{} + var walk func(*cobra.Command) + walk = func(c *cobra.Command) { + visit := func(f *pflag.Flag) { + if !seen[f] { + seen[f] = true + fn(f) + } + } + c.PersistentFlags().VisitAll(visit) + c.Flags().VisitAll(visit) + for _, sub := range c.Commands() { + walk(sub) + } + } + walk(cmd) +} + +// snapshotCommandFlags records the current state of every flag in cmd's +// tree as the baseline resetCommandFlags restores. Called once from +// TestMain; a second call replaces the baseline. +func snapshotCommandFlags(cmd *cobra.Command) { + baselines := map[*pflag.Flag]flagBaseline{} + visitCommandFlags(cmd, func(f *pflag.Flag) { + b := flagBaseline{} + if rv := reflect.ValueOf(f.Value); rv.Kind() == reflect.Pointer && !rv.IsNil() { + b.value = reflect.New(rv.Elem().Type()).Elem() + b.value.Set(rv.Elem()) + } + if sv, ok := f.Value.(pflag.SliceValue); ok { + b.isSlice = true + b.slice = append([]string(nil), sv.GetSlice()...) + } + baselines[f] = b + }) + flagBaselineMu.Lock() + flagBaselines = baselines + flagBaselineMu.Unlock() +} + +// resetCommandFlags returns every flag in cmd's tree to its baseline value +// with Changed=false. Flags bound to package globals (dbPath, actor, +// jsonOutput, readonlyMode, ...) are reset with them, so the runners +// overwrite any value a test assigned to such a global directly, before and +// after each run. Flags registered after the snapshot (cobra adds +// --help lazily) go back to their DefValue. +func resetCommandFlags(cmd *cobra.Command) { + flagBaselineMu.Lock() + baselines := flagBaselines + flagBaselineMu.Unlock() + visitCommandFlags(cmd, func(f *pflag.Flag) { + defer func() { f.Changed = false }() + b, ok := baselines[f] + if !ok { + if _, isSlice := f.Value.(pflag.SliceValue); !isSlice { + _ = f.Value.Set(f.DefValue) + } + return + } + if b.value.IsValid() { + reflect.ValueOf(f.Value).Elem().Set(b.value) + } + if b.isSlice { + _ = f.Value.(pflag.SliceValue).Replace(append([]string(nil), b.slice...)) + } + }) +} + +// Every in-process execution of the bd command tree (the runners above, and +// the many tests that call rootCmd.Execute directly) mutates process state +// that nothing restores: PersistentPreRun exports the selected workspace as +// BEADS_DIR and loads its .beads/.env, and records the workspace's storage +// mode in serverMode/proxiedServerMode. Left behind, a since-deleted +// fixture's BEADS_DIR sends a later test's workspace discovery to the wrong +// place ("no active beads workspace found", "migration sidecar is +// missing"), and a leaked proxiedServerMode routes later RunE-level tests +// down the proxied path ("proxied-server UOW provider not initialized", a +// nil-UOW panic in reopenProxiedResolve). installExecuteIsolation (called +// once from TestMain) registers a cobra initializer/finalizer pair that +// snapshots that state when an execution starts and puts it back when it +// ends, whatever the outcome. Executions can overlap (parallel tests that +// build their own commands): the outermost snapshot wins and is restored +// when the last one finishes. +var ( + executeIsolationMu sync.Mutex + executeIsolationDepth int + executeIsolationEnv []string + executeIsolationModes [2]bool + executeIsolationOnce sync.Once +) + +func installExecuteIsolation() { + executeIsolationOnce.Do(func() { + cobra.OnInitialize(func() { + executeIsolationMu.Lock() + defer executeIsolationMu.Unlock() + if executeIsolationDepth == 0 { + executeIsolationEnv = os.Environ() + executeIsolationModes = [2]bool{serverMode, proxiedServerMode} + } + executeIsolationDepth++ + }) + cobra.OnFinalize(func() { + executeIsolationMu.Lock() + defer executeIsolationMu.Unlock() + if executeIsolationDepth == 0 { + return + } + executeIsolationDepth-- + if executeIsolationDepth == 0 { + restoreProcessEnv(executeIsolationEnv) + serverMode, proxiedServerMode = executeIsolationModes[0], executeIsolationModes[1] + } + }) + }) +} + +// restoreProcessEnv puts the process environment back to before, a prior +// os.Environ(), touching only the keys that differ. +func restoreProcessEnv(before []string) { + want := make(map[string]string, len(before)) + for _, kv := range before { + if k, v, ok := strings.Cut(kv, "="); ok && k != "" { + want[k] = v + } + } + for _, kv := range os.Environ() { + k, v, ok := strings.Cut(kv, "=") + if !ok || k == "" { + continue + } + if old, had := want[k]; !had { + _ = os.Unsetenv(k) + } else if old != v { + _ = os.Setenv(k, old) + } + } + for k, v := range want { + if _, ok := os.LookupEnv(k); !ok { + _ = os.Setenv(k, v) + } + } +} + +// TestResetCommandFlagsRestoresEveryFlag sets every flag of the real bd +// command tree to a non-default value, resets, and checks each flag is back +// to its baseline: same String(), Changed=false, and (for slice flags) the +// next Set replaces rather than appends to the old value. +func TestResetCommandFlagsRestoresEveryFlag(t *testing.T) { + // Not parallel: no other top-level test runs while this one mutates the + // shared command tree. + t.Cleanup(func() { resetCommandFlags(rootCmd) }) + + type state struct { + value string + changed bool + } + before := map[*pflag.Flag]state{} + visitCommandFlags(rootCmd, func(f *pflag.Flag) { + before[f] = state{f.Value.String(), false} + }) + if len(before) < 100 { + t.Fatalf("visited only %d flags; the walk does not reach the command tree", len(before)) + } + + sample := map[string]string{ + "bool": "true", "int": "7", "int64": "7", "uint": "7", "float64": "1.5", + "duration": "3s", "string": "carried-over", "stringSlice": "carried-over", + "stringArray": "carried-over", "intSlice": "7", + } + visitCommandFlags(rootCmd, func(f *pflag.Flag) { + // resetCommandFlags restores a Value through its pointee; a + // non-pointer Value (or a map type, whose Set re-points the bound + // map) would need its own handling. + if reflect.ValueOf(f.Value).Kind() != reflect.Pointer { + t.Errorf("flag --%s: Value %T is not a pointer; resetCommandFlags cannot restore it", f.Name, f.Value) + } + v, ok := sample[f.Value.Type()] + if !ok { + t.Errorf("flag --%s: no sample value for type %q; add one so its reset is tested", f.Name, f.Value.Type()) + return + } + if err := f.Value.Set(v); err != nil { + t.Errorf("flag --%s (%s): Set(%q): %v; pick a sample it accepts so its reset is tested", f.Name, f.Value.Type(), v, err) + return + } + f.Changed = true + }) + + resetCommandFlags(rootCmd) + + visitCommandFlags(rootCmd, func(f *pflag.Flag) { + want, ok := before[f] + if !ok { + return + } + if got := f.Value.String(); got != want.value { + t.Errorf("flag --%s (%s): value after reset = %q, want %q", f.Name, f.Value.Type(), got, want.value) + } + if f.Changed { + t.Errorf("flag --%s: Changed still true after reset", f.Name) + } + }) + + // A reset slice flag must behave like a fresh one: the first Set + // replaces the default instead of appending to it. + labels := createCmd.Flags().Lookup("labels") + if labels == nil { + t.Fatal("create --labels not registered") + } + if err := labels.Value.Set("a"); err != nil { + t.Fatal(err) + } + resetCommandFlags(rootCmd) + if err := labels.Value.Set("b"); err != nil { + t.Fatal(err) + } + if got := labels.Value.String(); got != "[b]" { + t.Errorf("create --labels after reset+Set(b) = %q, want [b]", got) + } +} diff --git a/cmd/bd/completions_test.go b/cmd/bd/completions_test.go index debc87e60..6d8605302 100644 --- a/cmd/bd/completions_test.go +++ b/cmd/bd/completions_test.go @@ -17,6 +17,7 @@ import ( ) func TestIssueIDCompletion(t *testing.T) { + saveAndRestoreGlobals(t) // Save original store and restore after test originalStore := store originalRootCtx := rootCtx @@ -34,6 +35,11 @@ func TestIssueIDCompletion(t *testing.T) { testDB := filepath.Join(tmpDir, "test.db") testStore := newTestStoreWithPrefix(t, testDB, "bd") store = testStore + // issueIDCompletion resolves a database path before it looks at the + // open store (an unresolvable path means no completions), as the CLI + // does; point it at this store's (dbPath is restored by + // saveAndRestoreGlobals above). + dbPath = testDB // Create test issues now := time.Now() @@ -268,7 +274,10 @@ func TestIssueIDCompletion_UsesWorktreeFallbackWhenStoreNil(t *testing.T) { }) testDB := filepath.Join(mainRepoDir, ".beads", "beads.db") - testStore := newTestStoreWithPrefix(t, testDB, "wt") + // Its own database, not a branch of the shared one: completion reopens + // the store from .beads/metadata.json, which can name a database but not + // a test branch. + testStore := newTestStoreIsolatedDB(t, testDB, "wt") if err := testStore.CreateIssue(ctx, &types.Issue{ ID: "wt-abc1", Title: "Worktree completion target", diff --git a/cmd/bd/config_test.go b/cmd/bd/config_test.go index 2713083a7..03435d498 100644 --- a/cmd/bd/config_test.go +++ b/cmd/bd/config_test.go @@ -206,34 +206,13 @@ func TestYamlOnlyConfigWithoutDatabase(t *testing.T) { } } -// setupTestDB creates a temporary test database +// setupTestDB creates a test store on the suite's Dolt test server +// (skipped when it is not running). The store is closed by t.Cleanup; the +// returned cleanup is kept for the callers' defer and does nothing more. func setupTestDB(t *testing.T) (*dolt.DoltStore, func()) { - tmpDir, err := os.MkdirTemp("", "bd-test-config-*") - if err != nil { - t.Fatalf("Failed to create temp dir: %v", err) - } - - testDB := filepath.Join(tmpDir, "test.db") - store, err := dolt.New(context.Background(), &dolt.Config{Path: testDB}) - if err != nil { - os.RemoveAll(tmpDir) - t.Skipf("skipping: Dolt server not available: %v", err) - } - - // CRITICAL (bd-166): Set issue_prefix to prevent "database not initialized" errors - ctx := context.Background() - if err := store.SetConfig(ctx, "issue_prefix", "bd"); err != nil { - store.Close() - os.RemoveAll(tmpDir) - t.Fatalf("Failed to set issue_prefix: %v", err) - } - - cleanup := func() { - store.Close() - os.RemoveAll(tmpDir) - } - - return store, cleanup + t.Helper() + store := newTestStoreWithPrefix(t, filepath.Join(t.TempDir(), "test.db"), "bd") + return store, func() {} } // TestBeadsRoleGitConfig verifies that beads.role is stored in git config, @@ -874,11 +853,17 @@ func TestCustomStatusConfig(t *testing.T) { if len(detailed) != 2 { t.Fatalf("expected 2 statuses, got %d", len(detailed)) } - if detailed[0].Category != types.CategoryActive { - t.Errorf("review should be active, got %q", detailed[0].Category) + // The custom_statuses table is read back in name order, not + // config order: look each status up by name. + categories := map[string]types.StatusCategory{} + for _, d := range detailed { + categories[d.Name] = d.Category } - if detailed[1].Category != types.CategoryUnspecified { - t.Errorf("legacy should be unspecified, got %q", detailed[1].Category) + if got := categories["review"]; got != types.CategoryActive { + t.Errorf("review should be active, got %q", got) + } + if got, ok := categories["legacy"]; !ok || got != types.CategoryUnspecified { + t.Errorf("legacy should be unspecified, got %q (present=%v)", got, ok) } }) @@ -894,7 +879,10 @@ func TestCustomStatusConfig(t *testing.T) { if len(names) != 3 { t.Fatalf("expected 3 names, got %d", len(names)) } - want := []string{"review", "testing", "qa"} + // Name order: since the normalized custom_statuses table (#2961, + // 8537d943b) statuses are read back ORDER BY name; the table has + // no ordinal column, and custom_types reads the same way. + want := []string{"qa", "review", "testing"} for i, name := range names { if name != want[i] { t.Errorf("name[%d] = %q, want %q", i, name, want[i]) diff --git a/cmd/bd/delete_test.go b/cmd/bd/delete_test.go index d4358116a..76d24dac4 100644 --- a/cmd/bd/delete_test.go +++ b/cmd/bd/delete_test.go @@ -154,14 +154,16 @@ func TestBulkDeleteNoResurrection(t *testing.T) { t.Errorf("After delete: expected %d issues in DB, got %d", expectedRemaining, stats.TotalIssues) } + // GetIssue reports a missing issue as storage.ErrNotFound. for _, id := range toDelete { issue, err := s.GetIssue(ctx, id) + if errors.Is(err, storage.ErrNotFound) { + continue + } if err != nil { t.Fatalf("GetIssue failed for %s: %v", id, err) } - if issue != nil { - t.Errorf("Deleted issue %s was resurrected!", id) - } + t.Errorf("Deleted issue %s was resurrected: %+v", id, issue) } } diff --git a/cmd/bd/doctor_health_integration_test.go b/cmd/bd/doctor_health_integration_test.go index b6e495500..11b4bd9f5 100644 --- a/cmd/bd/doctor_health_integration_test.go +++ b/cmd/bd/doctor_health_integration_test.go @@ -27,9 +27,12 @@ func TestDoctorCheckHealthReportsVersionMismatchOnRepoLocalPort(t *testing.T) { t.Skip("skipping: Dolt test container not available") } - env := append(os.Environ(), "BEADS_TEST_MODE=1") + // A repo-local server of its own (bd init --server, see + // repoLocalServerEnv), so bd derives the repo-local port this test is + // about instead of reusing the shared test server's. + env := repoLocalServerEnv(t, tmpDir) - initOut, initErr := runBDExecAllowErrorWithEnv(t, tmpDir, env, "init", "--backend", "dolt", "--prefix", "test", "--quiet") + initOut, initErr := runBDExecAllowErrorWithEnv(t, tmpDir, env, "init", "--backend", "dolt", "--server", "--prefix", "test", "--quiet") if initErr != nil { lower := strings.ToLower(initOut) if strings.Contains(lower, "dolt") && (strings.Contains(lower, "not supported") || strings.Contains(lower, "not available") || strings.Contains(lower, "unknown")) { diff --git a/cmd/bd/doctor_max_rows_test.go b/cmd/bd/doctor_max_rows_test.go index 1972c16a0..53a34915e 100644 --- a/cmd/bd/doctor_max_rows_test.go +++ b/cmd/bd/doctor_max_rows_test.go @@ -59,7 +59,7 @@ func TestDoctorConventionsMaxRows_EnvOnly_Exits2(t *testing.T) { } // BEADS_MAX_ROWS=3 with 6 open issues → cap fires → exit 2. - out, code := bdRunRaw(t, bdBin, tmpDir, []string{"BEADS_MAX_ROWS=3"}, "doctor", "--check=conventions") + out, code := bdRunRaw(t, bdBin, tmpDir, doctorMaxRowsEnv("BEADS_MAX_ROWS=3"), "doctor", "--check=conventions") if code != 2 { t.Fatalf("be-pc8c: expected exit 2 (cap exceeded), got %d\n%s", code, out) } @@ -97,7 +97,7 @@ func TestDoctorPollutionMaxRows_EnvOnly_Exits2(t *testing.T) { } } - out, code := bdRunRaw(t, bdBin, tmpDir, []string{"BEADS_MAX_ROWS=3"}, "doctor", "--check=pollution") + out, code := bdRunRaw(t, bdBin, tmpDir, doctorMaxRowsEnv("BEADS_MAX_ROWS=3"), "doctor", "--check=pollution") if code != 2 { t.Fatalf("be-pc8c: expected exit 2 (cap exceeded), got %d\n%s", code, out) } @@ -105,3 +105,13 @@ func TestDoctorPollutionMaxRows_EnvOnly_Exits2(t *testing.T) { t.Errorf("be-pc8c: stderr missing BEADS_MAX_ROWS=3 source attribution:\n%s", out) } } + +// doctorMaxRowsEnv is the subprocess env for the doctor max-rows tests on +// top of bdRunRaw's bdEnv, which drops every BEADS_* variable: the +// workspace's database is a testdb_* on the suite's dedicated test server, +// which dolt.New's test-database firewall opens only with BEADS_TEST_SERVER=1 +// (else "refusing to connect test database ... set BEADS_TEST_SERVER=1" and +// exit 1 before the cap is ever checked). +func doctorMaxRowsEnv(extra ...string) []string { + return append([]string{"BEADS_TEST_SERVER=1"}, extra...) +} diff --git a/cmd/bd/doctor_validate_test.go b/cmd/bd/doctor_validate_test.go index 5e3281cc4..e66c5cf5a 100644 --- a/cmd/bd/doctor_validate_test.go +++ b/cmd/bd/doctor_validate_test.go @@ -57,8 +57,10 @@ func TestValidateCheck_AllClean(t *testing.T) { t.Errorf("%s: status = %q, want %q (message: %s)", cr.check.Name, cr.check.Status, statusOK, cr.check.Message) } } - if len(checks) != 4 { - t.Errorf("Expected 4 checks, got %d", len(checks)) + // Cross-table duplicates, duplicate issues, orphaned dependencies, test + // pollution, git conflicts. + if len(checks) != 5 { + t.Errorf("Expected 5 checks, got %d", len(checks)) } } diff --git a/cmd/bd/dolt_autocommit_integration_test.go b/cmd/bd/dolt_autocommit_integration_test.go index d87030405..9003524ab 100644 --- a/cmd/bd/dolt_autocommit_integration_test.go +++ b/cmd/bd/dolt_autocommit_integration_test.go @@ -72,35 +72,38 @@ func runCommandInDirCombinedOutput(dir string, name string, args ...string) (str func findDoltRepoDir(t *testing.T, dir string) string { t.Helper() - // Embedded driver may create either: - // - a dolt repo directly at .beads/dolt/ - // - a dolt environment at .beads/dolt/ with a db subdir containing .dolt/ - base := filepath.Join(dir, ".beads", "dolt") - candidates := []string{ - base, - filepath.Join(base, "beads"), - } - for _, c := range candidates { - if _, err := os.Stat(filepath.Join(c, ".dolt")); err == nil { - return c + // bd init defaults to the embedded engine, whose data lives under + // .beads/embeddeddolt//; a server-mode workspace keeps it under + // .beads/dolt/ (a dolt repo there directly, or a db subdir with .dolt/). + var bases []string + for _, name := range []string{"embeddeddolt", "dolt"} { + base := filepath.Join(dir, ".beads", name) + bases = append(bases, base) + for _, c := range []string{base, filepath.Join(base, "beads")} { + if _, err := os.Stat(filepath.Join(c, ".dolt")); err == nil { + return c + } } } var found string - _ = filepath.WalkDir(base, func(path string, d fs.DirEntry, err error) error { - if err != nil { + for _, base := range bases { + _ = filepath.WalkDir(base, func(path string, d fs.DirEntry, err error) error { + if err != nil || found != "" { + return nil + } + if d.IsDir() && d.Name() == ".dolt" { + found = filepath.Dir(path) + return fs.SkipDir + } return nil + }) + if found != "" { + return found } - if d.IsDir() && d.Name() == ".dolt" { - found = filepath.Dir(path) - return fs.SkipDir - } - return nil - }) - if found == "" { - t.Fatalf("could not find Dolt repo dir under %s", base) } - return found + t.Fatalf("could not find Dolt repo dir under %v", bases) + return "" } func doltHeadAuthor(t *testing.T, dir string) string { diff --git a/cmd/bd/dolt_autostart_lifecycle_integration_test.go b/cmd/bd/dolt_autostart_lifecycle_integration_test.go index f2812f5e9..ae38b967c 100644 --- a/cmd/bd/dolt_autostart_lifecycle_integration_test.go +++ b/cmd/bd/dolt_autostart_lifecycle_integration_test.go @@ -12,18 +12,24 @@ import ( "strings" "testing" "time" + + "github.com/steveyegge/beads/internal/testutil" ) func TestE2E_AutoStartedRepoLocalServerPersistsAcrossCommands(t *testing.T) { - if !usesSQLServer() { - t.Skip("skipping: bd dolt status not supported in embedded mode") - } + // Every bd here is a subprocess on a workspace initialized with + // --server, so this process's own storage mode (embedded by default) + // does not matter; the subprocess needs the dolt binary. + testutil.RequireDoltBinary(t) if runtime.GOOS == windowsOS { t.Skip("repo-local dolt lifecycle integration test not supported on windows") } bdBinary := buildLifecycleTestBinary(t) tmpDir := t.TempDir() + // Stops the auto-started repo-local server if an assertion fails + // before the test's own final stop. + stopDoltServerCleanup(t, filepath.Join(tmpDir, ".beads")) if err := runCommandInDir(tmpDir, "git", "init"); err != nil { t.Fatalf("git init failed: %v", err) } diff --git a/cmd/bd/dolt_metadata_e2e_test.go b/cmd/bd/dolt_metadata_e2e_test.go index e3c8e2867..9067b38ad 100644 --- a/cmd/bd/dolt_metadata_e2e_test.go +++ b/cmd/bd/dolt_metadata_e2e_test.go @@ -91,12 +91,10 @@ func TestE2E_DoctorFixMetadataRoundtrip(t *testing.T) { _ = runCommandInDir(tmpDir, "git", "config", "user.name", "Test User") _ = runCommandInDir(tmpDir, "git", "config", "remote.origin.url", "https://github.com/test/repo.git") - env := append(os.Environ(), - "BEADS_TEST_MODE=1", - ) + env := repoLocalServerEnv(t, tmpDir) // Init dolt backend (which now writes metadata via Phase 1) - initOut, initErr := runBDExecAllowErrorWithEnv(t, tmpDir, env, "init", "--backend", "dolt", "--prefix", "test", "--quiet") + initOut, initErr := runBDExecAllowErrorWithEnv(t, tmpDir, env, "init", "--backend", "dolt", "--server", "--prefix", "test", "--quiet") if initErr != nil { lower := strings.ToLower(initOut) if strings.Contains(lower, "dolt") && (strings.Contains(lower, "not supported") || strings.Contains(lower, "not available") || strings.Contains(lower, "unknown")) { @@ -105,6 +103,12 @@ func TestE2E_DoctorFixMetadataRoundtrip(t *testing.T) { t.Fatalf("bd init --backend dolt failed: %v\n%s", initErr, initOut) } + // BEADS_TEST_MODE=1 never auto-starts a server (dolt.New pins an + // unresolved port to 1, an immediate failure), so start it explicitly. + if out, err := runBDExecAllowErrorWithEnv(t, tmpDir, env, "dolt", "start"); err != nil { + t.Fatalf("bd dolt start failed: %v\n%s", err, out) + } + // Delete metadata to simulate a pre-Phase-1 database // bd_version is now in local_metadata (dolt-ignored), repo_id/clone_id remain in metadata sqlOut, sqlErr := runBDExecAllowErrorWithEnv(t, tmpDir, env, "sql", @@ -161,12 +165,10 @@ func TestE2E_MigrateDoltMetadata(t *testing.T) { _ = runCommandInDir(tmpDir, "git", "config", "user.name", "Test User") _ = runCommandInDir(tmpDir, "git", "config", "remote.origin.url", "https://github.com/test/repo.git") - env := append(os.Environ(), - "BEADS_TEST_MODE=1", - ) + env := repoLocalServerEnv(t, tmpDir) // Init dolt backend (writes all metadata via Phase 1) - initOut, initErr := runBDExecAllowErrorWithEnv(t, tmpDir, env, "init", "--backend", "dolt", "--prefix", "test", "--quiet") + initOut, initErr := runBDExecAllowErrorWithEnv(t, tmpDir, env, "init", "--backend", "dolt", "--server", "--prefix", "test", "--quiet") if initErr != nil { lower := strings.ToLower(initOut) if strings.Contains(lower, "dolt") && (strings.Contains(lower, "not supported") || strings.Contains(lower, "not available") || strings.Contains(lower, "unknown")) { @@ -175,10 +177,16 @@ func TestE2E_MigrateDoltMetadata(t *testing.T) { t.Fatalf("bd init --backend dolt failed: %v\n%s", initErr, initOut) } + // BEADS_TEST_MODE=1 never auto-starts a server (dolt.New pins an + // unresolved port to 1, an immediate failure), so start it explicitly. + if out, err := runBDExecAllowErrorWithEnv(t, tmpDir, env, "dolt", "start"); err != nil { + t.Fatalf("bd dolt start failed: %v\n%s", err, out) + } + // Delete repo_id and clone_id to simulate a pre-Phase-3 database // (bd_version is set by init, but identity fields are missing) sqlOut, sqlErr := runBDExecAllowErrorWithEnv(t, tmpDir, env, "sql", - "DELETE FROM metadata WHERE key IN ('repo_id', 'clone_id')") + "DELETE FROM metadata WHERE `key` IN ('repo_id', 'clone_id')") if sqlErr != nil { t.Fatalf("bd sql DELETE failed: %v\n%s", sqlErr, sqlOut) } @@ -209,3 +217,23 @@ func TestE2E_MigrateDoltMetadata(t *testing.T) { } } } + +// repoLocalServerEnv is the environment for a subprocess bd that runs a +// repo-local dolt sql-server of its own under dir/.beads (bd init --server): +// bd init now defaults to embedded mode, where `bd sql` and `bd dolt start` +// are refused, so tests that need SQL access to the workspace's database +// init with --server. The suite's BEADS_DOLT_PORT / BEADS_DOLT_SERVER_PORT +// point at the shared test server and are cleared, so bd starts its own +// server on a port it derives (never the shared server's, never a fixed +// default), and the server is stopped when the test ends. BEADS_TEST_MODE=1 +// never auto-starts it: run `bd dolt start` after init. +func repoLocalServerEnv(t *testing.T, dir string) []string { + t.Helper() + stopDoltServerCleanup(t, filepath.Join(dir, ".beads")) + return append(os.Environ(), + "BEADS_TEST_MODE=1", + "BEADS_DOLT_PORT=", + "BEADS_DOLT_SERVER_PORT=", + "BEADS_DOLT_SHARED_SERVER=", + ) +} diff --git a/cmd/bd/dolt_test.go b/cmd/bd/dolt_test.go index 0c085ae4b..c2c8e8d83 100644 --- a/cmd/bd/dolt_test.go +++ b/cmd/bd/dolt_test.go @@ -283,7 +283,9 @@ func TestDoltSetConfigValidation(t *testing.T) { func TestDoltSetConfigJSONOutput(t *testing.T) { tmpDir := t.TempDir() beadsDir := filepath.Join(tmpDir, ".beads") - if err := os.MkdirAll(beadsDir, 0755); err != nil { + // 0700: bd warns about a group/world-readable .beads, and the warning + // would land in the JSON output under test. + if err := os.MkdirAll(beadsDir, 0o700); err != nil { t.Fatalf("failed to create .beads dir: %v", err) } @@ -315,7 +317,7 @@ func TestDoltSetConfigJSONOutput(t *testing.T) { var result map[string]any if err := json.Unmarshal([]byte(output), &result); err != nil { - t.Skipf("output not pure JSON: %s", output) + t.Fatalf("output not pure JSON: %s", output) } if result["key"] != "database" { @@ -332,7 +334,9 @@ func TestDoltSetConfigJSONOutput(t *testing.T) { func TestDoltSetConfigWithUpdateConfig(t *testing.T) { tmpDir := t.TempDir() beadsDir := filepath.Join(tmpDir, ".beads") - if err := os.MkdirAll(beadsDir, 0755); err != nil { + // 0700: bd warns about a group/world-readable .beads, and the warning + // would land in the JSON output under test. + if err := os.MkdirAll(beadsDir, 0o700); err != nil { t.Fatalf("failed to create .beads dir: %v", err) } @@ -371,7 +375,7 @@ func TestDoltSetConfigWithUpdateConfig(t *testing.T) { var result map[string]any if err := json.Unmarshal([]byte(output), &result); err != nil { - t.Skipf("output not pure JSON: %s", output) + t.Fatalf("output not pure JSON: %s", output) } if result["config_yaml_updated"] != true { @@ -399,11 +403,11 @@ func TestTestServerConnection(t *testing.T) { }) t.Run("localhost with unlikely port", func(t *testing.T) { - // Clear test server port override so GetDoltServerPort() returns 59999 + // Clear the test server port override so the config's port is used. t.Setenv("BEADS_DOLT_SERVER_PORT", "") cfg := configfile.DefaultConfig() cfg.DoltServerHost = "127.0.0.1" - cfg.DoltServerPort = 59999 // Unlikely to be in use + cfg.DoltServerPort = closedLoopbackPort(t) result := testServerConnection(cfg.DoltServerHost, cfg.DoltServerPort) if result { @@ -493,8 +497,7 @@ func TestDoltConfigEnvironmentOverrides(t *testing.T) { // Only database, host, port, user support env overrides t.Run("BEADS_DOLT_SERVER_DATABASE overrides", func(t *testing.T) { - os.Setenv("BEADS_DOLT_SERVER_DATABASE", "envdb") - defer os.Unsetenv("BEADS_DOLT_SERVER_DATABASE") + t.Setenv("BEADS_DOLT_SERVER_DATABASE", "envdb") if cfg.GetDoltDatabase() != "envdb" { t.Errorf("expected env override to 'envdb', got %s", cfg.GetDoltDatabase()) @@ -502,8 +505,7 @@ func TestDoltConfigEnvironmentOverrides(t *testing.T) { }) t.Run("BEADS_DOLT_SERVER_HOST overrides", func(t *testing.T) { - os.Setenv("BEADS_DOLT_SERVER_HOST", "envhost") - defer os.Unsetenv("BEADS_DOLT_SERVER_HOST") + t.Setenv("BEADS_DOLT_SERVER_HOST", "envhost") if cfg.GetDoltServerHost() != "envhost" { t.Errorf("expected env override to 'envhost', got %s", cfg.GetDoltServerHost()) @@ -511,8 +513,7 @@ func TestDoltConfigEnvironmentOverrides(t *testing.T) { }) t.Run("BEADS_DOLT_SERVER_PORT overrides", func(t *testing.T) { - os.Setenv("BEADS_DOLT_SERVER_PORT", "9999") - defer os.Unsetenv("BEADS_DOLT_SERVER_PORT") + t.Setenv("BEADS_DOLT_SERVER_PORT", "9999") if cfg.GetDoltServerPort() != 9999 { t.Errorf("expected env override to 9999, got %d", cfg.GetDoltServerPort()) @@ -520,8 +521,7 @@ func TestDoltConfigEnvironmentOverrides(t *testing.T) { }) t.Run("BEADS_DOLT_SERVER_USER overrides", func(t *testing.T) { - os.Setenv("BEADS_DOLT_SERVER_USER", "envuser") - defer os.Unsetenv("BEADS_DOLT_SERVER_USER") + t.Setenv("BEADS_DOLT_SERVER_USER", "envuser") if cfg.GetDoltServerUser() != "envuser" { t.Errorf("expected env override to 'envuser', got %s", cfg.GetDoltServerUser()) diff --git a/cmd/bd/export_auto.go b/cmd/bd/export_auto.go index d366f8d52..95f8ca705 100644 --- a/cmd/bd/export_auto.go +++ b/cmd/bd/export_auto.go @@ -28,8 +28,9 @@ import ( // incrementalExportThreshold caps the number of changed issue IDs we'll // incrementally re-encode before falling back to a full export. At high // change counts the per-issue SQL work (bulk loaders × changed set) stops -// being cheaper than one `SearchIssues(Limit:0)` sweep. -const incrementalExportThreshold = 5000 +// being cheaper than one `SearchIssues(Limit:0)` sweep. A var only so a +// test can exercise the fallback without seeding 5000 issues. +var incrementalExportThreshold = 5000 // doltWorkingRef is the literal dolt_diff() accepts as an endpoint meaning // "the current working set", including uncommitted writes. Passing this diff --git a/cmd/bd/export_auto_test.go b/cmd/bd/export_auto_test.go index b24596cbd..097addcf3 100644 --- a/cmd/bd/export_auto_test.go +++ b/cmd/bd/export_auto_test.go @@ -1801,6 +1801,14 @@ func TestTryIncrementalExport_FallsBackWhenFileMissing(t *testing.T) { } func TestTryIncrementalExport_ThresholdExceededFallsBack(t *testing.T) { + // The behavior under test is "more changes than the threshold -> full + // export fallback", not the production value: seeding 5001 issues took + // minutes (per-issue statements, under -race). Not parallel, so no other + // test reads the threshold meanwhile. + origThreshold := incrementalExportThreshold + incrementalExportThreshold = 20 + t.Cleanup(func() { incrementalExportThreshold = origThreshold }) + h, ctx := setupIncrementalExportTestWithReadTimeout(t, bulkSeedPoolReadTimeout) // Seed one issue so the file exists; baseline commit. diff --git a/cmd/bd/fs_adapters.go b/cmd/bd/fs_adapters.go index fa14070ee..e2ecd96aa 100644 --- a/cmd/bd/fs_adapters.go +++ b/cmd/bd/fs_adapters.go @@ -32,8 +32,14 @@ func newFileSystemAdapters() domain.BeadsDirFSAdapters { AddAgentsInstructions: func(p domain.AgentsFileParams) { addAgentsInstructions(p.File, p.Verbose, p.TemplatePath, agents.Profile(p.Profile), agents.RenderOpts{HasRemote: p.HasRemote, NoPush: p.NoPush}) }, - InstallClaudeProject: setup.InstallClaudeProject, - SetYAMLConfig: config.SetYamlConfig, + InstallClaudeProject: func(stealth, quiet bool) error { + out := setup.ProjectInstallOutput{} + if quiet { + out = setup.QuietProjectInstallOutput() + } + return setup.InstallClaudeProjectTo(stealth, out) + }, + SetYAMLConfig: config.SetYamlConfig, } } diff --git a/cmd/bd/init.go b/cmd/bd/init.go index c57f26478..8e3cf7435 100644 --- a/cmd/bd/init.go +++ b/cmd/bd/init.go @@ -2126,22 +2126,23 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1): // Auto-setup Claude hooks, Codex, and Cursor project integration. Skip in // stealth mode or when agents are skipped. if !stealth && !skipAgents && !isBareGitRepo() { - if err := setup.InstallClaudeProject(stealth); err != nil { - if !quiet { - fmt.Fprintf(os.Stderr, "Warning: failed to setup Claude hooks: %v\n", err) - } + // --quiet drops the installers' progress like every other init + // message; failures still reach stderr (the installers' own + // error lines and the warnings below), quiet or not. + installOut := setup.ProjectInstallOutput{} + if quiet { + installOut = setup.QuietProjectInstallOutput() + } + if err := setup.InstallClaudeProjectTo(stealth, installOut); err != nil { + fmt.Fprintf(os.Stderr, "Warning: failed to setup Claude hooks: %v\n", err) // Non-fatal - continue with init } - if err := setup.InstallCodexProject(); err != nil { - if !quiet { - fmt.Fprintf(os.Stderr, "Warning: failed to setup Codex integration: %v\n", err) - } + if err := setup.InstallCodexProjectTo(installOut); err != nil { + fmt.Fprintf(os.Stderr, "Warning: failed to setup Codex integration: %v\n", err) // Non-fatal - continue with init } - if err := setup.InstallCursorProject(); err != nil { - if !quiet { - fmt.Fprintf(os.Stderr, "Warning: failed to setup Cursor integration: %v\n", err) - } + if err := setup.InstallCursorProjectTo(installOut); err != nil { + fmt.Fprintf(os.Stderr, "Warning: failed to setup Cursor integration: %v\n", err) // Non-fatal - continue with init } } diff --git a/cmd/bd/init_embedded_test.go b/cmd/bd/init_embedded_test.go index 730d2e6bd..cfc9d0c8a 100644 --- a/cmd/bd/init_embedded_test.go +++ b/cmd/bd/init_embedded_test.go @@ -369,6 +369,14 @@ func TestEmbeddedInitA(t *testing.T) { if strings.Contains(out, "bd initialized") { t.Error("--quiet should suppress success message") } + // The project installers (Claude, Codex, Cursor) run here (agents + // are not skipped); --quiet drops their progress too. + requireFile(t, filepath.Join(dir, ".claude", "settings.json")) + for _, chatter := range []string{"Installing Claude hooks", "Beads agent skill installed", "Cursor integration installed"} { + if strings.Contains(out, chatter) { + t.Errorf("--quiet should suppress installer output %q, got:\n%s", chatter, out) + } + } // bd_version is in local_metadata (dolt-ignored), not metadata func() { diff --git a/cmd/bd/init_proxied_server.go b/cmd/bd/init_proxied_server.go index 05b3c8821..e52944a66 100644 --- a/cmd/bd/init_proxied_server.go +++ b/cmd/bd/init_proxied_server.go @@ -648,7 +648,9 @@ func runInitProxiedServerTail(cmd *cobra.Command, ctx context.Context, in initPr HasRemote: t.remoteURL != "", NoPush: config.GetBool("no-push"), }) - if err := t.fsUseCase.InstallClaudeProject(ctx, in.stealth); err != nil && !in.quiet { + // As in direct init: --quiet drops the installer's progress, + // a failure still reaches stderr. + if err := t.fsUseCase.InstallClaudeProject(ctx, in.stealth, in.quiet); err != nil { fmt.Fprintf(os.Stderr, "Warning: failed to setup Claude hooks: %v\n", err) } } diff --git a/cmd/bd/init_test.go b/cmd/bd/init_test.go index 2b264d52a..8ba943d05 100644 --- a/cmd/bd/init_test.go +++ b/cmd/bd/init_test.go @@ -11,6 +11,7 @@ import ( "os/exec" "path/filepath" "runtime" + "strconv" "strings" "testing" @@ -20,6 +21,7 @@ import ( "github.com/steveyegge/beads/internal/storage/dolt" "github.com/steveyegge/beads/internal/testutil" "github.com/steveyegge/beads/internal/utils" + "gopkg.in/yaml.v3" ) // skipIfNoDolt skips the test when no Dolt server is available. @@ -34,6 +36,7 @@ func skipIfNoDolt(t *testing.T) { func TestInitCommand(t *testing.T) { skipIfNoDolt(t) + resetInitFlagsForServerInit(t) tests := []struct { name string prefix string @@ -100,7 +103,8 @@ func TestInitCommand(t *testing.T) { }() // Build command arguments - args := []string{"init"} + // A database of its own: the suite shares one test server. + args := []string{"init", "--server", "--database", uniqueTestDBName(t)} if tt.prefix != "" { args = append(args, "--prefix", tt.prefix) } @@ -223,6 +227,7 @@ func TestInitTargetsRequestedProjectBelowOSTempRoot(t *testing.T) { func TestInitAlreadyInitialized(t *testing.T) { skipIfNoDolt(t) + resetInitFlagsForServerInit(t) // Reset global state origDBPath := dbPath defer func() { dbPath = origDBPath }() @@ -232,7 +237,10 @@ func TestInitAlreadyInitialized(t *testing.T) { t.Chdir(tmpDir) // Initialize once - rootCmd.SetArgs([]string{"init", "--prefix", "test", "--quiet"}) + // A database of its own: the suite shares one test server, where a + // "test" database another test created belongs to another project. + database := uniqueTestDBName(t) + rootCmd.SetArgs([]string{"init", "--server", "--database", database, "--prefix", "test", "--quiet"}) if err := rootCmd.Execute(); err != nil { t.Fatalf("First init failed: %v", err) @@ -240,7 +248,7 @@ func TestInitAlreadyInitialized(t *testing.T) { // Initialize again with same prefix and --force flag (bd-emg: safety guard) // Without --force, init should refuse when database already exists - rootCmd.SetArgs([]string{"init", "--prefix", "test", "--quiet", "--force"}) + rootCmd.SetArgs([]string{"init", "--server", "--database", database, "--prefix", "test", "--quiet", "--force"}) if err := rootCmd.Execute(); err != nil { t.Fatalf("Second init with --force failed: %v", err) @@ -1166,6 +1174,7 @@ func TestInitNonInteractiveAlwaysSetsRole(t *testing.T) { // TestInitRedirect groups redirect-related init tests. func TestInitRedirect(t *testing.T) { skipIfNoDolt(t) + resetInitFlagsForServerInit(t) resetRedirectState := func(t *testing.T) { t.Helper() origDBPath := dbPath @@ -1204,6 +1213,12 @@ func TestInitRedirect(t *testing.T) { if err := os.MkdirAll(targetBeadsDir, 0755); err != nil { t.Fatal(err) } + // A provisioned canonical workspace (config.yaml, no database yet): + // bd follows a redirect only to a target that holds project files, + // and ignores one to a bare directory (gastownhall/beads#4692). + if err := os.WriteFile(filepath.Join(targetBeadsDir, "config.yaml"), []byte("# provisioned canonical workspace\n"), 0644); err != nil { + t.Fatal(err) + } redirectPath := filepath.Join(localBeadsDir, beads.RedirectFileName) if err := os.WriteFile(redirectPath, []byte("../canonical/.beads\n"), 0644); err != nil { @@ -1212,7 +1227,7 @@ func TestInitRedirect(t *testing.T) { t.Chdir(projectDir) - rootCmd.SetArgs([]string{"init", "--prefix", "redirect-test", "--quiet"}) + rootCmd.SetArgs([]string{"init", "--server", "--database", uniqueTestDBName(t), "--prefix", "redirect-test", "--quiet"}) if err := rootCmd.Execute(); err != nil { t.Fatalf("Init with redirect failed: %v", err) } @@ -1307,6 +1322,7 @@ func TestInitRedirect(t *testing.T) { // Tests requirements FR-001, FR-002, FR-004, NFR-001. func TestInitBEADS_DIR(t *testing.T) { skipIfNoDolt(t) + resetInitFlagsForServerInit(t) // resetBeadsDirState resets global state and env vars for each subtest. resetBeadsDirState := func(t *testing.T) { t.Helper() @@ -1422,7 +1438,7 @@ func TestInitBEADS_DIR(t *testing.T) { os.MkdirAll(cwdPath, 0755) t.Chdir(cwdPath) - rootCmd.SetArgs([]string{"init", "--prefix", "beadsdir-test", "--quiet"}) + rootCmd.SetArgs([]string{"init", "--server", "--database", uniqueTestDBName(t), "--prefix", "beadsdir-test", "--quiet"}) if err := rootCmd.Execute(); err != nil { t.Fatalf("Init with BEADS_DIR failed: %v", err) } @@ -1466,7 +1482,7 @@ func TestInitBEADS_DIR(t *testing.T) { tmpDir := t.TempDir() t.Chdir(tmpDir) - rootCmd.SetArgs([]string{"init", "--prefix", "no-beadsdir", "--quiet"}) + rootCmd.SetArgs([]string{"init", "--server", "--database", uniqueTestDBName(t), "--prefix", "no-beadsdir", "--quiet"}) if err := rootCmd.Execute(); err != nil { t.Fatalf("Init without BEADS_DIR failed: %v", err) } @@ -1686,6 +1702,7 @@ func TestInit_WithBEADS_DIR_DoltBackend(t *testing.T) { // Covers FR-001, FR-002, FR-003, FR-004. func TestInitDoltMetadata(t *testing.T) { skipIfNoDolt(t) + resetInitFlagsForServerInit(t) if runtime.GOOS == "windows" { t.Skip("Skipping Dolt metadata test on Windows") } @@ -1712,7 +1729,8 @@ func TestInitDoltMetadata(t *testing.T) { // Add remote.origin.url so ComputeRepoID succeeds _ = runCommandInDir(tmpDir, "git", "config", "remote.origin.url", "https://github.com/test/repo.git") - rootCmd.SetArgs([]string{"init", "--prefix", "test", "--quiet"}) + database := uniqueTestDBName(t) + rootCmd.SetArgs([]string{"init", "--server", "--database", database, "--prefix", "test", "--quiet"}) if err := rootCmd.Execute(); err != nil { t.Fatalf("init --backend dolt failed: %v", err) } @@ -1720,7 +1738,7 @@ func TestInitDoltMetadata(t *testing.T) { // Open the dolt store to verify metadata was written ctx := context.Background() doltPath := filepath.Join(tmpDir, ".beads", "dolt") - doltStore, err := openDoltStoreForTest(t, ctx, doltPath, "test") + doltStore, err := openDoltStoreForTest(t, ctx, doltPath, database) if err != nil { t.Fatalf("failed to open dolt store for verification: %v", err) } @@ -1797,6 +1815,7 @@ func TestVerifyMetadataSuccess(t *testing.T) { // Covers FR-015 (skip repo_id outside git). func TestInitDoltMetadataNoGit(t *testing.T) { skipIfNoDolt(t) + resetInitFlagsForServerInit(t) if runtime.GOOS == "windows" { t.Skip("Skipping Dolt metadata test on Windows") } @@ -1822,7 +1841,7 @@ func TestInitDoltMetadataNoGit(t *testing.T) { t.Chdir(tmpDir) // Run init in a non-git directory (bd init will create git repo internally) - rootCmd.SetArgs([]string{"init", "--prefix", "nogit"}) + rootCmd.SetArgs([]string{"init", "--server", "--database", uniqueTestDBName(t), "--prefix", "nogit"}) if err := rootCmd.Execute(); err != nil { t.Fatalf("init --prefix nogit failed: %v", err) } @@ -1864,6 +1883,11 @@ func TestInitServerModeWritesDoltCompatibilityMarker(t *testing.T) { if err := os.MkdirAll(filepath.Join(doltDir, ".dolt"), 0o750); err != nil { t.Fatalf("creating simulated server data dir: %v", err) } + // A current-era version witness: without it the legacy-upgrade guard + // reads a non-empty .beads/dolt as a pre-1.0 workspace and refuses. + if err := writeLocalVersion(filepath.Join(beadsDir, localVersionFile), Version); err != nil { + t.Fatalf("writing version witness: %v", err) + } database := uniqueTestDBName(t) t.Cleanup(func() { @@ -1911,6 +1935,11 @@ func TestInitServerModeWarnsOnMarkerFailureInQuietMode(t *testing.T) { if err := os.WriteFile(filepath.Join(doltDir, ".dolt"), []byte("not a dir"), 0o600); err != nil { t.Fatalf("creating invalid dot-dolt marker: %v", err) } + // As in TestInitServerModeWritesDoltCompatibilityMarker: a current-era + // witness keeps the legacy-upgrade guard from refusing the seeded root. + if err := writeLocalVersion(filepath.Join(beadsDir, localVersionFile), Version); err != nil { + t.Fatalf("writing version witness: %v", err) + } database := uniqueTestDBName(t) t.Cleanup(func() { @@ -2019,7 +2048,7 @@ func TestInitDatabaseFlag(t *testing.T) { tmpDir := t.TempDir() // Run init with --database to specify a pre-existing database name - cmd := exec.Command(bd, "init", "--database", "myapp_production", "--quiet") + cmd := exec.Command(bd, "init", "--server", "--database", "myapp_production", "--quiet") cmd.Dir = tmpDir cmd.Env = os.Environ() out, err := cmd.CombinedOutput() @@ -2070,9 +2099,13 @@ func TestInitDatabaseFlag(t *testing.T) { // t.Fatalf paths are covered too. stopSharedServerCleanup(t) + // The shared server's own port: the inherited BEADS_DOLT_SERVER_PORT + // is the suite's test server, which a shared-server start refuses as + // another project's, and without one it would default to 3308. + sharedEnv := sharedServerTestEnv(t) cmd := exec.Command(bd, "init", "--prefix", "bare-fallback", "--skip-hooks", "--quiet") cmd.Dir = worktreeDir - cmd.Env = append(os.Environ(), "BEADS_DOLT_SHARED_SERVER=1") + cmd.Env = sharedEnv out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("bd init from bare-parent worktree failed: %v\n%s", err, out) @@ -2087,7 +2120,7 @@ func TestInitDatabaseFlag(t *testing.T) { retry := exec.Command(bd, "init", "--prefix", "bare-fallback", "--skip-hooks", "--quiet") retry.Dir = worktreeDir - retry.Env = append(os.Environ(), "BEADS_DOLT_SHARED_SERVER=1") + retry.Env = sharedEnv retryOut, retryErr := retry.CombinedOutput() if retryErr == nil { t.Fatal("expected second bd init to fail against existing bare-parent .beads") @@ -2102,7 +2135,7 @@ func TestInitDatabaseFlag(t *testing.T) { // Run init with both --database and --prefix // --database should override prefix for DB name, but prefix still sets issue_prefix - cmd := exec.Command(bd, "init", "--database", "shared_db", "--prefix", "team-alpha", "--quiet") + cmd := exec.Command(bd, "init", "--server", "--database", "shared_db", "--prefix", "team-alpha", "--quiet") cmd.Dir = tmpDir cmd.Env = os.Environ() out, err := cmd.CombinedOutput() @@ -2148,7 +2181,7 @@ func TestInitDatabaseFlag(t *testing.T) { tmpDir := t.TempDir() // Run init with --database - cmd := exec.Command(bd, "init", "--database", "test_server_cfg", "--quiet") + cmd := exec.Command(bd, "init", "--server", "--database", "test_server_cfg", "--quiet") cmd.Dir = tmpDir cmd.Env = os.Environ() out, err := cmd.CombinedOutput() @@ -2180,9 +2213,16 @@ func TestInitDatabaseFlag(t *testing.T) { t.Run("shared_server_flag_selects_server_mode", func(t *testing.T) { tmpDir := t.TempDir() + // --shared-server starts (daemonizes) the shared server: give it a + // free port of its own (see sharedServerTestEnv) and stop it after. + port, err := testutil.FindFreePort() + if err != nil { + t.Fatalf("find free port: %v", err) + } + stopSharedServerCleanup(t) cmd := exec.Command(bd, "init", "--shared-server", "--prefix", "shared-mode-test", "--skip-hooks") cmd.Dir = tmpDir - cmd.Env = os.Environ() + cmd.Env = append(os.Environ(), "BEADS_DOLT_SERVER_PORT="+strconv.Itoa(port)) out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("bd init --shared-server failed: %v\n%s", err, out) @@ -2205,7 +2245,18 @@ func TestInitDatabaseFlag(t *testing.T) { if err != nil { t.Fatalf("Failed to read config.yaml: %v", err) } - if !strings.Contains(string(configYAML), "dolt.shared-server: true") { + // SetYamlConfig writes dotted keys as nested YAML (dolt: + // shared-server: true); accept the old flat form as well. + var parsedYAML struct { + Dolt struct { + SharedServer bool `yaml:"shared-server"` + } `yaml:"dolt"` + Flat bool `yaml:"dolt.shared-server"` + } + if err := yaml.Unmarshal(configYAML, &parsedYAML); err != nil { + t.Fatalf("parse config.yaml: %v\n%s", err, configYAML) + } + if !parsedYAML.Dolt.SharedServer && !parsedYAML.Flat { t.Fatalf("expected config.yaml to enable shared server, got:\n%s", configYAML) } @@ -2291,7 +2342,8 @@ func TestBareParentWorktreeCoreCommandsWithoutRedirect(t *testing.T) { bd := buildBDForInitTests(t) bareDir, worktreeDir := setupBareParentInitWorktree(t) bareBeadsDir := filepath.Join(bareDir, ".beads") - sharedEnv := append(os.Environ(), "BEADS_DOLT_SHARED_SERVER=1") + // See TestInitDatabaseFlag/BareParentWorktreeAutoInit. + sharedEnv := sharedServerTestEnv(t) // Same daemonized shared server, same missing stop (wy-j2zc8q). stopSharedServerCleanup(t) @@ -2548,3 +2600,31 @@ func TestInitDatabaseAdoptsExistingProjectID(t *testing.T) { t.Errorf("ProjectID = %q, want %q (should adopt existing project_id from server)", cfg.ProjectID, knownProjectID) } } + +// resetInitFlagsForServerInit puts every rootCmd flag back to its registered +// default before and after a test that executes `bd init --server ...` +// in-process: embedded is init's default mode, so the Dolt-server-gated init +// tests pass --server, and cobra would otherwise carry that (and any other +// parsed flag) into the next in-process init. +func resetInitFlagsForServerInit(t *testing.T) { + t.Helper() + resetCommandFlags(rootCmd) + t.Cleanup(func() { resetCommandFlags(rootCmd) }) +} + +// sharedServerTestEnv is os.Environ() in shared-server mode with a free port +// of its own for the shared server (as dolt_worktree_remote_test.go does): +// the inherited BEADS_DOLT_SERVER_PORT names this suite's test server, which +// a shared-server start refuses as another project's, and the shared +// server's default port (3308) may be a developer's live server. +func sharedServerTestEnv(t *testing.T) []string { + t.Helper() + port, err := testutil.FindFreePort() + if err != nil { + t.Fatalf("find free port: %v", err) + } + return append(os.Environ(), + "BEADS_DOLT_SHARED_SERVER=1", + "BEADS_DOLT_SERVER_PORT="+strconv.Itoa(port), + ) +} diff --git a/cmd/bd/legacy_upgrade_guard_e2e_test.go b/cmd/bd/legacy_upgrade_guard_e2e_test.go index 0eb09ed87..56dd46045 100644 --- a/cmd/bd/legacy_upgrade_guard_e2e_test.go +++ b/cmd/bd/legacy_upgrade_guard_e2e_test.go @@ -468,7 +468,7 @@ func TestLegacyGuardUsesSelectedTargetSharedServerConfig(t *testing.T) { "BD_DISABLE_METRICS=1", "BD_DISABLE_EVENT_FLUSH=1", "BEADS_DOLT_AUTO_START=0", - "BEADS_DOLT_SERVER_PORT=59999", + "BEADS_DOLT_SERVER_PORT="+strconv.Itoa(closedLoopbackPort(t)), "BEADS_DOLT_SHARED_SERVER=0", "HOME="+t.TempDir(), ) @@ -533,7 +533,7 @@ func TestLegacyGuardAdmitsConfigYamlServerWorkspace(t *testing.T) { "BD_DISABLE_METRICS=1", "BD_DISABLE_EVENT_FLUSH=1", "BEADS_DOLT_AUTO_START=0", - "BEADS_DOLT_SERVER_PORT=59999", + "BEADS_DOLT_SERVER_PORT="+strconv.Itoa(closedLoopbackPort(t)), "BEADS_DOLT_SHARED_SERVER=0", "HOME="+t.TempDir(), ) diff --git a/cmd/bd/linear_roundtrip_test.go b/cmd/bd/linear_roundtrip_test.go index da73d31bd..bcc0d41e0 100644 --- a/cmd/bd/linear_roundtrip_test.go +++ b/cmd/bd/linear_roundtrip_test.go @@ -66,6 +66,11 @@ func (m *mockLinearServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { data, err = m.handleCreate(req) case strings.Contains(req.Query, "issueUpdate"): data, err = m.handleUpdate(req) + case strings.Contains(req.Query, "issueBatchCreate"): + data, err = m.handleBatchCreate(req) + case strings.Contains(req.Query, "TeamLabels"): + // Batch push builds a label cache first; the mock team has none. + data = m.handleTeamLabels() case strings.Contains(req.Query, "TeamStates") || strings.Contains(req.Query, "team(id:") || (strings.Contains(req.Query, "team(") && strings.Contains(req.Query, "states")): data = m.handleTeamStates() case strings.Contains(req.Query, "issues"): @@ -100,6 +105,45 @@ func (m *mockLinearServer) handleCreate(req linear.GraphQLRequest) (interface{}, return nil, fmt.Errorf("input is not a map") } + return map[string]interface{}{ + "issueCreate": map[string]interface{}{ + "success": true, + "issue": m.createLocked(input), + }, + }, nil +} + +// handleBatchCreate serves issueBatchCreate, which batch push uses. +func (m *mockLinearServer) handleBatchCreate(req linear.GraphQLRequest) (interface{}, error) { + m.mu.Lock() + defer m.mu.Unlock() + + input, ok := req.Variables["input"].(map[string]interface{}) + if !ok { + return nil, fmt.Errorf("missing input") + } + rawIssues, ok := input["issues"].([]interface{}) + if !ok { + return nil, fmt.Errorf("input.issues is not a list") + } + created := make([]*linear.Issue, 0, len(rawIssues)) + for _, raw := range rawIssues { + one, ok := raw.(map[string]interface{}) + if !ok { + return nil, fmt.Errorf("input.issues entry is not a map") + } + created = append(created, m.createLocked(one)) + } + return map[string]interface{}{ + "issueBatchCreate": map[string]interface{}{ + "success": true, + "issues": created, + }, + }, nil +} + +// createLocked records one created issue; m.mu must be held. +func (m *mockLinearServer) createLocked(input map[string]interface{}) *linear.Issue { m.nextSeq++ id := fmt.Sprintf("uuid-%d", m.nextSeq) identifier := fmt.Sprintf("%s-%d", m.teamKey, m.nextSeq) @@ -131,13 +175,7 @@ func (m *mockLinearServer) handleCreate(req linear.GraphQLRequest) (interface{}, } m.issues[id] = issue - - return map[string]interface{}{ - "issueCreate": map[string]interface{}{ - "success": true, - "issue": issue, - }, - }, nil + return issue } func (m *mockLinearServer) handleUpdate(req linear.GraphQLRequest) (interface{}, error) { @@ -204,6 +242,17 @@ func (m *mockLinearServer) handleTeamStates() interface{} { } } +func (m *mockLinearServer) handleTeamLabels() interface{} { + return map[string]interface{}{ + "team": map[string]interface{}{ + "labels": map[string]interface{}{ + "nodes": []interface{}{}, + "pageInfo": map[string]interface{}{"hasNextPage": false, "endCursor": ""}, + }, + }, + } +} + func (m *mockLinearServer) handleFetchIssues(req linear.GraphQLRequest) (interface{}, error) { m.mu.Lock() defer m.mu.Unlock() @@ -276,14 +325,21 @@ func TestLinearRoundTripCoreFields(t *testing.T) { teamID := "test-team-uuid" // --- 1. Setup source DB --- + // linear.api_key is a secret, read from config.yaml or LINEAR_API_KEY + // and never from the database (config.IsYamlOnlyKey). + t.Setenv("LINEAR_API_KEY", "test-api-key") sourceStore, cleanup := setupTestDB(t) defer cleanup() // Configure Linear settings in source store + // Push refuses without an explicit linear.state_map (it no longer + // guesses from defaults); map the mock team's states by name. for k, v := range map[string]string{ - "linear.api_key": "test-api-key", - "linear.team_id": teamID, - "issue_prefix": "bd", + "linear.team_id": teamID, + "issue_prefix": "bd", + "linear.state_map.todo": "open", + "linear.state_map.in progress": "in_progress", + "linear.state_map.done": "closed", } { if err := sourceStore.SetConfig(ctx, k, v); err != nil { t.Fatalf("SetConfig(%s): %v", k, err) @@ -371,7 +427,6 @@ func TestLinearRoundTripCoreFields(t *testing.T) { defer cleanup2() for k, v := range map[string]string{ - "linear.api_key": "test-api-key", "linear.team_id": teamID, "linear.api_endpoint": server.URL, "issue_prefix": "bd", @@ -466,6 +521,9 @@ func TestLinearPullMilestonesCreatesEpicHierarchy(t *testing.T) { ctx := context.Background() teamID := "test-team-uuid" + // linear.api_key is a secret, read from config.yaml or LINEAR_API_KEY + // and never from the database (config.IsYamlOnlyKey). + t.Setenv("LINEAR_API_KEY", "test-api-key") targetStore, cleanup := setupTestDB(t) defer cleanup() @@ -474,7 +532,6 @@ func TestLinearPullMilestonesCreatesEpicHierarchy(t *testing.T) { defer server.Close() for k, v := range map[string]string{ - "linear.api_key": "test-api-key", "linear.team_id": teamID, "linear.api_endpoint": server.URL, "issue_prefix": "bd", diff --git a/cmd/bd/migrate_dolt_mode_frontdoor_integration_test.go b/cmd/bd/migrate_dolt_mode_frontdoor_integration_test.go index 7b0862527..5afee9bac 100644 --- a/cmd/bd/migrate_dolt_mode_frontdoor_integration_test.go +++ b/cmd/bd/migrate_dolt_mode_frontdoor_integration_test.go @@ -112,6 +112,10 @@ func TestMigrateDoltModeFrontDoor(t *testing.T) { dir := t.TempDir() home := t.TempDir() env := migrationFrontDoorEnv(home) + // The closing show/list after the reverse migration auto-start a + // repo-local server again; stop it before t.TempDir removes the tree it + // serves (the suite fails on a leaked dolt sql-server). Idempotent. + t.Cleanup(func() { _, _ = runBDExecWithBinary(t, bd, dir, env, "dolt", "stop") }) out, err := runBDExecWithBinary(t, bd, dir, env, "init", "--backend", "dolt", "--server", "--prefix", "fd", "--quiet") if err != nil { t.Fatalf("init: %v\n%s", err, out) diff --git a/cmd/bd/mol_ready_gated_test.go b/cmd/bd/mol_ready_gated_test.go index ea739114c..374f8dd84 100644 --- a/cmd/bd/mol_ready_gated_test.go +++ b/cmd/bd/mol_ready_gated_test.go @@ -5,7 +5,6 @@ package main import ( "context" "fmt" - "os" "path/filepath" "testing" "time" @@ -14,42 +13,14 @@ import ( "github.com/steveyegge/beads/internal/types" ) -// setupGatedTestDB creates a temporary file-based test database +// setupGatedTestDB creates a test store on the suite's Dolt test server +// (skipped when it is not running), with the orchestrator custom types +// newTestStore configures. Closed by t.Cleanup; the returned cleanup is a +// no-op kept for the callers' defer. func setupGatedTestDB(t *testing.T) (*dolt.DoltStore, func()) { t.Helper() - tmpDir, err := os.MkdirTemp("", "bd-test-gated-*") - if err != nil { - t.Fatalf("Failed to create temp dir: %v", err) - } - - testDB := filepath.Join(tmpDir, "test.db") - store, err := dolt.New(context.Background(), &dolt.Config{Path: testDB}) - if err != nil { - os.RemoveAll(tmpDir) - t.Skipf("skipping: Dolt server not available: %v", err) - } - - // Set issue_prefix (required for beads) - ctx := context.Background() - if err := store.SetConfig(ctx, "issue_prefix", "test"); err != nil { - store.Close() - os.RemoveAll(tmpDir) - t.Fatalf("Failed to set issue_prefix: %v", err) - } - - // Configure orchestrator custom types for test compatibility (bd-find4) - if err := store.SetConfig(ctx, "types.custom", "molecule,gate,convoy,merge-request,slot,agent,role,rig,event,message"); err != nil { - store.Close() - os.RemoveAll(tmpDir) - t.Fatalf("Failed to set types.custom: %v", err) - } - - cleanup := func() { - store.Close() - os.RemoveAll(tmpDir) - } - - return store, cleanup + store := newTestStore(t, filepath.Join(t.TempDir(), "test.db")) + return store, func() {} } // ============================================================================= diff --git a/cmd/bd/notion_store_test.go b/cmd/bd/notion_store_test.go index 8c0ac3650..27ac9d091 100644 --- a/cmd/bd/notion_store_test.go +++ b/cmd/bd/notion_store_test.go @@ -12,7 +12,10 @@ func TestGetNotionConfigReadsDBPathWhenStoreUnset(t *testing.T) { saveAndRestoreGlobals(t) tempDir := t.TempDir() testDBPath := filepath.Join(tempDir, "test.db") - testStore := newTestStore(t, testDBPath) + // Its own database, not a branch of the shared one: getNotionConfig + // reopens the store from metadata.json, which can name a database but + // not a test branch. + testStore := newTestStoreIsolatedDB(t, testDBPath, "test") defer testStore.Close() ctx := context.Background() diff --git a/cmd/bd/readonly_policy_test.go b/cmd/bd/readonly_policy_test.go index 8a4017535..84c5f3fe5 100644 --- a/cmd/bd/readonly_policy_test.go +++ b/cmd/bd/readonly_policy_test.go @@ -319,7 +319,7 @@ func TestConfigValidateReadOnlyIsHermetic(t *testing.T) { t.Fatalf("write isolated config: %v", err) } - beforeBeads := snapshotReadonlyTree(t, beadsDir) + beforeBeads := withoutGateLockFiles(snapshotReadonlyTree(t, beadsDir)) beforeCircuit := snapshotReadonlyTree(t, circuitDir) home := t.TempDir() if err := os.MkdirAll(filepath.Join(home, "xdg"), 0o755); err != nil { @@ -341,7 +341,13 @@ func TestConfigValidateReadOnlyIsHermetic(t *testing.T) { t.Fatalf("bd config validate --readonly: %v\n%s", err, output) } - afterBeads := snapshotReadonlyTree(t, beadsDir) + // A store-opening command, readonly ones included, takes a SHARED + // workspace gate (cmd/bd/workspace_gate.go), and workspacegate creates + // each gate's lock file beside the directory it guards on first use and + // never removes it (.beads/dolt.gate.lock for the .beads/dolt root): an + // empty coordination file, not database, server or version state, so + // it is not a readonly side effect this canary guards against. + afterBeads := withoutGateLockFiles(snapshotReadonlyTree(t, beadsDir)) if !reflect.DeepEqual(afterBeads, beforeBeads) { t.Fatalf("target .beads changed\nbefore: %#v\nafter: %#v\noutput: %s", beforeBeads, afterBeads, output) } @@ -355,3 +361,17 @@ func TestConfigValidateReadOnlyIsHermetic(t *testing.T) { } } } + +// withoutGateLockFiles drops the physical-root gate a readonly command takes +// (workspacegate.ForPhysicalRoot(.beads/dolt): .beads/dolt.gate.lock) and its +// advisory holder-info sidecar from a snapshot. Any other new file, another +// gate file included, still trips the canary. +func withoutGateLockFiles(s readonlyTreeSnapshot) readonlyTreeSnapshot { + for rel := range s.Entries { + switch filepath.ToSlash(rel) { + case "dolt.gate.lock", "dolt.gate.lock.info": + delete(s.Entries, rel) + } + } + return s +} diff --git a/cmd/bd/rename_prefix_test.go b/cmd/bd/rename_prefix_test.go index d4b1e1885..69b1e2979 100644 --- a/cmd/bd/rename_prefix_test.go +++ b/cmd/bd/rename_prefix_test.go @@ -4,11 +4,9 @@ package main import ( "context" - "os" "path/filepath" "testing" - "github.com/steveyegge/beads/internal/storage/dolt" "github.com/steveyegge/beads/internal/types" ) @@ -44,11 +42,7 @@ func TestRenamePrefixCommand(t *testing.T) { tmpDir := t.TempDir() dbPath := filepath.Join(tmpDir, "test.db") - testStore, err := dolt.New(context.Background(), &dolt.Config{Path: dbPath}) - if err != nil { - t.Skipf("skipping: Dolt server not available: %v", err) - } - defer testStore.Close() + testStore := newTestStore(t, dbPath) ctx := context.Background() @@ -172,14 +166,7 @@ func TestRenamePrefixInDB(t *testing.T) { tmpDir := t.TempDir() dbPath := filepath.Join(tmpDir, "test.db") - testStore, err := dolt.New(context.Background(), &dolt.Config{Path: dbPath}) - if err != nil { - t.Skipf("skipping: Dolt server not available: %v", err) - } - t.Cleanup(func() { - testStore.Close() - os.Remove(dbPath) - }) + testStore := newTestStore(t, dbPath) ctx := context.Background() store = testStore @@ -203,7 +190,7 @@ func TestRenamePrefixInDB(t *testing.T) { } issues := []*types.Issue{issue1} - err = renamePrefixInDB(ctx, "old", "new", issues) + err := renamePrefixInDB(ctx, "old", "new", issues) if err != nil { t.Fatalf("renamePrefixInDB failed: %v", err) } @@ -231,14 +218,7 @@ func TestRenamePrefixInDB_HalfMigratedConfigNotDoubled(t *testing.T) { tmpDir := t.TempDir() dbPath := filepath.Join(tmpDir, "test.db") - testStore, err := dolt.New(context.Background(), &dolt.Config{Path: dbPath}) - if err != nil { - t.Skipf("skipping: Dolt server not available: %v", err) - } - t.Cleanup(func() { - testStore.Close() - os.Remove(dbPath) - }) + testStore := newTestStore(t, dbPath) ctx := context.Background() store = testStore diff --git a/cmd/bd/setup/BUILD.bazel b/cmd/bd/setup/BUILD.bazel index a7dc4ddbf..1dbae3b11 100644 --- a/cmd/bd/setup/BUILD.bazel +++ b/cmd/bd/setup/BUILD.bazel @@ -17,6 +17,7 @@ go_library( "junie.go", "mux.go", "opencode.go", + "project_output.go", "setup_test_helpers.go", "utils.go", ], @@ -45,6 +46,7 @@ go_test( "mux_test.go", "opencode_test.go", "plugin_layout_test.go", + "project_output_test.go", "utils_test.go", ], data = [ diff --git a/cmd/bd/setup/claude.go b/cmd/bd/setup/claude.go index fb10326d4..ed9e25266 100644 --- a/cmd/bd/setup/claude.go +++ b/cmd/bd/setup/claude.go @@ -225,10 +225,16 @@ func InstallClaude(global bool, stealth bool) error { // InstallClaudeProject installs project-local Claude hooks, returning an error // instead of exiting. Used by bd init to integrate Claude setup automatically. func InstallClaudeProject(stealth bool) error { + return InstallClaudeProjectTo(stealth, ProjectInstallOutput{}) +} + +// InstallClaudeProjectTo is InstallClaudeProject reporting to out. +func InstallClaudeProjectTo(stealth bool, out ProjectInstallOutput) error { env, err := claudeEnvProvider() if err != nil { return err } + env.stdout, env.stderr = out.stdout(), out.stderr() return installClaude(env, false, stealth) } diff --git a/cmd/bd/setup/codex.go b/cmd/bd/setup/codex.go index 8ff57f0c3..11044b1be 100644 --- a/cmd/bd/setup/codex.go +++ b/cmd/bd/setup/codex.go @@ -82,10 +82,16 @@ func InstallCodex(global bool) error { // error instead of exiting. Used by bd init to integrate Codex setup // automatically. func InstallCodexProject() error { + return InstallCodexProjectTo(ProjectInstallOutput{}) +} + +// InstallCodexProjectTo is InstallCodexProject reporting to out. +func InstallCodexProjectTo(out ProjectInstallOutput) error { env, err := codexEnvProvider() if err != nil { return err } + env.stdout, env.stderr = out.stdout(), out.stderr() return installCodex(env, false) } diff --git a/cmd/bd/setup/cursor.go b/cmd/bd/setup/cursor.go index 12106d6a9..f96c2a7be 100644 --- a/cmd/bd/setup/cursor.go +++ b/cmd/bd/setup/cursor.go @@ -155,6 +155,11 @@ func InstallCursor(global bool) error { // error instead of exiting. Used by bd init to integrate Cursor setup // automatically (parallels InstallClaudeProject / InstallCodexProject). func InstallCursorProject() error { + return InstallCursorProjectTo(ProjectInstallOutput{}) +} + +// InstallCursorProjectTo is InstallCursorProject reporting to out. +func InstallCursorProjectTo(out ProjectInstallOutput) error { if err := installCursorRules(); err != nil { return err } @@ -162,13 +167,14 @@ func InstallCursorProject() error { if err != nil { return err } + skillEnv.stdout, skillEnv.stderr = out.stdout(), out.stderr() if err := installAgentSkill(skillEnv); err != nil { return err } if err := installCursorHooks(cursorHooksPath); err != nil { return err } - fmt.Println("✓ Cursor integration installed (rules + skill + hooks)") + _, _ = fmt.Fprintln(out.stdout(), "✓ Cursor integration installed (rules + skill + hooks)") return nil } diff --git a/cmd/bd/setup/project_output.go b/cmd/bd/setup/project_output.go new file mode 100644 index 000000000..8b61f62c1 --- /dev/null +++ b/cmd/bd/setup/project_output.go @@ -0,0 +1,35 @@ +package setup + +import ( + "io" + "os" +) + +// ProjectInstallOutput is where the project installers bd init runs +// (InstallClaudeProjectTo, InstallCodexProjectTo, InstallCursorProjectTo) +// report: progress on Stdout, failures on Stderr. A nil writer means the +// process's own stream. +type ProjectInstallOutput struct { + Stdout io.Writer + Stderr io.Writer +} + +// QuietProjectInstallOutput discards the installers' progress and keeps +// their error lines on stderr, as bd init --quiet does. +func QuietProjectInstallOutput() ProjectInstallOutput { + return ProjectInstallOutput{Stdout: io.Discard, Stderr: os.Stderr} +} + +func (o ProjectInstallOutput) stdout() io.Writer { + if o.Stdout == nil { + return os.Stdout + } + return o.Stdout +} + +func (o ProjectInstallOutput) stderr() io.Writer { + if o.Stderr == nil { + return os.Stderr + } + return o.Stderr +} diff --git a/cmd/bd/setup/project_output_test.go b/cmd/bd/setup/project_output_test.go new file mode 100644 index 000000000..9c0256598 --- /dev/null +++ b/cmd/bd/setup/project_output_test.go @@ -0,0 +1,58 @@ +package setup + +import ( + "bytes" + "io" + "strings" + "testing" +) + +// bd init --quiet hands the project installers QuietProjectInstallOutput: +// none of their progress may reach the writers the env provider set up (the +// process's stdout in production), and their error stream stays stderr. +func TestProjectInstallersReportToTheGivenOutput(t *testing.T) { + q := QuietProjectInstallOutput() + if q.Stdout != io.Discard || q.stderr() == io.Discard { + t.Fatalf("QuietProjectInstallOutput = %+v; want progress discarded, errors kept", q) + } + + env, envOut, envErr := newClaudeTestEnv(t) + stubClaudeEnvProvider(t, env, nil) + var out, errOut bytes.Buffer + if err := InstallClaudeProjectTo(false, ProjectInstallOutput{Stdout: &out, Stderr: &errOut}); err != nil { + t.Fatalf("InstallClaudeProjectTo: %v", err) + } + if envOut.Len() != 0 || envErr.Len() != 0 { + t.Errorf("Claude installer wrote to the env's own streams: stdout %q stderr %q", envOut, envErr) + } + if !strings.Contains(out.String(), "Installing Claude hooks for this project") { + t.Errorf("Claude installer progress not on the given stdout: %q", out.String()) + } + + codex, codexOut, codexErr := newCodexTestEnv(t) + origCodex := codexEnvProvider + codexEnvProvider = func() (codexEnv, error) { return codex, nil } + t.Cleanup(func() { codexEnvProvider = origCodex }) + out.Reset() + if err := InstallCodexProjectTo(ProjectInstallOutput{Stdout: &out, Stderr: &errOut}); err != nil { + t.Fatalf("InstallCodexProjectTo: %v", err) + } + if codexOut.Len() != 0 || codexErr.Len() != 0 { + t.Errorf("Codex installer wrote to the env's own streams: stdout %q stderr %q", codexOut, codexErr) + } + if out.Len() == 0 { + t.Error("Codex installer progress not on the given stdout") + } + + inTempDir(t) + out.Reset() + if err := InstallCursorProjectTo(ProjectInstallOutput{Stdout: &out, Stderr: &errOut}); err != nil { + t.Fatalf("InstallCursorProjectTo: %v", err) + } + if !strings.Contains(out.String(), "Cursor integration installed") || !strings.Contains(out.String(), "Beads agent skill installed") { + t.Errorf("Cursor installer progress not on the given stdout: %q", out.String()) + } + if errOut.Len() != 0 { + t.Errorf("successful installs wrote errors: %q", errOut.String()) + } +} diff --git a/cmd/bd/shared_server_cfgnil_test.go b/cmd/bd/shared_server_cfgnil_test.go index 8f6e65be0..2cff4acce 100644 --- a/cmd/bd/shared_server_cfgnil_test.go +++ b/cmd/bd/shared_server_cfgnil_test.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "runtime" + "strconv" "strings" "testing" "time" @@ -56,7 +57,7 @@ func TestSharedServerCfgNilHonorsSharedServer(t *testing.T) { // Disable auto-start and point at a port nothing listens on so the // shared-server path fails fast instead of spinning up a server. "BEADS_DOLT_AUTO_START=0", - "BEADS_DOLT_SERVER_PORT=59999", + "BEADS_DOLT_SERVER_PORT=" + strconv.Itoa(closedLoopbackPort(t)), // Disable the metrics flusher: bd otherwise spawns a DETACHED // `bd send-metrics` child that keeps writing $HOME/.beads/eventsData // after the parent exits. HOME here is a t.TempDir(), so that child diff --git a/cmd/bd/shared_server_cleanup_test.go b/cmd/bd/shared_server_cleanup_test.go index 72cc49bce..6e6d0c584 100644 --- a/cmd/bd/shared_server_cleanup_test.go +++ b/cmd/bd/shared_server_cleanup_test.go @@ -11,6 +11,7 @@ import ( "github.com/steveyegge/beads/internal/procid" "github.com/steveyegge/beads/internal/storage/dbproxy/proxy" "github.com/steveyegge/beads/internal/storage/dbproxy/server" + "github.com/steveyegge/beads/internal/testutil" ) // stopSharedServerCleanup is the cleanup every fixture in this package that @@ -181,3 +182,17 @@ func sharedServerStillRunning(t *testing.T, pid int, token procid.Token) bool { } return same } + +// closedLoopbackPort returns a 127.0.0.1 port nothing listens on right now, +// for tests that need a server connection to fail. A fixed "unlikely" port +// (59999 used to be hard-coded here) is not: any process on the host may +// hold it, and a listener that accepts and resets turns the expected +// "connection refused" into a different error. +func closedLoopbackPort(t *testing.T) int { + t.Helper() + port, err := testutil.FindFreePort() + if err != nil { + t.Fatalf("find free port: %v", err) + } + return port +} diff --git a/cmd/bd/sql_test.go b/cmd/bd/sql_test.go index 81cf55d19..617aef866 100644 --- a/cmd/bd/sql_test.go +++ b/cmd/bd/sql_test.go @@ -10,7 +10,6 @@ import ( "strings" "testing" - "github.com/steveyegge/beads/internal/storage/dolt" "github.com/steveyegge/beads/internal/types" ) @@ -22,11 +21,7 @@ func TestSqlCommand(t *testing.T) { t.Fatalf("Failed to create .beads directory: %v", err) } - testStore, err := dolt.New(context.Background(), &dolt.Config{Path: testDBPath}) - if err != nil { - t.Skipf("skipping: Dolt server not available: %v", err) - } - defer testStore.Close() + testStore := newTestStore(t, testDBPath) ctx := context.Background() @@ -60,20 +55,24 @@ func TestSqlCommand(t *testing.T) { oldStore := store oldCtx := rootCtx oldJSON := jsonOutput + oldServerMode, oldProxied := serverMode, proxiedServerMode defer func() { store = oldStore rootCtx = oldCtx jsonOutput = oldJSON + serverMode, proxiedServerMode = oldServerMode, oldProxied }() store = testStore rootCtx = ctx + // testStore is a server-mode store; bd sql refuses in the default + // embedded mode before it looks at the store. + serverMode, proxiedServerMode = true, false t.Run("select count", func(t *testing.T) { jsonOutput = true output := captureStdout(t, func() error { - _ = sqlCmd.RunE(sqlCmd, []string{"SELECT COUNT(*) as count FROM issues"}) - return nil + return sqlCmd.RunE(sqlCmd, []string{"SELECT COUNT(*) as count FROM issues"}) }) var result []map[string]interface{} @@ -96,8 +95,7 @@ func TestSqlCommand(t *testing.T) { t.Run("select with filter", func(t *testing.T) { jsonOutput = true output := captureStdout(t, func() error { - _ = sqlCmd.RunE(sqlCmd, []string{`SELECT id, title FROM issues WHERE status = 'open'`}) - return nil + return sqlCmd.RunE(sqlCmd, []string{`SELECT id, title FROM issues WHERE status = 'open'`}) }) var result []map[string]interface{} @@ -117,8 +115,7 @@ func TestSqlCommand(t *testing.T) { t.Run("empty result json", func(t *testing.T) { jsonOutput = true output := captureStdout(t, func() error { - _ = sqlCmd.RunE(sqlCmd, []string{`SELECT * FROM issues WHERE title = 'nonexistent'`}) - return nil + return sqlCmd.RunE(sqlCmd, []string{`SELECT * FROM issues WHERE title = 'nonexistent'`}) }) var result []map[string]interface{} @@ -134,8 +131,7 @@ func TestSqlCommand(t *testing.T) { t.Run("table output", func(t *testing.T) { jsonOutput = false output := captureStdout(t, func() error { - _ = sqlCmd.RunE(sqlCmd, []string{"SELECT COUNT(*) as count FROM issues"}) - return nil + return sqlCmd.RunE(sqlCmd, []string{"SELECT COUNT(*) as count FROM issues"}) }) if !strings.Contains(output, "count") { @@ -149,8 +145,7 @@ func TestSqlCommand(t *testing.T) { t.Run("empty table output", func(t *testing.T) { jsonOutput = false output := captureStdout(t, func() error { - _ = sqlCmd.RunE(sqlCmd, []string{`SELECT * FROM issues WHERE title = 'nonexistent'`}) - return nil + return sqlCmd.RunE(sqlCmd, []string{`SELECT * FROM issues WHERE title = 'nonexistent'`}) }) if !strings.Contains(output, "(0 rows)") { diff --git a/cmd/bd/stdio_race_guard_test.go b/cmd/bd/stdio_race_guard_test.go index 1518ba4bb..0b57efc68 100644 --- a/cmd/bd/stdio_race_guard_test.go +++ b/cmd/bd/stdio_race_guard_test.go @@ -20,6 +20,9 @@ var cobraParallelUnsafeMethods = []string{ ".Help(", ".InheritedFlags(", ".Execute(", + ".ExecuteC(", + ".ExecuteContext(", + ".ExecuteContextC(", ".Print(", ".Printf(", ".Println(", @@ -30,6 +33,17 @@ var cobraParallelUnsafeMethods = []string{ ".UsageString(", } +// cobraParallelUnsafeHelpers lists this package's test helpers that execute +// the shared rootCmd tree in-process: they chdir, swap os.Stdout/os.Stderr, +// set process env and re-initialize the global config, all under +// inProcessMutex, which serializes them only against each other. Any other +// parallel test (a store-backed suite reading config, a test globbing its +// own source files) races with them, so a parallel test must not call them. +var cobraParallelUnsafeHelpers = []string{ + "runBDInProcess(", + "runBDInProcessAllowError(", +} + // TestCobraParallelPolicyGuard scans test source files and fails if any // parallel test calls shared Cobra methods that are not safe under parallel // execution. This prevents races where cobra's OutOrStdout()/ErrOrStderr() @@ -91,7 +105,7 @@ func TestCobraParallelPolicyGuard(t *testing.T) { // guard scans itself, so its string constants would match). strippedStrings := stripStringLiterals(stripped) - for _, method := range cobraParallelUnsafeMethods { + for _, method := range append(append([]string{}, cobraParallelUnsafeMethods...), cobraParallelUnsafeHelpers...) { if strings.Contains(strippedStrings, method) { t.Errorf("%s:%s is t.Parallel() and calls method name %s — "+ "this can race with stdio capture or shared command flag caches. "+ diff --git a/cmd/bd/test_repo_beads_guard_test.go b/cmd/bd/test_repo_beads_guard_test.go index 94f4b7838..8a12e3ee9 100644 --- a/cmd/bd/test_repo_beads_guard_test.go +++ b/cmd/bd/test_repo_beads_guard_test.go @@ -244,6 +244,14 @@ func testMainInner(m *testing.M) int { // Also reset viper state that was loaded by main.go's init(). config.ResetForTesting() + // Record every flag's registered state before any test executes a + // command, so in-process runners can put the tree back between runs + // (resetCommandFlags). + snapshotCommandFlags(rootCmd) + // And undo what each in-process execution leaves in the process env and + // the storage-mode globals (installExecuteIsolation). + installExecuteIsolation() + // Enable test mode that forces accessor functions to use legacy globals. // This ensures backward compatibility with tests that manipulate globals directly. enableTestModeGlobals() diff --git a/cmd/bd/write_verbs_parity_test.go b/cmd/bd/write_verbs_parity_test.go index 7404070b0..b48100783 100644 --- a/cmd/bd/write_verbs_parity_test.go +++ b/cmd/bd/write_verbs_parity_test.go @@ -286,6 +286,23 @@ func newParityEnv(t *testing.T) *parityEnv { // the ambient git identity. Pin it: CI commonly sets GIT_AUTHOR_EMAIL, a developer // shell usually does not, and the suite must render the same verdict in both. t.Setenv("GIT_AUTHOR_EMAIL", parityOwnerEmail) + // bd create auto-routes by routing.DetectUserRole("."), which reads + // `git config beads.role` in the process's working directory and, when it + // is unset, prints a "beads.role not configured" warning to stderr before + // falling back to a URL heuristic. The pinned stderr contract is the + // configured case, so configure the role (maintainer: route to ".") in a + // global git config of this test's own, whatever repository, if any, the + // test binary runs in. Not GIT_CONFIG_*: the role lookup scrubs every + // GIT_CONFIG variable (gitenv.ScrubRoutingAndSuppression), but git also + // reads $XDG_CONFIG_HOME/git/config as global config. + xdg := t.TempDir() + if err := os.MkdirAll(filepath.Join(xdg, "git"), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(xdg, "git", "config"), []byte("[beads]\n\trole = maintainer\n"), 0o644); err != nil { + t.Fatal(err) + } + t.Setenv("XDG_CONFIG_HOME", xdg) t.Setenv("BEADS_DIR", beadsDir) // Pin every config key the write verbs read. config.Initialize() merges diff --git a/engdocs/TESTING.md b/engdocs/TESTING.md index d65b0970e..ac3d6bed4 100644 --- a/engdocs/TESTING.md +++ b/engdocs/TESTING.md @@ -122,6 +122,17 @@ runs the manifest's own 30-shard block — bin-packed by measured duration, not the legacy jobs' 15-shard, bd-init-cost-proxy block — so shard k there is not job k+1's tests; it is a different split of the same tests. +`--config=doltserver-cmd` (`//cmd/bd:bd_dolt_server_test`, bazel.yml's +advisory `bazel-cmd-dolt` job) runs the whole integration-tagged cmd/bd +suite on the `local` backend: the Dolt-gated cmd/bd tests (`TestCLI_*`, the +init and store-backed suites) that every other lane skips with +`BEADS_TEST_SKIP=dolt` or leaves out of its manifest. It shares +`--config=integration`'s build, passes the binary no test selection (the Go +binary shards itself over every top-level test, 16 shards), and runs where +the integration lane runs (remote, or with the read-only cache). pr.yml's +gate requires it once `BAZEL_CMD_DOLT_REQUIRED` is `"true"`. Locally: +`bazel test //cmd/bd:bd_dolt_server_test --config=doltserver-cmd`. + An ambient `BEADS_DOLT_SERVER_PORT` or `BEADS_DOLT_PORT` is never honored by the suites that call `testutil.EnsureDoltContainerForTestMain`. When a test container is started, that container's port overwrites both variables; when one diff --git a/internal/storage/domain/beads.go b/internal/storage/domain/beads.go index c8c5b8c04..13ea46bae 100644 --- a/internal/storage/domain/beads.go +++ b/internal/storage/domain/beads.go @@ -39,7 +39,7 @@ type BeadsDirFSUseCase interface { InstallGitHooks(ctx context.Context, params HooksInstallParams) error InstallJJHooks(ctx context.Context) error AddAgentsInstructions(ctx context.Context, params AgentsFileParams) error - InstallClaudeProject(ctx context.Context, stealth bool) error + InstallClaudeProject(ctx context.Context, stealth, quiet bool) error SetYAMLConfig(ctx context.Context, key, value string) error } @@ -110,7 +110,7 @@ type BeadsDirFSAdapters struct { InstallGitHooks func(params HooksInstallParams) error InstallJJHooks func() error AddAgentsInstructions func(params AgentsFileParams) - InstallClaudeProject func(stealth bool) error + InstallClaudeProject func(stealth, quiet bool) error SetYAMLConfig func(key, value string) error } @@ -248,11 +248,11 @@ func (u *beadsDirFSUseCaseImpl) AddAgentsInstructions(ctx context.Context, param return nil } -func (u *beadsDirFSUseCaseImpl) InstallClaudeProject(ctx context.Context, stealth bool) error { +func (u *beadsDirFSUseCaseImpl) InstallClaudeProject(ctx context.Context, stealth, quiet bool) error { if u.adapters.InstallClaudeProject == nil { return fmt.Errorf("InstallClaudeProject: adapter not configured") } - return u.adapters.InstallClaudeProject(stealth) + return u.adapters.InstallClaudeProject(stealth, quiet) } func (u *beadsDirFSUseCaseImpl) SetYAMLConfig(ctx context.Context, key, value string) error { diff --git a/scripts/bazel_cache_mode_test.go b/scripts/bazel_cache_mode_test.go index 2d24667c6..e942f7378 100644 --- a/scripts/bazel_cache_mode_test.go +++ b/scripts/bazel_cache_mode_test.go @@ -215,7 +215,7 @@ func TestBazelCacheModeReachesTheRC(t *testing.T) { // bazel.yml's comment on the job). case strings.HasPrefix(mode, "fork-") && !runs && !bazelPackageJobs[name] && !(name == bazelRBEPrewarmJobName && strings.HasPrefix(mode, "fork-")): t.Errorf("%s does not run in mode %s (every lane runs remotely)", name, mode) - case mode == "cache" && name == bazelIntegJobName && !runs: + case mode == "cache" && (name == bazelIntegJobName || name == bazelCmdDoltJobName) && !runs: t.Errorf("%s does not run in mode cache", name) case mode == "cache" && bazelRemoteOnlyJobs[name] && runs: t.Errorf("remote-only %s runs in mode cache", name) @@ -277,6 +277,12 @@ func TestBazelCacheModeReachesTheRC(t *testing.T) { wantSkips = append(wantSkips, id) } } + // Flag-gated lanes' skips are accepted whatever the flag says. + for lane, g := range bazelFlagGatedLanes { + if !ran[lane] { + wantSkips = append(wantSkips, g.id) + } + } if len(lanes) == 0 { wantSkips = append(wantSkips, bazelAggregateGateID) } diff --git a/scripts/bazel_policy_test.go b/scripts/bazel_policy_test.go index a2c6cdd00..7e6f3c4e0 100644 --- a/scripts/bazel_policy_test.go +++ b/scripts/bazel_policy_test.go @@ -783,10 +783,12 @@ var allowedBazelTestTags = map[string]string{ "dolt-server": "starts hermetic dolt sql-servers (or completes the lane's job without -short); excluded from --config=prcore/ci, run by --config=doltserver", // The same rules as dolt-server (hermetic servers, remote, fail-closed: // checkDoltServerRules), for the tiers bazel.yml runs only with remote - // execution and, for the server storage tier, under the integration - // lane's build flags. + // execution (and, for the cmd/bd tier, the read-only cache) and, for the + // server storage and cmd/bd tiers, under the integration lane's build + // flags. "dolt-server-proxied": "proxied-server cmd/bd tier: starts hermetic dolt sql-servers; excluded from --config=prcore/ci, run by --config=doltserver-proxied", "dolt-server-integration": "server-Dolt storage tier: starts hermetic dolt sql-servers and needs the integration build tag; excluded from --config=prcore/ci, run by --config=doltserver-integration", + "dolt-server-cmd": "cmd/bd Dolt-server tier: the whole integration-tagged cmd/bd suite against hermetic dolt sql-servers; excluded from --config=prcore/ci, run by --config=doltserver-cmd", "embedded": "embedded-Dolt tier variant; excluded from --config=prcore/ci, run by --config=embedded", "manual": "never part of //...: a repro/bench harness, or a build input only another target needs; excluded from --config=prcore/ci", // For a go_test whose every test file is `//go:build integration`: in any @@ -802,7 +804,7 @@ var allowedBazelTestTags = map[string]string{ // and vice versa (TestBazelPRCoreExcludedTagsMatchTaxonomy), so a new lane // tag lands here, and through bazelIntegrationExcludedTags in the // integration lane's filter too. -var bazelPRCoreExcludedTags = []string{"requires-docker", "dolt-server", "dolt-server-proxied", "dolt-server-integration", "embedded", "manual", "integration-only"} +var bazelPRCoreExcludedTags = []string{"requires-docker", "dolt-server", "dolt-server-proxied", "dolt-server-integration", "dolt-server-cmd", "embedded", "manual", "integration-only"} // bazelIntegrationRunsTags are the PR-core-excluded tags --config=integration // runs: the integration lane is main.yml's integration jobs, whose diff --git a/scripts/ci_workflow_test.go b/scripts/ci_workflow_test.go index 99573dcdf..3d93f8cdf 100644 --- a/scripts/ci_workflow_test.go +++ b/scripts/ci_workflow_test.go @@ -2478,6 +2478,7 @@ const ( bazelIntegJobName = "bazel-integration" bazelProxiedJobName = "bazel-proxied" bazelServerJobName = "bazel-server-storage" + bazelCmdDoltJobName = "bazel-cmd-dolt" // F3: the MCP and npm package gates, moved here from pr.yml so they need // only the rbe job. Unlike every other lane they do not mirror a Bazel // config; pr.yml opts them in with package-gates: "on". @@ -2511,7 +2512,7 @@ const ( // bazel.yml's jobs: the rbe job that decides the execution mode, the // --config=ci lane, one job per CI job a Bazel config mirrors, and the two // package gates (F3), sorted alphabetically to match TestBazelWorkflowJobsAndExecutionMode's sort.Strings. -var bazelJobNames = []string{bazelDoltJobName, bazelEmbedJobName, bazelIntegJobName, bazelProxiedJobName, bazelPureJobName, bazelServerJobName, bazelJobName, bazelPackageMCPJobName, bazelPackageNPMJobName, bazelRBEJobName, bazelRBEPrewarmJobName} +var bazelJobNames = []string{bazelCmdDoltJobName, bazelDoltJobName, bazelEmbedJobName, bazelIntegJobName, bazelProxiedJobName, bazelPureJobName, bazelServerJobName, bazelJobName, bazelPackageMCPJobName, bazelPackageNPMJobName, bazelRBEJobName, bazelRBEPrewarmJobName} // The lanes that only run remotely (skipped unless the rbe job chose remote); // bazel-integration runs remotely or with the read-only cache (bazelIntegIf); @@ -2580,6 +2581,33 @@ var bazelLaneGateIDs = map[string]string{ bazelPackageNPMJobName: "PACKAGE_NPM", } +// bazelFlagGate: a lane pr.yml's ci-gate requires only while a committed +// top-level pr.yml flag is "true" (the WINDOWS_PREBUILT_REQUIRED pattern): +// the lane runs in pr.yml's call and always reports, its id is read into the +// gate step's env but is never in the static CI_GATE_REQUIRED list, and the +// run script adds it when the flag is "true". Until then it is advisory, so +// it carries job-level continue-on-error (a failure must not turn the call's +// aggregate, BAZEL, red); once required, its own id still reddens the gate. +type bazelFlagGate struct { + id string // its CI_GATE_REQUIRED id + flag string // pr.yml's top-level env flag + want string // the flag's committed value: flipping it is a reviewed change +} + +var bazelFlagGatedLanes = map[string]bazelFlagGate{ + // The cmd/bd Dolt-server tier (the Dolt-gated cmd/bd tests no other lane + // runs), advisory until its first clean runs. + bazelCmdDoltJobName: {id: "BAZEL_CMD_DOLT", flag: "BAZEL_CMD_DOLT_REQUIRED", want: "false"}, +} + +// bazelFlagGateRun is the run-script fragment that adds a flag-gated lane's +// id to CI_GATE_REQUIRED. +func bazelFlagGateRun(g bazelFlagGate) string { + return "if [[ \"$" + g.flag + "\" == \"true\" ]]; then\n" + + " CI_GATE_REQUIRED=\"$CI_GATE_REQUIRED " + g.id + "\"\n" + + "fi\n" +} + // rbe-prewarm: not a build/test lane at all (no Bazel config mirrors it), so // it has nothing for ci-gate to require; it is advisory by design // (continue-on-error, best-effort pre-warm dispatch) and reports only so @@ -2773,9 +2801,15 @@ func TestBazelWorkflowJobsAndExecutionMode(t *testing.T) { // continue-on-error here a pre-warm hiccup would still turn // bazel.yml's own run - and thus pr.yml's ci-gate - red for a job // nothing requires. - if job.ContinueOnError && name != bazelRBEPrewarmJobName { + _, flagGated := bazelFlagGatedLanes[name] + if job.ContinueOnError && name != bazelRBEPrewarmJobName && !flagGated { t.Errorf("%s continue-on-error hides failures from pr.yml's ci-gate", name) } + // A flag-gated lane is advisory until its flag says otherwise, and + // an advisory job's failure must not fail the call (BAZEL). + if flagGated && !job.ContinueOnError { + t.Errorf("%s is flag-gated (advisory until pr.yml's %s is \"true\") but lacks job-level continue-on-error", name, bazelFlagGatedLanes[name].flag) + } if job.TimeoutMinutes == 0 { t.Errorf("%s has no timeout-minutes", name) } @@ -2838,7 +2872,9 @@ func TestBazelWorkflowJobsAndExecutionMode(t *testing.T) { wantJobRunsOn, wantJobIf, wantJobSetupEnv = bazelPackageRunsOn, bazelPackageGatesIf, wantPackageSetupEnv } else if bazelRemoteOnlyJobs[name] { wantJobIf = wantRemoteOnlyIf - } else if name == bazelIntegJobName { + } else if name == bazelIntegJobName || name == bazelCmdDoltJobName { + // The cmd/bd Dolt-server tier runs exactly where the integration + // lane does: its build is --config=integration's. wantJobIf = bazelIntegIf } else if name == bazelRBEPrewarmJobName { // Same runs-on ternary as every other lane (wantRunsOn, set @@ -3350,9 +3386,10 @@ func TestBazelLaneIsGatedAlongsideLegacy(t *testing.T) { } _, gated := bazelLaneGateIDs[name] _, advisory := bazelAdvisoryLanes[name] - if gated == advisory { - t.Errorf("%s job %s: gated=%v advisory=%v; add it to exactly one of bazelLaneGateIDs (with a CI_GATE_REQUIRED id in pr.yml) or bazelAdvisoryLanes (with the reason)", - bazelWorkflowName, name, gated, advisory) + _, flagGated := bazelFlagGatedLanes[name] + if n := btoi(gated) + btoi(advisory) + btoi(flagGated); n != 1 { + t.Errorf("%s job %s: gated=%v advisory=%v flag-gated=%v; add it to exactly one of bazelLaneGateIDs (with a CI_GATE_REQUIRED id in pr.yml), bazelFlagGatedLanes (with its pr.yml flag) or bazelAdvisoryLanes (with the reason)", + bazelWorkflowName, name, gated, advisory, flagGated) } // Each lane reports its job.status from an always() last step. if !reflect.DeepEqual(job.Outputs, map[string]string{"result": "${{ steps.result.outputs.result }}"}) { @@ -3375,6 +3412,11 @@ func TestBazelLaneIsGatedAlongsideLegacy(t *testing.T) { t.Errorf("bazelAdvisoryLanes lists %s, which %s does not have", name, bazelWorkflowName) } } + for name := range bazelFlagGatedLanes { + if _, ok := workflow.Jobs[name]; !ok { + t.Errorf("bazelFlagGatedLanes lists %s, which %s does not have", name, bazelWorkflowName) + } + } gotCallOutputs := map[string]string{} for name, out := range call.Outputs { gotCallOutputs[name] = out.Value @@ -3509,6 +3551,27 @@ func TestBazelLaneIsGatedAlongsideLegacy(t *testing.T) { if _, ok := evaluate.Env[bazelAggregateGateID]; ok { t.Errorf("ci-gate env sets %s; it must come from %s aggregate", bazelAggregateGateID, bazelGateScript) } + // Flag-gated lanes: read like a gated lane, never statically required, + // added by the run script exactly when the committed flag is "true", + // and the flag's value is pinned (flipping it is a reviewed one-line + // change, here and in pr.yml). + for lane, g := range bazelFlagGatedLanes { + if want := "${{ needs.bazel.outputs." + lane + " || 'skipped' }}"; evaluate.Env[g.id] != want { + t.Errorf("ci-gate env %s = %q, want %q", g.id, evaluate.Env[g.id], want) + } + if contains(required, g.id) { + t.Errorf("CI_GATE_REQUIRED statically lists %s; only the %s conditional in the run script may add it", g.id, g.flag) + } + if got := pr.Env[g.flag]; got != g.want { + t.Errorf("pr.yml top-level env %s = %q, want %q (update bazelFlagGatedLanes in the same change that flips it)", g.flag, got, g.want) + } + frag := bazelFlagGateRun(g) + if strings.Count(evaluate.Run, frag) != 1 { + t.Errorf("ci-gate run script lacks exactly one\n%s", frag) + } else if strings.Index(evaluate.Run, frag) > strings.Index(evaluate.Run, "export CI_GATE_REQUIRED") { + t.Errorf("ci-gate run script adds %s after export CI_GATE_REQUIRED", g.id) + } + } for id, value := range evaluate.Env { for lane := range bazelAdvisoryLanes { if strings.Contains(value, "outputs."+lane) { @@ -3680,7 +3743,10 @@ type bazelGateScenario struct { // Non-Bazel needs' results that differ from what the jobs' if: give // (success, or skipped for pr.yml's legacy jobs that bazel-coverage // retired, D2 step 3). - results map[string]string + results map[string]string + // pr.yml's top-level flags the run script reads (the workflow env is + // not part of the step's env): bazelFlagGatedLanes' flags. + flags map[string]string wantPass bool wantMention string // a red gate must name this id } @@ -3706,6 +3772,9 @@ func runPRGateStep(t *testing.T, step ciWorkflowStep, sc bazelGateScenario) (boo t.Helper() expr := regexp.MustCompile(`^\$\{\{ needs\.([A-Za-z0-9_-]+)\.(result|outputs\.([A-Za-z0-9_-]+))( \|\| 'skipped')? \}\}$`) env := []string{"PATH=" + os.Getenv("PATH"), "GITHUB_EVENT_NAME=" + sc.event} + for key, value := range sc.flags { + env = append(env, key+"="+value) + } for key, value := range step.Env { if !strings.Contains(value, "${{") { env = append(env, key+"="+value) @@ -3833,6 +3902,10 @@ func TestBazelGateSimulation(t *testing.T) { anyRuns = true } else if id, gated := bazelLaneGateIDs[lane]; gated { wantSkips = append(wantSkips, id) + } else if g, flagGated := bazelFlagGatedLanes[lane]; flagGated { + // Listed even while advisory, so flipping the flag is the + // only change that makes it required. + wantSkips = append(wantSkips, g.id) } } if !anyRuns { @@ -3861,14 +3934,26 @@ func TestBazelGateSimulation(t *testing.T) { } return out } - sc := func(name, call string, outputs map[string]string, pass bool, mention string) { + // Flags at their committed values, unless a scenario overrides one. + committedFlags := map[string]string{} + for _, g := range bazelFlagGatedLanes { + committedFlags[g.flag] = pr.Env[g.flag] + } + scFlags := func(name, call string, outputs map[string]string, flags map[string]string, pass bool, mention string) { + all := copyMap(committedFlags) + for k, v := range flags { + all[k] = v + } for _, event := range []string{"pull_request", "merge_group"} { scenarios = append(scenarios, bazelGateScenario{ name: mode + "/" + event + "/" + name, event: event, mode: mode, enabled: enabledFor(mode), - call: call, outputs: outputs, wantPass: pass, wantMention: mention, + call: call, outputs: outputs, flags: all, wantPass: pass, wantMention: mention, }) } } + sc := func(name, call string, outputs map[string]string, pass bool, mention string) { + scFlags(name, call, outputs, nil, pass, mention) + } sc("every lane as designed", "success", base(), true, "") sc("aggregate failure no lane explains", "failure", base(), false, bazelAggregateGateID) @@ -3876,6 +3961,26 @@ func TestBazelGateSimulation(t *testing.T) { sc("aggregate skipped", "skipped", base(), mode == "skip", bazelAggregateGateID) for lane, modes := range lanes { + if g, flagGated := bazelFlagGatedLanes[lane]; flagGated { + off := map[string]string{g.flag: "false"} + on := map[string]string{g.flag: "true"} + // Advisory (flag "false"): continue-on-error keeps the + // aggregate green, and nothing the lane reports reddens + // the gate. + for _, result := range []string{"", "failure", "cancelled"} { + scFlags(lane+" reports "+result+" while advisory", "success", with(lane, result), off, true, "") + } + // Required (flag "true"): exactly a gated lane. + scFlags(lane+" required, as designed", "success", base(), on, true, "") + if !modes[mode] { + scFlags(lane+" required, ran and failed", "success", with(lane, "failure"), on, false, g.id) + continue + } + for _, result := range []string{"", "failure", "cancelled"} { + scFlags(lane+" required, alone "+result, "success", with(lane, result), on, false, g.id) + } + continue + } id, gated := bazelLaneGateIDs[lane] if !gated { // Advisory: not in the call, so it cannot report, and @@ -4370,7 +4475,7 @@ var ( // doltServerLaneTags are the tags of the lanes whose targets start // hermetic dolt sql-servers; checkDoltServerRules holds each of them to // the same rules. - doltServerLaneTags = []string{"dolt-server", "dolt-server-proxied", "dolt-server-integration"} + doltServerLaneTags = []string{"dolt-server", "dolt-server-proxied", "dolt-server-integration", "dolt-server-cmd"} bazelRuleNameRe = regexp.MustCompile(`(?m)^\s*name\s*=\s*"([^"]+)"`) ) @@ -5420,12 +5525,15 @@ func TestBazelWorkflowSecretsAndFailureSurface(t *testing.T) { secretRef := regexp.MustCompile(`\bsecrets\s*(\.|\[)`) walkYAML(root, "", func(path string, key bool, value string) { if key && value == "continue-on-error" { - // rbe-prewarm's job-level continue-on-error is the one - // deliberate exception (TestBazelWorkflowJobsAndExecutionMode's - // comment on the job has the full reasoning): without it, this - // advisory job's failure would still fail bazel.yml's own run - // and cascade into pr.yml's ci-gate. - if path != bazelRBEPrewarmContinueOnErrorPath { + // rbe-prewarm's job-level continue-on-error is a deliberate + // exception (TestBazelWorkflowJobsAndExecutionMode's comment on + // the job has the full reasoning): without it, this advisory + // job's failure would still fail bazel.yml's own run and + // cascade into pr.yml's ci-gate. So is each flag-gated lane's + // (bazelFlagGatedLanes): advisory until its pr.yml flag is + // "true", and required then through its own gate id. + _, flagGated := bazelFlagGatedLanes[strings.TrimSuffix(strings.TrimPrefix(path, ".jobs."), ".continue-on-error")] + if path != bazelRBEPrewarmContinueOnErrorPath && !flagGated { t.Errorf("%s: %s hides failures from pr.yml's ci-gate", bazelWorkflowName, path) } } @@ -6422,3 +6530,123 @@ func TestBazelWorkflowUploadsSurviveJobReruns(t *testing.T) { t.Errorf("found %d upload steps in %s, want at least 8", uploads, bazelWorkflowName) } } + +func btoi(b bool) int { + if b { + return 1 + } + return 0 +} + +// The cmd/bd Dolt-server tier (bazel.yml's bazel-cmd-dolt, advisory until +// pr.yml's BAZEL_CMD_DOLT_REQUIRED is "true": bazelFlagGatedLanes): the +// whole integration-tagged cmd/bd suite against a hermetic dolt sql-server, +// because every other lane, Bazel or go test, runs cmd/bd with +// BEADS_TEST_SKIP=dolt or only a manifest's tests, so its Dolt-gated tests +// ran nowhere. Coverage is exact by construction: the target passes the +// binary no test selection, the Go binary shards itself over every +// top-level test, and check_testcases.py fails a shard that ran none. +func TestBazelCmdDoltJob(t *testing.T) { + const config, target = "doltserver-cmd", "bd_dolt_server_test" + workflow := readCIWorkflow(t, bazelWorkflowName) + job := workflow.job(t, bazelCmdDoltJobName) + assertBazelTierStep(t, job, bazelCmdDoltJobName, config) + test := job.step(t, "bazel test //... --config="+config) + // Mode cache runs the shards on a 4-CPU GitHub-hosted runner; with the + // read-only cache closed the cold tagged race build comes first (as in + // TestBazelIntegrationJob). + if test.TimeoutMinutes < 60 || job.TimeoutMinutes <= test.TimeoutMinutes || job.TimeoutMinutes > test.TimeoutMinutes+15 { + t.Errorf("%s timeouts: step %d, job %d; want a step of at least 60 and a job above it by at most 15", bazelCmdDoltJobName, test.TimeoutMinutes, job.TimeoutMinutes) + } + if logs := job.step(t, "Upload test logs"); logs.If != "${{ failure() && steps.test.outcome != 'skipped' }}" || logs.With["name"] != "bazel-cmd-dolt-testlogs" { + t.Errorf("test-log upload: if=%q name=%q", logs.If, logs.With["name"]) + } + if !job.ContinueOnError { + t.Errorf("%s lacks job-level continue-on-error; while advisory its failure must not fail the call", bazelCmdDoltJobName) + } + + // .bazelrc: exactly --config=integration's build flags (so the lanes + // share every compile), the lane's own tag, no dolt skip, no test + // selection, Go's timeout under rbe-west's action cap. + rcText := readPolicyFile(t, bazelPolicyRoot(t), ".bazelrc") + rc := bazelrcLines(t) + for _, want := range []string{ + "build:" + config + " --@rules_go//go/config:tags=gms_pure_go,integration", + "build:integration --@rules_go//go/config:tags=gms_pure_go,integration", + "test:" + config + " --@rules_go//go/config:race", + "test:integration --@rules_go//go/config:race", + "test:" + config + " --test_tag_filters=dolt-server-cmd", + "test:" + config + " --test_timeout=-1,-1,-1,1200", + "test:" + config + " --test_arg=-test.timeout=19m", + "test:" + config + " --test_arg=-test.parallel=4", + "test:" + config + " --test_env=BEADS_TEST_REQUIRE_EXCLUDE_PERMISSION=1", + "test:" + config + " --test_env=GO_TEST_WRAP_TESTV=1", + } { + if !rc[want] { + t.Errorf(".bazelrc lacks %q", want) + } + } + for _, o := range bazelrcLaneOptions(parseBazelrcOptions(rcText), config) { + arg, _ := strings.CutPrefix(o.flag, "--test_arg=") + arg = strings.TrimLeft(arg, "-") + switch { + case strings.HasPrefix(o.flag, "--test_env=BEADS_TEST_SKIP"): + t.Errorf("%s sets %s: the lane exists to run the Dolt-gated tests", o.source(), o.flag) + case strings.HasPrefix(o.flag, "--test_filter"), + strings.HasPrefix(arg, "test.run"), strings.HasPrefix(arg, "test.skip"), strings.HasPrefix(arg, "test.short"): + t.Errorf("%s sets %s: the lane runs every cmd/bd test", o.source(), o.flag) + case strings.HasPrefix(o.flag, "--@rules_go//go/config:") && o.command == "build" && + o.flag != "--@rules_go//go/config:tags=gms_pure_go,integration": + t.Errorf("%s sets %s: its build must stay --config=integration's", o.source(), o.flag) + } + } + + // The target: the dolt-server rules (local backend, fail closed, remote), + // the integration build's bd_test with no test selection, every env + // bd_test's own go_test sets, and the 4-vCPU runner shape. + if os.Getenv("TEST_SRCDIR") != "" { + return // scripts_test's runfiles hold no other package's BUILD + } + build := readPolicyFile(t, sourceRepoRoot(t), "cmd/bd/BUILD.bazel") + var rule, bdTest string + for _, r := range bazelTopRules(stripStarlarkComments(build)) { + if m := bazelRuleNameRe.FindStringSubmatch(r); m != nil { + switch m[1] { + case target: + rule = r + case "bd_test": + bdTest = r + } + } + } + if rule == "" || bdTest == "" { + t.Fatalf("cmd/bd/BUILD.bazel lacks %s or bd_test", target) + } + for _, err := range checkDoltServerRules("cmd/bd", rule) { + t.Error(err) + } + for _, want := range []string{ + `srcs = ["//tools/bazel:go_test_variant.sh"]`, + `args = ["$(rootpath :bd_test)"]`, + `tags = ["dolt-server-cmd"]`, + `"GOMAXPROCS": "4"`, + `"BEADS_TEST_GIT_IDENTITY": "1"`, + } { + if !strings.Contains(rule, want) { + t.Errorf("%s lacks %s", target, want) + } + } + // 16 shards: each pays ~30-40 s of fixed cost (TestMain, its own dolt + // sql-server), so more shards mostly add farm actions; with the suite's + // long pole gone the slowest of 16 stays within bazel-integration's + // critical path. Change it together with bazel.yml's comment. + if m := regexp.MustCompile(`shard_count = (\d+)`).FindStringSubmatch(rule); m == nil || m[1] != "16" { + t.Errorf("%s shard_count = %v, want 16", target, m) + } + envKey := regexp.MustCompile(`"(BEADS_TEST_[A-Z_]+)": "\$\(rlocationpath [^)]+\)"`) + for _, m := range envKey.FindAllStringSubmatch(bdTest, -1) { + if !strings.Contains(rule, m[0]) { + t.Errorf("%s does not repeat bd_test's env %s", target, m[0]) + } + } +} diff --git a/scripts/pr_lanes_bazel_coverage_test.go b/scripts/pr_lanes_bazel_coverage_test.go index d673deb23..fe11f1062 100644 --- a/scripts/pr_lanes_bazel_coverage_test.go +++ b/scripts/pr_lanes_bazel_coverage_test.go @@ -69,7 +69,7 @@ var bazelPRLaneRCLines = map[string][]string{ "test:prcore --test_arg=-test.skip=^TestEmbedded", "test:prcore --test_env=BEADS_TEST_SKIP=dolt", "test:prcore --test_env=BEADS_TEST_REQUIRE_EXCLUDE_PERMISSION=1", - "test:prcore --test_tag_filters=-requires-docker,-dolt-server,-dolt-server-proxied,-dolt-server-integration,-embedded,-manual,-integration-only", + "test:prcore --test_tag_filters=-requires-docker,-dolt-server,-dolt-server-proxied,-dolt-server-integration,-dolt-server-cmd,-embedded,-manual,-integration-only", }, "ci": { "test:ci --config=prcore",