|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Write docs/badges/coverage.svg from coverage.json (pytest --cov-report=json).""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import json |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +ROOT = Path(__file__).resolve().parents[1] |
| 11 | +DEFAULT_IN = ROOT / "coverage.json" |
| 12 | +DEFAULT_OUT = ROOT / "docs" / "badges" / "coverage.svg" |
| 13 | + |
| 14 | + |
| 15 | +def _color(pct: float) -> str: |
| 16 | + if pct >= 80: |
| 17 | + return "#4c1" |
| 18 | + if pct >= 60: |
| 19 | + return "#97ca00" |
| 20 | + if pct >= 40: |
| 21 | + return "#dfb317" |
| 22 | + if pct >= 20: |
| 23 | + return "#fe7d37" |
| 24 | + return "#e05d44" |
| 25 | + |
| 26 | + |
| 27 | +def _svg(label: str, value: str, fill: str) -> str: |
| 28 | + lw = len(label) * 6.5 + 10 |
| 29 | + vw = len(value) * 6.5 + 10 |
| 30 | + tw = lw + vw |
| 31 | + return f"""<svg xmlns="http://www.w3.org/2000/svg" width="{tw:.0f}" height="20" role="img" aria-label="{label}: {value}"> |
| 32 | + <title>{label}: {value}</title> |
| 33 | + <linearGradient id="s" x2="0" y2="100%"> |
| 34 | + <stop offset="0" stop-color="#bbb" stop-opacity=".1"/> |
| 35 | + <stop offset="1" stop-opacity=".1"/> |
| 36 | + </linearGradient> |
| 37 | + <clipPath id="r"><rect width="{tw:.0f}" height="20" rx="3" fill="#fff"/></clipPath> |
| 38 | + <g clip-path="url(#r)"> |
| 39 | + <rect width="{lw:.0f}" height="20" fill="#555"/> |
| 40 | + <rect x="{lw:.0f}" width="{vw:.0f}" height="20" fill="{fill}"/> |
| 41 | + <rect width="{tw:.0f}" height="20" fill="url(#s)"/> |
| 42 | + </g> |
| 43 | + <g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"> |
| 44 | + <text x="{lw/2:.1f}" y="14" fill="#010101" fill-opacity=".3">{label}</text> |
| 45 | + <text x="{lw/2:.1f}" y="13">{label}</text> |
| 46 | + <text x="{lw + vw/2:.1f}" y="14" fill="#010101" fill-opacity=".3">{value}</text> |
| 47 | + <text x="{lw + vw/2:.1f}" y="13">{value}</text> |
| 48 | + </g> |
| 49 | +</svg> |
| 50 | +""" |
| 51 | + |
| 52 | + |
| 53 | +def main(argv: list[str] | None = None) -> int: |
| 54 | + argv = argv or sys.argv[1:] |
| 55 | + in_path = Path(argv[0]) if argv else DEFAULT_IN |
| 56 | + out_path = Path(argv[1]) if len(argv) > 1 else DEFAULT_OUT |
| 57 | + if not in_path.is_file(): |
| 58 | + print(f"coverage.json not found: {in_path}", file=sys.stderr) |
| 59 | + print( |
| 60 | + "Run: USE_SQLITE=true pytest -q --cov --cov-report=json:coverage.json", |
| 61 | + file=sys.stderr, |
| 62 | + ) |
| 63 | + return 1 |
| 64 | + data = json.loads(in_path.read_text(encoding="utf-8")) |
| 65 | + pct = float(data["totals"]["percent_covered"]) |
| 66 | + value = f"{pct:.0f}%" |
| 67 | + out_path.parent.mkdir(parents=True, exist_ok=True) |
| 68 | + out_path.write_text(_svg("coverage", value, _color(pct)), encoding="utf-8") |
| 69 | + print(f"Wrote {out_path} ({value})") |
| 70 | + return 0 |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + raise SystemExit(main()) |
0 commit comments