|
1 | 1 | """Configuration for the pytest test suite.""" |
| 2 | + |
| 3 | +from collections import ChainMap |
| 4 | + |
| 5 | +import pytest |
| 6 | +from markdown.core import Markdown |
| 7 | +from mkdocs import config |
| 8 | + |
| 9 | +try: |
| 10 | + from mkdocs.config.defaults import get_schema |
| 11 | +except ImportError: |
| 12 | + |
| 13 | + def get_schema(): # noqa: WPS440 |
| 14 | + """Fallback for old versions of MkDocs.""" # noqa: DAR201 |
| 15 | + return config.DEFAULT_SCHEMA |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture(name="mkdocs_conf") |
| 19 | +def fixture_mkdocs_conf(request, tmp_path): |
| 20 | + """Yield a MkDocs configuration object. |
| 21 | +
|
| 22 | + Parameters: |
| 23 | + request: Pytest request fixture. |
| 24 | + tmp_path: Pytest temporary path fixture. |
| 25 | +
|
| 26 | + Yields: |
| 27 | + MkDocs configuration object. |
| 28 | + """ |
| 29 | + conf = config.Config(schema=get_schema()) |
| 30 | + while hasattr(request, "_parent_request") and hasattr(request._parent_request, "_parent_request"): # noqa: WPS437 |
| 31 | + request = request._parent_request # noqa: WPS437 |
| 32 | + |
| 33 | + conf_dict = { |
| 34 | + "site_name": "foo", |
| 35 | + "site_url": "https://example.org/", |
| 36 | + "site_dir": str(tmp_path), |
| 37 | + "plugins": [{"mkdocstrings": {"default_handler": "python"}}], |
| 38 | + **getattr(request, "param", {}), |
| 39 | + } |
| 40 | + # Re-create it manually as a workaround for https://github.com/mkdocs/mkdocs/issues/2289 |
| 41 | + mdx_configs = dict(ChainMap(*conf_dict.get("markdown_extensions", []))) |
| 42 | + |
| 43 | + conf.load_dict(conf_dict) |
| 44 | + assert conf.validate() == ([], []) |
| 45 | + |
| 46 | + conf["mdx_configs"] = mdx_configs |
| 47 | + conf["markdown_extensions"].insert(0, "toc") # Guaranteed to be added by MkDocs. |
| 48 | + |
| 49 | + conf = conf["plugins"]["mkdocstrings"].on_config(conf) |
| 50 | + conf = conf["plugins"]["autorefs"].on_config(conf) |
| 51 | + yield conf |
| 52 | + conf["plugins"]["mkdocstrings"].on_post_build(conf) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture(name="plugin") |
| 56 | +def fixture_plugin(mkdocs_conf): |
| 57 | + """Return a plugin instance. |
| 58 | +
|
| 59 | + Parameters: |
| 60 | + mkdocs_conf: MkDocs configuration object (fixture). |
| 61 | +
|
| 62 | + Returns: |
| 63 | + Configurated plugin instance. |
| 64 | + """ |
| 65 | + plugin = mkdocs_conf["plugins"]["mkdocstrings"] |
| 66 | + plugin.md = Markdown(extensions=mkdocs_conf["markdown_extensions"], extension_configs=mkdocs_conf["mdx_configs"]) |
| 67 | + return plugin |
| 68 | + |
| 69 | + |
| 70 | +@pytest.fixture(name="ext_markdown") |
| 71 | +def fixture_ext_markdown(plugin): |
| 72 | + """Return a Markdown instance with MkdocstringsExtension. |
| 73 | +
|
| 74 | + Parameters: |
| 75 | + plugin: A configurated plugin instance. (fixture). |
| 76 | +
|
| 77 | + Returns: |
| 78 | + The plugin Markdown instance. |
| 79 | + """ |
| 80 | + return plugin.md |
0 commit comments