|
| 1 | +"""Render a concise Rust coverage summary table from cargo-llvm-cov JSON.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import json |
| 7 | +import pathlib |
| 8 | +import sys |
| 9 | +from typing import Iterable, List, Tuple |
| 10 | + |
| 11 | + |
| 12 | +def parse_args(argv: Iterable[str] | None = None) -> argparse.Namespace: |
| 13 | + parser = argparse.ArgumentParser(description=__doc__ or "") |
| 14 | + parser.add_argument( |
| 15 | + "summary_path", |
| 16 | + type=pathlib.Path, |
| 17 | + help="Path to cargo-llvm-cov JSON summary (e.g. summary.json)", |
| 18 | + ) |
| 19 | + parser.add_argument( |
| 20 | + "--root", |
| 21 | + type=pathlib.Path, |
| 22 | + default=pathlib.Path.cwd(), |
| 23 | + help="Repository root used to relativise file paths (default: current working directory)", |
| 24 | + ) |
| 25 | + return parser.parse_args(argv) |
| 26 | + |
| 27 | + |
| 28 | +def load_rows(summary_path: pathlib.Path, repo_root: pathlib.Path) -> List[Tuple[str, int, int, float]]: |
| 29 | + try: |
| 30 | + payload = json.loads(summary_path.read_text(encoding="utf-8")) |
| 31 | + except FileNotFoundError as exc: |
| 32 | + raise SystemExit(f"Rust coverage summary not found: {summary_path}") from exc |
| 33 | + |
| 34 | + repo_root = repo_root.resolve() |
| 35 | + rows: List[Tuple[str, int, int, float]] = [] |
| 36 | + |
| 37 | + for dataset in payload.get("data", []): |
| 38 | + for entry in dataset.get("files", []): |
| 39 | + filename = entry.get("filename") |
| 40 | + if not filename: |
| 41 | + continue |
| 42 | + path = pathlib.Path(filename) |
| 43 | + try: |
| 44 | + rel_path = path.resolve().relative_to(repo_root) |
| 45 | + except Exception: |
| 46 | + # Skip entries outside the repository (stdlib, third-party deps, etc.). |
| 47 | + continue |
| 48 | + |
| 49 | + line_summary = (entry.get("summary") or {}).get("lines") or {} |
| 50 | + total = int(line_summary.get("count", 0)) |
| 51 | + covered = int(line_summary.get("covered", 0)) |
| 52 | + missed = max(total - covered, 0) |
| 53 | + percent = float(line_summary.get("percent", 0.0)) |
| 54 | + rows.append((rel_path.as_posix(), total, missed, percent)) |
| 55 | + |
| 56 | + rows.sort(key=lambda item: item[0]) |
| 57 | + return rows |
| 58 | + |
| 59 | + |
| 60 | +def render(rows: List[Tuple[str, int, int, float]]) -> str: |
| 61 | + if not rows: |
| 62 | + return "Rust coverage summary: no project files found" |
| 63 | + |
| 64 | + name_width = max(len(name) for name, *_ in rows) |
| 65 | + lines = ["Rust coverage summary (lines):", f"{'Name'.ljust(name_width)} Lines Miss Cover"] |
| 66 | + |
| 67 | + for name, total, missed, percent in rows: |
| 68 | + lines.append(f"{name.ljust(name_width)} {total:5d} {missed:4d} {percent:5.1f}%") |
| 69 | + |
| 70 | + return "\n".join(lines) |
| 71 | + |
| 72 | + |
| 73 | +def main(argv: Iterable[str] | None = None) -> int: |
| 74 | + args = parse_args(argv) |
| 75 | + rows = load_rows(args.summary_path, args.root) |
| 76 | + output = render(rows) |
| 77 | + print(output) |
| 78 | + return 0 |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + sys.exit(main()) |
0 commit comments