diff --git a/.travis.yml b/.travis.yml index f93e6e0522..2405a9a467 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,6 @@ before_install: - sudo apt-get install -y graphviz install: - pip install -e . - - pip install "pytest>=3" pytest-cov coveralls + - pip install mock "pytest>=3" pytest-cov coveralls script: pytest after_success: if [[ $TRAVIS_PYTHON_VERSION == 3.6* ]]; then coveralls; fi diff --git a/CHANGES b/CHANGES index f85ff19b4c..d71791a2f3 100644 --- a/CHANGES +++ b/CHANGES @@ -5,7 +5,7 @@ Changelog Version 0.7.1 (in development) ------------------------------ - +Fix graphivz.view exception on unsupported platform. Version 0.7 diff --git a/graphviz/backend.py b/graphviz/backend.py index b2a86e03d6..57abbd3a73 100644 --- a/graphviz/backend.py +++ b/graphviz/backend.py @@ -155,7 +155,7 @@ def view(filepath): """ try: view_func = getattr(view, PLATFORM) - except KeyError: + except AttributeError: raise RuntimeError('platform %r not supported' % PLATFORM) view_func(filepath) diff --git a/requirements.txt b/requirements.txt index e7ef3f3df0..6634a2d7d0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ # exact dependency versions used in development # dependencies for testing and building +mock pytest>=3 pytest-cov coverage diff --git a/tests/test_backend.py b/tests/test_backend.py index 02f73af63d..88b6635ba6 100644 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -2,15 +2,10 @@ import subprocess +import mock import pytest -from graphviz.backend import render, pipe - - -def test_render_filepath_missing(): - with pytest.raises(subprocess.CalledProcessError) as e: - render('dot', 'pdf', 'doesnotexist') - assert e.value.returncode == 2 +from graphviz.backend import render, pipe, view def test_render_engine_unknown(): @@ -25,7 +20,13 @@ def test_render_format_unknown(): e.match(r'format') -def test_pipe_invalid_dot(): +def test_render_missingfile(): + with pytest.raises(subprocess.CalledProcessError) as e: + render('dot', 'pdf', 'doesnotexist') + assert e.value.returncode == 2 + + +def test_pipe_invalid_data(): with pytest.raises(subprocess.CalledProcessError) as e: pipe('dot', 'svg', b'spam', quiet=True) assert e.value.returncode == 1 @@ -34,3 +35,31 @@ def test_pipe_invalid_dot(): def test_pipe(svg_pattern): src = pipe('dot', 'svg', b'graph { spam }').decode('ascii') assert svg_pattern.match(src) + + +@mock.patch('graphviz.backend.PLATFORM', 'spam') +def test_view_unsupported(): + with pytest.raises(RuntimeError) as e: + view('spam') + e.match(r'platform') + + +@mock.patch('graphviz.backend.PLATFORM', 'darwin') +@mock.patch('subprocess.Popen') +def test_view_darwin(Popen): + view('spam') + Popen.assert_called_once_with(['open', 'spam']) + + +@mock.patch('graphviz.backend.PLATFORM', 'linux') +@mock.patch('subprocess.Popen') +def test_view_linux(Popen): + view('spam') + Popen.assert_called_once_with(['xdg-open', 'spam']) + + +@mock.patch('graphviz.backend.PLATFORM', 'windows') +@mock.patch('os.startfile') +def test_view_windows(startfile): + view('spam') + startfile.assert_called_once_with('spam')