Files
seaweedfs/.github/block/scripts/doc-links-check-test.py
T
Chris LuandClaude Opus 5.5 b3403d5da6 block-ci: match doc links per paragraph, with nested parentheses and long fences
A link whose label wraps onto the next line was never checked, a target
such as guide(v2).md was cut at its first ')', and a three-backtick line
inside a four-backtick fence closed it. Close a fence only with the same
character at least as long, keep one level of balanced parentheses in a
destination, and match links over each paragraph.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-05 04:09:42 -08:00

89 lines
3.4 KiB
Python

"""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]: <docs/a b.md>\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",
"````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")])
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()