Skip to content

Commit 285809e

Browse files
authored
Merge pull request #9 from petercorke/test/no-popup-in-tests
Mock webbrowser.open() in test_dotfile to stop popping up a PDF
2 parents 6c24cfe + ea2336a commit 285809e

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎.github/workflows/ci.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ jobs:
2222
run: |
2323
python -m pip install --upgrade pip
2424
pip install .[dev]
25+
sudo apt-get update
26+
sudo apt-get install -y graphviz
2527
- name: Test with pytest
2628
run: pytest
2729

@@ -59,6 +61,8 @@ jobs:
5961
run: |
6062
python -m pip install --upgrade pip
6163
pip install -e .[dev]
64+
sudo apt-get update
65+
sudo apt-get install -y graphviz
6266
- name: Run coverage
6367
run: |
6468
pytest --cov=pgraph --cov-report=xml:coverage.xml

‎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)