Repository navigation
test relative links in example documentation #1811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,45 @@ | ||||||||||||||||||||||||||||
| """Check that links in the example documentation point at real files.""" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| from __future__ import annotations | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| import re | ||||||||||||||||||||||||||||
| from pathlib import Path | ||||||||||||||||||||||||||||
| from urllib.parse import urlsplit | ||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Paired with the
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| EXAMPLES_ROOT = Path(__file__).parent / "examples" | ||||||||||||||||||||||||||||
| REPOSITORY_ROOT = EXAMPLES_ROOT.parent.parent | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # The CLI page is generated during the documentation build and intentionally is not | ||||||||||||||||||||||||||||
| # checked into the repository. | ||||||||||||||||||||||||||||
| GENERATED_DOCS = {REPOSITORY_ROOT / "docs" / "docs" / "reference" / "cli.md"} | ||||||||||||||||||||||||||||
| LINK_RE = re.compile(r"!?\[[^\]]*\]\(\s*(?:<([^>]+)>|([^\s)]+))") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def _local_links(path: Path): | ||||||||||||||||||||||||||||
| in_code_block = False | ||||||||||||||||||||||||||||
| for line in path.read_text(encoding="utf-8").splitlines(): | ||||||||||||||||||||||||||||
| if line.strip().startswith("```"): | ||||||||||||||||||||||||||||
| in_code_block = not in_code_block | ||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||
| if in_code_block: | ||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||
| yield from (first or second for first, second in LINK_RE.findall(line)) | ||||||||||||||||||||||||||||
|
Comment on lines
+21
to
+26
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two parsing cases cause false positives: links inside inline code (
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def _missing_links() -> list[str]: | ||||||||||||||||||||||||||||
| missing = [] | ||||||||||||||||||||||||||||
| for markdown_file in EXAMPLES_ROOT.rglob("*.md"): | ||||||||||||||||||||||||||||
| for target in _local_links(markdown_file): | ||||||||||||||||||||||||||||
| parsed = urlsplit(target) | ||||||||||||||||||||||||||||
| if parsed.scheme or parsed.netloc: | ||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||
| target_path = (markdown_file.parent / parsed.path).resolve() | ||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. URL-encoded links like
Suggested change
|
||||||||||||||||||||||||||||
| if target_path.exists() or target_path in GENERATED_DOCS: | ||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||
| missing.append(f"{markdown_file.relative_to(REPOSITORY_ROOT)}: {target}") | ||||||||||||||||||||||||||||
| return missing | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_example_markdown_links_resolve(): | ||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This only checks the current tree, which has no broken links, so nothing shows the check can actually fail. Could you add a couple of |
||||||||||||||||||||||||||||
| """Prevent broken relative links from being added outside Docusaurus.""" | ||||||||||||||||||||||||||||
| assert not (missing := _missing_links()), "Broken example links:\n" + "\n".join(missing) | ||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI won't collect this file. The pytest step in
.github/workflows/quality.ymlruns onlytestandtooling/docs-autogen/test_validate.py, so a test indocs/only ever runs locally. Could you move it totest/test_example_links.py, next totest/test_example_collection.py? It will then run in CI and pick up the auto-appliedunitmarker.test/files also need the SPDX license header; the hook excludesdocs/, which is why it passes now. UpdateEXAMPLES_ROOT/REPOSITORY_ROOTfor the new location.