Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e493663

Browse filesBrowse files
committed
tests: Refactor fixtures and helpers
1 parent 0d953e3 commit e493663
Copy full SHA for e493663

File tree

3 files changed

+174
-44
lines changed
Filter options

3 files changed

+174
-44
lines changed

‎tests/conftest.py

Copy file name to clipboardExpand all lines: tests/conftest.py
+77-37Lines changed: 77 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22

33
from __future__ import annotations
44

5-
from collections import ChainMap
6-
from typing import TYPE_CHECKING, Any
5+
from collections.abc import Iterator
6+
from typing import TYPE_CHECKING
77

88
import pytest
9-
from markdown.core import Markdown
10-
from mkdocs.config.defaults import MkDocsConfig
9+
10+
from tests import helpers
1111

1212
if TYPE_CHECKING:
1313
from collections.abc import Iterator
1414
from pathlib import Path
1515

16-
from mkdocs import config
16+
from markdown.core import Markdown
17+
from mkdocs.config.defaults import MkDocsConfig
1718
from mkdocstrings.plugin import MkdocstringsPlugin
1819

1920
from mkdocstrings_handlers.python.handler import PythonHandler
2021

2122

23+
# --------------------------------------------
24+
# Function-scoped fixtures.
25+
# --------------------------------------------
2226
@pytest.fixture(name="mkdocs_conf")
23-
def fixture_mkdocs_conf(request: pytest.FixtureRequest, tmp_path: Path) -> Iterator[config.Config]:
27+
def fixture_mkdocs_conf(request: pytest.FixtureRequest, tmp_path: Path) -> Iterator[MkDocsConfig]:
2428
"""Yield a MkDocs configuration object.
2529
2630
Parameters:
@@ -30,34 +34,12 @@ def fixture_mkdocs_conf(request: pytest.FixtureRequest, tmp_path: Path) -> Itera
3034
Yields:
3135
MkDocs config.
3236
"""
33-
conf = MkDocsConfig()
34-
while hasattr(request, "_parent_request") and hasattr(request._parent_request, "_parent_request"):
35-
request = request._parent_request
36-
37-
conf_dict = {
38-
"site_name": "foo",
39-
"site_url": "https://example.org/",
40-
"site_dir": str(tmp_path),
41-
"plugins": [{"mkdocstrings": {"default_handler": "python"}}],
42-
**getattr(request, "param", {}),
43-
}
44-
# Re-create it manually as a workaround for https://github.com/mkdocs/mkdocs/issues/2289
45-
mdx_configs: dict[str, Any] = dict(ChainMap(*conf_dict.get("markdown_extensions", [])))
46-
47-
conf.load_dict(conf_dict)
48-
assert conf.validate() == ([], [])
49-
50-
conf["mdx_configs"] = mdx_configs
51-
conf["markdown_extensions"].insert(0, "toc") # Guaranteed to be added by MkDocs.
52-
53-
conf = conf["plugins"]["mkdocstrings"].on_config(conf)
54-
conf = conf["plugins"]["autorefs"].on_config(conf)
55-
yield conf
56-
conf["plugins"]["mkdocstrings"].on_post_build(conf)
37+
with helpers.mkdocs_conf(request, tmp_path) as mkdocs_conf:
38+
yield mkdocs_conf
5739

5840

5941
@pytest.fixture(name="plugin")
60-
def fixture_plugin(mkdocs_conf: config.Config) -> MkdocstringsPlugin:
42+
def fixture_plugin(mkdocs_conf: MkDocsConfig) -> MkdocstringsPlugin:
6143
"""Return a plugin instance.
6244
6345
Parameters:
@@ -66,11 +48,11 @@ def fixture_plugin(mkdocs_conf: config.Config) -> MkdocstringsPlugin:
6648
Returns:
6749
mkdocstrings plugin instance.
6850
"""
69-
return mkdocs_conf["plugins"]["mkdocstrings"]
51+
return helpers.plugin(mkdocs_conf)
7052

7153

7254
@pytest.fixture(name="ext_markdown")
73-
def fixture_ext_markdown(mkdocs_conf: config.Config) -> Markdown:
55+
def fixture_ext_markdown(mkdocs_conf: MkDocsConfig) -> Markdown:
7456
"""Return a Markdown instance with MkdocstringsExtension.
7557
7658
Parameters:
@@ -79,7 +61,7 @@ def fixture_ext_markdown(mkdocs_conf: config.Config) -> Markdown:
7961
Returns:
8062
A Markdown instance.
8163
"""
82-
return Markdown(extensions=mkdocs_conf["markdown_extensions"], extension_configs=mkdocs_conf["mdx_configs"])
64+
return helpers.ext_markdown(mkdocs_conf)
8365

8466

8567
@pytest.fixture(name="handler")
@@ -92,6 +74,64 @@ def fixture_handler(plugin: MkdocstringsPlugin, ext_markdown: Markdown) -> Pytho
9274
Returns:
9375
A handler instance.
9476
"""
95-
handler = plugin.handlers.get_handler("python")
96-
handler._update_env(ext_markdown, plugin.handlers._config)
97-
return handler # type: ignore[return-value]
77+
return helpers.handler(plugin, ext_markdown)
78+
79+
80+
# --------------------------------------------
81+
# Session-scoped fixtures.
82+
# --------------------------------------------
83+
@pytest.fixture(name="session_mkdocs_conf", scope="session")
84+
def fixture_session_mkdocs_conf(
85+
request: pytest.FixtureRequest,
86+
tmp_path_factory: pytest.TempPathFactory,
87+
) -> Iterator[MkDocsConfig]:
88+
"""Yield a MkDocs configuration object.
89+
90+
Parameters:
91+
request: Pytest fixture.
92+
tmp_path: Pytest fixture.
93+
94+
Yields:
95+
MkDocs config.
96+
"""
97+
with helpers.mkdocs_conf(request, tmp_path_factory.mktemp("project")) as mkdocs_conf:
98+
yield mkdocs_conf
99+
100+
101+
@pytest.fixture(name="session_plugin", scope="session")
102+
def fixture_session_plugin(session_mkdocs_conf: MkDocsConfig) -> MkdocstringsPlugin:
103+
"""Return a plugin instance.
104+
105+
Parameters:
106+
mkdocs_conf: Pytest fixture (see conftest.py).
107+
108+
Returns:
109+
mkdocstrings plugin instance.
110+
"""
111+
return helpers.plugin(session_mkdocs_conf)
112+
113+
114+
@pytest.fixture(name="session_ext_markdown", scope="session")
115+
def fixture_session_ext_markdown(session_mkdocs_conf: MkDocsConfig) -> Markdown:
116+
"""Return a Markdown instance with MkdocstringsExtension.
117+
118+
Parameters:
119+
mkdocs_conf: Pytest fixture (see conftest.py).
120+
121+
Returns:
122+
A Markdown instance.
123+
"""
124+
return helpers.ext_markdown(session_mkdocs_conf)
125+
126+
127+
@pytest.fixture(name="session_handler", scope="session")
128+
def fixture_session_handler(session_plugin: MkdocstringsPlugin, session_ext_markdown: Markdown) -> PythonHandler:
129+
"""Return a handler instance.
130+
131+
Parameters:
132+
plugin: Pytest fixture (see conftest.py).
133+
134+
Returns:
135+
A handler instance.
136+
"""
137+
return helpers.handler(session_plugin, session_ext_markdown)

‎tests/helpers.py

Copy file name to clipboard
+94Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""Configuration for the pytest test suite."""
2+
3+
from __future__ import annotations
4+
5+
from collections import ChainMap
6+
from contextlib import contextmanager
7+
from typing import TYPE_CHECKING, Any
8+
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+
import pytest
17+
from mkdocstrings.plugin import MkdocstringsPlugin
18+
19+
from mkdocstrings_handlers.python.handler import PythonHandler
20+
21+
22+
@contextmanager
23+
def mkdocs_conf(request: pytest.FixtureRequest, tmp_path: Path) -> Iterator[MkDocsConfig]:
24+
"""Yield a MkDocs configuration object.
25+
26+
Parameters:
27+
request: Pytest request fixture.
28+
tmp_path: Temporary path.
29+
30+
Yields:
31+
MkDocs config.
32+
"""
33+
conf = MkDocsConfig()
34+
while hasattr(request, "_parent_request") and hasattr(request._parent_request, "_parent_request"):
35+
request = request._parent_request
36+
37+
conf_dict = {
38+
"site_name": "foo",
39+
"site_url": "https://example.org/",
40+
"site_dir": str(tmp_path),
41+
"plugins": [{"mkdocstrings": {"default_handler": "python"}}],
42+
**getattr(request, "param", {}),
43+
}
44+
# Re-create it manually as a workaround for https://github.com/mkdocs/mkdocs/issues/2289
45+
mdx_configs: dict[str, Any] = dict(ChainMap(*conf_dict.get("markdown_extensions", [])))
46+
47+
conf.load_dict(conf_dict)
48+
assert conf.validate() == ([], [])
49+
50+
conf["mdx_configs"] = mdx_configs
51+
conf["markdown_extensions"].insert(0, "toc") # Guaranteed to be added by MkDocs.
52+
53+
conf = conf["plugins"]["mkdocstrings"].on_config(conf)
54+
conf = conf["plugins"]["autorefs"].on_config(conf)
55+
yield conf
56+
conf["plugins"]["mkdocstrings"].on_post_build(conf)
57+
58+
59+
def plugin(mkdocs_conf: MkDocsConfig) -> MkdocstringsPlugin:
60+
"""Return a plugin instance.
61+
62+
Parameters:
63+
mkdocs_conf: MkDocs configuration.
64+
65+
Returns:
66+
mkdocstrings plugin instance.
67+
"""
68+
return mkdocs_conf["plugins"]["mkdocstrings"]
69+
70+
71+
def ext_markdown(mkdocs_conf: MkDocsConfig) -> Markdown:
72+
"""Return a Markdown instance with MkdocstringsExtension.
73+
74+
Parameters:
75+
mkdocs_conf: MkDocs configuration.
76+
77+
Returns:
78+
A Markdown instance.
79+
"""
80+
return Markdown(extensions=mkdocs_conf["markdown_extensions"], extension_configs=mkdocs_conf["mdx_configs"])
81+
82+
83+
def handler(plugin: MkdocstringsPlugin, ext_markdown: Markdown) -> PythonHandler:
84+
"""Return a handler instance.
85+
86+
Parameters:
87+
plugin: Plugin instance.
88+
89+
Returns:
90+
A handler instance.
91+
"""
92+
handler = plugin.handlers.get_handler("python")
93+
handler._update_env(ext_markdown, plugin.handlers._config)
94+
return handler # type: ignore[return-value]

‎tests/test_themes.py

Copy file name to clipboardExpand all lines: tests/test_themes.py
+3-7Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import pytest
88

99
if TYPE_CHECKING:
10-
from markdown import Markdown
11-
from mkdocstrings.plugin import MkdocstringsPlugin
10+
from mkdocstrings.handlers.python import PythonHandler
1211

1312

1413
@pytest.mark.parametrize(
@@ -32,15 +31,12 @@
3231
"mkdocstrings_handlers.python",
3332
],
3433
)
35-
def test_render_themes_templates_python(identifier: str, plugin: MkdocstringsPlugin, ext_markdown: Markdown) -> None:
34+
def test_render_themes_templates_python(identifier: str, handler: PythonHandler) -> None:
3635
"""Test rendering of a given theme's templates.
3736
3837
Parameters:
3938
identifier: Parametrized identifier.
40-
plugin: Pytest fixture (see conftest.py).
41-
ext_markdown: Pytest fixture (see conftest.py).
39+
handler: Python handler (fixture).
4240
"""
43-
handler = plugin.handlers.get_handler("python")
44-
handler._update_env(ext_markdown, plugin.handlers._config)
4541
data = handler.collect(identifier, {})
4642
handler.render(data, {})

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.