|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Render a Cobertura-style coverage.xml (as produced by `coverage xml` / |
| 3 | +pytest-cov's --cov-report=xml) as Markdown suitable for a GitHub Actions job |
| 4 | +summary. |
| 5 | +
|
| 6 | +Usage: |
| 7 | + python3 python_coverage_summary.py <path-to-coverage.xml> |
| 8 | +
|
| 9 | +Prints to stdout; the caller is expected to redirect into $GITHUB_STEP_SUMMARY. |
| 10 | +Deliberately has no third-party dependencies so it can run with the stock |
| 11 | +`python3` already available on GitHub-hosted runners -- no extra permissions |
| 12 | +or installs are needed, which keeps it safe to run on pull requests from |
| 13 | +forks. |
| 14 | +""" |
| 15 | + |
| 16 | +import sys |
| 17 | +import xml.etree.ElementTree as ET |
| 18 | + |
| 19 | + |
| 20 | +def _pct(rate_attr): |
| 21 | + try: |
| 22 | + return float(rate_attr) * 100 |
| 23 | + except (TypeError, ValueError): |
| 24 | + return None |
| 25 | + |
| 26 | + |
| 27 | +def _fmt_pct(value): |
| 28 | + return "N/A" if value is None else f"{value:.1f}%" |
| 29 | + |
| 30 | + |
| 31 | +def main(argv): |
| 32 | + if len(argv) != 2: |
| 33 | + print("Usage: python_coverage_summary.py <coverage.xml>", file=sys.stderr) |
| 34 | + return 2 |
| 35 | + |
| 36 | + path = argv[1] |
| 37 | + |
| 38 | + try: |
| 39 | + root = ET.parse(path).getroot() |
| 40 | + except (OSError, ET.ParseError) as exc: |
| 41 | + print("## Python test coverage") |
| 42 | + print() |
| 43 | + print(f"No coverage report found at `{path}` ({exc}).") |
| 44 | + return 0 |
| 45 | + |
| 46 | + line_rate = _pct(root.get("line-rate")) |
| 47 | + branch_rate = _pct(root.get("branch-rate")) |
| 48 | + lines_covered = root.get("lines-covered", "?") |
| 49 | + lines_valid = root.get("lines-valid", "?") |
| 50 | + |
| 51 | + print("## Python test coverage") |
| 52 | + print() |
| 53 | + print( |
| 54 | + f"**Overall line coverage: {_fmt_pct(line_rate)}** " |
| 55 | + f"({lines_covered}/{lines_valid} lines) | " |
| 56 | + f"branch coverage: {_fmt_pct(branch_rate)}" |
| 57 | + ) |
| 58 | + print() |
| 59 | + print("<details><summary>Per-file coverage</summary>") |
| 60 | + print() |
| 61 | + print("| File | Line coverage | Lines covered |") |
| 62 | + print("| --- | --- | --- |") |
| 63 | + |
| 64 | + classes = sorted(root.iter("class"), key=lambda c: c.get("filename", "")) |
| 65 | + for cls in classes: |
| 66 | + filename = cls.get("filename", "?") |
| 67 | + file_line_rate = _pct(cls.get("line-rate")) |
| 68 | + |
| 69 | + total = covered = 0 |
| 70 | + lines_elem = cls.find("lines") |
| 71 | + if lines_elem is not None: |
| 72 | + for line in lines_elem.findall("line"): |
| 73 | + total += 1 |
| 74 | + if int(line.get("hits", "0")) > 0: |
| 75 | + covered += 1 |
| 76 | + |
| 77 | + print(f"| `{filename}` | {_fmt_pct(file_line_rate)} | {covered}/{total} |") |
| 78 | + |
| 79 | + print() |
| 80 | + print("</details>") |
| 81 | + |
| 82 | + return 0 |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + raise SystemExit(main(sys.argv)) |
0 commit comments