Skip to content

Commit 65efcc4

Browse files
author
Sylvain MARIE
committed
Improved badge generator
1 parent 6da203f commit 65efcc4

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

ci_tools/generate-junit-badge.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515

1616

1717
class TestStats(object):
18-
def __init__(self, success_percentage, success, runned, skipped):
18+
def __init__(self, success_percentage, success, runned, skipped, errors):
1919
self.success_percentage = success_percentage
2020
self.success = success
2121
self.runned = runned
2222
self.skipped = skipped
23+
self.errors = errors
2324

2425

2526
def get_test_stats(junit_xml='reports/junit/junit.xml' # type: str
@@ -34,15 +35,20 @@ def get_test_stats(junit_xml='reports/junit/junit.xml' # type: str
3435
skipped = len(tr.skipped)
3536
runned = tr.testsRun - skipped
3637
failed = len(tr.failures)
38+
errors = len(tr.errors)
3739
success = runned - failed
3840

39-
success_percentage = floor(success * 100 / runned)
41+
if runned > 0:
42+
success_percentage = floor(success * 100 / (runned + errors))
43+
else:
44+
success_percentage = 100
4045

41-
return TestStats(success_percentage, success, runned, skipped)
46+
return TestStats(success_percentage, success, runned, skipped, errors)
4247

4348

44-
def download_badge(test_stats, # type: TestStats
45-
dest_folder='reports/junit' # type: str
49+
def download_badge(test_stats, # type: TestStats
50+
dest_folder='reports/junit', # type: str
51+
badge_name='junit-badge.svg' # type: str
4652
):
4753
"""
4854
Downloads the badge corresponding to the provided success percentage, from https://img.shields.io.
@@ -65,10 +71,10 @@ def download_badge(test_stats, # type: TestStats
6571

6672
left_txt = "tests"
6773
# right_txt = "%s%%" % test_stats.success_percentage
68-
right_txt = "%s/%s" % (test_stats.success, test_stats.runned)
74+
right_txt = "%s/%s" % (test_stats.success, (test_stats.runned + test_stats.errors))
6975
url = 'https://img.shields.io/badge/%s-%s-%s.svg' % (left_txt, quote_plus(right_txt), color)
7076

71-
dest_file = path.join(dest_folder, 'junit-badge.svg')
77+
dest_file = path.join(dest_folder, badge_name)
7278

7379
print('Generating junit badge from : ' + url)
7480
response = requests.get(url, stream=True)
@@ -81,11 +87,20 @@ def download_badge(test_stats, # type: TestStats
8187
if __name__ == "__main__":
8288
# Execute only if run as a script.
8389
# Check the arguments
84-
assert len(sys.argv[1:]) == 1, "a single mandatory argument is required: <threshold>"
85-
threshold = float(sys.argv[1])
90+
nbargs = len(sys.argv[1:])
91+
if nbargs < 1:
92+
raise ValueError("a mandatory argument is required: <threshold>, and an optional: <dest_folder>")
93+
else:
94+
threshold = float(sys.argv[1])
95+
if nbargs < 2:
96+
dest_folder = None
97+
elif nbargs < 3:
98+
dest_folder = sys.argv[2]
99+
else:
100+
raise ValueError("too many arguments received: 2 maximum (<threshold>, <dest_folder>)")
86101

87102
# First retrieve the success percentage from the junit xml
88-
test_stats = get_test_stats()
103+
test_stats = get_test_stats(junit_xml='%s/junit.xml' % ('reports/junit' if dest_folder is None else dest_folder))
89104

90105
# Validate against the threshold
91106
print("Success percentage is %s%%. Checking that it is >= %s" % (test_stats.success_percentage, threshold))
@@ -94,4 +109,4 @@ def download_badge(test_stats, # type: TestStats
94109
"" % (test_stats.success_percentage, threshold))
95110

96111
# Download the badge
97-
download_badge(test_stats)
112+
download_badge(test_stats, dest_folder=dest_folder)

0 commit comments

Comments
 (0)