|
| 1 | +"""Tests for the MkDocs ``on_pre_build`` hook. |
| 2 | +
|
| 3 | +The hook in ``tools/mkdocs_hooks.py`` generates architecture diagrams |
| 4 | +before MkDocs validates inter-page links. These tests exercise the |
| 5 | +hook's two code paths: successful generation and graceful fallback to |
| 6 | +placeholder SVGs when the diagram toolchain is unavailable. |
| 7 | +
|
| 8 | +The module under test lives outside the ``haclient`` package, so it is |
| 9 | +loaded directly from its file path. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import importlib.util |
| 15 | +import sys |
| 16 | +from pathlib import Path |
| 17 | +from types import ModuleType |
| 18 | +from typing import Any |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 23 | +HOOK_PATH = REPO_ROOT / "tools" / "mkdocs_hooks.py" |
| 24 | + |
| 25 | + |
| 26 | +def _load_hook_module() -> ModuleType: |
| 27 | + """Import ``tools/mkdocs_hooks.py`` as a standalone module.""" |
| 28 | + spec = importlib.util.spec_from_file_location("haclient_mkdocs_hooks", HOOK_PATH) |
| 29 | + assert spec is not None |
| 30 | + assert spec.loader is not None |
| 31 | + module = importlib.util.module_from_spec(spec) |
| 32 | + sys.modules[spec.name] = module |
| 33 | + spec.loader.exec_module(module) |
| 34 | + return module |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def hook(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> ModuleType: |
| 39 | + """Load the hook module with ``OUTPUT_DIR`` redirected to a temp dir.""" |
| 40 | + module = _load_hook_module() |
| 41 | + monkeypatch.setattr(module, "OUTPUT_DIR", tmp_path / "architecture") |
| 42 | + return module |
| 43 | + |
| 44 | + |
| 45 | +def test_placeholders_written_when_toolchain_missing( |
| 46 | + hook: ModuleType, monkeypatch: pytest.MonkeyPatch |
| 47 | +) -> None: |
| 48 | + """``on_pre_build`` writes placeholder SVGs if prerequisites are missing.""" |
| 49 | + monkeypatch.setattr(hook, "_has_prerequisites", lambda: False) |
| 50 | + |
| 51 | + hook.on_pre_build(config=None) |
| 52 | + |
| 53 | + for name in hook.EXPECTED_FILES: |
| 54 | + target = hook.OUTPUT_DIR / name |
| 55 | + assert target.exists(), f"expected placeholder {name}" |
| 56 | + content = target.read_text(encoding="utf-8") |
| 57 | + assert "<svg" in content |
| 58 | + assert "placeholder" in content.lower() |
| 59 | + |
| 60 | + |
| 61 | +def test_placeholders_written_when_generator_fails( |
| 62 | + hook: ModuleType, monkeypatch: pytest.MonkeyPatch |
| 63 | +) -> None: |
| 64 | + """If the generator fails at runtime, the hook still writes placeholders.""" |
| 65 | + monkeypatch.setattr(hook, "_has_prerequisites", lambda: True) |
| 66 | + monkeypatch.setattr(hook, "_run_generator", lambda: False) |
| 67 | + |
| 68 | + hook.on_pre_build(config=None) |
| 69 | + |
| 70 | + for name in hook.EXPECTED_FILES: |
| 71 | + assert (hook.OUTPUT_DIR / name).exists() |
| 72 | + |
| 73 | + |
| 74 | +def test_generator_invoked_when_toolchain_available( |
| 75 | + hook: ModuleType, monkeypatch: pytest.MonkeyPatch |
| 76 | +) -> None: |
| 77 | + """When prerequisites exist the generator runs and no placeholders are needed.""" |
| 78 | + calls: list[str] = [] |
| 79 | + |
| 80 | + def fake_run_generator() -> bool: |
| 81 | + calls.append("ran") |
| 82 | + return True |
| 83 | + |
| 84 | + def fail_placeholders() -> None: # pragma: no cover - must not be called |
| 85 | + raise AssertionError("placeholders must not be written on success") |
| 86 | + |
| 87 | + monkeypatch.setattr(hook, "_has_prerequisites", lambda: True) |
| 88 | + monkeypatch.setattr(hook, "_run_generator", fake_run_generator) |
| 89 | + monkeypatch.setattr(hook, "_write_placeholders", fail_placeholders) |
| 90 | + |
| 91 | + hook.on_pre_build(config=None) |
| 92 | + |
| 93 | + assert calls == ["ran"] |
| 94 | + |
| 95 | + |
| 96 | +def test_placeholder_writer_does_not_overwrite_existing(hook: ModuleType) -> None: |
| 97 | + """``_write_placeholders`` must preserve existing files (real diagrams).""" |
| 98 | + hook.OUTPUT_DIR.mkdir(parents=True, exist_ok=True) |
| 99 | + real_file = hook.OUTPUT_DIR / hook.EXPECTED_FILES[0] |
| 100 | + real_file.write_text("REAL DIAGRAM", encoding="utf-8") |
| 101 | + |
| 102 | + hook._write_placeholders() |
| 103 | + |
| 104 | + assert real_file.read_text(encoding="utf-8") == "REAL DIAGRAM" |
| 105 | + # The other expected file should be created as a placeholder. |
| 106 | + other = hook.OUTPUT_DIR / hook.EXPECTED_FILES[1] |
| 107 | + assert other.exists() |
| 108 | + assert "placeholder" in other.read_text(encoding="utf-8").lower() |
| 109 | + |
| 110 | + |
| 111 | +def test_has_prerequisites_false_when_dot_missing( |
| 112 | + hook: ModuleType, monkeypatch: pytest.MonkeyPatch |
| 113 | +) -> None: |
| 114 | + """``_has_prerequisites`` returns False when ``dot`` is not on PATH.""" |
| 115 | + monkeypatch.setattr(hook.shutil, "which", lambda _name: None) |
| 116 | + |
| 117 | + assert hook._has_prerequisites() is False |
| 118 | + |
| 119 | + |
| 120 | +def test_has_prerequisites_false_when_pyreverse_missing( |
| 121 | + hook: ModuleType, monkeypatch: pytest.MonkeyPatch |
| 122 | +) -> None: |
| 123 | + """``_has_prerequisites`` returns False when ``pylint.pyreverse`` is missing.""" |
| 124 | + monkeypatch.setattr(hook.shutil, "which", lambda _name: "/usr/bin/dot") |
| 125 | + |
| 126 | + def fake_run(*_args: Any, **_kwargs: Any) -> None: |
| 127 | + raise hook.subprocess.CalledProcessError(returncode=1, cmd=["python"]) |
| 128 | + |
| 129 | + monkeypatch.setattr(hook.subprocess, "run", fake_run) |
| 130 | + |
| 131 | + assert hook._has_prerequisites() is False |
| 132 | + |
| 133 | + |
| 134 | +def test_run_generator_reports_failure(hook: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None: |
| 135 | + """``_run_generator`` returns False when the subprocess errors out.""" |
| 136 | + |
| 137 | + def fake_run(*_args: Any, **_kwargs: Any) -> None: |
| 138 | + raise hook.subprocess.CalledProcessError(returncode=2, cmd=["script"]) |
| 139 | + |
| 140 | + monkeypatch.setattr(hook.subprocess, "run", fake_run) |
| 141 | + |
| 142 | + assert hook._run_generator() is False |
0 commit comments