1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
"""Tests for the mkdocstrings plugin."""
from __future__ import annotations
from typing import TYPE_CHECKING
from mkdocs.commands.build import build
from mkdocs.config import load_config
from mkdocstrings import MkdocstringsPlugin
if TYPE_CHECKING:
from pathlib import Path
def test_disabling_plugin(tmp_path: Path) -> None:
"""Test disabling plugin."""
docs_dir = tmp_path / "docs"
site_dir = tmp_path / "site"
docs_dir.mkdir()
site_dir.mkdir()
docs_dir.joinpath("index.md").write_text("::: mkdocstrings")
config_file = tmp_path / "mkdocs.yml"
config_file.write_text(
"""
site_name: Test
theme: mkdocs
plugins:
- mkdocstrings:
enabled: false
""",
)
mkdocs_config = load_config(str(config_file))
mkdocs_config["docs_dir"] = str(docs_dir)
mkdocs_config["site_dir"] = str(site_dir)
mkdocs_config["plugins"].run_event("startup", command="build", dirty=False)
try:
build(mkdocs_config)
finally:
mkdocs_config["plugins"].run_event("shutdown")
# make sure the instruction was not processed
assert "::: mkdocstrings" in site_dir.joinpath("index.html").read_text(encoding="utf8")
def test_plugin_default_config(tmp_path: Path) -> None:
"""Test default config options are set for Plugin."""
config_file_path = tmp_path / "mkdocs.yml"
plugin = MkdocstringsPlugin()
errors, warnings = plugin.load_config({}, config_file_path=str(config_file_path))
assert errors == []
assert warnings == []
assert plugin.config == {
"handlers": {},
"default_handler": "python",
"custom_templates": None,
"enable_inventory": None,
"enabled": True,
"locale": None,
}
def test_plugin_config_custom_templates(tmp_path: Path) -> None:
"""Test custom_templates option is relative to config file."""
config_file_path = tmp_path / "mkdocs.yml"
options = {"custom_templates": "docs/templates"}
template_dir = tmp_path / options["custom_templates"]
# Path must exist or config validation will fail.
template_dir.mkdir(parents=True)
plugin = MkdocstringsPlugin()
errors, warnings = plugin.load_config(options, config_file_path=str(config_file_path))
assert errors == []
assert warnings == []
assert plugin.config == {
"handlers": {},
"default_handler": "python",
"custom_templates": str(template_dir),
"enable_inventory": None,
"enabled": True,
"locale": None,
}
|