-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathupdate_coverage.py
More file actions
executable file
·80 lines (62 loc) · 2.71 KB
/
update_coverage.py
File metadata and controls
executable file
·80 lines (62 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
"""
Script to update README.md with current test coverage percentage.
"""
import re
import sys
from pathlib import Path
def update_readme_coverage(coverage_percentage: str):
"""Update the README.md file with the new coverage percentage."""
readme_path = Path("README.md")
if not readme_path.exists():
print(f"README.md not found at {readme_path}")
return False
content = readme_path.read_text(encoding="utf-8")
# Determine badge color based on coverage percentage
coverage_float = float(coverage_percentage)
if coverage_float >= 90:
color = "brightgreen"
elif coverage_float >= 80:
color = "green"
elif coverage_float >= 70:
color = "yellowgreen"
elif coverage_float >= 60:
color = "yellow"
else:
color = "red"
# Update the coverage badge at the top (link-wrapped format)
badge_pattern = r"\[!\[Coverage\]\(https://img\.shields\.io/badge/coverage-[\d.]+%25-[a-z-]+\)\]\([^)]+\)"
new_badge = f"[](https://github.com/bsv-blockchain/py-sdk/actions/workflows/build.yml)"
if not re.search(badge_pattern, content):
print("Warning: Coverage badge pattern not found in README")
return False
content = re.sub(badge_pattern, new_badge, content)
# Update the coverage percentage in the Testing & Quality section
coverage_text_pattern = r"\*\*(\d+(?:\.\d+)?)%\+ code coverage\*\* across the entire codebase"
new_coverage_text = f"**{coverage_percentage}%+ code coverage** across the entire codebase"
if not re.search(coverage_text_pattern, content):
print("Warning: Coverage text pattern not found in README")
return False
content = re.sub(coverage_text_pattern, new_coverage_text, content)
# Write the updated content back to the file
readme_path.write_text(content, encoding="utf-8")
print(f"Updated README.md with coverage percentage: {coverage_percentage}%")
return True
def main():
if len(sys.argv) != 2:
print("Usage: python update_coverage.py <coverage_percentage>")
sys.exit(1)
coverage_percentage = sys.argv[1]
# Validate that it's a number within the valid percentage range (0-100)
try:
coverage_float = float(coverage_percentage)
except ValueError:
print(f"Invalid coverage percentage: {coverage_percentage}")
sys.exit(1)
if not 0.0 <= coverage_float <= 100.0:
print(f"Coverage percentage must be between 0 and 100: {coverage_percentage}")
sys.exit(1)
success = update_readme_coverage(coverage_percentage)
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()