Skip to content

Commit

Permalink
support diff-cover (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin authored Jan 7, 2025
1 parent de0b1c6 commit 8b9ce3a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
24 changes: 16 additions & 8 deletions cli/smokeshow/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ def fmt_size(num: int) -> str:


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


def get_github_status_info(path: Path, description: str, coverage_threshold: Optional[float]) -> tuple[str, str]:
Expand All @@ -226,14 +232,16 @@ def get_github_status_info(path: Path, description: str, coverage_threshold: Opt
cov_sub = '{COVERAGE NOT FOUND}'
index_path = path / 'index.html'
if index_path.is_file():
m = re.search(r'<span\s+class="pc_cov">\s*([\d.]+)%\s*</span>', index_path.read_text())
if m:
coverage = float(m.group(1))
if coverage_threshold is not None and coverage < coverage_threshold:
state = 'failure'
cov_sub = f'{coverage:0.2f}% < {coverage_threshold:0.2f}%'
else:
cov_sub = f'{coverage:0.2f}%'
for regex in COVERAGE_REGEXES:
m = regex.search(index_path.read_text())
if m:
coverage = float(m.group(1))
if coverage_threshold is not None and coverage < coverage_threshold:
state = 'failure'
cov_sub = f'{coverage:0.2f}% < {coverage_threshold:0.2f}%'
else:
cov_sub = f'{coverage:0.2f}%'
break

description = re.sub('{coverage-percentage}', cov_sub, description, flags=re.I)
return state, description
Expand Down
6 changes: 6 additions & 0 deletions cli/tests/test_gh_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def test_no_index(tmp_path):
assert get_github_status_info(tmp_path, 'test {coverage-percentage}', 1) == ('success', 'test {COVERAGE NOT FOUND}')


def test_diff_cover(tmp_path):
f = tmp_path / 'index.html'
f.write_text('<li><b>Coverage</b>: 98%</li>')
assert get_github_status_info(tmp_path, 'test {coverage-percentage}', 96) == ('success', 'test 98.00%')


def test_root_is_file(tmp_path):
f = tmp_path / 'foobar.html'
f.write_text('hello')
Expand Down

0 comments on commit 8b9ce3a

Please sign in to comment.