Skip to content

Commit

Permalink
test: check that our generated html files have valid hrefs
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed May 19, 2022
1 parent 56ccf32 commit fa4e6d1
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

"""Tests that HTML generation is awesome."""

import collections
import datetime
import glob
import json
Expand Down Expand Up @@ -52,7 +53,9 @@ def run_coverage(self, covargs=None, htmlargs=None):
self.clean_local_file_imports()
cov = coverage.Coverage(**(covargs or {}))
self.start_import_stop(cov, "main_file")
return cov.html_report(**(htmlargs or {}))
ret = cov.html_report(**(htmlargs or {}))
self.assert_valid_hrefs()
return ret

def get_html_report_content(self, module):
"""Return the content of the HTML report for `module`."""
Expand Down Expand Up @@ -95,6 +98,27 @@ def assert_correct_timestamp(self, html):
msg=f"Timestamp is wrong: {timestamp}",
)

def assert_valid_hrefs(self):
"""Assert that the hrefs in htmlcov/*.html to see the references are valid.
Doesn't check external links (those with a protocol).
"""
hrefs = collections.defaultdict(set)
for fname in glob.glob("htmlcov/*.html"):
with open(fname) as fhtml:
html = fhtml.read()
for href in re.findall(r""" href=['"]([^'"]*)['"]""", html):
if href.startswith("#"):
assert re.search(rf""" id=['"]{href[1:]}['"]""", html)
continue
if "://" in href:
continue
hrefs[href].add(fname)
for href, sources in hrefs.items():
assert os.path.exists(f"htmlcov/{href}"), (
f"These files link to {href!r}, which doesn't exist: {', '.join(sources)}"
)


class FileWriteTracker:
"""A fake object to track how `open` is used to write files."""
Expand Down

0 comments on commit fa4e6d1

Please sign in to comment.