Skip to content

Commit b3841d4

Browse files
committed
Add more test coverage and reduce repetition
1 parent 2b2f51f commit b3841d4

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

cmd2/cmd2.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,8 @@
152152
if sys.platform == "win32":
153153
from prompt_toolkit.output.win32 import NoConsoleScreenBufferError # type: ignore[attr-defined]
154154
else:
155-
156-
class NoConsoleScreenBufferError(Exception): # type: ignore[no-redef]
157-
"""Dummy exception to use when prompt_toolkit.output.win32.NoConsoleScreenBufferError is not available."""
158-
159-
155+
# Trigger the except block for non-Windows platforms
156+
raise ImportError # noqa: TRY301
160157
except ImportError:
161158

162159
class NoConsoleScreenBufferError(Exception): # type: ignore[no-redef]

tests/test_cmd2.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3399,3 +3399,36 @@ def test_complete_optional_args_defaults(base_app) -> None:
33993399
# Test that complete can be called with just text and state
34003400
complete_val = base_app.complete('test', 0)
34013401
assert complete_val is None
3402+
3403+
3404+
def test_prompt_session_init_no_console_error(monkeypatch):
3405+
from prompt_toolkit.shortcuts import PromptSession
3406+
3407+
from cmd2.cmd2 import NoConsoleScreenBufferError
3408+
3409+
# Mock PromptSession to raise NoConsoleScreenBufferError on first call, then succeed
3410+
valid_session_mock = mock.MagicMock(spec=PromptSession)
3411+
mock_session = mock.MagicMock(side_effect=[NoConsoleScreenBufferError, valid_session_mock])
3412+
monkeypatch.setattr("cmd2.cmd2.PromptSession", mock_session)
3413+
3414+
cmd2.Cmd()
3415+
3416+
# Check that fallback to DummyInput/Output happened
3417+
from prompt_toolkit.input import DummyInput
3418+
from prompt_toolkit.output import DummyOutput
3419+
3420+
assert mock_session.call_count == 2
3421+
# Check args of second call
3422+
call_args = mock_session.call_args_list[1]
3423+
kwargs = call_args[1]
3424+
assert isinstance(kwargs['input'], DummyInput)
3425+
assert isinstance(kwargs['output'], DummyOutput)
3426+
3427+
3428+
def test_no_console_screen_buffer_error_dummy():
3429+
from cmd2.cmd2 import NoConsoleScreenBufferError
3430+
3431+
# Check that it behaves like a normal exception
3432+
err = NoConsoleScreenBufferError("test message")
3433+
assert str(err) == "test message"
3434+
assert isinstance(err, Exception)

0 commit comments

Comments
 (0)