Repository navigation
test(bats): isolate worktree.bats and tighten the BATS suite - #1709
Merged
Merged
Conversation
`just worktree-clean` force-removes every entry in its `<repo>-worktrees` base and `git branch -D`s each branch, so the tests that point it at the real sibling directory delete a developer's or an agent's live worktrees. Assert the base lives under BATS_TEST_TMPDIR before any recipe runs and before any directory is created; 6 of the 10 tests are red against the current wiring. Refs: #1694
`_wt_base` is `../<basename $(git rev-parse --show-toplevel)>-worktrees` resolved against just's working directory, so `--working-directory "$FIX"` on a throwaway `git init` fixture under BATS_TEST_TMPDIR moves the whole base into the test's own tmpdir. No product change: justfile.worktree and its scaffolded mirror are untouched, and the #1463 tests in this file already used the idiom. Consequences: the fixtures can no longer collide with a real worktree, so the five `CI=true` skips go (tmux is on the CI PATH via setup-env and `tmux new-session -d` needs no TTY) and the file rejoins bats' within-file parallelism — the #1687 `BATS_NO_PARALLELIZE_WITHIN_FILE` opt-out was a symptom of the shared base, not of the tmux server. Per-test issue numbers carry a `$$-$BATS_TEST_NUMBER` tag so derived tmux session names stay unique. The two remaining host paths (`/tmp/bats-claude-$$`, `mktemp -d`) move under BATS_TEST_TMPDIR, which also retires the manual `rm -rf`/`rmdir` cleanups. Refs: #1694
bats 1.5 gates the `run` flags behind `bats_require_minimum_version` and warns (BW02) on every use without one. The declaration has to sit at file top level, so it cannot be centralised in test_helper.bash — a per-file rule that only review enforces. Pin it instead: one parametrized case per `tests/bats/*.bats`, asserting the declaration is the file's first statement. 20 of the 21 files are red; githooks.bats already declares it. Refs: #1697
One line per file, the first statement after the header comment, satisfying the guard added with the previous commit. githooks.bats already carried it, so the other 20 files gain it; no test body changes. Today the suite emits exactly one BW02 (init-workspace.bats:1801, `run --separate-stderr`), so the value here is drift-proofing rather than log volume: the next `run -0` in any of the 21 files cannot reintroduce the warning. Refs: #1697
setup_file rendered seven fixtures one after another, and that prologue is pure critical path under `bats -j`: no test in the file can start until it finishes while the other cores idle. Each render already wrote to its own log, so the only serial element was the `|| return 1` short-circuit. Background them all, `wait` on each pid, and dump EVERY failed fixture's log rather than the first. Measured in `-j 8` runs on an 8-core host: 4.85/5.81/6.25 s before, 0.58/0.64/0.61 s after — with nine fixtures instead of seven. The fixture list becomes declarative (one `<name> <mode> [extra args]` line each) so the next cluster that needs a shared tree adds a line instead of a per-test render, and marker seeding moves into a `_seed_shared` helper. A fixture now costs ~0 wall time, which is what #1687 (WP3) was waiting for before giving the two-test Cargo.toml and nix/module.nix seed groups their own fixtures: cargo-both and nix-module-both are added and the four tests clone them (250 -> 248 renders). The two nix tests pre-created `$ws/nix`, so they now create only `$ws` — `_clone_shared` refuses a non-empty target by contract. No red-first test: this is test infrastructure whose contract is the suite itself. Evidence instead — 300/300 under `-j 8` and sequentially, the invocation count above, and a deliberate two-fixture break confirming both failures are reported. Refs: #1695
`_upgrade` and `_scaffold` differ only in their stubs: `_scaffold` stubs `just` so the tail `just sync` never runs, `_upgrade` keeps the real `just` with `uv` stubbed so the recipe runs and no-ops. Nobody had checked whether that leaves a trace, so the `_upgrade both` sites kept re-rendering. Settled by evidence: rendered into empty directories three times over, the two trees are `diff -r` clean with identical `find -printf '%m %y %p'` listings. The only difference is on stdout — the extra `just sync: no pyproject.toml — skipping` line (plus the workspace path and rsync's byte counters). The verdict is recorded in `_clone_shared`'s CONTRACT so it is not re-litigated. So the 5 pure-setup sites clone the `both` fixture instead (the first run of the #878 stock test, whose subject is the second run, and the four #886 `--preview` setups): 248 -> 243 renders. The other 30 of the 35 `_upgrade` tests seed a consumer file first or assert on the run itself and keep their real invocation. Refs: #1696
Three review fixes on the #1694 isolation. The guard pinned WT_BASE, which no recipe reads: the recipes derive `../<basename $(git rev-parse --show-toplevel)>-worktrees` from just's working directory, and that is FIX. Pin FIX first, keep the WT_BASE asserts so the two cannot drift, and call the guard from `_wt_fixture` before its `git init` — that helper `git add -A`s and commits inside FIX, so it mutates a real checkout before any recipe would run. Proven by temporarily pointing FIX at PROJECT_ROOT: the assert fires from `_wt_fixture`, index untouched. `_run_wt_start` is the file's one other real-recipe path; worktree-start creates `<main>-worktrees/<issue>`, so it now runs the same guard against its own sandbox. The tmux server is process-global, so an assertion failing before a test reaches its own `kill-session` leaked a session — now that the file runs in CI and under `-j`, into the runner and its peers. A teardown reaps only what the test derived: a `wt-` name ending in this test's unique WT_TAG, or the claude probe's PID-tagged name. An unset WT_TAG matches nothing, so `wt-*` can never sweep a developer's real sessions. Test 1 gave a backgrounded `just` exactly 2 s before a single `has-session`, which on a cold runner is a false negative rather than a slow pass. Poll up to 5 s instead. Refs: #1694
A bare `assert` left the failure as `assert 'setup() {' == 'bats_require…'`,
which does not say which file or what to add. Both asserts now name the file
relative to the repo root, the exact line required and where it goes.
Refs: #1697
The Unreleased #1687 entry still says worktree.bats "opts out of within-file jobs: its tests drive real tmux sessions and the repo's sibling worktrees directory". Both halves are now false: the tests drive an isolated fixture repo under BATS_TEST_TMPDIR and the file runs under `-j` and in CI. This corrects an unreleased sentence in place rather than adding an entry — the isolation work itself is changelog-exempt. The synced mirror is regenerated by sync-manifest. Refs: #1694
Contributor
Author
Verification on the first CI runRun 36124206119: all 15 checks green,
Lane timing: |
This was referenced Sep 25, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Four tests-only issues in one PR. The load-bearing one is #1694:
tests/bats/worktree.batsdrove
just worktree-cleanagainst the repository's live<repo>-worktreessiblingdirectory, and that recipe force-removes every entry in it and
git branch -Ds its branch.On 2026-09-25 a local full-suite run deleted three agent worktrees and their branches
mid-work. The file now drives every recipe against a throwaway
git initfixture underBATS_TEST_TMPDIR, which also lets it drop itsCI=trueskips and rejoinbats -j'swithin-file parallelism. The other three tighten the same suite: a per-file
bats_require_minimum_versiondeclaration with a pytest guard (#1697), concurrent fixturerendering in
init-workspace.bats'ssetup_file(#1695), and the settled_upgrade≡_scaffoldequivalence that collapses the last pure-setup renders (#1696).No product code changes:
justfile.worktree, its scaffolded mirror andassets/init-workspace.share untouched. The only non-test file isCHANGELOG.md(plus its synced mirror), where an unreleased #1687 sentencehad to be corrected — see Changelog Entry.
Review fixes are appended as their own commits rather than folded into the
reviewed history.
Type of Change
feat-- New featurefix-- Bug fixdocs-- Documentation onlychore-- Maintenance task (deps, config, etc.)refactor-- Code restructuring (no behavior change)test-- Adding or updating testsci-- CI/CD pipeline changesbuild-- Build system or dependency changesrevert-- Reverts a previous commitstyle-- Code style (formatting, whitespace)Modifiers
!) -- This change breaks backward compatibilityChanges Made
#1694 — worktree.bats no longer touches the live worktrees directory
_wt_assert_throwaway(committed red-first) asserts the base is non-empty and under$BATS_TEST_TMPDIRbefore any recipe runs or any directory is created. 6 of the 10 testswere red against the old wiring.
_wt_fixturegit inits a one-commit throwaway repo at$BATS_TEST_TMPDIR/fixture;_wt_justruns the recipes with--justfile "$WT_MAIN" --working-directory "$FIX"._wt_baseis../<basename $(git rev-parse --show-toplevel)>-worktreesresolved againstjust's working directory, so that one flag moves the whole base to
$FIX-worktrees—no product knob, and the
#1463tests in the same file already used the idiom.[ "${CI:-}" = "true" ] && skiplines go: tmux reaches the CI PATH viasetup-envandtmux new-session -dneeds no TTY. Thecommand -v tmux/just/claudeskips stay.
setup_file'sBATS_NO_PARALLELIZE_WITHIN_FILE(added by chore(ci): cut the BATS suite runtime #1690) goes too — it was asymptom of the shared base, not of the tmux server. Issue numbers carry a
$$-$BATS_TEST_NUMBERtag so derived session names stay unique./tmp/bats-claude-$$and amktemp -dmove underBATS_TEST_TMPDIR, which retires themanual
rm -rf/rmdircleanups.tests/bats/worktree-claude-cli.batsneeds no change: pure recipe-grep, no directories,no recipe invocations, no tmux.
#1697 —
bats_require_minimum_versionin every filetests/test_bats_shape.py: one parametrized case pertests/bats/*.batsassertingthe declaration is the file's first statement, plus a discovery guard so a zero-file glob
cannot pass silently. Committed red (20 failures) before the fix.
githooks.batsalready had it.#1695 — concurrent
setup_filerenders<name> <mode> [extra args]line each), markerseeding moves into
_seed_shared, and all renders are backgrounded andwaited on, withevery failed fixture's log dumped rather than the first.
cargo-both,nix-module-both) now that a fixture costs ~0 wall time;the four
Cargo.toml/nix/module.nixseed-group tests clone them. The two nix testscreate
$wsinstead of$ws/nix—_clone_sharedrefuses a non-empty target.#1696 — collapse the pure-setup
_upgrade bothrenders_upgrade≡_scaffoldquestion is settled by evidence (below) and the verdict iswritten into
_clone_shared's CONTRACT comment so it is not re-litigated.bothfixture: the first run of the#878stock test(whose subject is the second run) and the four
#886--previewsetups. The other 30 ofthe 35
_upgradetests seed a consumer file first or assert on the run itself and keeptheir real invocation.
Review round
WT_BASE, which no recipe reads — the recipes derive../<basename $(git rev-parse --show-toplevel)>-worktreesfrom just's working directory,and that is
FIX.FIXis now pinned first (theWT_BASEasserts stay, so the two cannotdrift), and
_wt_fixtureruns the guard before itsgit init: that helpergit add -As and commits insideFIX, so it mutates a real checkout before any recipewould.
_run_wt_start— the file's one other real-recipe path, and the one that actuallygit worktree adds — runs the same guard against its own sandbox.teardown()reaps leaked tmux sessions: only awt-name ending in this test's uniqueWT_TAG, or the claude probe's PID-tagged name. An unsetWT_TAGmatches nothing, sowt-*can never sweep a developer's real sessions.justexactly 2 s before a singlehas-session; it now pollsup to 5 s, so a cold runner is a slow pass rather than a false negative.
tests/test_bats_shape.pyname the offending file, the exact line requiredand where it goes.
CHANGELOG.md: the unreleased [CHORE] BATS runtime: parallelize the suite, batch the init-workspace.sh fork loops, collapse redundant scaffolds #1687 sub-bullet is corrected (below).Changelog Entry
No changelog needed for the work itself. All four issues are labelled "No changelog needed":
the change is confined to
tests/and alters no consumer-visible behaviour, no scaffoldedasset and no recipe.
What this PR does touch is a correction to an existing, still-unreleased sentence, not a
new entry. The #1687 bullet under
## Unreleasedclaimed the opposite of what is now true:Nothing below
## Unreleasedis touched.assets/workspace/.devcontainer/CHANGELOG.mdisthe hook-synced mirror, regenerated by
sync-manifest.Testing
just test)Manual Testing Details
#1694 red-first pin —
bats tests/bats/worktree.bats,CIunset:The pin fires before the
mkdirand before the recipe, so nothing was created outside atmpdir and no worktree or branch was removed. Note test 6 (
wt-clean alias) had noCIskip — it ranjust wt-cleanagainst the real base even underCI=true.#1694 green —
worktree.bats10/10 with 0 skips serially (CIunset), 10/10 underCI=true bats -j 8, and 10/10 on five consecutivebats -j 8runs — 0 flakes. The fiveworktrees of this checkout (
devkit,1692,1693,1694,1698) are listed unchangedbefore and after every run.
#1697 — before: exactly one
BW02, frominit-workspace.bats:1801(
run --separate-stderr), printed in a trailing warnings block rather than interleaved.After:
bats -j 8 tests/bats/ 2>&1 | grep -c BW02= 0, 602/602 unchanged.pytest tests/test_bats_shape.py22 passed.#1695 —
setup_filewall time, measured withEPOCHREALTIMEinsidebats -j 8runs onan 8-core host (instrumentation not committed):
init-workspace.shinvocations per file run (counting shim, not committed): 250 -> 248.init-workspace.bats300/300 underbats -j 8and 300/300 sequentially.Multi-failure diagnostics proved by temporarily breaking two fixtures (
direnv,bare):#1696 equivalence verdict — identical.
_upgrade bothand_scaffold bothrenderedinto empty directories, three rounds:
diff -ris clean and thefind -printf '%m %y %p' | sortlistings are identical in everyround. Only stdout differs — the workspace path echo, rsync's byte counters, and the extra
line the real
justproduces:Invocations after the conversion: 248 -> 243.
init-workspace.bats300/300.Review round — the
FIXassert is load-bearing. Temporarily pointingFIXat$PROJECT_ROOT(not committed): the guard fires from_wt_fixtureat the line beforegit init, so nothing is created, staged or committed.That is why the guard moved into
_wt_fixture: the reviewed version protected only therecipes, and
_wt_fixtureitself doesgit add -A+commitinsideFIX.Review round — green.
worktree.bats10/10 on three consecutivebats -j 8runs with0 skips, and 10/10 under
CI=true bats -j 8.tmux list-sessions | grep -c '^wt-'= 0after every run, so the new
teardownleaks nothing.Review round — the assertion message. With the declaration removed from
just.bats:Whole suite —
bats -j 8 tests/bats/: 602/602, 0 failures,grep -c BW02= 0, and0 skipped tests (
grep -c '# skip'= 0). An earlier draft of this body said "19 skips";that number came from
grep -c skip, which counts test names containing the word — e.g.init-precommit.sh skips cleanly when no config is present (#854). Nothing in the suiteskips.
prek run --all-files: 53 hooks, every one Passed, exit 0, in both hook roots (.andassets/workspace).Checklist
docs/templates/, then runjust docs)CHANGELOG.mdin the[Unreleased]section (and pasted the entry above)Additional Notes
Three of the four issues carry factual corrections, posted separately as issue comments:
#1694 misses that test 6 was unguarded by the
CIskip and asserts a TTY requirement thatdoes not exist; #1697 states that no file declares the version and implies many warnings,
where
githooks.batsalready declared it and the suite emitted exactly one; #1696 estimates~13 convertible sites where 5 qualify under WP3's conversion rule.
The
#1694fix is what makes the rest of this PR safe to run: until it landed, any localbats tests/bats/orjust test-batscould delete a sibling worktree.Refs: #1694, #1697, #1695, #1696