File: conftest.py

package info (click to toggle)
sphinx-autoapi 3.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 900 kB
  • sloc: python: 5,146; makefile: 7
file content (67 lines) | stat: -rw-r--r-- 1,695 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
import os
import pathlib
import shutil

from bs4 import BeautifulSoup
import pytest
from sphinx.application import Sphinx


@pytest.fixture(scope="session")
def rebuild():
    def _rebuild(confdir=".", **kwargs) -> int:
        app = Sphinx(
            srcdir=".",
            confdir=confdir,
            outdir="_build/html",
            doctreedir="_build/.doctrees",
            buildername="html",
            pdb=True,
            **kwargs,
        )
        app.build()
        return app.statuscode

    return _rebuild


@pytest.fixture(scope="class")
def builder(rebuild):
    cwd = os.getcwd()

    def build(test_dir, **kwargs) -> int:
        if kwargs.get("warningiserror"):
            # Add any warnings raised when using `Sphinx` more than once
            # in a Python session.
            confoverrides = kwargs.setdefault("confoverrides", {})
            confoverrides.setdefault("suppress_warnings", [])
            suppress = confoverrides["suppress_warnings"]
            suppress.append("app.add_node")
            suppress.append("app.add_directive")
            suppress.append("app.add_role")

        os.chdir(f"tests/python/{test_dir}")
        return rebuild(**kwargs)

    yield build

    try:
        shutil.rmtree("_build")
        if (pathlib.Path("autoapi") / "index.rst").exists():
            shutil.rmtree("autoapi")
    finally:
        os.chdir(cwd)


@pytest.fixture(scope="class")
def parse():
    cache = {}

    def parser(path):
        if path not in cache:
            with open(path, encoding="utf8") as file_handle:
                cache[path] = BeautifulSoup(file_handle, features="html.parser")

        return cache[path]

    yield parser