Skip to content

Commit

Permalink
improve version()
Browse files Browse the repository at this point in the history
  • Loading branch information
xflr6 committed May 30, 2017
1 parent 2525bb6 commit af6c863
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGES
Expand Up @@ -5,7 +5,7 @@ Changelog
Version 0.7.2 (in development)
------------------------------

Add backend.version() function.
Add grapviz.version() function.


Version 0.7.1
Expand Down
3 changes: 2 additions & 1 deletion docs/api.rst
Expand Up @@ -50,7 +50,6 @@ documented above.

.. autofunction:: graphviz.render
.. autofunction:: graphviz.pipe
.. autofunction:: graphviz.version
.. autofunction:: graphviz.view


Expand All @@ -65,3 +64,5 @@ Other

.. autodata:: graphviz.ExecutableNotFound
:annotation:

.. autofunction:: graphviz.version
7 changes: 5 additions & 2 deletions graphviz/backend.py
Expand Up @@ -172,10 +172,11 @@ def version():
"""Return the version number tuple from the stderr output of ``dot -V``.
Returns:
Two/three int version tuple or None (if the output cannot be parsed).
Two or three int version tuple.
Raises:
graphviz.ExecutableNotFound: If the Graphviz executable is not found.
subprocess.CalledProcessError: If the exit status is non-zero.
RuntimmeError: If the output cannot be parsed into a version number.
"""
args = ['dot', '-V']
try:
Expand All @@ -189,7 +190,9 @@ def version():

info = outs.decode('ascii')
ma = re.search(r'graphviz version (\d+\.\d+(?:\.\d+)?) ', info)
return tuple(int(d) for d in ma.group(1).split('.')) if ma is not None else None
if ma is None:
raise RuntimeError
return tuple(int(d) for d in ma.group(1).split('.'))


def view(filepath):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_backend.py
Expand Up @@ -135,6 +135,14 @@ def test_version(capsys):
assert capsys.readouterr() == ('', '')


def test_version_parsefail_mocked(check_output):
check_output.return_value = b'nonversioninfo'
with pytest.raises(RuntimeError):
version()
check_output.assert_called_once_with(['dot', '-V'], startupinfo=STARTUPINFO,
stderr=subprocess.STDOUT)


def test_version_mocked(check_output):
check_output.return_value = b'dot - graphviz version 1.2.3 (mocked)'
assert version() == (1, 2, 3)
Expand Down

0 comments on commit af6c863

Please sign in to comment.