|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Check that requires_* tests are exercised in CI. |
| 3 | +
|
| 4 | +The CI test matrix should run tests guarded by ``requires_*`` markers in at |
| 5 | +least one environment whenever possible. This script inspects pytest report-log |
| 6 | +files from the test matrix and fails if a test is only ever skipped for a |
| 7 | +``requires`` reason, unless that skip reason is explicitly allowlisted. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import argparse |
| 13 | +import json |
| 14 | +import sys |
| 15 | +from collections import defaultdict |
| 16 | +from pathlib import Path |
| 17 | +from typing import Iterable |
| 18 | + |
| 19 | +_SKIP_PREFIX = "Skipped: " |
| 20 | + |
| 21 | + |
| 22 | +def iter_reportlog_paths(paths: Iterable[Path]) -> list[Path]: |
| 23 | + reportlogs: list[Path] = [] |
| 24 | + for path in paths: |
| 25 | + if path.is_dir(): |
| 26 | + reportlogs.extend(sorted(path.rglob("*.jsonl"))) |
| 27 | + elif path.suffix == ".jsonl": |
| 28 | + reportlogs.append(path) |
| 29 | + return reportlogs |
| 30 | + |
| 31 | + |
| 32 | +def load_allowlist(path: Path | None) -> set[str]: |
| 33 | + if path is None: |
| 34 | + return set() |
| 35 | + |
| 36 | + allowlist = set() |
| 37 | + for line in path.read_text().splitlines(): |
| 38 | + stripped = line.strip() |
| 39 | + if stripped and not stripped.startswith("#"): |
| 40 | + allowlist.add(stripped) |
| 41 | + return allowlist |
| 42 | + |
| 43 | + |
| 44 | +def _skip_reason(longrepr: object) -> str | None: |
| 45 | + if isinstance(longrepr, str): |
| 46 | + reason = longrepr |
| 47 | + elif isinstance(longrepr, list) and len(longrepr) >= 3: |
| 48 | + reason = str(longrepr[2]) |
| 49 | + else: |
| 50 | + return None |
| 51 | + |
| 52 | + if reason.startswith(_SKIP_PREFIX): |
| 53 | + return reason.removeprefix(_SKIP_PREFIX) |
| 54 | + return reason |
| 55 | + |
| 56 | + |
| 57 | +def collect_reportlog_data(reportlogs: Iterable[Path]) -> dict[str, dict[str, set[str]]]: |
| 58 | + """Return per-nodeid execution and skip information.""" |
| 59 | + data: dict[str, dict[str, set[str]]] = defaultdict( |
| 60 | + lambda: {"call_outcomes": set(), "skip_reasons": set()} |
| 61 | + ) |
| 62 | + |
| 63 | + for reportlog in reportlogs: |
| 64 | + for line in reportlog.read_text().splitlines(): |
| 65 | + record = json.loads(line) |
| 66 | + if record.get("$report_type") != "TestReport": |
| 67 | + continue |
| 68 | + |
| 69 | + nodeid = record.get("nodeid") |
| 70 | + if not nodeid: |
| 71 | + continue |
| 72 | + |
| 73 | + when = record.get("when") |
| 74 | + outcome = record.get("outcome") |
| 75 | + if when == "call": |
| 76 | + data[nodeid]["call_outcomes"].add(str(outcome)) |
| 77 | + elif when == "setup" and outcome == "skipped": |
| 78 | + reason = _skip_reason(record.get("longrepr")) |
| 79 | + if reason is not None: |
| 80 | + data[nodeid]["skip_reasons"].add(reason) |
| 81 | + |
| 82 | + return data |
| 83 | + |
| 84 | + |
| 85 | +def uncovered_requires_tests( |
| 86 | + reportlogs: Iterable[Path], |
| 87 | + allowlist: set[str], |
| 88 | +) -> dict[str, set[str]]: |
| 89 | + """Return tests that only ever skipped for a requires reason.""" |
| 90 | + uncovered: dict[str, set[str]] = {} |
| 91 | + for nodeid, info in collect_reportlog_data(reportlogs).items(): |
| 92 | + if info["call_outcomes"]: |
| 93 | + continue |
| 94 | + |
| 95 | + skip_reasons = info["skip_reasons"] |
| 96 | + if not skip_reasons: |
| 97 | + continue |
| 98 | + |
| 99 | + if all(reason.startswith("requires ") for reason in skip_reasons): |
| 100 | + missing_reasons = skip_reasons - allowlist |
| 101 | + if missing_reasons: |
| 102 | + uncovered[nodeid] = missing_reasons |
| 103 | + |
| 104 | + return uncovered |
| 105 | + |
| 106 | + |
| 107 | +def main(argv: list[str] | None = None) -> int: |
| 108 | + parser = argparse.ArgumentParser( |
| 109 | + description="Validate that requires_* tests are exercised somewhere in CI." |
| 110 | + ) |
| 111 | + parser.add_argument( |
| 112 | + "paths", |
| 113 | + nargs="+", |
| 114 | + type=Path, |
| 115 | + help="Report-log files or directories containing report-log files.", |
| 116 | + ) |
| 117 | + parser.add_argument( |
| 118 | + "--allowlist", |
| 119 | + type=Path, |
| 120 | + default=None, |
| 121 | + help="Optional allowlist of skip reasons that are intentionally uncovered in CI.", |
| 122 | + ) |
| 123 | + args = parser.parse_args(argv) |
| 124 | + |
| 125 | + reportlogs = iter_reportlog_paths(args.paths) |
| 126 | + if not reportlogs: |
| 127 | + raise SystemExit("No report-log files were found.") |
| 128 | + |
| 129 | + uncovered = uncovered_requires_tests( |
| 130 | + reportlogs, allowlist=load_allowlist(args.allowlist) |
| 131 | + ) |
| 132 | + |
| 133 | + if uncovered: |
| 134 | + print("The following tests are only ever skipped for a requires reason:") |
| 135 | + for nodeid, reasons in sorted(uncovered.items()): |
| 136 | + print(f"- {nodeid}: {', '.join(sorted(reasons))}") |
| 137 | + return 1 |
| 138 | + |
| 139 | + print(f"Checked {len(reportlogs)} report-log file(s); no uncovered requires tests found.") |
| 140 | + return 0 |
| 141 | + |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + raise SystemExit(main()) |
0 commit comments