From 907d646952297e8116d247b94868fe173e76043b Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Fri, 20 May 2022 17:26:12 -0400 Subject: test: tests of HTML's helper assert_valid_hrefs --- tests/test_html.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'tests/test_html.py') diff --git a/tests/test_html.py b/tests/test_html.py index cd2b0aa7..d649c67f 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -109,7 +109,9 @@ class HtmlTestHelpers(CoverageTest): html = fhtml.read() for href in re.findall(r""" href=['"]([^'"]*)['"]""", html): if href.startswith("#"): - assert re.search(rf""" id=['"]{href[1:]}['"]""", html) + assert re.search(rf""" id=['"]{href[1:]}['"]""", html), ( + f"Fragment {href!r} in {fname} has no anchor" + ) continue if "://" in href: continue @@ -1188,3 +1190,21 @@ class HtmlWithContextsTest(HtmlTestHelpers, CoverageTest): if label == ld.contexts_label or label in (ld.contexts or ()) ] assert sorted(expected) == sorted(actual) + + +class HtmlHelpersTest(HtmlTestHelpers, CoverageTest): + """Tests of the helpers in HtmlTestHelpers.""" + + def test_bad_link(self): + # Does assert_valid_hrefs detect links to non-existent files? + self.make_file("htmlcov/index.html", "Nothing") + msg = "These files link to 'nothing.html', which doesn't exist: htmlcov.index.html" + with pytest.raises(AssertionError, match=msg): + self.assert_valid_hrefs() + + def test_bad_anchor(self): + # Does assert_valid_hrefs detect fragments that go nowhere? + self.make_file("htmlcov/index.html", "Nothing") + msg = "Fragment '#nothing' in htmlcov.index.html has no anchor" + with pytest.raises(AssertionError, match=msg): + self.assert_valid_hrefs() -- cgit v1.2.1