Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed May 16, 2024
1 parent 6c97a1a commit df666ed
Show file tree
Hide file tree
Showing 2 changed files with 2,955 additions and 159 deletions.
112 changes: 75 additions & 37 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

from __future__ import annotations

from collections import ChainMap
from typing import TYPE_CHECKING, Any, Iterator
from typing import TYPE_CHECKING, Iterator

import pytest
from markdown.core import Markdown
from mkdocs.config.defaults import MkDocsConfig

from tests import helpers

if TYPE_CHECKING:
from pathlib import Path

from mkdocs import config
from markdown.core import Markdown
from mkdocs.config.defaults import MkDocsConfig
from mkdocstrings.plugin import MkdocstringsPlugin

from mkdocstrings_handlers.python.handler import PythonHandler


# --------------------------------------------
# Function-scoped fixtures.
# --------------------------------------------
@pytest.fixture(name="mkdocs_conf")
def fixture_mkdocs_conf(request: pytest.FixtureRequest, tmp_path: Path) -> Iterator[config.Config]:
def fixture_mkdocs_conf(request: pytest.FixtureRequest, tmp_path: Path) -> Iterator[MkDocsConfig]:
"""Yield a MkDocs configuration object.
Parameters:
Expand All @@ -29,34 +32,12 @@ def fixture_mkdocs_conf(request: pytest.FixtureRequest, tmp_path: Path) -> Itera
Yields:
MkDocs config.
"""
conf = MkDocsConfig()
while hasattr(request, "_parent_request") and hasattr(request._parent_request, "_parent_request"):
request = request._parent_request

conf_dict = {
"site_name": "foo",
"site_url": "https://example.org/",
"site_dir": str(tmp_path),
"plugins": [{"mkdocstrings": {"default_handler": "python"}}],
**getattr(request, "param", {}),
}
# Re-create it manually as a workaround for https://github.com/mkdocs/mkdocs/issues/2289
mdx_configs: dict[str, Any] = dict(ChainMap(*conf_dict.get("markdown_extensions", [])))

conf.load_dict(conf_dict)
assert conf.validate() == ([], [])

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

conf = conf["plugins"]["mkdocstrings"].on_config(conf)
conf = conf["plugins"]["autorefs"].on_config(conf)
yield conf
conf["plugins"]["mkdocstrings"].on_post_build(conf)
with helpers.mkdocs_conf(request, tmp_path) as mkdocs_conf:
yield mkdocs_conf


@pytest.fixture(name="plugin")
def fixture_plugin(mkdocs_conf: config.Config) -> MkdocstringsPlugin:
def fixture_plugin(mkdocs_conf: MkDocsConfig) -> MkdocstringsPlugin:
"""Return a plugin instance.
Parameters:
Expand All @@ -65,11 +46,11 @@ def fixture_plugin(mkdocs_conf: config.Config) -> MkdocstringsPlugin:
Returns:
mkdocstrings plugin instance.
"""
return mkdocs_conf["plugins"]["mkdocstrings"]
return helpers.plugin(mkdocs_conf)


@pytest.fixture(name="ext_markdown")
def fixture_ext_markdown(mkdocs_conf: config.Config) -> Markdown:
def fixture_ext_markdown(mkdocs_conf: MkDocsConfig) -> Markdown:
"""Return a Markdown instance with MkdocstringsExtension.
Parameters:
Expand All @@ -78,7 +59,7 @@ def fixture_ext_markdown(mkdocs_conf: config.Config) -> Markdown:
Returns:
A Markdown instance.
"""
return Markdown(extensions=mkdocs_conf["markdown_extensions"], extension_configs=mkdocs_conf["mdx_configs"])
return helpers.ext_markdown(mkdocs_conf)


@pytest.fixture(name="handler")
Expand All @@ -91,6 +72,63 @@ def fixture_handler(plugin: MkdocstringsPlugin, ext_markdown: Markdown) -> Pytho
Returns:
A handler instance.
"""
handler = plugin.handlers.get_handler("python")
handler._update_env(ext_markdown, plugin.handlers._config)
return handler # type: ignore[return-value]
return helpers.handler(plugin, ext_markdown)


# --------------------------------------------
# Session-scoped fixtures.
# --------------------------------------------
@pytest.fixture(name="session_mkdocs_conf", scope="session")
def fixture_session_mkdocs_conf(
request: pytest.FixtureRequest, tmp_path_factory: pytest.TempPathFactory
) -> Iterator[MkDocsConfig]:
"""Yield a MkDocs configuration object.
Parameters:
request: Pytest fixture.
tmp_path: Pytest fixture.
Yields:
MkDocs config.
"""
with helpers.mkdocs_conf(request, tmp_path_factory.mktemp("project")) as mkdocs_conf:
yield mkdocs_conf


@pytest.fixture(name="session_plugin", scope="session")
def fixture_session_plugin(session_mkdocs_conf: MkDocsConfig) -> MkdocstringsPlugin:
"""Return a plugin instance.
Parameters:
mkdocs_conf: Pytest fixture (see conftest.py).
Returns:
mkdocstrings plugin instance.
"""
return helpers.plugin(session_mkdocs_conf)


@pytest.fixture(name="session_ext_markdown", scope="session")
def fixture_session_ext_markdown(session_mkdocs_conf: MkDocsConfig) -> Markdown:
"""Return a Markdown instance with MkdocstringsExtension.
Parameters:
mkdocs_conf: Pytest fixture (see conftest.py).
Returns:
A Markdown instance.
"""
return helpers.ext_markdown(session_mkdocs_conf)


@pytest.fixture(name="session_handler", scope="session")
def fixture_session_handler(session_plugin: MkdocstringsPlugin, session_ext_markdown: Markdown) -> PythonHandler:
"""Return a handler instance.
Parameters:
plugin: Pytest fixture (see conftest.py).
Returns:
A handler instance.
"""
return helpers.handler(session_plugin, session_ext_markdown)
Loading

0 comments on commit df666ed

Please sign in to comment.