testsAndMisc/meta/scripts/pytest_changed_packages.py
Krzysztof kuhy Rudnicki d8f8605275 Drop -n 4 xdist parallelism from the pytest-coverage pre-commit hook
The coverage gate failed twice in a row on GitHub Actions on the exact
same line (wake_alarm/_alarm_display.py:71), with the run rerun on the
identical commit -- ruling out a one-off flake. Could not reproduce
locally across 6 attempts, including running the exact same -n 4
invocation directly. The likely cause is a pytest-xdist/pytest-cov
worker coverage-combine timing issue specific to the CI runner that
isn't practical to root-cause further from here. Running the hook
serially removes the risk outright; local timing shows no meaningful
slowdown.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A7vbgtFfZmfxJtN5DdtJky
2026-06-22 08:29:54 +02:00

89 lines
2.8 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.
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``).
"""
from __future__ import annotations
import os
from pathlib import Path
import shutil
import subprocess
import sys
_TOTAL_MEM = "4G"
# 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",)
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
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()]
cmd = [
sys.executable,
"-m",
"pytest",
"--cov",
"python_pkg",
"--cov-branch",
"--cov-report=term-missing",
"--cov-fail-under=100",
"-q",
# No -n/xdist: pytest-cov's worker coverage combine has shown a
# reproducible (not flaky -- failed identically twice in a row) gap
# under -n 4 on the GitHub Actions runner that never occurs running
# serially, including run repeatedly with the same -n 4 locally.
# Serial is slightly slower but has never produced a false coverage
# failure, which matters more for a 100%-coverage gate.
# Override addopts from pyproject.toml to avoid double --cov flags.
"-o",
"addopts=--strict-markers --strict-config -ra",
*test_dirs,
]
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())