def patch_open_read(files):
"""
Patch open() command and mock read() results
:type files: dict
"""
files = {os.path.abspath(path): content for path, content in files.items()}
def mock_open_wrapper(path, *args, **kwargs):
__tracebackhide__ = True # pylint: disable=unused-variable
assert path in files, 'try to open a non-mocked path\n desired={desired!r}\n mocked={mocked!r}'.format(
desired=path, mocked=files.keys())
open_mock = mock.mock_open(read_data=files[path])
return open_mock(path, *args, **kwargs)
return mock.patch(__get_open_ref(), mock_open_wrapper)
评论列表
文章目录