|
| 1 | +import pytest |
| 2 | +import json |
| 3 | +from pathlib import Path |
| 4 | +from sphinx_needs.data import SphinxNeedsData |
| 5 | +from sphinx.testing.util import SphinxTestApp |
| 6 | + |
| 7 | + |
| 8 | +# ╭──────────────────────────────────────────────────────────────────────────────╮ |
| 9 | +# │ Integration Tests via Sphinx │ |
| 10 | +# ╰──────────────────────────────────────────────────────────────────────────────╯ |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(scope="session") |
| 14 | +def sphinx_base_dir(tmp_path_factory): |
| 15 | + return tmp_path_factory.mktemp("sphinx") |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture(scope="session") |
| 19 | +def sphinx_app_setup(sphinx_base_dir): |
| 20 | + def _create_app(conf_content, rst_content, filter_tags=None): |
| 21 | + src_dir = sphinx_base_dir / "src" |
| 22 | + src_dir.mkdir(exist_ok=True) |
| 23 | + |
| 24 | + (src_dir / "conf.py").write_text(conf_content) |
| 25 | + (src_dir / "index.rst").write_text(rst_content) |
| 26 | + (src_dir / "filter_tags.txt").write_text(filter_tags) |
| 27 | + |
| 28 | + app = SphinxTestApp( |
| 29 | + freshenv=True, |
| 30 | + srcdir=Path(src_dir), |
| 31 | + confdir=Path(src_dir), |
| 32 | + outdir=sphinx_base_dir / "out", |
| 33 | + buildername="html", |
| 34 | + warningiserror=True, |
| 35 | + confoverrides={"filter_tags_file_path": str(src_dir / "filter_tags.txt")}, |
| 36 | + ) |
| 37 | + |
| 38 | + return app |
| 39 | + |
| 40 | + return _create_app |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture(scope="session") |
| 44 | +def positive_filter_tags(): |
| 45 | + return "test-feat, some-ip" |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture(scope="session") |
| 49 | +def negative_filter_tags(): |
| 50 | + return "tag1, tag2" |
| 51 | + |
| 52 | + |
| 53 | +@pytest.fixture(scope="session") |
| 54 | +def empty_filter_tags(): |
| 55 | + return "" |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture(scope="session") |
| 59 | +def basic_conf(): |
| 60 | + return """ |
| 61 | +extensions = [ |
| 62 | + "sphinx_needs", |
| 63 | + "modularity", |
| 64 | +] |
| 65 | +needs_types = [ |
| 66 | + dict(directive="test_req", title="Testing Requirement", prefix="TREQ_", color="#BFD8D2", style="node"), |
| 67 | + dict(directive="tool_req", title="Tooling Requirement", prefix="TOOL_", color="#BFD8D2", style="node"), |
| 68 | +] |
| 69 | +needs_extra_links = [ |
| 70 | + { |
| 71 | + "option": "satisfies", |
| 72 | + "incoming": "is satisfied by", |
| 73 | + "outgoing": "satisfies", |
| 74 | + "style_start": "-up", |
| 75 | + "style_end": "->", |
| 76 | + } |
| 77 | +] |
| 78 | +""" |
| 79 | + |
| 80 | + |
| 81 | +@pytest.fixture(scope="session") |
| 82 | +def basic_rst_file(): |
| 83 | + return """ |
| 84 | +.. tool_req:: Test Tool Requirement |
| 85 | + :id: TOOL_REQ_1 |
| 86 | + :tags: test-feat, feature1 |
| 87 | + :satisfies: TEST_REQ_1, TEST_REQ_20 |
| 88 | +
|
| 89 | + Some content for the tool requirement |
| 90 | +
|
| 91 | +.. test_req:: Testing Requirement 1 |
| 92 | + :id: TEST_REQ_1 |
| 93 | + :tags: test-feat |
| 94 | +
|
| 95 | + Some content should be here |
| 96 | +
|
| 97 | +.. test_req:: Testing Requirement 20 |
| 98 | + :id: TEST_REQ_20 |
| 99 | + :tags: feature1 |
| 100 | +
|
| 101 | + More content, this one is different |
| 102 | +""" |
| 103 | + |
| 104 | + |
| 105 | +def test_modularity_hide_ok( |
| 106 | + sphinx_app_setup, basic_conf, basic_rst_file, positive_filter_tags, sphinx_base_dir |
| 107 | +): |
| 108 | + app = sphinx_app_setup(basic_conf, basic_rst_file, positive_filter_tags) |
| 109 | + try: |
| 110 | + app.build() |
| 111 | + Needs_Data = SphinxNeedsData(app.env) |
| 112 | + needs_data = {x["id"]: x for x in Needs_Data.get_needs_view().values()} |
| 113 | + html = (app.outdir / "index.html").read_text() |
| 114 | + |
| 115 | + assert "TOOL_REQ_1" in needs_data |
| 116 | + assert "TEST_REQ_1" in needs_data |
| 117 | + assert "TEST_REQ_20" in needs_data |
| 118 | + assert needs_data["TOOL_REQ_1"]["hide"] == False |
| 119 | + assert needs_data["TOOL_REQ_1"]["satisfies"] == ["TEST_REQ_20"] |
| 120 | + assert needs_data["TEST_REQ_1"]["hide"] == True |
| 121 | + assert needs_data["TEST_REQ_20"]["hide"] == False |
| 122 | + |
| 123 | + assert "TOOL_REQ_1" in html |
| 124 | + assert "TEST_REQ_20" in html |
| 125 | + # making sure we do not see this in the final HTML |
| 126 | + assert "TEST_REQ_1" not in html |
| 127 | + finally: |
| 128 | + app.cleanup() |
| 129 | + |
| 130 | + |
| 131 | +def test_modularity_hide_no_match( |
| 132 | + sphinx_app_setup, basic_conf, basic_rst_file, negative_filter_tags, sphinx_base_dir |
| 133 | +): |
| 134 | + app = sphinx_app_setup(basic_conf, basic_rst_file, negative_filter_tags) |
| 135 | + try: |
| 136 | + app.build() |
| 137 | + Needs_Data = SphinxNeedsData(app.env) |
| 138 | + needs_data = {x["id"]: x for x in Needs_Data.get_needs_view().values()} |
| 139 | + html = (app.outdir / "index.html").read_text() |
| 140 | + |
| 141 | + assert "TOOL_REQ_1" in needs_data |
| 142 | + assert "TEST_REQ_1" in needs_data |
| 143 | + assert "TEST_REQ_20" in needs_data |
| 144 | + assert needs_data["TEST_REQ_1"]["hide"] == False |
| 145 | + assert needs_data["TEST_REQ_20"]["hide"] == False |
| 146 | + assert needs_data["TOOL_REQ_1"]["hide"] == False |
| 147 | + assert needs_data["TOOL_REQ_1"]["satisfies"] == ["TEST_REQ_1", "TEST_REQ_20"] |
| 148 | + |
| 149 | + assert "TOOL_REQ_1" in html |
| 150 | + assert "TEST_REQ_1" in html |
| 151 | + assert "TEST_REQ_20" in html |
| 152 | + finally: |
| 153 | + app.cleanup() |
| 154 | + |
| 155 | + |
| 156 | +def test_modularity_hide_no_filters( |
| 157 | + sphinx_app_setup, basic_conf, basic_rst_file, empty_filter_tags, sphinx_base_dir |
| 158 | +): |
| 159 | + app = sphinx_app_setup(basic_conf, basic_rst_file, empty_filter_tags) |
| 160 | + try: |
| 161 | + app.build() |
| 162 | + Needs_Data = SphinxNeedsData(app.env) |
| 163 | + needs_data = {x["id"]: x for x in Needs_Data.get_needs_view().values()} |
| 164 | + html = (app.outdir / "index.html").read_text() |
| 165 | + |
| 166 | + # The outcome should be the same as when we found no matches, just making sure it doesn't error and nothing happens |
| 167 | + assert "TOOL_REQ_1" in needs_data |
| 168 | + assert "TEST_REQ_1" in needs_data |
| 169 | + assert "TEST_REQ_20" in needs_data |
| 170 | + assert needs_data["TEST_REQ_1"]["hide"] == False |
| 171 | + assert needs_data["TEST_REQ_20"]["hide"] == False |
| 172 | + assert needs_data["TOOL_REQ_1"]["hide"] == False |
| 173 | + assert needs_data["TOOL_REQ_1"]["satisfies"] == ["TEST_REQ_1", "TEST_REQ_20"] |
| 174 | + |
| 175 | + assert "TOOL_REQ_1" in html |
| 176 | + assert "TEST_REQ_1" in html |
| 177 | + assert "TEST_REQ_20" in html |
| 178 | + finally: |
| 179 | + app.cleanup() |
0 commit comments