Coverage for packages/workshop/src/livery/workshop/_pytest_layout.py: 100%

Measured by coverage.py 7.16.2 on CPython 3.14 while the suites of tests, packages/workshop, packages/extensions/ty, packages/extensions/ruff, packages/extensions/python, packages/extensions/pytest, packages/extensions/pyrefly, packages/extensions/nanobind, packages/extensions/mypy, packages/extensions/mkdocstrings, packages/extensions/lfs, packages/extensions/docs, packages/extensions/cpp, packages/extensions/coverage, packages/extensions/conan, packages/extensions/cmake, packages/extensions/claude, packages/extensions/clang-tidy, packages/extensions/clang-format, packages/extensions/changelog, packages/extensions/basedpyright ran.

1"""The test layout rule the suite enforces at session start.
2
3Test modules are named by their path (pytest's importlib mode, which
4the project template sets), so two packages may both have a
5``tests/test_lifecycle.py``. That leaves the tests directories off
6``sys.path``; the template puts every package's ``tests`` directory on
7``pythonpath`` instead, one shared path, so a helper module in
8``packages/<dir>/tests/`` starts with ``<dir>_`` (dashes as
9underscores) and two packages never ship the same helper name. A
10session whose layout breaks that is refused before any test runs,
11naming each module and the name it wants.
12"""
13
14from __future__ import annotations(empty)
15
16from pathlib import Path(empty)
17
18import pytest(empty)
19
20#: Files every tests directory may hold under any name.
21UNNAMED = ("conftest.py", "__init__.py")(empty)
22
23
24def offenders(root: Path) -> list[tuple[Path, str]]:(empty)
25 """Helper modules under a package's ``tests/`` without their package's prefix.
26
27 Each entry is the module and the name it wants. A directory under
28 ``packages`` counts when it carries a ``workshop.toml``; test files
29 and the files every directory may hold are never offenders. The
30 prefix is the package's path under ``packages/``, so a package in
31 a group directory carries the group's name too
32 (``extensions_widgets_``).
33 """
34 from livery.workshop import package_directories
35
36 found: list[tuple[Path, str]] = []
37 for package in package_directories(root):
38 tests = package / "tests"
39 if not (package / "workshop.toml").is_file() or not tests.is_dir():
40 continue(empty)
41 member = package.relative_to(root / "packages").as_posix()
42 prefix = member.replace("-", "_").replace("/", "_") + "_"
43 for module in sorted(tests.glob("*.py")):
44 if module.name.startswith("test_") or module.name in UNNAMED:
45 continue
46 if not module.name.startswith(prefix):
47 found.append((module, prefix + module.name))
48 return found
49
50
51def pytest_sessionstart(session: pytest.Session) -> None:(empty)
52 """Refuse the session when a helper module lacks its package's prefix."""
53 root = Path(session.config.rootpath)
54 bad = offenders(root)
55 if not bad:
56 return
57 listed = "\n".join(
58 f" {path.relative_to(root)}: rename to {wanted}" for path, wanted in bad
59 )
60 raise pytest.UsageError(
61 "a helper module in a package's tests directory carries the package's"
62 " name, so every tests directory can share one pythonpath without a"
63 f" clash:\n{listed}"
64 )