mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
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>
This commit is contained in:
1 parent
915909cf6b
commit
ed7592dc4d
4 files changed
+155
-3
No files matched your search
@@ -18,8 +18,8 @@ class PortTests(unittest.TestCase):
|
||||
self.assertEqual(text.count(marker), 1)
|
||||
step = text.split(marker, 1)[1].split("\n - name:", 1)[0]
|
||||
self.assertIn("if: matrix.suite == 'unit'", step)
|
||||
self.assertIn("python3 -B .github/scripts/doc-links-check-test.py", step)
|
||||
self.assertIn("python3 -I .github/scripts/doc-links-check.py .", step)
|
||||
self.assertIn("python3 -B ../.github/block/scripts/doc-links-check-test.py", step)
|
||||
self.assertIn("python3 -I ../.github/block/scripts/doc-links-check.py .", step)
|
||||
self.assertNotIn("continue-on-error", step)
|
||||
self.assertNotIn("|| true", step)
|
||||
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
"""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)  [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()
|
||||
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Fail when a Markdown file links to a relative path that does not exist.
|
||||
|
||||
Usage: doc-links-check.py <root>
|
||||
"""
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import urllib.parse
|
||||
|
||||
SKIP_DIRS = {".git", "node_modules", "vendor"}
|
||||
FENCE = re.compile(r"^\s*(```|~~~)")
|
||||
INLINE_CODE = re.compile(r"(`+)(?:(?!\1).)+?\1")
|
||||
INLINE_LINK = re.compile(r"!?\[(?:[^\[\]]|\[[^\[\]]*\])*\]\(\s*(<[^>]*>|[^\s)]+)")
|
||||
REFERENCE = re.compile(r"^\s{0,3}\[[^\]]+\]:\s*(<[^>]*>|\S+)")
|
||||
SCHEME = re.compile(r"^[A-Za-z][A-Za-z0-9+.-]*:")
|
||||
|
||||
|
||||
def link_targets(text):
|
||||
fence = None
|
||||
for lineno, line in enumerate(text.splitlines(), 1):
|
||||
m = FENCE.match(line)
|
||||
if m:
|
||||
if fence is None:
|
||||
fence = m.group(1)
|
||||
elif m.group(1) == fence:
|
||||
fence = None
|
||||
continue
|
||||
if fence is not None:
|
||||
continue
|
||||
line = INLINE_CODE.sub("", line)
|
||||
for m in INLINE_LINK.finditer(line):
|
||||
yield lineno, m.group(1)
|
||||
m = REFERENCE.match(line)
|
||||
if m:
|
||||
yield lineno, m.group(1)
|
||||
|
||||
|
||||
def local_path(target):
|
||||
if target.startswith("<") and target.endswith(">"):
|
||||
target = target[1:-1]
|
||||
if not target or target.startswith(("#", "//")) or SCHEME.match(target):
|
||||
return None
|
||||
path = urllib.parse.unquote(target.split("#", 1)[0].split("?", 1)[0])
|
||||
return path or None
|
||||
|
||||
|
||||
def broken_links(root):
|
||||
root = os.path.abspath(root)
|
||||
for dirpath, dirnames, filenames in os.walk(root):
|
||||
dirnames[:] = sorted(d for d in dirnames if d not in SKIP_DIRS)
|
||||
for name in sorted(filenames):
|
||||
if not name.endswith(".md"):
|
||||
continue
|
||||
source = os.path.join(dirpath, name)
|
||||
with open(source, encoding="utf-8", errors="replace") as f:
|
||||
text = f.read()
|
||||
for lineno, target in link_targets(text):
|
||||
path = local_path(target)
|
||||
if path is None:
|
||||
continue
|
||||
if path.startswith("/"):
|
||||
resolved = os.path.join(root, path.lstrip("/"))
|
||||
else:
|
||||
resolved = os.path.join(dirpath, path)
|
||||
if not os.path.exists(resolved):
|
||||
yield os.path.relpath(source, root), lineno, target
|
||||
|
||||
|
||||
def main(argv):
|
||||
if len(argv) != 2:
|
||||
print(__doc__.strip(), file=sys.stderr)
|
||||
return 2
|
||||
broken = list(broken_links(argv[1]))
|
||||
for source, lineno, target in broken:
|
||||
print(f"{source}:{lineno}: broken relative link {target}")
|
||||
return 1 if broken else 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main(sys.argv))
|
||||
Reference in new issue
Block a user