Skip to content
Open
Prev Previous commit
Next Next commit
Refactor test_build_changes: Optimize change item search logic in fin…
…d_change_item function
  • Loading branch information
dariomesic committed Oct 28, 2025
commit d2f170e2aa38a548da8f157be72b2ad47c205e47
33 changes: 20 additions & 13 deletions tests/test_builders/test_build_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,35 @@
htmltext = (app.outdir / 'changes.html').read_text(encoding='utf8')
soup = BeautifulSoup(htmltext, 'html.parser')

# Get all <li> items up front
all_items = soup.find_all('li')
assert len(all_items) >= 5, "Did not find all 5 change items"

Check failure on line 27 in tests/test_builders/test_build_changes.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (Q000)

tests/test_builders/test_build_changes.py:27:33: Q000 Double quotes found but single quotes preferred

def find_change_item(type_text: str, version: str, content: str) -> dict[str, Any]:
"""Helper to find and validate change items."""

Check failure on line 30 in tests/test_builders/test_build_changes.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (D202)

tests/test_builders/test_build_changes.py:30:9: D202 No blank lines allowed after function docstring (found 1)
# Use regex to find text content, ignoring surrounding whitespace/newlines
item = soup.find( # type: ignore[call-overload]
'li',
string=re.compile(r'\s*' + re.escape(content) + r'\s*'),
)
assert item is not None, f"Could not find change item containing '{content}'"

type_elem = item.find('i')

Check failure on line 31 in tests/test_builders/test_build_changes.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

tests/test_builders/test_build_changes.py:31:1: W293 Blank line contains whitespace
found_item = None
# Loop through all <li> tags
for item in all_items:
# Check if the content is in the item's *full text*
if re.search(re.escape(content), item.text):
found_item = item
break # Found it!

Check failure on line 39 in tests/test_builders/test_build_changes.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

tests/test_builders/test_build_changes.py:39:1: W293 Blank line contains whitespace
# This is the assertion that was failing
assert found_item is not None, f"Could not find change item containing '{content}'"

type_elem = found_item.find('i')
assert type_elem is not None, f"Missing type indicator for '{content}'"
assert type_text in type_elem.text, (
f"Expected type '{type_text}' for '{content}'"
)

assert f'version {version}' in item.text, (
assert f'version {version}' in found_item.text, (
f'Version {version} not found in {content!r}'
)

return {'item': item, 'type': type_elem}
return {'item': found_item, 'type': type_elem}

# Test simple changes
changes = [
Expand Down Expand Up @@ -69,9 +78,6 @@
)
assert malloc_change['item'].find('b').text == 'void *Test_Malloc(size_t n)'

# Test the other test function still works
assert len(soup.find_all('li')) >= 5 # Make sure we found all items


@pytest.mark.sphinx(
'changes',
Expand All @@ -84,3 +90,4 @@

assert 'no changes in version 0.7.' in app.status.getvalue()
assert not (app.outdir / 'changes.html').exists()

Check failure on line 93 in tests/test_builders/test_build_changes.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

tests/test_builders/test_build_changes.py:93:5: W292 No newline at end of file

Check failure on line 93 in tests/test_builders/test_build_changes.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

tests/test_builders/test_build_changes.py:93:1: W293 Blank line contains whitespace
Loading