Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix(tests): compare Pygments HTML output semantically across 2.19–2.21 (#10642)

Kiran Gadhavead02456fbcc9
diff --git a/tests/_output/test_md.py b/tests/_output/test_md.py
index 5870833c11..f464a0b0ff 100644
--- a/tests/_output/test_md.py
+++ b/tests/_output/test_md.py
@@ -8,6 +8,7 @@ import pytest
from inline_snapshot import snapshot
from marimo._output.md import _md, latex
+from tests.mocks import normalize_html_entities
if TYPE_CHECKING:
from pathlib import Path
@@ -37,7 +38,10 @@ def test_md_code_blocks() -> None:
# Test code block conversion
code_input = "```python\nprint('Hello, world!')\n```"
expected_output = '<div class="language-python codehilite"><pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Hello, world!&#39;</span><span class="p">)</span>\n</code></pre></div>'
- assert _md(code_input, apply_markdown_class=False).text == expected_output
+ result = _md(code_input, apply_markdown_class=False).text
+ assert normalize_html_entities(result) == normalize_html_entities(
+ expected_output
+ )
def test_md_latex() -> None:
diff --git a/tests/_runtime/test_trace.py b/tests/_runtime/test_trace.py
index a53f3e965a..336ec76b6a 100644
--- a/tests/_runtime/test_trace.py
+++ b/tests/_runtime/test_trace.py
@@ -18,6 +18,7 @@ from marimo._runtime.commands import (
from marimo._runtime.runtime import Kernel
from marimo._utils import async_path
from tests._messaging.mocks import MockStderr, MockStream
+from tests.mocks import normalize_html_entities
if TYPE_CHECKING:
from pathlib import Path
@@ -481,7 +482,8 @@ class TestEmbedTrace:
)
assert "ZeroDivisionError: division by zero" in result
- assert (file_path + "&quot;, line 17") in result
+ normalized = normalize_html_entities(result)
+ assert f'{file_path}", line 17' in normalized
assert "y / x" in result
diff --git a/tests/mocks.py b/tests/mocks.py
index 3052c42b99..4c1453f777 100644
--- a/tests/mocks.py
+++ b/tests/mocks.py
@@ -19,6 +19,35 @@ class SnapshotFunc(Protocol):
) -> None: ...
+_QUOTE_ENTITY_PATTERNS: tuple[tuple[re.Pattern[str], str], ...] = (
+ (re.compile(r"&amp;quot;", re.IGNORECASE), "&quot;"),
+ (re.compile(r"&amp;#39;", re.IGNORECASE), "&#39;"),
+ (re.compile(r"&amp;#x27;", re.IGNORECASE), "&#x27;"),
+ (re.compile(r"&amp;apos;", re.IGNORECASE), "&apos;"),
+ (re.compile(r"&quot;", re.IGNORECASE), '"'),
+ (re.compile(r"&#34;", re.IGNORECASE), '"'),
+ (re.compile(r"&#x22;", re.IGNORECASE), '"'),
+ (re.compile(r"&#39;", re.IGNORECASE), "'"),
+ (re.compile(r"&#x27;", re.IGNORECASE), "'"),
+ (re.compile(r"&apos;", re.IGNORECASE), "'"),
+)
+
+
+def normalize_html_entities(value: str) -> str:
+ """Normalize HTML quote entity encodings for semantic comparisons.
+
+ Pygments 2.21+ emits quotes literally in HTML text nodes while earlier
+ versions use `&#39;`/`&quot;` entities. Only quote entities are
+ normalized so other encodings like `&lt;`/`&gt;` remain comparable.
+ """
+ previous = None
+ while previous != value:
+ previous = value
+ for pattern, replacement in _QUOTE_ENTITY_PATTERNS:
+ value = pattern.sub(replacement, value)
+ return value
+
+
class ToText(HTMLParser):
def __init__(self) -> None:
HTMLParser.__init__(self)
@@ -64,7 +93,10 @@ def snapshotter(current_file: str) -> SnapshotFunc:
maybe_make_dirs(filepath)
def normalize(string: str) -> str:
- return string.replace("\r\n", "\n")
+ string = string.replace("\r\n", "\n")
+ if not filename.endswith(".json"):
+ string = normalize_html_entities(string)
+ return string
result = normalize(result)