|
| 1 | +import json |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +import os |
| 6 | + |
| 7 | + |
| 8 | +def run_tests(): |
| 9 | + print("Running tests and coverage...") |
| 10 | + subprocess.run( |
| 11 | + [ |
| 12 | + "uv", |
| 13 | + "run", |
| 14 | + "pytest", |
| 15 | + "--cov=src/openapi_client", |
| 16 | + "--cov-report=json", |
| 17 | + "tests/", |
| 18 | + ], |
| 19 | + check=True, |
| 20 | + ) |
| 21 | + with open("coverage.json", "r") as f: |
| 22 | + data = json.load(f) |
| 23 | + test_cov = data["totals"]["percent_covered"] |
| 24 | + if os.path.exists("coverage.json"): |
| 25 | + os.remove("coverage.json") |
| 26 | + return test_cov |
| 27 | + |
| 28 | + |
| 29 | +def run_interrogate(): |
| 30 | + print("Running interrogate...") |
| 31 | + res = subprocess.run( |
| 32 | + [ |
| 33 | + "uv", |
| 34 | + "run", |
| 35 | + "interrogate", |
| 36 | + "--ignore-init-method", |
| 37 | + "--ignore-init-module", |
| 38 | + "src/openapi_client/", |
| 39 | + ], |
| 40 | + capture_output=True, |
| 41 | + text=True, |
| 42 | + ) |
| 43 | + match = re.search(r"actual: ([\d.]+)%", res.stdout) |
| 44 | + if match: |
| 45 | + return float(match.group(1)) |
| 46 | + match = re.search(r"actual: ([\d.]+)%", res.stdout + res.stderr) |
| 47 | + if match: |
| 48 | + return float(match.group(1)) |
| 49 | + print("Could not find interrogate coverage") |
| 50 | + return 0.0 |
| 51 | + |
| 52 | + |
| 53 | +def update_readme(test_cov, doc_cov): |
| 54 | + print(f"Test Coverage: {test_cov:.1f}%") |
| 55 | + print(f"Doc Coverage: {doc_cov:.1f}%") |
| 56 | + |
| 57 | + with open("README.md", "r") as f: |
| 58 | + content = f.read() |
| 59 | + |
| 60 | + def get_color(cov): |
| 61 | + if cov >= 95: |
| 62 | + return "brightgreen" |
| 63 | + if cov >= 80: |
| 64 | + return "green" |
| 65 | + if cov >= 70: |
| 66 | + return "yellow" |
| 67 | + if cov >= 60: |
| 68 | + return "orange" |
| 69 | + return "red" |
| 70 | + |
| 71 | + test_color = get_color(test_cov) |
| 72 | + doc_color = get_color(doc_cov) |
| 73 | + |
| 74 | + test_badge = f"" |
| 75 | + doc_badge = f"" |
| 76 | + |
| 77 | + has_test_badge = "![Test Coverage]" in content |
| 78 | + has_doc_badge = "![Doc Coverage]" in content |
| 79 | + |
| 80 | + if has_test_badge and has_doc_badge: |
| 81 | + content = re.sub( |
| 82 | + r"!\[Test Coverage\]\(https://img\.shields\.io/badge/Test_Coverage-[^\)]+\)", |
| 83 | + test_badge, |
| 84 | + content, |
| 85 | + ) |
| 86 | + content = re.sub( |
| 87 | + r"!\[Doc Coverage\]\(https://img\.shields\.io/badge/Doc_Coverage-[^\)]+\)", |
| 88 | + doc_badge, |
| 89 | + content, |
| 90 | + ) |
| 91 | + else: |
| 92 | + lines = content.split("\n") |
| 93 | + for i, line in enumerate(lines): |
| 94 | + if "[![uv]" in line: |
| 95 | + lines[i] = f"{line}\n{test_badge}\n{doc_badge}" |
| 96 | + break |
| 97 | + content = "\n".join(lines) |
| 98 | + |
| 99 | + with open("README.md", "w") as f: |
| 100 | + f.write(content) |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + try: |
| 105 | + t_cov = run_tests() |
| 106 | + d_cov = run_interrogate() |
| 107 | + update_readme(t_cov, d_cov) |
| 108 | + except Exception as e: |
| 109 | + print(f"Failed to update badges: {e}") |
| 110 | + sys.exit(1) |
0 commit comments