Skip to content

Commit 792c574

Browse files
committed
Added more tests.
1 parent 581cedd commit 792c574

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

tests/test_cmd2.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,6 +2187,76 @@ 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):
2191+
mock_session_cls = mocker.patch('cmd2.cmd2.PromptSession')
2192+
mock_history_cls = mocker.patch('cmd2.cmd2.InMemoryHistory')
2193+
2194+
# Test with custom history first
2195+
my_history_list = ["help", "help alias", "help help"]
2196+
base_app.read_input(history=my_history_list)
2197+
mock_history_cls.assert_called_once_with(my_history_list)
2198+
2199+
called_kwargs = mock_session_cls.call_args.kwargs
2200+
assert called_kwargs['history'] == mock_history_cls.return_value
2201+
2202+
# Test with no history
2203+
mock_history_cls.reset_mock()
2204+
my_history_list = ["help", "help alias", "help help"]
2205+
base_app.read_input(history=None)
2206+
mock_history_cls.assert_called_once_with()
2207+
2208+
called_kwargs = mock_session_cls.call_args.kwargs
2209+
assert called_kwargs['history'] == mock_history_cls.return_value
2210+
2211+
2212+
def test_read_raw_input_session_usage_and_restore(base_app, mocker):
2213+
mock_session = mocker.MagicMock(name="temp_session")
2214+
base_app.main_session = mocker.MagicMock(name="main_session")
2215+
2216+
# Make sure we look like a terminal
2217+
mocker.patch.object(base_app, '_is_tty_session', return_value=True)
2218+
2219+
command_text = "help alias"
2220+
2221+
def check_and_return_input(*args, **kwargs):
2222+
# Check if the active session was the one we passed in
2223+
assert base_app.active_session == mock_session
2224+
return command_text
2225+
2226+
mock_session.prompt.side_effect = check_and_return_input
2227+
2228+
# Call _read_raw_input()
2229+
result = base_app._read_raw_input("prompt> ", mock_session)
2230+
assert result == command_text
2231+
2232+
# Check if session.prompt() was called
2233+
mock_session.prompt.assert_called_once()
2234+
2235+
# Verify that active session was restored
2236+
assert base_app.active_session == base_app.main_session
2237+
2238+
2239+
def test_read_raw_input_restores_on_error(base_app, mocker):
2240+
mock_session = mocker.MagicMock()
2241+
base_app.main_session = mocker.MagicMock(name="main_session")
2242+
2243+
# Make sure we look like a terminal
2244+
mocker.patch.object(base_app, '_is_tty_session', return_value=True)
2245+
2246+
def check_and_raise(*args, **kwargs):
2247+
# Check if the active session was the one we passed in
2248+
assert base_app.active_session == mock_session
2249+
raise KeyboardInterrupt
2250+
2251+
mock_session.prompt.side_effect = check_and_raise
2252+
2253+
with pytest.raises(KeyboardInterrupt):
2254+
base_app._read_raw_input("prompt> ", mock_session)
2255+
2256+
# Even though an error occurred, the finally block restored active session
2257+
assert base_app.active_session == base_app.main_session
2258+
2259+
21902260
def test_poutput_string(outsim_app) -> None:
21912261
msg = 'This is a test'
21922262
outsim_app.poutput(msg)

0 commit comments

Comments
 (0)