-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
57 lines (42 loc) · 1.32 KB
/
test.py
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
"""Run tests and analyse coverage
Requires pytest, pytest-cov and anybadge
"""
from html.parser import HTMLParser
import shlex
import subprocess
import sys
class TotalCoverageParser(HTMLParser):
"""Hacky HTML parser
Find total code coverage in pytest-cov output
"""
def __init__(self):
super().__init__()
self.found = False
self.coverage_value = 0
def handle_starttag(self, tag, attrs):
for attr in attrs:
if attr == ("class", "total"):
self.found = True
continue
def handle_data(self, data):
if not self.found:
return
if "%" in data:
self.coverage_value = data.rstrip("%")
self.found = False
if __name__ == "__main__":
test_command_list = [
f"coverage run -m pytest --doctest-glob='*.md' {' '.join(sys.argv[1:])}",
"coverage html"
]
for command in test_command_list:
subprocess.run(shlex.split(command))
with open("htmlcov/index.html") as f_:
coverage_content = f_.read()
parser = TotalCoverageParser()
parser.feed(coverage_content)
gen_badge_command = (
f"anybadge --value={parser.coverage_value} "
f"--file=badges/coverage.svg --overwrite coverage"
)
subprocess.run(shlex.split(gen_badge_command))