|
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.""" |
| 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 | + conf = config.Config(schema=get_schema()) |
| 22 | + while hasattr(request, "_parent_request") and hasattr(request._parent_request, "_parent_request"): # noqa: WPS437 |
| 23 | + request = request._parent_request # noqa: WPS437 |
| 24 | + |
| 25 | + conf_dict = { |
| 26 | + "site_name": "foo", |
| 27 | + "site_url": "https://example.org/", |
| 28 | + "site_dir": str(tmp_path), |
| 29 | + "plugins": [{"mkdocstrings": {"default_handler": "python"}}], |
| 30 | + **getattr(request, "param", {}), |
| 31 | + } |
| 32 | + # Re-create it manually as a workaround for https://github.com/mkdocs/mkdocs/issues/2289 |
| 33 | + mdx_configs = dict(ChainMap(*conf_dict.get("markdown_extensions", []))) |
| 34 | + |
| 35 | + conf.load_dict(conf_dict) |
| 36 | + assert conf.validate() == ([], []) |
| 37 | + |
| 38 | + conf["mdx_configs"] = mdx_configs |
| 39 | + conf["markdown_extensions"].insert(0, "toc") # Guaranteed to be added by MkDocs. |
| 40 | + |
| 41 | + conf = conf["plugins"]["mkdocstrings"].on_config(conf) |
| 42 | + conf = conf["plugins"]["autorefs"].on_config(conf) |
| 43 | + yield conf |
| 44 | + conf["plugins"]["mkdocstrings"].on_post_build(conf) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture(name="plugin") |
| 48 | +def fixture_plugin(mkdocs_conf): |
| 49 | + """Return a plugin instance.""" |
| 50 | + plugin = mkdocs_conf["plugins"]["mkdocstrings"] |
| 51 | + plugin.md = Markdown(extensions=mkdocs_conf["markdown_extensions"], extension_configs=mkdocs_conf["mdx_configs"]) |
| 52 | + return plugin |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture(name="ext_markdown") |
| 56 | +def fixture_ext_markdown(plugin): |
| 57 | + """Return a Markdown instance with MkdocstringsExtension.""" |
| 58 | + return plugin.md |
0 commit comments