Add run-tests, which runs the suite as README → Test says and saves the whole log
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
"""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 four workers 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 and prints pytest's short test summary, a line per failing test with its
|
||||
error, then pytest's 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
|
||||
|
||||
BIN = pathlib.Path(__file__).resolve().parents[1]
|
||||
# README.md → Test names these pins, and ci.sh runs this command, so they live here alone.
|
||||
PYTEST = ["uvx", "--python", shutil.which("python3") or sys.executable, "--with", "pytest-xdist==3.8.0", "pytest==9.2.0", "-n", "4"]
|
||||
SUMMARY_HEADER = re.compile(r"^=+ short test summary info =+$")
|
||||
# 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.*$")
|
||||
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]:
|
||||
"""The short test summary and the summary line from pytest's output, or [] where it printed no summary line."""
|
||||
lines = log.splitlines()
|
||||
ends = [index for index, line in enumerate(lines) if SUMMARY_LINE.match(line)]
|
||||
if not ends:
|
||||
return []
|
||||
end = ends[-1]
|
||||
starts = [index for index, line in enumerate(lines[:end]) if SUMMARY_HEADER.match(line)]
|
||||
return lines[starts[-1] + 1:end + 1] if starts else [lines[end]]
|
||||
|
||||
|
||||
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]) -> int:
|
||||
if argv[:1] in (["-h"], ["--help"]):
|
||||
print(__doc__.strip())
|
||||
return 0
|
||||
return run(argv)
|
||||
Reference in New Issue
Block a user