101 lines
3.9 KiB
Python
101 lines
3.9 KiB
Python
"""Run this repo's test suite as README.md → Test says, and print only what a reader needs from it.
|
|
|
|
usage: run-tests [<pytest args>...]
|
|
|
|
It runs pytest, pinned with pytest-xdist in one worker per CPU of a heavy slot through uvx, on the arguments given,
|
|
adding the repo's bin/ folder where none of them names a path that exists. It saves the whole
|
|
output to a new file. It prints each failing test's name, its first error lines and where it
|
|
failed, then pytest's short test summary and summary line, then `Full log: <path>`. Where pytest
|
|
printed no summary line, it prints the log's last lines in their place.
|
|
|
|
Exit 0 where every test passed, 1 otherwise, or where pytest cannot start, saying why.
|
|
"""
|
|
import os
|
|
import pathlib
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import typing
|
|
|
|
from machine_load.slots import CPUS_PER_SLOT
|
|
|
|
BIN = pathlib.Path(__file__).resolve().parents[1]
|
|
# ci.sh runs this command, so its pins live here; README.md → Test's mutation command repeats the pytest pin, so change both. The workers fill one heavy slot, since run-heavy grants one.
|
|
PYTEST = ["uvx", "--python", shutil.which("python3") or sys.executable, "--with", "pytest-xdist==3.8.0", "pytest==9.2.0", "-n", str(CPUS_PER_SLOT)]
|
|
SECTION = re.compile(r"^=+ (.+) =+$")
|
|
TEST_HEADER = re.compile(r"^_+ .+ _+$")
|
|
# A traceback's last line, such as `bin/x/test_x.py:12: AssertionError`.
|
|
LOCATION = re.compile(r"^\S+:\d+: \S")
|
|
# pytest's last line, such as `==== 2 failed, 1 passed in 0.31s ====`, or without the rules under -q.
|
|
SUMMARY_LINE = re.compile(r"^(=+ )?(no tests ran|\d+ \w+).* in [\d.]+s\b.*$")
|
|
ERROR_LINES = 10
|
|
LOG_TAIL = 20
|
|
|
|
|
|
def pytest_args(argv: list[str]) -> list[str]:
|
|
"""argv, with bin/ added where no argument names a path, so `run-tests -k name` still runs the suite."""
|
|
if any(os.path.exists(arg.split("::")[0]) for arg in argv):
|
|
return argv
|
|
return [*argv, str(BIN)]
|
|
|
|
|
|
def report(log: str) -> list[str]:
|
|
"""What to print of pytest's output: each failing test's header, error lines and location, then the short test summary and the summary line; [] where pytest printed no summary line.
|
|
|
|
The error lines come from the FAILURES and ERRORS sections, since pytest-xdist leaves them out of the short summary's lines."""
|
|
lines = log.splitlines()
|
|
ends = [index for index, line in enumerate(lines) if SUMMARY_LINE.match(line)]
|
|
if not ends:
|
|
return []
|
|
printed: list[str] = []
|
|
section = ""
|
|
errors = 0
|
|
for line in lines[:ends[-1]]:
|
|
heading = SECTION.match(line)
|
|
if heading:
|
|
section = heading.group(1)
|
|
elif section in ("FAILURES", "ERRORS"):
|
|
if TEST_HEADER.match(line):
|
|
errors = 0
|
|
printed.append(line)
|
|
elif line.startswith("E "):
|
|
errors += 1
|
|
if errors <= ERROR_LINES:
|
|
printed.append(line)
|
|
elif LOCATION.match(line):
|
|
if errors > ERROR_LINES:
|
|
printed.append(f"E … {errors - ERROR_LINES} more lines in the log")
|
|
printed.append(line)
|
|
elif section == "short test summary info":
|
|
printed.append(line)
|
|
return [*printed, lines[ends[-1]]]
|
|
|
|
|
|
def run(argv: list[str], pytest: list[str] = PYTEST, cwd: str | None = None) -> int:
|
|
descriptor, log_path = tempfile.mkstemp(prefix="run-tests-", suffix=".log")
|
|
with os.fdopen(descriptor, "w") as log:
|
|
try:
|
|
code = subprocess.run([*pytest, *pytest_args(argv)], stdout=log, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL, cwd=cwd).returncode
|
|
except OSError as error:
|
|
os.unlink(log_path)
|
|
print(f"run-tests: cannot start {pytest[0]}: {error.strerror}", file=sys.stderr)
|
|
return 1
|
|
output = pathlib.Path(log_path).read_text(errors="replace")
|
|
lines = report(output)
|
|
if lines:
|
|
print("\n".join(lines))
|
|
else:
|
|
print("run-tests: pytest printed no summary line; the log ends:")
|
|
print("\n".join(output.splitlines()[-LOG_TAIL:]))
|
|
print(f"Full log: {log_path}")
|
|
return 0 if code == 0 else 1
|
|
|
|
|
|
def main(argv: list[str], run: typing.Callable[[list[str]], int]) -> int:
|
|
if argv[:1] in (["-h"], ["--help"]):
|
|
print(__doc__.strip())
|
|
return 0
|
|
return run(argv)
|