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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
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.pyindex 5870833c11..f464a0b0ff 100644--- a/tests/_output/test_md.py+++ b/tests/_output/test_md.py@@ -8,6 +8,7 @@ import pytestfrom inline_snapshot import snapshotfrom marimo._output.md import _md, latex+from tests.mocks import normalize_html_entitiesif TYPE_CHECKING:from pathlib import Path@@ -37,7 +38,10 @@ def test_md_code_blocks() -> None:# Test code block conversioncode_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">'Hello, world!'</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.pyindex 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 Kernelfrom marimo._utils import async_pathfrom tests._messaging.mocks import MockStderr, MockStream+from tests.mocks import normalize_html_entitiesif TYPE_CHECKING:from pathlib import Path@@ -481,7 +482,8 @@ class TestEmbedTrace:)assert "ZeroDivisionError: division by zero" in result- assert (file_path + "", line 17") in result+ normalized = normalize_html_entities(result)+ assert f'{file_path}", line 17' in normalizedassert "y / x" in resultdiff --git a/tests/mocks.py b/tests/mocks.pyindex 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"&quot;", re.IGNORECASE), """),+ (re.compile(r"&#39;", re.IGNORECASE), "'"),+ (re.compile(r"&#x27;", re.IGNORECASE), "'"),+ (re.compile(r"&apos;", re.IGNORECASE), "'"),+ (re.compile(r""", re.IGNORECASE), '"'),+ (re.compile(r""", re.IGNORECASE), '"'),+ (re.compile(r""", re.IGNORECASE), '"'),+ (re.compile(r"'", re.IGNORECASE), "'"),+ (re.compile(r"'", re.IGNORECASE), "'"),+ (re.compile(r"'", 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 `'`/`"` entities. Only quote entities are+ normalized so other encodings like `<`/`>` 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 stringresult = normalize(result)