10000 tests: Add tests (copied from mkdocstrings) · mkdocstrings/python@36465a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 36465a7

Browse files
committed
tests: Add tests (copied from mkdocstrings)
1 parent be922b8 commit 36465a7

File tree

4 files changed

+150
-23
lines changed

4 files changed

+150
-23
lines changed

tests/conftest.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,86 @@
11
"""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+
83+
Returns:
84+
A Markdown instance.
85+
"""
86+
return plugin.md

tests/test_cli.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/test_collector.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Tests for the handlers.python module."""
2+
3+
import pytest
4+
5+
from mkdocstrings.handlers.python.collector import CollectionError, PythonCollector
6+
7+
8+
def test_collect_missing_module():
9+
"""Assert error is raised for missing modules."""
10+
collector = PythonCollector()
11+
with pytest.raises(CollectionError):
12+
collector.collect("aaaaaaaa", {})
13+
14+
15+
def test_collect_missing_module_item():
16+
"""Assert error is raised for missing items within existing modules."""
17+
collector = PythonCollector()
18+
with pytest.raises(CollectionError):
19+
collector.collect("mkdocstrings.aaaaaaaa", {})
20+
21+
22+
def test_collect_module():
23+
"""Assert existing module can be collected."""
24+
collector = PythonCollector()
25+
assert collector.collect("mkdocstrings", {})

tests/test_themes.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Tests for the different themes we claim to support."""
2+
3+
import sys
4+
5+
import pytest
6+
7+
8+
@pytest.mark.parametrize(
9+
"plugin",
10+
[
11+
{"theme": "mkdocs"},
12+
{"theme": "readthedocs"},
13+
{"theme": {"name": "material"}},
14+
],
15+
indirect=["plugin"],
16+
)
17+
@pytest.mark.parametrize(
18+
"module",
19+
[
20+
"mkdocstrings.extension",
21+
"mkdocstrings.inventory",
22+
"mkdocstrings.loggers",
23+
"mkdocstrings.plugin",
24+
"mkdocstrings.handlers.base",
25+
"mkdocstrings.handlers.python",
26+
"mkdocstrings.handlers.rendering",
27+
],
28+
)
29+
@pytest.mark.skipif(sys.version_info < (3, 7), reason="material is not installed on Python 3.6")
30+
def test_render_themes_templates_python(module, plugin):
31+
"""Test rendering of a given theme's templates.
32+
33+
Parameters:
34+
module: Parametrized argument.
35+
plugin: Pytest fixture: [tests.conftest.fixture_plugin][].
36+
"""
37+
handler = plugin.handlers.get_handler("python")
38+
handler.renderer._update_env(plugin.md, plugin.handlers._config) # noqa: WPS437
39+
data = handler.collector.collect(module, {})
40+
handler.renderer.render(data, {})

0 commit comments

Comments
 (0)
0