Skip to content

Commit 4e735d3

Browse files
committed
🐛 Fix trailing separator in string_links for single-value fields
When a string_links field contained a single value, a trailing separator "; " was incorrectly rendered because len(data) checked the string length instead of the list length. Changed to len(data_list). Added a regression test that verifies single-value string_links fields render without a trailing separator.
1 parent 3ba0da0 commit 4e735d3

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

sphinx_needs/layout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ def meta(
546546
data_node += ref_item
547547

548548
if (
549-
name in needs_string_links_option and index + 1 < len(data)
549+
name in needs_string_links_option and index + 1 < len(data_list)
550550
) or index + 1 < len([data]):
551551
data_node += nodes.emphasis("; ", "; ")
552552

tests/doc_test/doc_needtable/test_options.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ TEST OPTIONS
1919

2020
Replaces the string from ``:config:`` and ``:github:`` with a link to the related website.
2121

22+
.. spec:: Single value string_link
23+
:id: SINGLE_STRING_LINK
24+
:github: 404
25+
26+
A need with a single-value string_links field to verify no trailing separator is rendered.
27+
2228

2329
table 1
2430
--------

tests/test_needtable.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,49 @@ def test_doc_needtable_options(test_app):
110110
assert string_column_order in html
111111

112112

113+
@pytest.mark.parametrize(
114+
"test_app",
115+
[{"buildername": "html", "srcdir": "doc_test/doc_needtable"}],
116+
indirect=True,
117+
)
118+
def test_string_links_no_trailing_separator(test_app):
119+
"""Test that single-value string_links fields don't get a trailing separator."""
120+
app = test_app
121+
app.build()
122+
html = Path(app.outdir, "test_options.html").read_text()
123+
124+
# Find the SINGLE_STRING_LINK need's github cell content
125+
assert "SINGLE_STRING_LINK" in html
126+
assert (
127+
'<a class="reference external" href="https://github.com/useblocks/sphinxcontrib-needs/issues/404">'
128+
"GitHub #404</a>" in html
129+
)
130+
131+
# The single-value field should NOT have a trailing "; " separator.
132+
# Before the fix, len(data) was used instead of len(data_list), causing
133+
# a trailing separator for any string value longer than 1 character.
134+
from lxml import html as html_parser
135+
136+
tree = html_parser.parse(str(Path(app.outdir, "test_options.html")))
137+
138+
# Find the need card/layout for SINGLE_STRING_LINK and check its github field
139+
# In the need layout, string_links values are rendered inside <span class="needs_data">
140+
# We look for the github content in the SINGLE_STRING_LINK need
141+
single_need_elements = tree.xpath(
142+
'//*[@id="SINGLE_STRING_LINK"]//span[@class="needs_data"]'
143+
)
144+
for elem in single_need_elements:
145+
text_content = elem.text_content()
146+
if "GitHub #404" in text_content:
147+
# Should contain exactly "GitHub #404" with no trailing "; "
148+
assert text_content.strip() == "GitHub #404", (
149+
f"Expected no trailing separator, got: {text_content!r}"
150+
)
151+
break
152+
else:
153+
pytest.fail("Could not find GitHub #404 in SINGLE_STRING_LINK need layout")
154+
155+
113156
@pytest.mark.parametrize(
114157
"test_app",
115158
[{"buildername": "html", "srcdir": "doc_test/doc_needtable"}],

0 commit comments

Comments
 (0)