FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Mock webbrowser.open() in test_dotfile to stop popping up a PDF · petercorke/pgraph-python@af31c62 · GitHub

Repository navigation

Commit af31c62

Browse files
andcommitted
Mock webbrowser.open() in test_dotfile to stop popping up a PDF
showgraph() calls webbrowser.open() on the rendered PDF, which was launching a real PDF viewer/browser window every test run -- purely incidental, since the test made no assertion on the result at all. Mocks webbrowser.open() so the test runs headless, and replaces the "doesn't crash" smoke test with a real assertion: the file webbrowser.open() was called with actually exists and starts with the PDF magic bytes (%PDF-), confirming dot's rendering pipeline genuinely produced a valid PDF -- a stronger check than before, not a weaker one. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 6c24cfe commit af31c62

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

‎tests/test_graph.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ def test_plot(self):
781781

782782
def test_dotfile(self):
783783
import pathlib
784+
import unittest.mock
784785

785786
g = UGraph()
786787
v1 = g.add_vertex(coord=[0,0], name='v1')
@@ -795,7 +796,17 @@ def test_dotfile(self):
795796
g.dotfile(str(path))
796797
self.assertTrue(path.is_file())
797798

798-
g.showgraph()
799+
# showgraph() pops a PDF up in a browser via webbrowser.open() --
800+
# mock that so the test runs headless, but still exercise the real
801+
# dot->PDF rendering and check the result is an actual PDF file.
802+
with unittest.mock.patch('pgraph.PGraph.webbrowser.open') as mock_open:
803+
g.showgraph()
804+
805+
mock_open.assert_called_once()
806+
pdf_url = mock_open.call_args[0][0]
807+
pdf_path = pathlib.Path(pdf_url.removeprefix('file://'))
808+
self.assertTrue(pdf_path.is_file())
809+
self.assertEqual(pdf_path.read_bytes()[:5], b'%PDF-')
799810

800811
class TestDGraph(unittest.TestCase):
801812

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL