Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,8 +680,9 @@ def _should_continue_multiline(self) -> bool:
def _create_main_session(self, auto_suggest: bool, completekey: str) -> PromptSession[str]:
"""Create and return the main PromptSession for the application.

Builds an interactive session if stdin is a TTY. Otherwise, uses
dummy drivers to support non-interactive streams like pipes or files.
Builds an interactive session if self.stdin and self.stdout are TTYs.
Otherwise, uses dummy drivers to support non-interactive streams like
pipes or files.
"""
key_bindings = None
if completekey != self.DEFAULT_COMPLETEKEY:
Expand Down Expand Up @@ -713,7 +714,7 @@ def _(event: Any) -> None: # pragma: no cover
"rprompt": self.get_rprompt,
}

if self.stdin.isatty():
if self.stdin.isatty() and self.stdout.isatty():
try:
if self.stdin != sys.stdin:
kwargs["input"] = create_input(stdin=self.stdin)
Expand Down Expand Up @@ -3245,7 +3246,8 @@ def _is_tty_session(session: PromptSession[str]) -> bool:
"""
# Validate against the session's assigned input driver rather than sys.stdin.
# This respects the fallback logic in _create_main_session() and allows unit
# tests to inject PipeInput for programmatic interaction.
# tests to inject PipeInput for programmatic interaction even if paired with
# a DummyOutput.
return not isinstance(session.input, DummyInput)

def _read_raw_input(
Expand Down
32 changes: 27 additions & 5 deletions tests/test_cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3842,10 +3842,16 @@ def test_create_main_session_exception(monkeypatch):
mock_session = mock.MagicMock(side_effect=[ValueError, valid_session_mock])
monkeypatch.setattr("cmd2.cmd2.PromptSession", mock_session)

cmd2.Cmd()
# Mock isatty to ensure we enter the try block
with (
mock.patch('sys.stdin.isatty', return_value=True),
mock.patch('sys.stdout.isatty', return_value=True),
):
cmd2.Cmd()

# Check that fallback to DummyInput/Output happened
assert mock_session.call_count == 2

# Check args of second call
call_args = mock_session.call_args_list[1]
kwargs = call_args[1]
Expand Down Expand Up @@ -3931,7 +3937,12 @@ def test_create_main_session_no_console_error(monkeypatch):
mock_session = mock.MagicMock(side_effect=[NoConsoleScreenBufferError, valid_session_mock])
monkeypatch.setattr("cmd2.cmd2.PromptSession", mock_session)

cmd2.Cmd()
# Mock isatty to ensure we enter the try block
with (
mock.patch('sys.stdin.isatty', return_value=True),
mock.patch('sys.stdout.isatty', return_value=True),
):
cmd2.Cmd()

# Check that fallback to DummyInput/Output happened
assert mock_session.call_count == 2
Expand All @@ -3949,8 +3960,9 @@ def test_create_main_session_with_custom_tty() -> None:
custom_stdin.isatty.return_value = True
assert custom_stdin is not sys.stdin

# Create a mock stdout which is not sys.stdout
# Create a mock stdout with says it's a TTY
custom_stdout = mock.MagicMock(spec=io.TextIOWrapper)
custom_stdout.isatty.return_value = True
assert custom_stdout is not sys.stdout

# Check if the streams were wrapped
Expand All @@ -3967,8 +3979,8 @@ def test_create_main_session_with_custom_tty() -> None:
mock_create_output.assert_called_once_with(stdout=custom_stdout)


def test_create_main_session_non_interactive() -> None:
# Set up a mock for a non-TTY stream (like a pipe)
def test_create_main_session_stdin_non_tty() -> None:
# Set up a mock for a non-TTY stdin stream
mock_stdin = mock.MagicMock(spec=io.TextIOWrapper)
mock_stdin.isatty.return_value = False

Expand All @@ -3977,6 +3989,16 @@ def test_create_main_session_non_interactive() -> None:
assert isinstance(app.main_session.output, DummyOutput)


def test_create_main_session_stdout_non_tty() -> None:
# Set up a mock for a non-TTY stdout stream
mock_stdout = mock.MagicMock(spec=io.TextIOWrapper)
mock_stdout.isatty.return_value = False

app = cmd2.Cmd(stdout=mock_stdout)
assert isinstance(app.main_session.input, DummyInput)
assert isinstance(app.main_session.output, DummyOutput)


def test_no_console_screen_buffer_error_dummy():
from cmd2.cmd2 import NoConsoleScreenBufferError

Expand Down
Loading