fix: take the repository hold in the test scratch

Modules run side by side by run_tests.py each started pipelines or
generators that hold the repository root, and the second was refused:
four modules failed on the busy lock in a full run. tests/__init__ names
a scratch folder in RETROBIOS_REPOSITORY_LOCK_DIR, and artifact_lock
takes a repository root's lock there, keyed by the root's path so a
scratch checkout and its child find the same one. Real runs are
unchanged.
This commit is contained in:
Abdessamad Derraz committed 2026-11-12 16:29:58 +01:00
1 parent 68cd495fdc
commit c61cc21d88
3 files changed
+51 -1

No files matched your search

+15 -1
View File
@@ -217,6 +217,8 @@ class ArtifactDirectoryMissing(FileNotFoundError):
# here, os.pathsep apart, for the steps it spawns: they would otherwise be
# refused by it. A pipeline holds the repository root and its --output-dir.
HELD_LOCK_ENV = "RETROBIOS_HELD_ARTIFACT_DIR"
# Where the repository root's lock is taken instead, set by tests/__init__.
REPOSITORY_LOCK_ENV = "RETROBIOS_REPOSITORY_LOCK_DIR"
def _held_directories() -> list[str]:
@@ -250,7 +252,19 @@ def artifact_lock(directory: str, exclusive: bool = True) -> Iterator[None]:
if os.path.realpath(directory) in _held_directories():
yield
return
lock_path = os.path.join(directory, ".lock")
lock_dir = directory
# The test suite runs its modules side by side, each a process with its
# own scratch folder (scripts/run_tests.py): the repository's hold is
# taken there, so a module that runs the pipeline or a generator is not
# refused because another module holds it. Nothing else sets it.
moved = os.environ.get(REPOSITORY_LOCK_ENV)
real = os.path.realpath(directory)
if moved and os.path.isfile(os.path.join(real, "scripts", "artifacts.py")):
# Keyed by the root's path: a scratch checkout and the process it
# spawns name the same root, and find the same lock.
lock_dir = os.path.join(moved, real.strip(os.sep).replace(os.sep, "__"))
os.makedirs(lock_dir, exist_ok=True)
lock_path = os.path.join(lock_dir, ".lock")
mode = fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH
with open(lock_path, "w") as handle:
try:
+4
View File
@@ -31,6 +31,10 @@ _SCRATCH = Path(os.environ.get("RETROBIOS_TEST_SCRATCH")
or Path(__file__).resolve().parent.parent / "tmp" / "tests")
_SCRATCH.mkdir(parents=True, exist_ok=True)
os.environ.setdefault("RETROBIOS_PROBE_LOCK", str(_SCRATCH / "probe.lock"))
# The repository's own hold (database.json, install/, README.md) too: a
# pipeline run by one module no longer refuses another module's, nor the
# maintainer's real run beside the suite.
os.environ.setdefault("RETROBIOS_REPOSITORY_LOCK_DIR", str(_SCRATCH / "repository-lock"))
# A TemporaryDirectory() without dir= landed in /tmp, a 4 GB tmpfs here: once
# another run filled it, modules unrelated to any change failed with ENOSPC.
# The default is the repository's tmp/, for this process and its children.
+32
View File
@@ -28,6 +28,8 @@ from common import ( # noqa: E402
publish_hold,
)
from tests.scratchtree import scratch_checkout # noqa: E402
import artifacts # noqa: E402
from unittest import mock # noqa: E402
def _tempdir(test: unittest.TestCase) -> str:
@@ -257,6 +259,36 @@ class PipelineHoldsItsFixedOutputs(unittest.TestCase):
pass
class TheSuiteTakesTheRepositoryHoldInItsScratch(unittest.TestCase):
"""scripts/run_tests.py runs modules side by side: a pipeline one module
ran held the repository root, and every module that ran another one, or
a generator writing database.json, failed on the busy lock."""
def test_the_hold_is_taken_where_the_variable_says(self):
scratch = Path(tempfile.mkdtemp(dir=REPO_ROOT / "tmp"))
self.addCleanup(shutil.rmtree, scratch, True)
moved = scratch / "repository-lock"
with mock.patch.dict(os.environ, {artifacts.REPOSITORY_LOCK_ENV: str(moved)}):
with artifacts.hold_repository_outputs(str(REPO_ROOT)):
key = str(REPO_ROOT.resolve()).strip(os.sep).replace(os.sep, "__")
self.assertTrue((moved / key / ".lock").is_file())
# Held there: a second hold in the same tree is refused.
with self.assertRaises(artifacts.ArtifactLockBusy):
with artifacts.hold_repository_outputs(str(REPO_ROOT)):
pass
def test_another_directory_keeps_its_own_lock(self):
out = Path(tempfile.mkdtemp(dir=REPO_ROOT / "tmp"))
self.addCleanup(shutil.rmtree, out, True)
with mock.patch.dict(os.environ, {artifacts.REPOSITORY_LOCK_ENV: str(out / "x")}):
with artifacts.artifact_lock(str(out)):
self.assertTrue((out / ".lock").is_file())
self.assertFalse((out / "x").exists())
def test_the_suite_sets_it(self):
self.assertTrue(os.environ.get(artifacts.REPOSITORY_LOCK_ENV))
class StandaloneWritersHonourTheRepositoryHold(unittest.TestCase):
"""Only pipeline runs took the repository's lock: generate_db.py beside
a pipeline replaced database.json between its verify and its packs, and