2026-03-25 21:56:37 +01:00
|
|
|
#!/usr/bin/env python3
|
2026-05-30 22:13:32 +02:00
|
|
|
"""Run pytest for all python_pkg subpackages whenever any Python file changes.
|
2026-03-25 21:56:37 +01:00
|
|
|
|
2026-05-30 22:13:32 +02:00
|
|
|
Used as a pre-commit hook entry point. Receives staged file paths as arguments.
|
|
|
|
|
If any Python file changed, discovers every subpackage under ``python_pkg/``
|
|
|
|
|
that has a ``tests/`` directory and runs them all in a single parallelised
|
|
|
|
|
invocation with whole-repo coverage measured against ``python_pkg``.
|
2026-03-25 21:56:37 +01:00
|
|
|
|
2026-05-30 22:13:32 +02:00
|
|
|
Running all packages together (rather than just the touched ones) ensures that
|
|
|
|
|
100% branch coverage is maintained across the entire codebase on every commit,
|
|
|
|
|
not just the files that happened to change.
|
2026-06-04 18:13:47 +02:00
|
|
|
|
|
|
|
|
Standalone script suites outside ``python_pkg/`` (currently
|
|
|
|
|
``linux_configuration/tests``) are also run so their behaviour is gated, but
|
|
|
|
|
they are not coverage-measured (coverage stays scoped to ``python_pkg``).
|
2026-03-25 21:56:37 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-04-12 21:58:18 +02:00
|
|
|
import os
|
2026-05-30 22:13:32 +02:00
|
|
|
from pathlib import Path
|
2026-04-12 21:43:18 +02:00
|
|
|
import shutil
|
2026-03-25 21:56:37 +01:00
|
|
|
import subprocess
|
|
|
|
|
import sys
|
|
|
|
|
|
2026-05-14 21:52:52 +02:00
|
|
|
_TOTAL_MEM = "4G"
|
2026-03-25 21:56:37 +01:00
|
|
|
|
2026-06-04 18:13:47 +02:00
|
|
|
# Standalone script test suites outside python_pkg/ that should be gated but
|
|
|
|
|
# not coverage-measured. Skipped silently if the directory does not exist.
|
|
|
|
|
_EXTRA_TEST_DIRS = ("linux_configuration/tests",)
|
|
|
|
|
|
2026-03-25 21:56:37 +01:00
|
|
|
|
2026-05-30 22:13:32 +02:00
|
|
|
def main() -> int:
|
|
|
|
|
"""Entry point."""
|
|
|
|
|
if not sys.argv[1:]:
|
|
|
|
|
return 0
|
2026-03-25 21:56:37 +01:00
|
|
|
|
2026-05-30 22:13:32 +02:00
|
|
|
packages = sorted(
|
|
|
|
|
entry.name
|
|
|
|
|
for entry in Path("python_pkg").iterdir()
|
|
|
|
|
if (entry / "tests").is_dir()
|
|
|
|
|
)
|
|
|
|
|
if not packages:
|
|
|
|
|
return 0
|
2026-03-25 21:56:37 +01:00
|
|
|
|
2026-06-04 18:13:47 +02:00
|
|
|
test_dirs = [f"python_pkg/{pkg}/tests" for pkg in packages]
|
|
|
|
|
test_dirs += [d for d in _EXTRA_TEST_DIRS if Path(d).is_dir()]
|
|
|
|
|
|
2026-05-14 21:52:52 +02:00
|
|
|
cmd = [
|
2026-03-25 21:56:37 +01:00
|
|
|
sys.executable,
|
|
|
|
|
"-m",
|
|
|
|
|
"pytest",
|
2026-05-30 22:13:32 +02:00
|
|
|
"--cov",
|
|
|
|
|
"python_pkg",
|
2026-03-25 21:56:37 +01:00
|
|
|
"--cov-branch",
|
|
|
|
|
"--cov-report=term-missing",
|
|
|
|
|
"--cov-fail-under=100",
|
|
|
|
|
"-q",
|
2026-05-14 21:52:52 +02:00
|
|
|
"-n",
|
2026-05-22 22:48:28 +02:00
|
|
|
"4",
|
2026-05-30 22:13:32 +02:00
|
|
|
# Override addopts from pyproject.toml to avoid double --cov flags.
|
2026-03-25 21:56:37 +01:00
|
|
|
"-o",
|
2026-05-14 21:52:52 +02:00
|
|
|
"addopts=--strict-markers --strict-config -ra",
|
2026-06-04 18:13:47 +02:00
|
|
|
*test_dirs,
|
2026-03-25 21:56:37 +01:00
|
|
|
]
|
2026-04-12 21:25:58 +02:00
|
|
|
|
2026-05-14 21:52:52 +02:00
|
|
|
if shutil.which("systemd-run") is not None:
|
|
|
|
|
cmd = [
|
|
|
|
|
"systemd-run",
|
|
|
|
|
"--user",
|
|
|
|
|
"--scope",
|
|
|
|
|
"--quiet",
|
|
|
|
|
"--collect",
|
|
|
|
|
"-p",
|
|
|
|
|
f"MemoryMax={_TOTAL_MEM}",
|
|
|
|
|
"-p",
|
|
|
|
|
"MemorySwapMax=0",
|
|
|
|
|
*cmd,
|
|
|
|
|
]
|
|
|
|
|
return subprocess.run(cmd, check=False, env=os.environ).returncode
|
2026-03-25 21:56:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
raise SystemExit(main())
|