chore(coherence): give each worktree its own test database automatically (#5680)

* chore(coherence): give each worktree its own test database automatically

The rootdir conftest now sets ROMM_TEST_DB_TAG to the linked worktree's
folder name when it is unset, so a worktree never runs against the shared
romm_test. With the rule enforced, the "worktree test database" practice
no longer fires on every `uv run pytest`: each firing reopened an owed
enactment at Stop, and sessions committed one identical record per run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: load the rootdir conftest by path

Under a full run, `from conftest import` resolves to whichever conftest
pytest imported first, so the worktree_tag tests failed to collect.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(tests): hash the worktree path into its test database tag

Addresses the Greptile review on #5680: worktree folders that sanitize or
truncate to the same name no longer share a temporary test database.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-11-21 07:04:27 -05:00
committed by GitHub
parent 2469839b66
commit 4564c9c5e6
4 changed files with 89 additions and 8 deletions

View File

@@ -81,13 +81,12 @@ place for full-suite coverage; local runs stay scoped.
**In a git worktree, run tests against a temporary database.** Every checkout shares
`romm_test`, and one on another branch can migrate it to a revision yours lacks, so
every test errors in setup with `Can't locate revision`. Set `ROMM_TEST_DB_TAG` to a
tag unique to the worktree: the run creates and migrates `romm_test_tmp_<tag>` (one per
xdist worker), then drops it at the end. Never `stamp` or downgrade the shared `romm_test`.
```bash
ROMM_TEST_DB_TAG=my_worktree uv run pytest -n auto tests/handler/
```
every test errors in setup with `Can't locate revision`. In a linked worktree,
`backend/conftest.py` sets `ROMM_TEST_DB_TAG` to the worktree's folder name plus a
short hash of its path: the run creates and migrates `romm_test_tmp_<tag>` (one per
xdist worker), then drops it at the end. Set the variable yourself to pick another tag,
for example to run two suites in one worktree at once. Never `stamp` or downgrade the
shared `romm_test`.
---

View File

@@ -1,5 +1,5 @@
- worktree test database: A backend test run in a git worktree uses its own temporary database, never the shared romm_test.
when: command uv run pytest
when: explicit
step: set ROMM_TEST_DB_TAG to a tag unique to the worktree before running pytest
step: select the tests affected by the change; never run the whole suite locally
step: leave the shared romm_test alone: never stamp or downgrade it

View File

@@ -1,5 +1,21 @@
import hashlib
import os
import re
from pathlib import Path
def worktree_tag(start: Path) -> str:
"""Tag the linked git worktree (whose `.git` is a file) holding `start`, or ""."""
for folder in (start, *start.parents):
dot_git = folder / ".git"
if dot_git.is_file():
# Folder names that sanitize or truncate alike still get separate databases.
digest = hashlib.sha256(str(folder).encode()).hexdigest()[:8]
return f"{folder.name[:31]}_{digest}"
if dot_git.exists():
return ""
return ""
# Tests must never inherit DB_NAME from the ambient environment (e.g. a
# sourced .env pointing at a real dev/prod database) -- the autouse
@@ -16,6 +32,13 @@ import re
#
# ROMM_TEST_DB_TAG runs use temporary databases that tests/conftest.py drops at
# session end; the `tmp_` prefix keeps them apart from the untagged worker names.
# A linked worktree gets one by default, since another checkout's migrations on
# the shared romm_test would break its runs.
if not os.environ.get("ROMM_TEST_DB_TAG") and (
_worktree := worktree_tag(Path(__file__).resolve().parent)
):
os.environ["ROMM_TEST_DB_TAG"] = _worktree
_tag = re.sub(r"[^A-Za-z0-9_]", "_", os.environ.get("ROMM_TEST_DB_TAG", ""))
_base = f"romm_test_tmp_{_tag}" if _tag else "romm_test"
_xdist_worker = os.environ.get("PYTEST_XDIST_WORKER")

View File

@@ -0,0 +1,59 @@
import importlib.util
from pathlib import Path
# Loaded by path: under a full run `conftest` resolves to whichever one pytest cached first.
_spec = importlib.util.spec_from_file_location(
"rootdir_conftest", Path(__file__).resolve().parents[1] / "conftest.py"
)
assert _spec and _spec.loader
_rootdir_conftest = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(_rootdir_conftest)
worktree_tag = _rootdir_conftest.worktree_tag
def test_worktree_tag_names_a_linked_worktree(tmp_path: Path) -> None:
worktree = tmp_path / "resilient-imagining-clover"
(worktree / "backend").mkdir(parents=True)
(worktree / ".git").write_text("gitdir: /repo/.git/worktrees/x\n")
assert worktree_tag(worktree / "backend").startswith("resilient-imagining-clover_")
def test_worktree_tag_is_empty_in_the_main_checkout(tmp_path: Path) -> None:
(tmp_path / ".git").mkdir()
(tmp_path / "backend").mkdir()
assert worktree_tag(tmp_path / "backend") == ""
def test_worktree_tag_is_empty_outside_a_repository(tmp_path: Path) -> None:
assert worktree_tag(tmp_path) == ""
def test_worktree_tag_fits_a_database_name(tmp_path: Path) -> None:
worktree = tmp_path / ("w" * 80)
worktree.mkdir()
(worktree / ".git").write_text("gitdir: /repo/.git/worktrees/x\n")
assert len(worktree_tag(worktree)) == 40
def test_worktree_tag_separates_names_that_sanitize_alike(tmp_path: Path) -> None:
for name in ("feature-auth", "feature_auth"):
(tmp_path / name).mkdir()
(tmp_path / name / ".git").write_text("gitdir: /repo/.git/worktrees/x\n")
assert worktree_tag(tmp_path / "feature-auth") != worktree_tag(
tmp_path / "feature_auth"
)
def test_worktree_tag_separates_names_that_truncate_alike(tmp_path: Path) -> None:
for suffix in ("a", "b"):
worktree = tmp_path / ("w" * 50 + suffix)
worktree.mkdir()
(worktree / ".git").write_text("gitdir: /repo/.git/worktrees/x\n")
assert worktree_tag(tmp_path / ("w" * 50 + "a")) != worktree_tag(
tmp_path / ("w" * 50 + "b")
)