2
2
3
3
from __future__ import annotations
4
4
5
- from collections import ChainMap
6
- from typing import TYPE_CHECKING , Any
5
+ from collections . abc import Iterator
6
+ from typing import TYPE_CHECKING
7
7
8
8
import pytest
9
- from markdown . core import Markdown
10
- from mkdocs . config . defaults import MkDocsConfig
9
+
10
+ from tests import helpers
11
11
12
12
if TYPE_CHECKING :
13
13
from collections .abc import Iterator
14
14
from pathlib import Path
15
15
16
- from mkdocs import config
16
+ from markdown .core import Markdown
17
+ from mkdocs .config .defaults import MkDocsConfig
17
18
from mkdocstrings .plugin import MkdocstringsPlugin
18
19
19
20
from mkdocstrings_handlers .python .handler import PythonHandler
20
21
21
22
23
+ # --------------------------------------------
24
+ # Function-scoped fixtures.
25
+ # --------------------------------------------
22
26
@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 ]:
24
28
"""Yield a MkDocs configuration object.
25
29
26
30
Parameters:
@@ -30,34 +34,12 @@ def fixture_mkdocs_conf(request: pytest.FixtureRequest, tmp_path: Path) -> Itera
30
34
Yields:
31
35
MkDocs config.
32
36
"""
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
57
39
58
40
59
41
@pytest .fixture (name = "plugin" )
60
- def fixture_plugin (mkdocs_conf : config . Config ) -> MkdocstringsPlugin :
42
+ def fixture_plugin (mkdocs_conf : MkDocsConfig ) -> MkdocstringsPlugin :
61
43
"""Return a plugin instance.
62
44
63
45
Parameters:
@@ -66,11 +48,11 @@ def fixture_plugin(mkdocs_conf: config.Config) -> MkdocstringsPlugin:
66
48
Returns:
67
49
mkdocstrings plugin instance.
68
50
"""
69
- return mkdocs_conf [ "plugins" ][ "mkdocstrings" ]
51
+ return helpers . plugin ( mkdocs_conf )
70
52
71
53
72
54
@pytest .fixture (name = "ext_markdown" )
73
- def fixture_ext_markdown (mkdocs_conf : config . Config ) -> Markdown :
55
+ def fixture_ext_markdown (mkdocs_conf : MkDocsConfig ) -> Markdown :
74
56
"""Return a Markdown instance with MkdocstringsExtension.
75
57
76
58
Parameters:
@@ -79,7 +61,7 @@ def fixture_ext_markdown(mkdocs_conf: config.Config) -> Markdown:
79
61
Returns:
80
62
A Markdown instance.
81
63
"""
82
- return Markdown ( extensions = mkdocs_conf [ "markdown_extensions" ], extension_configs = mkdocs_conf [ "mdx_configs" ] )
64
+ return helpers . ext_markdown ( mkdocs_conf )
83
65
84
66
85
67
@pytest .fixture (name = "handler" )
@@ -92,6 +74,64 @@ def fixture_handler(plugin: MkdocstringsPlugin, ext_markdown: Markdown) -> Pytho
92
74
Returns:
93
75
A handler instance.
94
76
"""
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 )
0 commit comments