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