|
1 | 1 | import unittest |
2 | 2 | import logging |
| 3 | +import os |
3 | 4 | import sys |
| 5 | +from contextlib import contextmanager |
4 | 6 | from sphinx_runpython.process_rst import rst2html |
5 | 7 | from sphinx_runpython.ext_test_case import ( |
6 | 8 | ExtTestCase, |
|
10 | 12 | ) |
11 | 13 |
|
12 | 14 |
|
| 15 | +@contextmanager |
| 16 | +def unittest_going(): |
| 17 | + """Context manager that sets UNITTEST_GOING=1 for the duration of the block.""" |
| 18 | + old = os.environ.get("UNITTEST_GOING", None) |
| 19 | + os.environ["UNITTEST_GOING"] = "1" |
| 20 | + try: |
| 21 | + yield |
| 22 | + finally: |
| 23 | + if old is None: |
| 24 | + os.environ.pop("UNITTEST_GOING", None) |
| 25 | + else: |
| 26 | + os.environ["UNITTEST_GOING"] = old |
| 27 | + |
| 28 | + |
13 | 29 | class TestGDotExtension(ExtTestCase): |
14 | 30 | def setUp(self): |
15 | 31 | logger = logging.getLogger("gdot") |
@@ -176,6 +192,58 @@ def test_gdot_script_cache(self): |
176 | 192 | count, 2, f"Expected the DOT code to appear twice, got {count}" |
177 | 193 | ) |
178 | 194 |
|
| 195 | + def test_gdot_unittest_going_svg(self): |
| 196 | + """When UNITTEST_GOING=1, a dummy SVG containing 'DISABLED FOR TESTS' is rendered.""" |
| 197 | + content = """ |
| 198 | + before |
| 199 | +
|
| 200 | + .. gdot:: |
| 201 | + :format: svg |
| 202 | +
|
| 203 | + digraph foo { |
| 204 | + "bar" -> "baz"; |
| 205 | + } |
| 206 | +
|
| 207 | + after |
| 208 | + """.replace(" ", "") |
| 209 | + |
| 210 | + with unittest_going(): |
| 211 | + html = rst2html( |
| 212 | + content, writer_name="html", new_extensions=["sphinx_runpython.gdot"] |
| 213 | + ) |
| 214 | + |
| 215 | + self.assertIn("DISABLED FOR TESTS", html) |
| 216 | + self.assertIn("<svg", html) |
| 217 | + self.assertNotIn("<img", html) |
| 218 | + |
| 219 | + @ignore_warnings(PendingDeprecationWarning) |
| 220 | + def test_gdot_unittest_going_png(self): |
| 221 | + """ |
| 222 | + When UNITTEST_GOING=1, a dummy image containing |
| 223 | + 'DISABLED FOR TESTS' is rendered. |
| 224 | + """ |
| 225 | + content = """ |
| 226 | + before |
| 227 | +
|
| 228 | + .. gdot:: |
| 229 | + :format: png |
| 230 | +
|
| 231 | + digraph foo { |
| 232 | + "bar" -> "baz"; |
| 233 | + } |
| 234 | +
|
| 235 | + after |
| 236 | + """.replace(" ", "") |
| 237 | + |
| 238 | + with unittest_going(): |
| 239 | + html = rst2html( |
| 240 | + content, writer_name="html", new_extensions=["sphinx_runpython.gdot"] |
| 241 | + ) |
| 242 | + |
| 243 | + self.assertIn("DISABLED FOR TESTS", html) |
| 244 | + self.assertIn("<img", html) |
| 245 | + self.assertNotIn("<svg", html) |
| 246 | + |
179 | 247 |
|
180 | 248 | if __name__ == "__main__": |
181 | 249 | unittest.main(verbosity=2) |
0 commit comments