-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
96 lines (79 loc) · 2.41 KB
/
utils.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import re
# Regex Schemas
R_GENERAL_INFO = r"([0-9]* passed in [0-9.]*s)"
R_TEST = r"(tests\/.+\[[0-9 ]*%\])"
R_TEST_TITLE = r"(([^\/]*)_([^\.])*)"
R_README_POSITION_TARGET = r"(\[Pytest Table\]: \<\>(?:.|\n)*\[Pytest Table\]: \<\>)"
def coverage_badge_editor(percent):
color = ""
if percent < 40:
color = "red"
elif percent >= 40 and percent < 60:
color = "orange"
elif percent >= 60 and percent < 80:
color = "yellow"
elif percent >= 80 and percent < 90:
color = "green"
else:
color = "success"
badge = (
"![coverage](https://img.shields.io/badge/coverage-"
+ str(percent)
+ "%25-"
+ color
+ ")"
)
return badge
def pytest_report_parser(report_content):
tests_table = []
general_infos = re.search(R_GENERAL_INFO, report_content).group()
general_duration = general_infos.split(" ")[-1]
general_nbr_of_tests = 0
general_nbr_of_success = 0
general_nbr_of_fails = 0
for test in re.findall(R_TEST, report_content):
test_title = re.search(R_TEST_TITLE, test).group()
test_nbr_total = len(test.split(" ")[1])
general_nbr_of_tests += len(test.split(" ")[1])
test_nbr_success = test.split(" ")[1].count(".")
general_nbr_of_success += test.split(" ")[1].count(".")
test_nbr_fail = test_nbr_total - test_nbr_success
general_nbr_of_fails += test_nbr_total - test_nbr_success
test_coverage_percent = test_nbr_success / test_nbr_total * 100
tests_table.append(
str(r) for r in [
test_title,
coverage_badge_editor(test_coverage_percent),
test_nbr_total,
test_nbr_success,
test_nbr_fail,
]
)
general_coverage = general_nbr_of_success / general_nbr_of_tests * 100
return (
str(general_nbr_of_tests),
str(general_nbr_of_success),
str(general_nbr_of_fails),
str(general_coverage),
str(general_duration),
tests_table,
)
def create_readme_replacement(coverage, detailed_table, general_table):
return (
"""
[Pytest Table]: <>
"""
+ coverage_badge_editor(float(coverage))
+ """
<details>
<summary>Coverage Report</summary>
"""
+ detailed_table
+ """
</details>
"""
+ general_table
+ """
[Pytest Table]: <>
"""
)