Coverage for tests/conftest.py: 100.00%

27 statements  

« prev     ^ index     » next       coverage.py v7.6.2, created at 2024-10-12 18:59 +0200

1"""Configuration for the pytest test suite.""" 

2 

3from __future__ import annotations 

4 

5from collections import ChainMap 

6from typing import TYPE_CHECKING, Any 

7 

8import pytest 

9from markdown.core import Markdown 

10from mkdocs.config.defaults import MkDocsConfig 

11 

12if TYPE_CHECKING: 

13 from collections.abc import Iterator 

14 from pathlib import Path 

15 

16 from mkdocs import config 

17 

18 from mkdocstrings.plugin import MkdocstringsPlugin 

19 

20 

21@pytest.fixture(name="mkdocs_conf") 

22def fixture_mkdocs_conf(request: pytest.FixtureRequest, tmp_path: Path) -> Iterator[config.Config]: 

23 """Yield a MkDocs configuration object.""" 

24 conf = MkDocsConfig() 

25 while hasattr(request, "_parent_request") and hasattr(request._parent_request, "_parent_request"): 

26 request = request._parent_request 

27 

28 conf_dict = { 

29 "site_name": "foo", 

30 "site_url": "https://example.org/", 

31 "site_dir": str(tmp_path), 

32 "plugins": [{"mkdocstrings": {"default_handler": "python"}}], 

33 **getattr(request, "param", {}), 

34 } 

35 # Re-create it manually as a workaround for https://github.com/mkdocs/mkdocs/issues/2289 

36 mdx_configs: dict[str, Any] = dict(ChainMap(*conf_dict.get("markdown_extensions", []))) 

37 

38 conf.load_dict(conf_dict) 

39 assert conf.validate() == ([], []) 

40 

41 conf["mdx_configs"] = mdx_configs 

42 conf["markdown_extensions"].insert(0, "toc") # Guaranteed to be added by MkDocs. 

43 

44 conf = conf["plugins"]["mkdocstrings"].on_config(conf) 

45 conf = conf["plugins"]["autorefs"].on_config(conf) 

46 yield conf 

47 conf["plugins"]["mkdocstrings"].on_post_build(conf) 

48 

49 

50@pytest.fixture(name="plugin") 

51def fixture_plugin(mkdocs_conf: config.Config) -> MkdocstringsPlugin: 

52 """Return a plugin instance.""" 

53 return mkdocs_conf["plugins"]["mkdocstrings"] 

54 

55 

56@pytest.fixture(name="ext_markdown") 

57def fixture_ext_markdown(mkdocs_conf: MkDocsConfig) -> Markdown: 

58 """Return a Markdown instance with MkdocstringsExtension.""" 

59 return Markdown(extensions=mkdocs_conf["markdown_extensions"], extension_configs=mkdocs_conf["mdx_configs"])