-
Notifications
You must be signed in to change notification settings - Fork 213
/
try-examples.py
executable file
·56 lines (42 loc) · 1.42 KB
/
try-examples.py
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
51
52
53
54
55
56
#!/usr/bin/env python3
# flake8: noqa
"""Import ``graphviz`` here and run all scripts in the ``examples/`` dir."""
import os
import pathlib
import sys
import unittest.mock
import warnings
import graphviz # noqa: F401
SELF = pathlib.Path(__file__)
EXAMPLES = pathlib.Path('examples')
IO_KWARGS = {'encoding': 'utf-8'}
DEFAULT_FORMAT = 'pdf'
print('run', [SELF.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 not raised:
print('PASSED: all examples passed without raising')
sys.exit(None)
else:
message = f'FAILED: {len(raised)} examples raised (WARNING)'
print(message, *raised, sep='\n')
sys.exit(message)