-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_coverage.py
executable file
·66 lines (53 loc) · 2 KB
/
check_coverage.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
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
"""Checks if the test coverage is above a threshold."""
import argparse
import json
import sys
COVERAGE_REPORT="coverage/coverage.json"
THRESHOLD=25
def get_value_by_path(data, path):
"""Returns the value from a dict specified by path
:param data: Dict the value is taken from.
:type data: dict
:param path: Value path, separated by "/".
:type path: str
:return: Value or None, if value is not found
:rtype: any
"""
current = data
for part in path.split("/"):
if part in current:
current = current[part]
else:
return None
return current
def get_percent_covered(filename):
"""Returns the coverage of note.py's tests in percent
:param filename: name of the json file containing the coverage info
:type filename: str
:return: Coverage of note.py's tests in percent.
:rtype: number
"""
with open(filename, "r") as report_file:
data = json.load(report_file)
return get_value_by_path(data, "files/note.py/summary/percent_covered")
def do_check(filename, threshold):
"""Checks if the coverage is above a given threshold
:param filename: name of the json file containing the actual coverage
:type filename: str
:param threshold: threshold to check
:type threshold: number
:return: True if actual coverage is above the threshold, False otherwise
:rtype: bool
"""
actual = get_percent_covered(filename)
if (actual < threshold):
print(f"error: coverage too low: threshold is {THRESHOLD}, but actual coverage is {actual}", file=sys.stderr)
return actual >= threshold
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--filename', type=str, required=False, default=COVERAGE_REPORT)
parser.add_argument('-t', '--threshold', type=int, required=False, default=THRESHOLD)
args = parser.parse_args()
check_okay = do_check(args.filename, args.threshold)
sys.exit(0 if check_okay else 1)