Skip to content

Commit 6e81162

Browse files
committed
Fixed Windows tests.
1 parent 792c574 commit 6e81162

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

tests/test_cmd2.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,9 +2187,11 @@ def test_read_input_passes_all_arguments_to_resolver(base_app):
21872187
)
21882188

21892189

2190-
def test_history_is_correctly_passed_to_session(base_app, mocker):
2190+
def test_read_input_history_is_passed_to_session(base_app, monkeypatch, mocker):
21912191
mock_session_cls = mocker.patch('cmd2.cmd2.PromptSession')
21922192
mock_history_cls = mocker.patch('cmd2.cmd2.InMemoryHistory')
2193+
read_command_mock = mocker.MagicMock(name='_read_command_line', return_value='command')
2194+
monkeypatch.setattr("cmd2.Cmd._read_command_line", read_command_mock)
21932195

21942196
# Test with custom history first
21952197
my_history_list = ["help", "help alias", "help help"]
@@ -2201,6 +2203,7 @@ def test_history_is_correctly_passed_to_session(base_app, mocker):
22012203

22022204
# Test with no history
22032205
mock_history_cls.reset_mock()
2206+
mock_session_cls.reset_mock()
22042207
my_history_list = ["help", "help alias", "help help"]
22052208
base_app.read_input(history=None)
22062209
mock_history_cls.assert_called_once_with()
@@ -2225,8 +2228,11 @@ def check_and_return_input(*args, **kwargs):
22252228

22262229
mock_session.prompt.side_effect = check_and_return_input
22272230

2228-
# Call _read_raw_input()
2229-
result = base_app._read_raw_input("prompt> ", mock_session)
2231+
# Mock patch_stdout to prevent it from attempting to access the Windows
2232+
# console buffer in a Windows test environment.
2233+
with mock.patch('cmd2.cmd2.patch_stdout'):
2234+
result = base_app._read_raw_input("prompt> ", mock_session)
2235+
22302236
assert result == command_text
22312237

22322238
# Check if session.prompt() was called
@@ -2250,7 +2256,9 @@ def check_and_raise(*args, **kwargs):
22502256

22512257
mock_session.prompt.side_effect = check_and_raise
22522258

2253-
with pytest.raises(KeyboardInterrupt):
2259+
# Mock patch_stdout to prevent it from attempting to access the Windows
2260+
# console buffer in a Windows test environment.
2261+
with mock.patch('cmd2.cmd2.patch_stdout'), pytest.raises(KeyboardInterrupt):
22542262
base_app._read_raw_input("prompt> ", mock_session)
22552263

22562264
# Even though an error occurred, the finally block restored active session

0 commit comments

Comments
 (0)