1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
"""Configuration for the pytest test suite."""
from __future__ import annotations
from collections.abc import Iterator
from typing import TYPE_CHECKING
import pytest
from tests import helpers
if TYPE_CHECKING:
from collections.abc import Iterator
from pathlib import Path
from markdown.core import Markdown
from mkdocs.config.defaults import MkDocsConfig
from mkdocstrings import MkdocstringsPlugin
from mkdocstrings_handlers.python import PythonHandler
# --------------------------------------------
# Function-scoped fixtures.
# --------------------------------------------
@pytest.fixture(name="mkdocs_conf")
def fixture_mkdocs_conf(request: pytest.FixtureRequest, tmp_path: Path) -> 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) as mkdocs_conf:
yield mkdocs_conf
@pytest.fixture(name="plugin")
def fixture_plugin(mkdocs_conf: MkDocsConfig) -> MkdocstringsPlugin:
"""Return a plugin instance.
Parameters:
mkdocs_conf: Pytest fixture (see conftest.py).
Returns:
mkdocstrings plugin instance.
"""
return helpers.plugin(mkdocs_conf)
@pytest.fixture(name="ext_markdown")
def fixture_ext_markdown(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(mkdocs_conf)
@pytest.fixture(name="handler")
def fixture_handler(plugin: MkdocstringsPlugin, ext_markdown: Markdown) -> PythonHandler:
"""Return a handler instance.
Parameters:
plugin: Pytest fixture (see conftest.py).
Returns:
A handler instance.
"""
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)
|