Coverage for genbadge/tests/test_core.py: 95%
83 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-11-10 20:37 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-11-10 20:37 +0000
1from __future__ import division
3import platform
4from distutils.version import LooseVersion
5import PIL
7import pytest
9try:
10 from pathlib import Path
11except ImportError: # pragma: no cover
12 from pathlib2 import Path # python 2
15from genbadge import Badge
16from genbadge.utils_badge import get_local_badge_template
17from genbadge.utils_coverage import parse_cov
18from genbadge.utils_junit import get_test_stats
19from genbadge.utils_flake8 import get_flake8_stats
22TESTS_FOLDER = Path(__file__).parent.absolute()
25def test_access_pkg_resources():
26 """Make sure accessing the resource from the package is ok"""
27 get_local_badge_template()
30@pytest.mark.parametrize("use_shields", [False], # TODO True but the contents are slightly different
31 ids="use_shields={}".format)
32def test_genbadge(tmpdir, use_shields):
33 """Test that the `Badge` class works as expected"""
35 b = Badge(left_txt="verytring", right_txt="1XYZ", color="green")
37 # Console representation
38 assert repr(b) == "[ verytring | 1XYZ ] color: green"
40 # SVG representation
41 if platform.system() == "Windows" or LooseVersion(PIL.__version__) < "8.0": 41 ↛ 42line 41 didn't jump to line 42, because the condition on line 41 was never true
42 ref_nbs = dict(left_width=63, right_width=41, tot_width=104, left_x=325.0, left_txt_length=530, right_x=825.0, right_txt_length=310)
43 else:
44 ref_nbs = dict(left_width=61, right_width=39, tot_width=100, left_x=315.0, left_txt_length=510, right_x=795.0, right_txt_length=290)
46 refsvg_str = """
47<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{tot_width}" height="20" role="img" aria-label="verytring: 1XYZ">
48 <title>verytring: 1XYZ</title>
49 <linearGradient id="s" x2="0" y2="100%">
50 <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
51 <stop offset="1" stop-opacity=".1"/>
52 </linearGradient>
53 <clipPath id="r">
54 <rect width="{tot_width}" height="20" rx="3" fill="#fff"/>
55 </clipPath>
56 <g clip-path="url(#r)">
57 <rect width="{left_width}" height="20" fill="#555"/>
58 <rect x="{left_width}" width="{right_width}" height="20" fill="#97ca00"/>
59 <rect width="{tot_width}" height="20" fill="url(#s)"/>
60 </g>
61 <g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110">
62 <text aria-hidden="true" x="{left_x}" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="{left_txt_length}">verytring</text>
63 <text x="{left_x}" y="140" transform="scale(.1)" fill="#fff" textLength="{left_txt_length}">verytring</text>
64 <text aria-hidden="true" x="{right_x}" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="{right_txt_length}">1XYZ</text>
65 <text x="{right_x}" y="140" transform="scale(.1)" fill="#fff" textLength="{right_txt_length}">1XYZ</text>
66 </g>
67</svg>""".format(**ref_nbs) # noqa
68 assert standardize_xml("\n" + b.as_svg(use_shields=use_shields)) == standardize_xml(refsvg_str)
70 # Write to file
71 b.write_to(str(tmpdir / "tmp_badge.svg"), use_shields=use_shields)
72 with open(str(tmpdir / "tmp_badge.svg"), mode="rt") as f:
73 svgtxt = f.read()
74 assert standardize_xml("\n" + svgtxt) == standardize_xml(refsvg_str)
76@pytest.mark.parametrize("use_shields", [False], # TODO True but the contents are slightly different
77 ids="use_shields={}".format)
78def test_genbadge_red(tmpdir, use_shields):
79 """Test that the `Badge` class works as expected"""
81 b = Badge(left_txt="verytring", right_txt="1XYZ", color="red")
83 # Console representation
84 assert repr(b) == "[ verytring | 1XYZ ] color: red"
86 # SVG representation
87 if platform.system() == "Windows" or LooseVersion(PIL.__version__) < "8.0": 87 ↛ 88line 87 didn't jump to line 88, because the condition on line 87 was never true
88 ref_nbs = dict(left_width=63, right_width=41, tot_width=104, left_x=325.0, left_txt_length=530, right_x=825.0, right_txt_length=310)
89 else:
90 ref_nbs = dict(left_width=61, right_width=39, tot_width=100, left_x=315.0, left_txt_length=510, right_x=795.0, right_txt_length=290)
92 refsvg_str = """
93<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{tot_width}" height="20" role="img" aria-label="verytring: 1XYZ">
94 <title>verytring: 1XYZ</title>
95 <linearGradient id="s" x2="0" y2="100%">
96 <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
97 <stop offset="1" stop-opacity=".1"/>
98 </linearGradient>
99 <clipPath id="r">
100 <rect width="{tot_width}" height="20" rx="3" fill="#fff"/>
101 </clipPath>
102 <g clip-path="url(#r)">
103 <rect width="{left_width}" height="20" fill="#555"/>
104 <rect x="{left_width}" width="{right_width}" height="20" fill="#e05d44"/>
105 <rect width="{tot_width}" height="20" fill="url(#s)"/>
106 </g>
107 <g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110">
108 <text aria-hidden="true" x="{left_x}" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="{left_txt_length}">verytring</text>
109 <text x="{left_x}" y="140" transform="scale(.1)" fill="#fff" textLength="{left_txt_length}">verytring</text>
110 <text aria-hidden="true" x="{right_x}" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="{right_txt_length}">1XYZ</text>
111 <text x="{right_x}" y="140" transform="scale(.1)" fill="#fff" textLength="{right_txt_length}">1XYZ</text>
112 </g>
113</svg>""".format(**ref_nbs) # noqa
114 assert standardize_xml("\n" + b.as_svg(use_shields=use_shields)) == standardize_xml(refsvg_str)
116 # Write to file
117 b.write_to(str(tmpdir / "tmp_badge.svg"), use_shields=use_shields)
118 with open(str(tmpdir / "tmp_badge.svg"), mode="rt") as f:
119 svgtxt = f.read()
120 assert standardize_xml("\n" + svgtxt) == standardize_xml(refsvg_str)
123def standardize_xml(xmltxt):
124 import xml.dom.minidom
125 dom = xml.dom.minidom.parseString(xmltxt) # or xml.dom.minidom.parseString(xml_string)
126 return dom.toprettyxml()
129def test_parse_tests():
130 """Check that we can parse a coverage.xml file successfully"""
131 res = get_test_stats(str(TESTS_FOLDER / "reports/junit/junit.xml"))
133 # not runned at all
134 assert res.errors == 1
135 # runned
136 assert res.runned == 5
137 assert res.failed == 2
138 assert res.success == 2
139 assert res.skipped == 1
141 assert res.total_with_skipped == res.runned + res.errors
142 assert res.total_without_skipped == res.runned - res.skipped + res.errors
144 assert res.success_percentage == res.success * 100 / res.total_without_skipped
147def test_parse_cov():
148 """Check that we can parse a coverage.xml file successfully"""
149 res = parse_cov(str(TESTS_FOLDER / "reports/coverage/coverage.xml"))
151 assert res.branches_valid == 18
152 assert res.branches_covered == 1
153 assert res.branch_rate == res.branches_covered / res.branches_valid
155 assert res.lines_valid == 73
156 assert res.lines_covered == 13
157 assert res.line_rate == res.lines_covered / res.lines_valid
159 assert res.branch_coverage == res.branch_rate * 100
160 assert res.line_coverage == res.line_rate * 100
162 assert res.total_rate == ((res.lines_covered + res.branches_covered) / (res.branches_valid + res.lines_valid))
163 assert res.total_coverage == 100 * res.total_rate
166def test_parse_cov_nobranch_issue_15():
167 """Check that we can parse a coverage.xml file successfully with the no branch option"""
168 res = parse_cov(str(TESTS_FOLDER / "reports/coverage/coverage_nobranch.xml"))
170 assert res.branches_valid == 0
171 assert res.branches_covered == 0
172 assert res.branch_rate == 0
174 assert res.lines_valid == 390
175 assert res.lines_covered == 328
176 assert res.line_rate == res.lines_covered / res.lines_valid
178 assert res.branch_coverage == res.branch_rate * 100
179 assert res.line_coverage == res.line_rate * 100
181 assert res.total_rate == ((res.lines_covered + res.branches_covered) / (res.branches_valid + res.lines_valid))
182 assert res.total_coverage == 100 * res.total_rate
185def test_parse_flake8():
186 """Check that we can parse a coverage.xml file successfully"""
187 res = get_flake8_stats(str(TESTS_FOLDER / "reports/flake8/flake8stats.txt"))
189 assert res.nb_critical == 6
190 assert res.nb_warning == 9
191 assert res.nb_info == 5
193 assert res.nb_total == res.nb_critical + res.nb_warning + res.nb_info