1"""The test layout rule the suite enforces at session start.
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"""
20#: Files every tests directory may hold under any name.
25 """Helper modules under a package's ``tests/`` without their package's prefix.
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
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():
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
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 )