File: test_api_usage.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 (121 lines) | stat: -rw-r--r-- 2,998 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
118
119
120
121
import sys
import types
from pathlib import Path

import pytest
from sphinx.testing.util import SphinxTestApp

from sphinx_needs.api import add_need_type, get_need_types


@pytest.fixture()
def add_extension1():
    dummy_code = """
def setup(app):
    from sphinx_needs.api import get_need_types

    def after_config(app, config):
        print(get_need_types(app))

    # app.connect('config-inited', after_config)
    print(get_need_types(app))
    return {'version': '0.1'}
    """
    dummy_extension = types.ModuleType("dummy_extension.dummy")
    exec(dummy_code, dummy_extension.__dict__)
    sys.modules["dummy_extension.dummy"] = dummy_extension
    yield
    sys.modules.pop("dummy_extension.dummy")


@pytest.fixture()
def add_extension2():
    dummy_code = """
def setup(app):
    return {'version': '0.1'}
"""
    dummy_extension = types.ModuleType("dummy_extension.dummy")
    exec(dummy_code, dummy_extension.__dict__)
    sys.modules["dummy_extension.dummy"] = dummy_extension
    yield
    sys.modules.pop("dummy_extension.dummy")


@pytest.mark.usefixtures("add_extension1")
@pytest.mark.parametrize(
    "test_app",
    [
        {
            "buildername": "html",
            "srcdir": "doc_test/doc_basic",
            "no_plantuml": True,
            "confoverrides": {"extensions": ["sphinx_needs", "dummy_extension.dummy"]},
        }
    ],
    indirect=True,
)
def test_api_configuration(test_app: SphinxTestApp):
    app = test_app

    app.build()
    assert app._warning.getvalue() == ""

    html = Path(app.outdir, "index.html").read_text()
    assert html is not None


@pytest.mark.usefixtures("add_extension2")
@pytest.mark.parametrize(
    "test_app",
    [
        {
            "buildername": "html",
            "srcdir": "doc_test/doc_basic",
            "no_plantuml": True,
            "confoverrides": {"extensions": ["sphinx_needs", "dummy_extension.dummy"]},
        }
    ],
    indirect=True,
)
def test_api_get_types(test_app: SphinxTestApp):
    app = test_app

    need_types = get_need_types(app)
    assert set(need_types) == {"story", "spec", "impl", "test"}


@pytest.mark.usefixtures("add_extension2")
@pytest.mark.parametrize(
    "test_app",
    [
        {
            "buildername": "html",
            "srcdir": "doc_test/doc_basic",
            "no_plantuml": True,
            "confoverrides": {"extensions": ["sphinx_needs", "dummy_extension.dummy"]},
        }
    ],
    indirect=True,
)
def test_api_add_type(test_app: SphinxTestApp, snapshot):
    app = test_app

    add_need_type(app, "awesome", "Awesome", "AW_", "#000000", "cloud")
    need_types = app.config.needs_types
    assert need_types == snapshot

    Path(app.srcdir, "other_api.rst").write_text(
        """
:orphan:

.. awesome:: my awesome need
    :id: AW_001
        """
    )

    app.build()
    assert app._warning.getvalue() == ""

    html = Path(app.outdir, "other_api.html").read_text()
    assert html is not None
    assert "my awesome need" in html