MCPcopy Index your code
hub / github.com/RustPython/RustPython / mock_open

Function mock_open

Lib/unittest/mock.py:2964–3047  ·  view source on GitHub ↗

A helper function to create a mock to replace the use of `open`. It works for `open` called directly or used as a context manager. The `mock` argument is the mock object to configure. If `None` (the default) then a `MagicMock` will be created for you, with the API limited to me

(mock=None, read_data='')

Source from the content-addressed store, hash-verified

2962
2963
2964def mock_open(mock=None, read_data=''):
2965 """
2966 A helper function to create a mock to replace the use of `open`. It works
2967 for `open` called directly or used as a context manager.
2968
2969 The `mock` argument is the mock object to configure. If `None` (the
2970 default) then a `MagicMock` will be created for you, with the API limited
2971 to methods or attributes available on standard file handles.
2972
2973 `read_data` is a string for the `read`, `readline` and `readlines` of the
2974 file handle to return. This is an empty string by default.
2975 """
2976 _read_data = _to_stream(read_data)
2977 _state = [_read_data, None]
2978
2979 def _readlines_side_effect(*args, **kwargs):
2980 if handle.readlines.return_value is not None:
2981 return handle.readlines.return_value
2982 return _state[0].readlines(*args, **kwargs)
2983
2984 def _read_side_effect(*args, **kwargs):
2985 if handle.read.return_value is not None:
2986 return handle.read.return_value
2987 return _state[0].read(*args, **kwargs)
2988
2989 def _readline_side_effect(*args, **kwargs):
2990 yield from _iter_side_effect()
2991 while True:
2992 yield _state[0].readline(*args, **kwargs)
2993
2994 def _iter_side_effect():
2995 if handle.readline.return_value is not None:
2996 while True:
2997 yield handle.readline.return_value
2998 for line in _state[0]:
2999 yield line
3000
3001 def _next_side_effect():
3002 if handle.readline.return_value is not None:
3003 return handle.readline.return_value
3004 return next(_state[0])
3005
3006 def _exit_side_effect(exctype, excinst, exctb):
3007 handle.close()
3008
3009 global file_spec
3010 if file_spec is None:
3011 import _io
3012 file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO))))
3013
3014 global open_spec
3015 if open_spec is None:
3016 import _io
3017 open_spec = list(set(dir(_io.open)))
3018 if mock is None:
3019 mock = MagicMock(name='open', spec=open_spec)
3020
3021 handle = MagicMock(spec=file_spec)

Callers 15

test_mock_openMethod · 0.90
test_explicit_mockMethod · 0.90
test_read_dataMethod · 0.90
test_readline_dataMethod · 0.90
test_dunder_iter_dataMethod · 0.90
test_next_dataMethod · 0.90
test_readlines_dataMethod · 0.90
test_read_bytesMethod · 0.90
test_readline_bytesMethod · 0.90
test_readlines_bytesMethod · 0.90

Calls 7

_to_streamFunction · 0.85
listClass · 0.85
setFunction · 0.85
dirFunction · 0.85
MagicMockClass · 0.85
_readline_side_effectFunction · 0.85
unionMethod · 0.45

Tested by 15

test_mock_openMethod · 0.72
test_explicit_mockMethod · 0.72
test_read_dataMethod · 0.72
test_readline_dataMethod · 0.72
test_dunder_iter_dataMethod · 0.72
test_next_dataMethod · 0.72
test_readlines_dataMethod · 0.72
test_read_bytesMethod · 0.72
test_readline_bytesMethod · 0.72
test_readlines_bytesMethod · 0.72