Skip to content

Commit e907ecd

Browse files
committed
test: mock builtins.open for argcomplete
Although mocking argcomplete.open may have worked at some point, it doesn't work now. Mock the builtin open, delegating to to the original saved as _OPEN, for use in argcomplete. Signed-off-by: Kevin Locke <[email protected]>
1 parent 728e15c commit e907ecd

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

tests/test_argcomplete.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
# Note: Path not created on the filesystem. Only used for mocking.
1414
_COMPLETIONS_FILENAME = 'unittest/mock/argcomplete'
15+
_OPEN = open
1516

1617

1718
def _open_mock_for(for_file: str) -> Callable[..., Any]:
@@ -51,8 +52,7 @@ def maybe_mock_open(file: str, *args: Any, **kwargs: Any) -> Any:
5152
# No need to ignore it if it doesn't exist
5253
pass
5354

54-
# pylint: disable-next=unspecified-encoding
55-
return open(file, *args, **kwargs)
55+
return _OPEN(file, *args, **kwargs)
5656

5757
# Save created Mocks so the caller can test them
5858
maybe_mock_open.mock_files = [] # type: ignore
@@ -69,7 +69,7 @@ def maybe_mock_open(file: str, *args: Any, **kwargs: Any) -> Any:
6969
'COMP_TYPE': '33',
7070
},
7171
)
72-
@patch('argcomplete.open', side_effect=_open_mock_for(_COMPLETIONS_FILENAME))
72+
@patch('builtins.open', side_effect=_open_mock_for(_COMPLETIONS_FILENAME))
7373
@patch('sys.argv', ['packagename'])
7474
@patch('sys.stdout', new_callable=StringIO)
7575
@patch('sys.stderr', new_callable=StringIO)

0 commit comments

Comments
 (0)