fix: visual_report markdown helpers crash on a non-string input (#1633)
This commit is contained in:
@@ -37,6 +37,8 @@ def _autolink_urls(md_text: str) -> str:
|
|||||||
|
|
||||||
Skips URLs already inside markdown link syntax [text](url).
|
Skips URLs already inside markdown link syntax [text](url).
|
||||||
"""
|
"""
|
||||||
|
if not isinstance(md_text, str):
|
||||||
|
return md_text
|
||||||
# Match bare URLs not already inside ](...)
|
# Match bare URLs not already inside ](...)
|
||||||
return re.sub(
|
return re.sub(
|
||||||
r'(?<!\]\()(?<!\()(https?://[^\s\)<>]+)',
|
r'(?<!\]\()(?<!\()(https?://[^\s\)<>]+)',
|
||||||
@@ -67,6 +69,8 @@ def _md_to_html(md_text: str) -> str:
|
|||||||
|
|
||||||
def _extract_headings(md_text: str) -> List[Dict[str, str]]:
|
def _extract_headings(md_text: str) -> List[Dict[str, str]]:
|
||||||
"""Pull h2/h3 headings from markdown for table of contents."""
|
"""Pull h2/h3 headings from markdown for table of contents."""
|
||||||
|
if not isinstance(md_text, str):
|
||||||
|
return []
|
||||||
headings = []
|
headings = []
|
||||||
seen_slugs: Dict[str, int] = {}
|
seen_slugs: Dict[str, int] = {}
|
||||||
|
|
||||||
|
|||||||
18
tests/test_visual_report_nonstring.py
Normal file
18
tests/test_visual_report_nonstring.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
"""Regression: visual_report markdown helpers must tolerate a non-string.
|
||||||
|
|
||||||
|
_autolink_urls did `re.sub(..., md_text)` and _extract_headings did
|
||||||
|
`re.finditer(..., md_text)`; a None/non-string raised TypeError. They now
|
||||||
|
return the input / [] respectively.
|
||||||
|
"""
|
||||||
|
from src.visual_report import _autolink_urls, _extract_headings
|
||||||
|
|
||||||
|
|
||||||
|
def test_non_string_does_not_crash():
|
||||||
|
assert _autolink_urls(None) is None
|
||||||
|
assert _extract_headings(None) == []
|
||||||
|
assert _extract_headings(123) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_valid_markdown_unchanged():
|
||||||
|
assert "](https://x.com)" in _autolink_urls("see https://x.com")
|
||||||
|
assert _extract_headings("## Title")[0]["text"] == "Title"
|
||||||
Reference in New Issue
Block a user