Skip to content

Commit 9ff0c3f

Browse files
danieldotnlclaude
andcommitted
Simulate RecursionError in JSON soft-fail test
Triggering a real RecursionError via 10000-deep nested arrays depends on sys.getrecursionlimit(), which is environment-dependent. On CI with a higher limit, json.loads succeeded and json.dumps(indent=2) produced a multi-megabyte string — the assertion diff then flooded the step log and hit the output cap. Monkeypatch json.loads instead so the soft-fail branch is exercised deterministically with a tiny input. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent ca4ade6 commit 9ff0c3f

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

tests/test_scraper.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -327,19 +327,24 @@ async def test_scraper_handles_malformed_json_softly(
327327
@pytest.mark.async_test
328328
@pytest.mark.timeout(5)
329329
async def test_scraper_handles_recursion_error_softly(
330-
hass: HomeAssistant, scraper_instance
330+
hass: HomeAssistant, scraper_instance, monkeypatch
331331
):
332-
"""Pathologically nested JSON (RecursionError) also soft-fails in formatted_content."""
333-
# 10000 levels of nesting reliably triggers RecursionError on CPython 3.13
334-
# while remaining a syntactically valid JSON-shaped string.
335-
deeply_nested = "[" * 10000 + "]" * 10000
332+
"""A JSON parse that raises RecursionError soft-fails in formatted_content."""
333+
# Triggering a real RecursionError depends on sys.getrecursionlimit(), which
334+
# varies across environments. Simulate the failure mode directly so the test
335+
# is deterministic and doesn't produce huge debug output on CI.
336+
await scraper_instance.set_content(SAMPLE_JSON_SIMPLE)
337+
assert scraper_instance._is_json is True
336338

337-
await scraper_instance.set_content(deeply_nested)
339+
def boom(*_args, **_kwargs):
340+
raise RecursionError("simulated deeply-nested JSON")
341+
342+
monkeypatch.setattr(
343+
"custom_components.multiscrape.scraper.json.loads", boom
344+
)
338345

339-
assert scraper_instance._is_json is True
340-
assert scraper_instance._data == deeply_nested
341346
# formatted_content falls back to raw on RecursionError
342-
assert scraper_instance.formatted_content == deeply_nested
347+
assert scraper_instance.formatted_content == SAMPLE_JSON_SIMPLE
343348

344349

345350
@pytest.mark.integration

0 commit comments

Comments
 (0)