Skip to content

Commit 30bbe65

Browse files
Copilotxadupre
andcommitted
Differentiate dummy SVG vs PNG output when UNITTEST_GOING=1, both contain DISABLED FOR TESTS
Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
1 parent 3f968ac commit 30bbe65

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

_unittests/ut_gdot/test_gdot_extension.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def test_gdot4_png(self):
164164

165165
@ignore_warnings(PendingDeprecationWarning)
166166
def test_gdot_unittest_going_svg(self):
167-
"""When UNITTEST_GOING=1, a dummy SVG is rendered instead of calling graphviz."""
167+
"""When UNITTEST_GOING=1, a dummy SVG containing 'DISABLED FOR TESTS' is rendered."""
168168
content = """
169169
before
170170
@@ -183,12 +183,13 @@ def test_gdot_unittest_going_svg(self):
183183
content, writer_name="html", new_extensions=["sphinx_runpython.gdot"]
184184
)
185185

186-
self.assertIn("dummy", html)
187-
self.assertIn("svg", html)
186+
self.assertIn("DISABLED FOR TESTS", html)
187+
self.assertIn("<svg", html)
188+
self.assertNotIn("<img", html)
188189

189190
@ignore_warnings(PendingDeprecationWarning)
190191
def test_gdot_unittest_going_png(self):
191-
"""When UNITTEST_GOING=1, a dummy SVG is rendered for png format too."""
192+
"""When UNITTEST_GOING=1, a dummy image containing 'DISABLED FOR TESTS' is rendered."""
192193
content = """
193194
before
194195
@@ -207,8 +208,9 @@ def test_gdot_unittest_going_png(self):
207208
content, writer_name="html", new_extensions=["sphinx_runpython.gdot"]
208209
)
209210

210-
self.assertIn("dummy", html)
211-
self.assertIn("svg", html)
211+
self.assertIn("DISABLED FOR TESTS", html)
212+
self.assertIn("<img", html)
213+
self.assertNotIn("<svg", html)
212214

213215

214216
if __name__ == "__main__":

sphinx_runpython/gdot/sphinx_gdot_extension.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,23 @@ def depart_gdot_node_rst(self, node):
218218

219219
_DUMMY_SVG = (
220220
'<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">'
221-
'<text x="10" y="50">dummy</text>'
221+
'<text x="10" y="50">DISABLED FOR TESTS</text>'
222222
"</svg>"
223223
)
224224

225+
_DUMMY_PNG_HTML = (
226+
'<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQI12NgAAIABQ'
227+
'AABjkB6QAAAABJRU5ErkJggg==" alt="DISABLED FOR TESTS" />'
228+
)
229+
225230

226-
def _emit_dummy_svg(self):
227-
"""Emit a placeholder SVG when ``UNITTEST_GOING=1`` is set."""
231+
def _emit_dummy_output(self, format: str = "svg"):
232+
"""Emit a placeholder graphic when ``UNITTEST_GOING=1`` is set."""
228233
self.body.append('<div class="graphviz">')
229-
self.body.append(_DUMMY_SVG)
234+
if format == "png":
235+
self.body.append(_DUMMY_PNG_HTML)
236+
else:
237+
self.body.append(_DUMMY_SVG)
230238
self.body.append("</div>\n")
231239
raise nodes.SkipNode
232240

@@ -243,7 +251,7 @@ def render_dot_html(
243251
format: str = "svg",
244252
) -> tuple[str, str]:
245253
if os.environ.get("UNITTEST_GOING", "0") == "1":
246-
_emit_dummy_svg(self)
254+
_emit_dummy_output(self, format=format)
247255

248256
if format not in {"png", "svg"}:
249257
logger = logging.getLogger(__name__)
@@ -381,7 +389,7 @@ def visit_gdot_node_html(self, node):
381389
"""
382390
if node["format"].lower() == "png":
383391
if os.environ.get("UNITTEST_GOING", "0") == "1":
384-
_emit_dummy_svg(self)
392+
_emit_dummy_output(self, format="png")
385393
from sphinx.ext.graphviz import html_visit_graphviz
386394

387395
return html_visit_graphviz(self, node)

0 commit comments

Comments
 (0)