File: test_render.py

package info (click to toggle)
python-sphinx-autodoc2 0.5.0-6
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,268 kB
  • sloc: python: 3,407; xml: 72; makefile: 9
file content (294 lines) | stat: -rw-r--r-- 8,304 bytes parent folder | download | duplicates (2)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""Tests for the rendering."""
import io
import os
from pathlib import Path
from textwrap import dedent

from autodoc2.analysis import analyse_module
from autodoc2.config import Config
from autodoc2.db import InMemoryDb
from autodoc2.render.base import RendererBase
from autodoc2.render.myst_ import MystRenderer
from autodoc2.render.rst_ import RstRenderer
from autodoc2.utils import yield_modules
import pytest
import sphinx
from sphinx.testing.util import SphinxTestApp

@pytest.mark.parametrize(
    "renderer,extension",
    [
        (RstRenderer, ".rst"),
        (MystRenderer, ".md"),
    ],
    ids=["rst", "myst"],
)
def test_basic(renderer: RendererBase, extension: str, tmp_path: Path, file_regression):
    """Test basic rendering."""
    package = build_package(tmp_path)
    db = InMemoryDb()
    for path, modname in yield_modules(package):
        for item in analyse_module(path, modname):
            db.add(item)
    content = "\n".join(renderer(db, Config()).render_item(package.name))
    file_regression.check(content, extension=extension)


@pytest.mark.parametrize(
    "renderer,extension",
    [
        (RstRenderer, ".rst"),
        (MystRenderer, ".md"),
    ],
    ids=["rst", "myst"],
)
def test_config_options(
    renderer: RendererBase, extension: str, tmp_path: Path, file_regression
):
    """Test basic rendering."""
    package = build_package(tmp_path)
    db = InMemoryDb()
    for path, modname in yield_modules(package):
        for item in analyse_module(path, modname):
            db.add(item)
    config = Config(no_index=True)
    content = "\n".join(renderer(db, config).render_item(package.name + ".func"))
    file_regression.check(content, extension=extension)


@pytest.mark.parametrize(
    "with_rebuild",
    [True, False],
    ids=["with_rebuild", "without_rebuild"],
)
def test_sphinx_build(tmp_path: Path, with_rebuild: bool):
    """Test building the Sphinx docs."""
    build_package(tmp_path)
    source = tmp_path / "source"
    source.mkdir()
    source.joinpath("conf.py").write_text(
        dedent(
            """\
        project = "tester"
        extensions = ["autodoc2"]
        autodoc2_packages = ["../package"]
        """
        ),
        "utf-8",
    )
    source.joinpath("index.rst").write_text(
        dedent(
            """\
        Test
        ====

        .. toctree::

            apidocs/index
        """
        ),
        "utf-8",
    )
    warnings = io.StringIO()
    build = tmp_path / "build"
    app = SphinxTestApp(
        buildername="html",
        srcdir=Path(os.path.abspath(source)),
        builddir=Path(os.path.abspath(build)),
        warning=warnings,
    )
    try:
        app.build()
    finally:
        app.cleanup()

    assertions = {}  # catch all the assertion failures, before failing the test
    if warnings.getvalue():
        assertions["warnings"] = warnings.getvalue()
    # print([p.relative_to(tmp_path).as_posix() for p in tmp_path.glob("**/*")])
    package_source = source / "apidocs" / "package" / "package.rst"
    if not (source / "apidocs" / "package" / "package.rst").exists():
        assertions["source/apidocs/package/package.rst"] = "not found"
    else:
        package_source_mtime = package_source.stat().st_mtime
    if not (build / "html" / "index.html").exists():
        assertions["build/index.html"] = "not found"
    if not (build / "html" / "apidocs" / "index.html").exists():
        assertions["build/apidocs/index.html"] = "not found"
    package_html = build / "html" / "apidocs" / "package" / "package.html"
    if not package_html.exists():
        assertions["build/apidocs/package/package.html"] = "not found"
    else:
        package_html_mtime = package_html.stat().st_mtime

    if not hasattr(app.env, "autodoc2_cache"):
        assertions["autodoc2_cache"] = "not found"
    if not getattr(app.env, "autodoc2_cache"):  # noqa: B009
        assertions["autodoc2_cache"] = "empty"

    if assertions:
        pytest.fail(f"Failed assertions {assertions}")

    if not with_rebuild:
        return

    # test rebuild
    rebuild_warnings = io.StringIO()
    rebuild_app = SphinxTestApp(
        buildername="html",
        srcdir=Path(os.path.abspath(source)),
        builddir=Path(os.path.abspath(build)),
        warning=rebuild_warnings,
    )
    try:
        rebuild_app.build()
    finally:
        rebuild_app.cleanup()

    if rebuild_warnings.getvalue():
        pytest.fail(f"Warnings on rebuild: {rebuild_warnings.getvalue()}")
    if package_source.stat().st_mtime != package_source_mtime:
        pytest.fail("Rebuild did not use cached source")
    if package_html.stat().st_mtime != package_html_mtime:
        pytest.fail("Rebuild did not use cached html")


def test_sphinx_build_directives(tmp_path: Path, file_regression):
    """Test building the Sphinx docs, using directives."""
    build_package(tmp_path)
    source = tmp_path / "source"
    source.mkdir()
    source.joinpath("conf.py").write_text(
        dedent(
            """\
        project = "tester"
        extensions = ["autodoc2"]
        autodoc2_packages = [
            {
                "path": "../package",
                "auto_mode": False,
            }
        ]
        """
        ),
        "utf-8",
    )
    source.joinpath("index.rst").write_text(
        dedent(
            """\
        Test
        ====

        .. autodoc2-docstring:: package.func
           :literal:
           :literal-linenos:
           :literal-lexer: restructuredtext

        .. autodoc2-docstring:: package.func

        .. autodoc2-object:: package.func
           :literal:
           :literal-lexer: restructuredtext

        .. autodoc2-object:: package.func
           :literal:

           render_plugin = "myst"

        .. autodoc2-object:: package.func

        .. autodoc2-summary::

            package.func
            package.a1
        """
        ),
        "utf-8",
    )
    warnings = io.StringIO()
    build = tmp_path / "build"
    app = SphinxTestApp(
        buildername="html",
        srcdir=Path(os.path.abspath(source)),
        builddir=Path(os.path.abspath(build)),
        warning=warnings,
    )
    try:
        app.build()
    finally:
        app.cleanup()

    assert not warnings.getvalue()

    doctree = app.env.get_doctree("index")
    doctree["source"] = "index.rst"
    content = "\n".join([line.rstrip() for line in doctree.pformat().splitlines()])
    if sphinx.version_info < (8, 2):
        content = content.replace(
            '<desc_parameterlist multi_line_parameter_list="False"',
            '<desc_parameterlist multi_line_parameter_list="False" multi_line_trailing_comma="True"',
        )
    file_regression.check(content, extension=".xml")


def build_package(tmp_path: Path) -> Path:
    """Build a simple package for testing."""
    package = tmp_path / "package"
    package.mkdir()
    package.joinpath("__init__.py").write_text(
        dedent(
            """\
        '''This is a test package.'''
        from package.a import a1
        from package.a.c import ac1 as alias
        __all__ = ['p', 'a1', 'alias']
        p = 1
        '''p can be documented here.'''

        def func(a: str, b: int) -> alias:
            '''This is a function.'''

        class Class:
            '''This is a class.'''

            x: int = 1
            '''x can be documented here.'''

            def method(self, a: str, b: int) -> ...:
                '''This is a method.'''

            @property
            def prop(self) -> alias | None:
                '''This is a property.'''

            class Nested:
                '''This is a nested class.'''
        """
        ),
        "utf-8",
    )
    package.joinpath("a").mkdir()
    package.joinpath("a", "__init__.py").write_text(
        dedent(
            """\
        '''This is a test module.'''
        from .c import *
        from .d import *
        __all__ = ['a1', 'ac1', 'ad1', 'ade1', 'adf1']
        a1 = 1
        '''a1 can be documented here.'''
        """
        ),
        "utf-8",
    )
    package.joinpath("a", "c.py").write_text(
        dedent(
            """\
        '''This is also a test module.'''
        __all__ = ['ac1']
        ac1 = 1
        """
        ),
        "utf-8",
    )
    return package