testsAndMisc/meta/scripts/pytest_changed_packages.py
Krzysztof kuhy Rudnicki 20d5d1f89b fix(usage_report): stop charging atop's HZ field as CPU; bundle since-last-report mode
atop's `-P PRC` output inserts the clock-tick rate (HZ=100) between the
`state` and `utime` columns. Both the Python parser and the native C
aggregator read that constant as utime for every record, charging a flat
1 CPU-second per record — so cpu_seconds collapsed to pid_count and
short-lived fork-storm commands (xset, dd, chronyc) topped the CPU table
(xset showed 67h). The old test fixtures lacked the HZ field, so code and
tests agreed on the bug.

- _parse_prc / atop_agg.c: read utime/stime past the HZ field (after+2/+3,
  tokens[10]/[11]); bump the length guards accordingly
- restore C/atop_agg (deleted in 89b4f59) under linux_configuration/C/,
  where the build path resolves; corrected test fixtures to include HZ
- _atop_agg_binary: fall back to the Python parser when the C source tree
  is gone instead of trusting an orphaned cached binary
- add regression tests proving HZ is not summed as CPU
- bundle the in-progress since-last-report multi-day aggregation (segments,
  -b/-e bounding, persisted state, window merging) and its tests/conftest
- meta: gate linux_configuration/tests in pytest_changed_packages.py

Verified by running usage_report.py --date 20260604: Top CPU now led by
SkyrimSE; xset/dd/chronyc fall to ~0. C unit tests + full pytest suite green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-04 18:13:47 +02:00

85 lines
2.4 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",
"-n",
"4",
# 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())