"""Relative Markdown links must resolve; code, anchors and URLs are not paths.""" import contextlib import importlib.util import io from pathlib import Path import tempfile import unittest spec = importlib.util.spec_from_file_location("links", Path(__file__).with_name("doc-links-check.py")) links = importlib.util.module_from_spec(spec) spec.loader.exec_module(links) class DocLinksTests(unittest.TestCase): def check(self, files): with tempfile.TemporaryDirectory() as root: for name, text in files.items(): path = Path(root, name) path.parent.mkdir(parents=True, exist_ok=True) path.write_text(text) return [(source, target) for source, _, target in links.broken_links(root)] def test_existing_targets_pass(self): broken = self.check({ "README.md": "[guide](docs/guide.md#start) ![d](docs/d.svg) [dir](docs/) [ref][r]\n\n[r]: \n", "docs/guide.md": "[up](../README.md) [root](/docs/d.svg) [esc](a%20b.md)\n", "docs/d.svg": "", "docs/a b.md": "", }) self.assertEqual(broken, []) def test_missing_targets_fail(self): broken = self.check({ "README.md": "[gone](docs/gone.md) [nested [x]](docs/x.md#a)\n\n[r]: missing.md\n", "docs/guide.md": "[up](../nope.md)\n", }) self.assertCountEqual(broken, [ ("README.md", "docs/gone.md"), ("README.md", "docs/x.md#a"), ("README.md", "missing.md"), ("docs/guide.md", "../nope.md"), ]) def test_urls_anchors_and_code_are_not_checked(self): broken = self.check({ "README.md": "\n".join([ "[web](https://example.invalid/x) [mail](mailto:a@example.invalid) [top](#top) [proto](//host/x)", "`[code](gone.md)` and ``[code](gone.md)``", "```", "[fenced](gone.md)", "```", "~~~md", "[tilde](gone.md)", "~~~", ]) + "\n", }) self.assertEqual(broken, []) def test_wrapped_labels_parentheses_and_long_fences(self): with tempfile.TemporaryDirectory() as root: Path(root, "guide(v2).md").write_text("") Path(root, "README.md").write_text("\n".join([ "[guide](guide(v2).md) [old](guide(v1).md)", "", "see the [wrapped", "label](wrapped.md) here", "", "`[wrapped", "code](gone.md)` then [after](after.md)", "````md", "```", "[example](missing.md)", "```", "````", ]) + "\n") broken = [(line, target) for _, line, target in links.broken_links(root)] self.assertEqual(broken, [(1, "guide(v1).md"), (3, "wrapped.md"), (7, "after.md")]) def test_code_spans_require_complete_equal_delimiter_runs(self): cases = [ ("`unmatched\n[real](real.md) ``", [(2, "real.md")]), ("``unmatched\n[real](real.md) `", [(2, "real.md")]), ("``unmatched\n[real](real.md) ```", [(2, "real.md")]), ("`[wrapped\ncode](hidden.md)`\n[after](after.md)", [(3, "after.md")]), ("``[wrapped `\ncode](hidden.md)``\n[after](after.md)", [(3, "after.md")]), ] for text, expected in cases: with self.subTest(text=text), tempfile.TemporaryDirectory() as root: Path(root, "README.md").write_text(text + "\n", encoding="utf-8") broken = [(line, target) for _, line, target in links.broken_links(root)] self.assertEqual(broken, expected) def test_escaped_backticks_do_not_hide_links(self): cases = [ ("\\`\n[real](real.md)\n\\`", [(2, "real.md")]), ("\\\\`[code](hidden.md)`\n[after](after.md)", [(2, "after.md")]), ("`[code](hidden.md)\\`\n[after](after.md)", [(2, "after.md")]), ] for text, expected in cases: with self.subTest(text=text): self.assertEqual(list(links.link_targets(text)), expected) def test_masked_code_preserves_reference_line_context(self): text = "Use `code\nmore`[r]: gone.md\n\n[r]: real.md\n" self.assertEqual(list(links.link_targets(text)), [(4, "real.md")]) def test_code_masking_contract(self): cases = [ ("unequal runs", "`unmatched\n[real](real.md) ``", [], [(2, "real.md")]), ("escaped delimiters", "\\`\n[real](real.md)\n\\`", [], [(2, "real.md")]), ("reference context", "Use `code\nmore`[r]: gone.md\n\n[r]: real.md", [], [(4, "real.md")]), ("wrapped span", "`[code\nhere](hidden.md)`\n[after](after.md)", [], [(3, "after.md")]), ("hidden-space invalid target", "[example](`foo bar`)", [], []), ("existing literal target", "[example](`foo.md`)", ["`foo.md`"], []), ("missing literal target", "[example](`foo.md`)", [], [(1, "`foo.md`")]), ] for name, text, existing, expected in cases: with self.subTest(name=name), tempfile.TemporaryDirectory() as root: Path(root, "README.md").write_text(text + "\n", encoding="utf-8") for target in existing: Path(root, target).touch() broken = [(line, target) for _, line, target in links.broken_links(root)] self.assertEqual(broken, expected) masked = links.mask_code_spans(text) self.assertEqual(len(masked), len(text)) self.assertEqual([i for i, c in enumerate(masked) if c == "\n"], [i for i, c in enumerate(text) if c == "\n"]) def test_main_exit_status(self): out = io.StringIO() with tempfile.TemporaryDirectory() as root, contextlib.redirect_stdout(out), contextlib.redirect_stderr(out): Path(root, "README.md").write_text("[gone](gone.md)\n") self.assertEqual(links.main(["doc-links-check.py", root]), 1) Path(root, "gone.md").write_text("") self.assertEqual(links.main(["doc-links-check.py", root]), 0) self.assertEqual(links.main(["doc-links-check.py"]), 2) self.assertIn("README.md:1: broken relative link gone.md", out.getvalue()) if __name__ == "__main__": unittest.main()