Files
romm/backend/tests/test_rootdir_conftest.py
Georges-Antoine Assi 4564c9c5e6 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>
2026-11-21 07:04:27 -05:00

60 lines
2.0 KiB
Python

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")
)