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