22
33from __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
88import pytest
9- from markdown . core import Markdown
10- from mkdocs . config . defaults import MkDocsConfig
9+
10+ from tests import helpers
1111
1212if 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 )
0 commit comments