testsAndMisc/meta/scripts/pytest_changed_packages.py
Krzysztof kuhy Rudnicki 07792e75d2 refactor: split wake_alarm modules, fix ruff violations, enforce global coverage
- Split _alarm.py (1059 lines) into _alarm.py + _audio.py + _challenges.py
- Split test files (1305 / 725 lines) into 6 files, all under 500 lines
- Replace random.* with secrets.* (S311); fix RUF001, SIM117, E501 ruff errors
- Rewrite pytest_changed_packages.py to always run all packages with global
  --cov python_pkg coverage (100% branch coverage enforced across whole tree)
- Add DISMISS_ROUNDS_REQUIRED=2 and DISMISS_FLASH_SECONDS=4 to _constants.py

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-30 22:13:32 +02:00

74 lines
1.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""Run pytest for all python_pkg subpackages whenever any Python file changes.
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``.
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.
"""
from __future__ import annotations
import os
from pathlib import Path
import shutil
import subprocess
import sys
_TOTAL_MEM = "4G"
def main() -> int:
"""Entry point."""
if not sys.argv[1:]:
return 0
packages = sorted(
entry.name
for entry in Path("python_pkg").iterdir()
if (entry / "tests").is_dir()
)
if not packages:
return 0
cmd = [
sys.executable,
"-m",
"pytest",
"--cov",
"python_pkg",
"--cov-branch",
"--cov-report=term-missing",
"--cov-fail-under=100",
"-q",
"-n",
"4",
# Override addopts from pyproject.toml to avoid double --cov flags.
"-o",
"addopts=--strict-markers --strict-config -ra",
*[f"python_pkg/{pkg}/tests" for pkg in packages],
]
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
if __name__ == "__main__":
raise SystemExit(main())