Repository navigation
fix: check documentation links on pull requests #363
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
Changes from 1 commit
26a3baa
63da7e1
36fb938
ac23eae
e57a48d
de3dfd2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| name: Check Documentation | ||
|
|
||
| on: [pull_request] | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| with: | ||
| persist-credentials: false | ||
| - name: Set up uv | ||
| uses: astral-sh/setup-uv@20cfd1bf945f4377ade1205e4dbc17946fc9a30d # v10.0.1 | ||
| with: | ||
| python-version: "3.12" | ||
| - name: Install dependencies | ||
| run: uv sync --group docs --frozen | ||
| - name: Test documentation check | ||
| run: uv run --frozen pytest tests/test_docs_build.py | ||
| - name: Build and check documentation links | ||
| run: uv run --frozen python docs/check_links.py |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| """Build docs, failing on MkDocs page/navigation warnings, not plugin warnings.""" | ||
|
|
||
| import argparse | ||
| import logging | ||
| from pathlib import Path | ||
|
|
||
| from mkdocs.commands.build import build | ||
| from mkdocs.config import load_config | ||
| from mkdocs.exceptions import Abort | ||
|
|
||
|
|
||
| class _LinkWarnings(logging.Handler): | ||
| def __init__(self): | ||
| super().__init__(level=logging.WARNING) | ||
| self.count = 0 | ||
|
|
||
| def emit(self, record): | ||
| self.count += 1 | ||
|
|
||
|
|
||
| def check_links(config_file: str, site_dir: str | None = None) -> int: | ||
| """Build a site and return nonzero for broken links; build errors propagate.""" | ||
| config = load_config(config_file=config_file, site_dir=site_dir) | ||
| # Anchors default to INFO, which even a strict build does not reject. | ||
| config.validation.links.anchors = logging.WARNING | ||
| config.validation.links.not_found = logging.WARNING | ||
| config.validation.nav.not_found = logging.WARNING | ||
| counter = _LinkWarnings() | ||
| loggers = [ | ||
| logging.getLogger(f"mkdocs.structure.{name}") for name in ("pages", "nav") | ||
| ] | ||
| for logger in loggers: | ||
| logger.addHandler(counter) | ||
| try: | ||
| config.plugins.on_startup(command="build", dirty=False) | ||
| try: | ||
| build(config) | ||
| finally: | ||
| config.plugins.on_shutdown() | ||
| finally: | ||
| for logger in loggers: | ||
| logger.removeHandler(counter) | ||
| counter.close() | ||
| if counter.count: | ||
| logging.getLogger("mkdocs").error( | ||
| "Documentation link check failed: %d page/navigation warning(s).", | ||
| counter.count, | ||
| ) | ||
| return 1 | ||
| return 0 | ||
|
|
||
|
|
||
| def main() -> int: | ||
| """Run the same build-only check locally and in pull-request CI.""" | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument( | ||
| "-f", "--config-file", default=str(Path(__file__).with_name("mkdocs.yml")) | ||
| ) | ||
| parser.add_argument("-d", "--site-dir") | ||
| args = parser.parse_args() | ||
| logging.basicConfig(level=logging.INFO, format="%(levelname)s - %(message)s") | ||
| try: | ||
| return check_links(args.config_file, args.site_dir) | ||
| except Abort as error: | ||
|
Contributor
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. Should fix. Only |
||
| logging.getLogger("mkdocs").error("%s", error) | ||
| return 1 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| """Exercise the documentation gate with real MkDocs builds (issue #355).""" | ||
|
|
||
| import logging | ||
| import re | ||
| import runpy | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
| import yaml | ||
|
Contributor
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. Nit. This module needs |
||
|
|
||
| ROOT = Path(__file__).resolve().parents[1] | ||
| BUILD_COMMAND = [sys.executable, str(ROOT / "docs" / "check_links.py")] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def docs_project(tmp_path): | ||
| docs = tmp_path / "docs" | ||
| docs.mkdir() | ||
| (docs / "other page.md").write_text("# Existing heading\n", encoding="utf-8") | ||
| config = { | ||
| "site_name": "Link gate fixture", | ||
| "theme": {"name": "mkdocs"}, | ||
| "plugins": [], | ||
| } | ||
|
|
||
| def build(markdown="# Home\n", *, extra_config=None, hook=None): | ||
| (docs / "index.md").write_text(markdown, encoding="utf-8") | ||
| settings = {**config, **(extra_config or {})} | ||
| if hook: | ||
| (tmp_path / "fixture_hook.py").write_text(hook, encoding="utf-8") | ||
| settings["hooks"] = ["fixture_hook.py"] | ||
| config_file = tmp_path / "mkdocs.yml" | ||
| config_file.write_text(yaml.safe_dump(settings), encoding="utf-8") | ||
| return subprocess.run( | ||
| [*BUILD_COMMAND, "-f", str(config_file), "-d", str(tmp_path / "site")], | ||
| cwd=tmp_path, | ||
| capture_output=True, | ||
| text=True, | ||
| encoding="utf-8", | ||
| errors="replace", | ||
| timeout=60, | ||
| ) | ||
|
|
||
| return build | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "markdown", | ||
| [ | ||
| "# Home\n", | ||
| "# Home\n[Local](#home)", | ||
| "# Home\n[Other](other%20page.md#existing-heading)", | ||
| "# Home\n[External](https://example.invalid/not-fetched#heading)", | ||
| "# Home\n`[Example](missing.md)`", | ||
| ], | ||
| ) | ||
| def test_valid_docs_build(docs_project, markdown): | ||
| result = docs_project(markdown) | ||
| assert result.returncode == 0, result.stdout + result.stderr | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("link", "diagnostic"), | ||
| [ | ||
| ("[Missing](missing.md)", "missing.md"), | ||
| ("", "missing.png"), | ||
| ("[Missing](#absent-heading)", "absent-heading"), | ||
| ("[Missing](other%20page.md#absent-heading)", "absent-heading"), | ||
| ], | ||
| ) | ||
| def test_broken_links_fail(docs_project, link, diagnostic): | ||
| result = docs_project(f"# Home\n{link}\n") | ||
| output = result.stdout + result.stderr | ||
| assert diagnostic in output, output | ||
| assert result.returncode != 0, output | ||
|
|
||
|
|
||
| def test_missing_navigation_target_fails(docs_project): | ||
| result = docs_project(extra_config={"nav": [{"Home": "missing.md"}]}) | ||
| output = result.stdout + result.stderr | ||
| assert "missing.md" in output, output | ||
| assert result.returncode != 0, output | ||
|
|
||
|
|
||
| def test_unrelated_warning_remains_visible_without_failing(docs_project): | ||
| result = docs_project( | ||
| hook="import logging\n" | ||
| "def on_pre_build(**kwargs):\n" | ||
| " logging.getLogger('mkdocs.plugins.fixture').warning('existing API warning')\n" | ||
| ) | ||
| assert "existing API warning" in result.stdout + result.stderr | ||
| assert result.returncode == 0, result.stdout + result.stderr | ||
|
|
||
|
|
||
| def test_invalid_configuration_fails(docs_project): | ||
| result = docs_project(extra_config={"docs_dir": "not-a-directory"}) | ||
| assert "not-a-directory" in result.stdout + result.stderr | ||
| assert result.returncode != 0 | ||
|
|
||
|
|
||
| def test_build_exception_fails(docs_project): | ||
| result = docs_project( | ||
| hook="def on_pre_build(**kwargs):\n" | ||
| " raise RuntimeError('fixture build failure')\n" | ||
| ) | ||
| assert "fixture build failure" in result.stdout + result.stderr | ||
| assert result.returncode != 0 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("fail", [False, True]) | ||
| def test_plugin_lifecycle_is_preserved(docs_project, fail): | ||
| result = docs_project( | ||
| hook="def on_startup(**kwargs):\n" | ||
| " print('fixture startup')\n" | ||
| "def on_shutdown():\n" | ||
| " print('fixture shutdown')\n" | ||
| "def on_pre_build(**kwargs):\n" | ||
| f" if {fail}: raise RuntimeError('fixture build failure')\n" | ||
| ) | ||
| assert "fixture startup" in result.stdout | ||
| assert "fixture shutdown" in result.stdout | ||
| assert (result.returncode != 0) == fail | ||
|
|
||
|
|
||
| def test_repeated_builds_do_not_leak_warning_state(docs_project, tmp_path, caplog): | ||
| docs_project("# Home\n[Missing](missing.md)") | ||
| checker = runpy.run_path(str(ROOT / "docs" / "check_links.py"))["check_links"] | ||
| loggers = [ | ||
| logging.getLogger(f"mkdocs.structure.{name}") for name in ("pages", "nav") | ||
| ] | ||
| original_handlers = [list(logger.handlers) for logger in loggers] | ||
| with caplog.at_level(logging.INFO): | ||
| assert checker(str(tmp_path / "mkdocs.yml")) == 1 | ||
| (tmp_path / "docs" / "index.md").write_text("# Home\n", encoding="utf-8") | ||
| assert checker(str(tmp_path / "mkdocs.yml")) == 0 | ||
| docs_project( | ||
| hook="def on_pre_build(**kwargs):\n" | ||
| " raise RuntimeError('fixture build failure')\n" | ||
| ) | ||
| with pytest.raises(RuntimeError, match="fixture build failure"): | ||
| checker(str(tmp_path / "mkdocs.yml")) | ||
| assert [logger.handlers for logger in loggers] == original_handlers | ||
|
|
||
|
|
||
| def test_pr_workflow_only_builds_with_read_permissions(): | ||
| workflow = yaml.load( | ||
| (ROOT / ".github" / "workflows" / "docs-check.yml").read_text(encoding="utf-8"), | ||
| Loader=yaml.BaseLoader, | ||
| ) | ||
| assert workflow["on"] == ["pull_request"] | ||
| assert workflow["permissions"] == {"contents": "read"} | ||
| steps = workflow["jobs"]["build"]["steps"] | ||
| assert "permissions" not in workflow["jobs"]["build"] | ||
| for step in steps: | ||
| if "uses" in step: | ||
| assert re.fullmatch(r"[\w/-]+@[0-9a-f]{40}", step["uses"]) | ||
| commands = [step["run"] for step in steps if "run" in step] | ||
| assert commands == [ | ||
| "uv sync --group docs --frozen", | ||
| "uv run --frozen pytest tests/test_docs_build.py", | ||
| "uv run --frozen python docs/check_links.py", | ||
| ] | ||
| assert steps[0]["with"]["persist-credentials"] == "false" | ||
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.
Non-blocking. This re-creates the CLI's build sequence but skips its
_enable_warnings(), so PythonDeprecationWarnings from mkdocs or plugins never reach themkdocslogger and aren't counted. That's a difference frommkdocs build --strict, which counts them, and the module docstring says this fails on MkDocs diagnostics. Worth either routing them the same way or noting the difference.