Files
romm/backend/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

46 lines
2.0 KiB
Python

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
# `clear_database` fixture in tests/conftest.py deletes every row in that
# database on every test. Always pin to a dedicated test database, made
# unique per pytest-xdist worker so parallel workers can't wipe rows another
# worker is mid-test with. This must run before any application module
# (config / database handlers) is imported, so the engine built at import
# time binds to the test name. As the rootdir conftest, this file is
# imported before `tests/conftest.py` (which imports those modules).
#
# The Redis cache needs no equivalent handling: under pytest it is an in-process
# FakeRedis, so each worker process is already isolated.
#
# 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")
os.environ["DB_NAME"] = f"{_base}_{_xdist_worker}" if _xdist_worker else _base