| 1 |
"""Configuration for the pytest test suite.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from collections import ChainMap |
| 6 |
from typing import TYPE_CHECKING, Any |
| 7 |
|
| 8 |
import pytest |
| 9 |
from markdown.core import Markdown |
| 10 |
from mkdocs.config.defaults import MkDocsConfig |
| 11 |
|
| 12 |
if TYPE_CHECKING: |
| 13 |
from collections.abc import Iterator |
| 14 |
from pathlib import Path |
| 15 |
|
| 16 |
from mkdocs import config |
| 17 |
|
| 18 |
from mkdocstrings._internal.plugin import MkdocstringsPlugin |
| 19 |
|
| 20 |
|
| 21 |
@pytest.fixture(name="mkdocs_conf") |
| 22 |
def fixture_mkdocs_conf(request: pytest.FixtureRequest, tmp_path: Path) -> Iterator[config.Config]: |
| 23 |
"""Yield a MkDocs configuration object.""" |
| 24 |
conf = MkDocsConfig() |
| 25 |
while hasattr(request, "_parent_request") and hasattr(request._parent_request, "_parent_request"): |
| 26 |
request = request._parent_request # ty: ignore[invalid-assignment] |
| 27 |
|
| 28 |
conf_dict = { |
| 29 |
"site_name": "foo", |
| 30 |
"site_url": "https://example.org/", |
| 31 |
"site_dir": str(tmp_path), |
| 32 |
"plugins": [{"mkdocstrings": {"default_handler": "python"}}], |
| 33 |
**getattr(request, "param", {}), |
| 34 |
} |
| 35 |
# Re-create it manually as a workaround for https://github.com/mkdocs/mkdocs/issues/2289 |
| 36 |
mdx_configs: dict[str, Any] = dict(ChainMap(*conf_dict.get("markdown_extensions", []))) # ty: ignore[invalid-argument-type,invalid-assignment] |
| 37 |
|
| 38 |
conf.load_dict(conf_dict) |
| 39 |
assert conf.validate() == ([], []) |
| 40 |
|
| 41 |
conf["mdx_configs"] = mdx_configs |
| 42 |
conf["markdown_extensions"].insert(0, "toc") # Guaranteed to be added by MkDocs. |
| 43 |
|
| 44 |
conf = conf["plugins"]["mkdocstrings"].on_config(conf) |
| 45 |
conf = conf["plugins"]["autorefs"].on_config(conf) |
| 46 |
yield conf |
| 47 |
conf["plugins"]["mkdocstrings"].on_post_build(conf) |
| 48 |
|
| 49 |
|
| 50 |
@pytest.fixture(name="plugin") |
| 51 |
def fixture_plugin(mkdocs_conf: config.Config) -> MkdocstringsPlugin: |
| 52 |
"""Return a plugin instance.""" |
| 53 |
return mkdocs_conf["plugins"]["mkdocstrings"] |
| 54 |
|
| 55 |
|
| 56 |
@pytest.fixture(name="ext_markdown") |
| 57 |
def fixture_ext_markdown(mkdocs_conf: MkDocsConfig) -> Markdown: |
| 58 |
"""Return a Markdown instance with MkdocstringsExtension.""" |
| 59 |
return Markdown(extensions=mkdocs_conf["markdown_extensions"], extension_configs=mkdocs_conf["mdx_configs"]) |