File: sphinx_fixtures.py

package info (click to toggle)
rocm-docs-core 1.31.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,728 kB
  • sloc: python: 2,002; sh: 199; javascript: 152; cpp: 29; makefile: 27
file content (72 lines) | stat: -rw-r--r-- 1,988 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
"""Defines helpers for testing the rocm_docs sphinx extension"""

from __future__ import annotations

import functools
import shutil
from collections.abc import Callable, Iterator
from pathlib import Path

import pytest
from sphinx.application import Sphinx

from .log_fixtures import ExpectLogFixture


@pytest.fixture
def with_no_git_repo(
    monkeypatch: pytest.MonkeyPatch,
    expect_log: ExpectLogFixture,
) -> Iterator[ExpectLogFixture.Validator]:
    """Setup environment to allow testing outside a git repository"""
    monkeypatch.setenv("ROCM_DOCS_REMOTE_DETAILS", ",")

    with expect_log(
        "git.exc.InvalidGitRepositoryError",
        "ERROR",
        "test_external_projects",
    ) as validator:
        yield validator


SITES_BASEFOLDER = Path(__file__).parent / "sites"


def build_sphinx(
    srcdir: Path, outdir: Path, confdir: Path | None = None
) -> None:
    confdir = confdir or srcdir
    doctreedir = outdir / ".doctrees"
    buildername = "html"
    app = Sphinx(srcdir, confdir, outdir, doctreedir, buildername)
    app.build()


@pytest.fixture
def build_factory(
    request: pytest.FixtureRequest,
    tmp_path: Path,
) -> Callable[..., tuple[Path, Path]]:
    """A factory to prepare Sphinx source and output directories"""

    def make(src_folder: Path, /) -> tuple[Path, Path]:
        srcdir = tmp_path.joinpath(src_folder)
        outdir = tmp_path.joinpath(f"{src_folder}_build")
        srcdir.parent.mkdir(parents=True, exist_ok=True)

        mark = request.node.get_closest_marker("template_folder")
        if mark is not None:
            for i, dir in enumerate(mark.args):
                shutil.copytree(dir, srcdir, dirs_exist_ok=(i > 0))

        shutil.copytree(
            SITES_BASEFOLDER / src_folder, srcdir, dirs_exist_ok=True
        )
        return srcdir, outdir

    if hasattr(request, "param"):
        return functools.partial(make, request.param)
    return make


__all__ = ["build_factory", "with_no_git_repo"]