From 6b862261a7cacbdd94e7f0ddf457568f4eb06c35 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Wed, 4 Sep 2024 15:20:34 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20dynamic=20functions=20in?= =?UTF-8?q?=20`needextract`=20need?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes #1267 --- sphinx_needs/directives/needextract.py | 2 + .../needextract_with_nested_needs/index.rst | 4 + tests/test_needextract.py | 75 +++++++++++++------ tests/test_needextract_with_nested_needs.py | 34 --------- 4 files changed, 58 insertions(+), 57 deletions(-) delete mode 100644 tests/test_needextract_with_nested_needs.py diff --git a/sphinx_needs/directives/needextract.py b/sphinx_needs/directives/needextract.py index 92ea581f9..aaae9678d 100644 --- a/sphinx_needs/directives/needextract.py +++ b/sphinx_needs/directives/needextract.py @@ -18,6 +18,7 @@ used_filter_paragraph, ) from sphinx_needs.filter_common import FilterBase, process_filters +from sphinx_needs.functions.functions import find_and_replace_node_content from sphinx_needs.layout import build_need_repr from sphinx_needs.logging import log_warning from sphinx_needs.utils import add_doc, remove_node_from_tree @@ -202,6 +203,7 @@ def _build_needextract( env.resolve_references(dummy_need, extract_data["docname"], app.builder) # type: ignore[arg-type] dummy_need.attributes["ids"].append(need_data["id"]) + find_and_replace_node_content(dummy_need, env, need_data) rendered_node = build_need_repr( dummy_need, # type: ignore[arg-type] need_data, diff --git a/tests/doc_test/needextract_with_nested_needs/index.rst b/tests/doc_test/needextract_with_nested_needs/index.rst index a72356065..df3bcd86f 100644 --- a/tests/doc_test/needextract_with_nested_needs/index.rst +++ b/tests/doc_test/needextract_with_nested_needs/index.rst @@ -10,6 +10,8 @@ Test Another, child spec + This is id [[copy("id")]] + .. spec:: Child spec :id: SPEC_1_1 @@ -27,5 +29,7 @@ Test :id: SPEC_1_1_2 awesome grandchild spec number 2. + + This is grandchild id [[copy("id")]] Some parent text \ No newline at end of file diff --git a/tests/test_needextract.py b/tests/test_needextract.py index e0d1b40cb..d87edc62e 100644 --- a/tests/test_needextract.py +++ b/tests/test_needextract.py @@ -2,37 +2,24 @@ from pathlib import Path import pytest +from lxml import html as html_parser @pytest.mark.parametrize( "test_app", - [{"buildername": "html", "srcdir": "doc_test/doc_needextract"}], + [ + { + "buildername": "html", + "srcdir": "doc_test/doc_needextract", + "no_plantuml": True, + } + ], indirect=True, ) -def test_needextract_filter_options(test_app): - import subprocess - - app = test_app - - srcdir = Path(app.srcdir) - out_dir = srcdir / "_build" - - out = subprocess.run( - ["sphinx-build", "-M", "html", srcdir, out_dir], capture_output=True - ) - assert out.returncode == 0 - - -@pytest.mark.parametrize( - "test_app", - [{"buildername": "html", "srcdir": "doc_test/doc_needextract"}], - indirect=True, -) -def test_needextract_basic_run(test_app): +def test_needextract_basic(test_app): app = test_app app.build() - - from lxml import html as html_parser + assert not app._warning.getvalue() def run_checks(checks, html_path): html_path = str(Path(app.outdir, html_path)) @@ -66,3 +53,45 @@ def run_checks(checks, html_path): index_html = Path(app.outdir, "check_need_refs.html").read_text() assert "Awesome Sphinx-Needs" in index_html + + +@pytest.mark.parametrize( + "test_app", + [ + { + "buildername": "html", + "srcdir": "doc_test/needextract_with_nested_needs", + "no_plantuml": True, + } + ], + indirect=True, +) +def test_needextract_with_nested_needs(test_app): + app = test_app + app.build() + assert not app._warning.getvalue() + + needextract_html = Path(app.outdir, "needextract.html").read_text() + + # ensure that the needs exist and that their hrefs point to the correct location + assert ( + 'SPEC_1' + in needextract_html + ) + assert ( + 'SPEC_1_1' + in needextract_html + ) + assert ( + 'SPEC_1_1_1' + in needextract_html + ) + assert ( + 'SPEC_1_1_2' + in needextract_html + ) + + assert "This is id SPEC_1" in needextract_html + assert "This is grandchild id SPEC_1_1_2" in needextract_html diff --git a/tests/test_needextract_with_nested_needs.py b/tests/test_needextract_with_nested_needs.py deleted file mode 100644 index 696ef4396..000000000 --- a/tests/test_needextract_with_nested_needs.py +++ /dev/null @@ -1,34 +0,0 @@ -from pathlib import Path - -import pytest - - -@pytest.mark.parametrize( - "test_app", - [{"buildername": "html", "srcdir": "doc_test/needextract_with_nested_needs"}], - indirect=True, -) -def test_needextract_with_nested_needs(test_app): - app = test_app - app.build() - needextract_html = Path(app.outdir, "needextract.html").read_text() - - # ensure that the needs exist and that their hrefs point to the correct location - assert ( - 'SPEC_1' - in needextract_html - ) - assert ( - 'SPEC_1_1' - in needextract_html - ) - assert ( - 'SPEC_1_1_1' - in needextract_html - ) - assert ( - 'SPEC_1_1_2' - in needextract_html - )