forked from xflr6/graphviz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry-examples.py
More file actions
executable file
·50 lines (37 loc) · 1.33 KB
/
try-examples.py
File metadata and controls
executable file
·50 lines (37 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
"""Import ``graphviz`` here and run all scripts in the ``examples/`` dir."""
import os
import pathlib
import sys
import unittest.mock
import warnings
import graphviz
EXAMPLES = pathlib.Path('examples')
IO_KWARGS = {'encoding': 'utf-8'}
DEFAULT_FORMAT = 'pdf'
print('run', [pathlib.Path(__file__).name] + sys.argv[1:])
os.chdir(EXAMPLES)
graphviz.set_default_format(DEFAULT_FORMAT)
raised = []
with unittest.mock.patch.object(graphviz.graphs.BaseGraph, '_view') as mock_view:
for path in pathlib.Path().glob('*.py'):
print(path)
code = path.read_text(**IO_KWARGS)
try:
exec(code)
except Exception as e:
raised.append(e)
warnings.warn(e)
else:
if path.name.endswith('_recipe.py'):
continue
rendered = f'{path.stem}.gv.{DEFAULT_FORMAT}'
assert pathlib.Path(rendered).stat().st_size, f'non-empty {rendered}'
mock_view.assert_called_once_with(rendered,
format=DEFAULT_FORMAT,
quiet=False)
mock_view.reset_mock()
if raised:
print(*raised, sep='\n')
sys.exit(f'FAILED: {len(raised)} example(s) raised an error (WARNING)')
print('PASSED: all examples passed without raising')