https://github.com/xflr6/graphviz
Revision 6fe8c4e3bc3d5a8a053115e238f1cc974cd793b5 authored by Eli Schwartz on 04 December 2023, 09:05:52 UTC, committed by GitHub on 04 December 2023, 09:05:52 UTC
The PyPI standalone mock package is a straight backport of the stdlib to older versions of python. It is usually not needed. In this case, the required version of mock is >=4, which backports the python 3.8 stdlib. The minimum version of python required is already 3.8, so all functionality guaranteed to exist is already part of the stdlib. Simply use the stdlib directly.
1 parent 448d1a0
Tip revision: 6fe8c4e3bc3d5a8a053115e238f1cc974cd793b5 authored by Eli Schwartz on 04 December 2023, 09:05:52 UTC
migrate from external mock package to stdlib unittest.mock (#212)
migrate from external mock package to stdlib unittest.mock (#212)
Tip revision: 6fe8c4e
try-examples.py
#!/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)
Computing file changes ...