Skip to content

Commit f255a8c

Browse files
committed
tests: Fix tests
1 parent 155a833 commit f255a8c

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

tests/conftest.py

+47-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,56 @@
11
"""Configuration for the pytest test suite."""
22

3+
from __future__ import annotations
4+
5+
from collections import ChainMap
6+
from typing import TYPE_CHECKING, Any
7+
38
import pytest
4-
from markdown import Markdown
5-
from mkdocstrings_handlers.python.handler import PythonHandler
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+
from mkdocstrings_handlers.python.handler import PythonHandler
18+
19+
20+
@pytest.fixture(name="mkdocs_conf")
21+
def fixture_mkdocs_conf(request: pytest.FixtureRequest, tmp_path: Path) -> Iterator[config.Config]:
22+
"""Yield a MkDocs configuration object."""
23+
conf = MkDocsConfig()
24+
while hasattr(request, "_parent_request") and hasattr(request._parent_request, "_parent_request"):
25+
request = request._parent_request
26+
27+
conf_dict = {
28+
"site_name": "foo",
29+
"site_url": "https://example.org/",
30+
"site_dir": str(tmp_path),
31+
"plugins": [{"mkdocstrings": {"default_handler": "python"}}],
32+
**getattr(request, "param", {}),
33+
}
34+
# Re-create it manually as a workaround for https://github.com/mkdocs/mkdocs/issues/2289
35+
mdx_configs: dict[str, Any] = dict(ChainMap(*conf_dict.get("markdown_extensions", [])))
36+
37+
conf.load_dict(conf_dict)
38+
assert conf.validate() == ([], [])
39+
40+
conf["mdx_configs"] = mdx_configs
41+
conf["markdown_extensions"].insert(0, "toc") # Guaranteed to be added by MkDocs.
42+
43+
conf = conf["plugins"]["mkdocstrings"].on_config(conf)
44+
conf = conf["plugins"]["autorefs"].on_config(conf)
45+
yield conf
46+
conf["plugins"]["mkdocstrings"].on_post_build(conf)
647

748

849
@pytest.fixture(name="python_handler")
9-
def fixture_python_handler() -> PythonHandler:
50+
def fixture_python_handler(mkdocs_conf: MkDocsConfig) -> PythonHandler:
1051
"""Return a PythonHandler instance."""
11-
handler = PythonHandler("python", "material")
12-
handler.update_env(md=Markdown(extensions=["toc"]), config={})
52+
handlers = mkdocs_conf.plugins["mkdocstrings"].handlers # type: ignore[attr-defined]
53+
handler = handlers.get_handler("python")
54+
handler._update_env(md=Markdown(extensions=["toc"]))
1355
handler.env.filters["convert_markdown"] = lambda *args, **kwargs: str(args) + str(kwargs)
1456
return handler

tests/test_extension.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class Model(BaseModel):
121121
modules={"__init__.py": code},
122122
extensions=Extensions(PydanticExtension(schema=False)),
123123
) as package:
124-
python_handler.render(package["Model"], {}) # Assert no errors.
124+
python_handler.render(package["Model"], python_handler.get_options({})) # Assert no errors.
125125

126126

127127
def test_not_crashing_on_dynamic_field_description(caplog: pytest.LogCaptureFixture) -> None:

0 commit comments

Comments
 (0)