File: test_needs_builder.py

package info (click to toggle)
sphinx-needs 5.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,924 kB
  • sloc: python: 21,132; javascript: 187; makefile: 89; sh: 29; xml: 10
file content (117 lines) | stat: -rw-r--r-- 3,300 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
117
import json
import os
import subprocess
from pathlib import Path

import pytest
from syrupy.filters import props


@pytest.mark.parametrize(
    "test_app",
    [{"buildername": "needs", "srcdir": "doc_test/doc_needs_builder"}],
    indirect=True,
)
def test_doc_needs_builder(test_app, snapshot):
    app = test_app
    app.build()

    needs_list = json.loads(Path(app.outdir, "needs.json").read_text())
    assert needs_list == snapshot(exclude=props("created", "project", "creator"))


@pytest.mark.parametrize(
    "test_app",
    [
        {
            "buildername": "needs",
            "srcdir": "doc_test/doc_needs_builder",
            "confoverrides": {"needs_reproducible_json": True},
        }
    ],
    indirect=True,
)
def test_doc_needs_builder_reproducible(test_app, snapshot):
    app = test_app
    app.build()

    needs_list = json.loads(Path(app.outdir, "needs.json").read_text())
    assert needs_list == snapshot(exclude=props("project", "creator"))


@pytest.mark.parametrize(
    "test_app",
    [
        {
            "buildername": "needs",
            "srcdir": "doc_test/doc_needs_builder",
            "confoverrides": {"needs_json_remove_defaults": True},
        }
    ],
    indirect=True,
)
def test_doc_needs_builder_remove_defaults(test_app, snapshot):
    app = test_app
    app.build()

    needs_list = json.loads(Path(app.outdir, "needs.json").read_text())
    assert needs_list == snapshot(exclude=props("created", "project", "creator"))


@pytest.mark.parametrize(
    "test_app",
    [{"buildername": "needs", "srcdir": "doc_test/doc_needs_builder_negative_tests"}],
    indirect=True,
)
def test_doc_needs_build_without_needs_file(test_app):
    app = test_app

    srcdir = Path(app.srcdir)
    out_dir = os.path.join(srcdir, "_build")

    out = subprocess.run(
        ["sphinx-build", "-b", "needs", srcdir, out_dir], capture_output=True
    )
    assert not out.stderr
    assert (
        "needs.json found, but will not be used because needs_file not configured."
        in out.stdout.decode("utf-8")
    )


@pytest.mark.parametrize(
    "test_app",
    [{"buildername": "html", "srcdir": "doc_test/doc_needs_builder_parallel"}],
    indirect=True,
)
def test_needs_html_and_json(test_app):
    """
    Build html output and needs.json in one sphinx-build
    """
    app = test_app
    app.build()

    needs_json_path = os.path.join(app.outdir, "needs.json")
    assert os.path.exists(needs_json_path)

    srcdir = app.srcdir
    build_dir = os.path.join(app.outdir, "../needs")
    print(build_dir)
    output = subprocess.run(
        ["sphinx-build", "-b", "needs", srcdir, build_dir],
        capture_output=True,
    )
    print(output)
    needs_json_path_2 = os.path.join(build_dir, "needs.json")
    assert os.path.exists(needs_json_path_2)

    # Check if the needs.json files from html/parallel build and builder are the same
    with open(needs_json_path) as f1:
        needs_1 = json.load(f1)
        with open(needs_json_path_2) as f2:
            needs_2 = json.load(f2)

            # Just check need-data, as the rest contains not matching timestamps
            need_data_1 = needs_1["versions"]["1.0"]["needs"]
            need_data_2 = needs_2["versions"]["1.0"]["needs"]
            assert need_data_1 == need_data_2