File: conftest.py

package info (click to toggle)
python-auto-pytabs 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 444 kB
  • sloc: python: 999; xml: 860; sh: 24; makefile: 13; javascript: 1
file content (116 lines) | stat: -rw-r--r-- 3,401 bytes parent folder | download
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
"""Largely adopted from
https://github.com/executablebooks/sphinx-design/blob/6df47513e9e221c61877e9308da7a41d216ae3c3/tests/conftest.py.
"""

from __future__ import annotations

import shutil
from pathlib import Path
from typing import TYPE_CHECKING, Any

import pytest
from auto_pytabs.core import Cache
from sphinx.testing.path import path as sphinx_path

if TYPE_CHECKING:
    from unittest.mock import MagicMock

    from docutils import nodes
    from sphinx.testing.util import SphinxTestApp


pytest_plugins = "sphinx.testing.fixtures"


@pytest.fixture(autouse=True, scope="session")
def purge_cache():
    Cache().clear_all()
    yield
    Cache().clear_all()


@pytest.fixture()
def mock_cache_persist(mocker) -> MagicMock:
    return mocker.patch("auto_pytabs.core.Cache.persist")


class SphinxBuilder:
    def __init__(self, app: SphinxTestApp, src_path: Path):
        self.app = app
        self._src_path = src_path

    @property
    def src_path(self) -> Path:
        return self._src_path

    @property
    def out_path(self) -> Path:
        return Path(self.app.outdir)

    def build(self, assert_pass=True):
        self.app.build()
        if assert_pass:
            assert self.warnings == "", self.status
        return self

    @property
    def status(self):
        return self.app._status.getvalue()

    @property
    def warnings(self):
        return self.app._warning.getvalue()

    def get_doctree(
        self, docname: str, post_transforms: bool = False
    ) -> nodes.document:
        doctree: nodes.document = self.app.env.get_doctree(docname)
        if post_transforms:
            self.app.env.apply_post_transforms(doctree, docname)
        # make source path consistent for test comparisons
        for node in doctree.findall(include_self=True):
            if not ("source" in node and node["source"]):
                continue
            node["source"] = Path(node["source"]).relative_to(self.src_path).as_posix()
            if node["source"].endswith(".rst"):
                node["source"] = node["source"][:-4]
            elif node["source"].endswith(".md"):
                node["source"] = node["source"][:-3]
        return doctree


@pytest.fixture()
def sphinx_builder(tmp_path: Path, make_app, monkeypatch):
    def _create_project(
        source: str,
        compat: bool = False,
        **conf_kwargs: dict[str, Any],
    ):
        if compat:
            conf_kwargs["auto_pytabs_compat_mode"] = True
        src_path = tmp_path / "srcdir"
        src_path.mkdir()
        conf_kwargs = {
            "extensions": [
                "sphinx_design",
                "auto_pytabs.sphinx_ext",
            ],
            "auto_pytabs_no_cache": True,
            **(conf_kwargs or {}),
        }
        content = "\n".join(
            [f"{key} = {value!r}" for key, value in conf_kwargs.items()]
        )
        src_path.joinpath("conf.py").write_text(content, encoding="utf8")
        app = make_app(srcdir=sphinx_path(str(src_path.resolve())), buildername="html")
        shutil.copy(
            "test/sphinx_ext_test_data/example.py", src_path.joinpath("example.py")
        )
        shutil.copy(
            "test/sphinx_ext_test_data/example.js", src_path.joinpath("example.js")
        )

        src_path.joinpath("index.rst").write_text(source)
        return SphinxBuilder(app, src_path)

    yield _create_project