Skip to content

Commit 8b9ce3a

Browse files
authored
support diff-cover (#35)
1 parent de0b1c6 commit 8b9ce3a

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

cli/smokeshow/main.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ def fmt_size(num: int) -> str:
216216

217217

218218
GITHUB_API_ROOT = 'https://api.github.com'
219+
COVERAGE_REGEXES = (
220+
# for coverage
221+
re.compile(r'<span\s+class="pc_cov">\s*([\d.]+)%\s*</span>'),
222+
# for diff-cover
223+
re.compile(r'<li><b>Coverage</b>: *([\d.]+)%</li>'),
224+
)
219225

220226

221227
def get_github_status_info(path: Path, description: str, coverage_threshold: Optional[float]) -> tuple[str, str]:
@@ -226,14 +232,16 @@ def get_github_status_info(path: Path, description: str, coverage_threshold: Opt
226232
cov_sub = '{COVERAGE NOT FOUND}'
227233
index_path = path / 'index.html'
228234
if index_path.is_file():
229-
m = re.search(r'<span\s+class="pc_cov">\s*([\d.]+)%\s*</span>', index_path.read_text())
230-
if m:
231-
coverage = float(m.group(1))
232-
if coverage_threshold is not None and coverage < coverage_threshold:
233-
state = 'failure'
234-
cov_sub = f'{coverage:0.2f}% < {coverage_threshold:0.2f}%'
235-
else:
236-
cov_sub = f'{coverage:0.2f}%'
235+
for regex in COVERAGE_REGEXES:
236+
m = regex.search(index_path.read_text())
237+
if m:
238+
coverage = float(m.group(1))
239+
if coverage_threshold is not None and coverage < coverage_threshold:
240+
state = 'failure'
241+
cov_sub = f'{coverage:0.2f}% < {coverage_threshold:0.2f}%'
242+
else:
243+
cov_sub = f'{coverage:0.2f}%'
244+
break
237245

238246
description = re.sub('{coverage-percentage}', cov_sub, description, flags=re.I)
239247
return state, description

cli/tests/test_gh_integration.py

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ def test_no_index(tmp_path):
3939
assert get_github_status_info(tmp_path, 'test {coverage-percentage}', 1) == ('success', 'test {COVERAGE NOT FOUND}')
4040

4141

42+
def test_diff_cover(tmp_path):
43+
f = tmp_path / 'index.html'
44+
f.write_text('<li><b>Coverage</b>: 98%</li>')
45+
assert get_github_status_info(tmp_path, 'test {coverage-percentage}', 96) == ('success', 'test 98.00%')
46+
47+
4248
def test_root_is_file(tmp_path):
4349
f = tmp_path / 'foobar.html'
4450
f.write_text('hello')

0 commit comments

Comments
 (0)