-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
98 lines (72 loc) · 2.93 KB
/
conftest.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Configuration for the pytest test suite."""
from __future__ import annotations
from collections import ChainMap
from typing import TYPE_CHECKING, Any
import pytest
from markdown.core import Markdown
from mkdocs import config
from mkdocs.config.defaults import get_schema
if TYPE_CHECKING:
from collections.abc import Iterator
from pathlib import Path
from mkdocstrings import MkdocstringsPlugin
from mkdocstrings_handlers.shell.handler import ShellHandler
@pytest.fixture(name="mkdocs_conf")
def fixture_mkdocs_conf(request: pytest.FixtureRequest, tmp_path: Path) -> Iterator[config.Config]:
"""Yield a MkDocs configuration object.
Parameters:
request: Pytest fixture.
tmp_path: Pytest fixture.
Yields:
MkDocs config.
"""
conf = config.Config(schema=get_schema()) # type: ignore[call-arg]
while hasattr(request, "_parent_request") and hasattr(request._parent_request, "_parent_request"):
request = request._parent_request
conf_dict = {
"config_file_path": "mkdocs.yml",
"site_name": "foo",
"site_url": "https://example.org/",
"site_dir": str(tmp_path),
"plugins": [{"mkdocstrings": {"default_handler": "shell"}}],
**getattr(request, "param", {}),
}
# Re-create it manually as a workaround for https://github.com/mkdocs/mkdocs/issues/2289
mdx_configs: dict[str, Any] = dict(ChainMap(*conf_dict.get("markdown_extensions", [])))
conf.load_dict(conf_dict)
assert conf.validate() == ([], [])
conf["mdx_configs"] = mdx_configs
conf["markdown_extensions"].insert(0, "toc") # Guaranteed to be added by MkDocs.
conf = conf["plugins"]["mkdocstrings"].on_config(conf)
conf = conf["plugins"]["autorefs"].on_config(conf)
yield conf
conf["plugins"]["mkdocstrings"].on_post_build(conf)
@pytest.fixture(name="plugin")
def fixture_plugin(mkdocs_conf: config.Config) -> MkdocstringsPlugin:
"""Return a plugin instance.
Parameters:
mkdocs_conf: Pytest fixture (see conftest.py).
Returns:
mkdocstrings plugin instance.
"""
return mkdocs_conf["plugins"]["mkdocstrings"]
@pytest.fixture(name="ext_markdown")
def fixture_ext_markdown(mkdocs_conf: config.Config) -> Markdown:
"""Return a Markdown instance with MkdocstringsExtension.
Parameters:
mkdocs_conf: Pytest fixture (see conftest.py).
Returns:
A Markdown instance.
"""
return Markdown(extensions=mkdocs_conf["markdown_extensions"], extension_configs=mkdocs_conf["mdx_configs"])
@pytest.fixture(name="handler")
def fixture_handler(plugin: MkdocstringsPlugin, ext_markdown: Markdown) -> ShellHandler:
"""Return a handler instance.
Parameters:
plugin: Pytest fixture (see conftest.py).
Returns:
A handler instance.
"""
handler = plugin.handlers.get_handler("shell")
handler._update_env(ext_markdown, config=plugin.handlers._tool_config)
return handler