Files
seaweedfs/.github/block/scripts/doc-links-check-test.py
T
Chris LuandClaude Opus 5.5 ed7592dc4d ci: run the block documentation link check from root scripts
The Relative documentation links step called .github/scripts/doc-links-check.py
and its test inside seaweed-block/, but neither the imported module nor
upstream seaweed-block has them, so the unit suite would fail once the step
runs. Add the checker and its test under .github/block/scripts/ with the
other port scripts and point the step and its contract test there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 19:22:24 -08:00

72 lines
2.7 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_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()