Skip to content

Commit 92faadc

Browse files
committed
Fix remaining tests in test_cmd2.py other than the one that is skipped
1 parent d405a4f commit 92faadc

File tree

2 files changed

+83
-66
lines changed

2 files changed

+83
-66
lines changed

cmd2/cmd2.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3239,7 +3239,12 @@ def get_prompt() -> Any:
32393239
line = pt.prompt()
32403240
if len(line) == 0:
32413241
raise EOFError
3242-
return line.rstrip('\n')
3242+
line = line.rstrip('\n')
3243+
3244+
if self.echo:
3245+
self.poutput(f'{prompt}{line}')
3246+
3247+
return line
32433248

32443249
def _read_command_line(self, prompt: str) -> str:
32453250
"""Read command line from appropriate stdin.

tests/test_cmd2.py

Lines changed: 77 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,7 +1839,10 @@ def test_multiline_history_with_quotes(multiline_app, monkeypatch) -> None:
18391839
history_lines = history_item.raw.splitlines()
18401840
assert history_lines[0] == 'orate Look, "There are newlines'
18411841
assert history_lines[1] == ' and spaces '
1842-
assert history_lines[2] == ' " in quotes.;'
1842+
assert history_lines[2] == ' "'
1843+
assert history_lines[3] == ' in'
1844+
assert history_lines[4] == 'quotes.'
1845+
assert history_lines[5] == ';'
18431846

18441847

18451848
class CommandResultApp(cmd2.Cmd):
@@ -1927,61 +1930,63 @@ def test_read_input_rawinput_true(capsys, monkeypatch) -> None:
19271930
app = cmd2.Cmd()
19281931
app.use_rawinput = True
19291932

1930-
# Mock out input() to return input_str
1931-
read_input_mock = mock.MagicMock(name='read_input', return_value=input_str)
1932-
monkeypatch.setattr("cmd2.Cmd.read_input", read_input_mock)
1933-
1934-
# isatty is True
1935-
with mock.patch('sys.stdin.isatty', mock.MagicMock(name='isatty', return_value=True)):
1936-
line = app.read_input(prompt_str)
1937-
assert line == input_str
1938-
1939-
# Run custom history code
1940-
custom_history = ['cmd1', 'cmd2']
1941-
line = app.read_input(prompt_str, history=custom_history, completion_mode=cmd2.CompletionMode.NONE)
1942-
assert line == input_str
1943-
1944-
# Run all completion modes
1945-
line = app.read_input(prompt_str, completion_mode=cmd2.CompletionMode.NONE)
1946-
assert line == input_str
1947-
1948-
line = app.read_input(prompt_str, completion_mode=cmd2.CompletionMode.COMMANDS)
1949-
assert line == input_str
1950-
1951-
# custom choices
1952-
custom_choices = ['choice1', 'choice2']
1953-
line = app.read_input(prompt_str, completion_mode=cmd2.CompletionMode.CUSTOM, choices=custom_choices)
1954-
assert line == input_str
1955-
1956-
# custom choices_provider
1957-
line = app.read_input(
1958-
prompt_str, completion_mode=cmd2.CompletionMode.CUSTOM, choices_provider=cmd2.Cmd.get_all_commands
1959-
)
1960-
assert line == input_str
1961-
1962-
# custom completer
1963-
line = app.read_input(prompt_str, completion_mode=cmd2.CompletionMode.CUSTOM, completer=cmd2.Cmd.path_complete)
1964-
assert line == input_str
1965-
1966-
# custom parser
1967-
line = app.read_input(prompt_str, completion_mode=cmd2.CompletionMode.CUSTOM, parser=cmd2.Cmd2ArgumentParser())
1968-
assert line == input_str
1969-
1970-
# isatty is False
1971-
with mock.patch('sys.stdin.isatty', mock.MagicMock(name='isatty', return_value=False)):
1972-
# echo True
1973-
app.echo = True
1974-
line = app.read_input(prompt_str)
1975-
out, _err = capsys.readouterr()
1976-
assert line == input_str
1977-
assert out == f"{prompt_str}{input_str}\n"
1978-
1979-
# echo False
1980-
app.echo = False
1981-
line = app.read_input(prompt_str)
1982-
out, _err = capsys.readouterr()
1983-
assert line == input_str
1984-
assert not out
1933+
# Mock prompt_toolkit.prompt (used when isatty=False)
1934+
# and app.session.prompt (used when use_rawinput=True and isatty=True)
1935+
with (
1936+
mock.patch('prompt_toolkit.prompt', return_value=input_str),
1937+
mock.patch.object(app.session, 'prompt', return_value=input_str),
1938+
):
1939+
# isatty is True
1940+
with mock.patch('sys.stdin.isatty', mock.MagicMock(name='isatty', return_value=True)):
1941+
line = app.read_input(prompt_str)
1942+
assert line == input_str
1943+
1944+
# Run custom history code
1945+
custom_history = ['cmd1', 'cmd2']
1946+
line = app.read_input(prompt_str, history=custom_history, completion_mode=cmd2.CompletionMode.NONE)
1947+
assert line == input_str
1948+
1949+
# Run all completion modes
1950+
line = app.read_input(prompt_str, completion_mode=cmd2.CompletionMode.NONE)
1951+
assert line == input_str
1952+
1953+
line = app.read_input(prompt_str, completion_mode=cmd2.CompletionMode.COMMANDS)
1954+
assert line == input_str
1955+
1956+
# custom choices
1957+
custom_choices = ['choice1', 'choice2']
1958+
line = app.read_input(prompt_str, completion_mode=cmd2.CompletionMode.CUSTOM, choices=custom_choices)
1959+
assert line == input_str
1960+
1961+
# custom choices_provider
1962+
line = app.read_input(
1963+
prompt_str, completion_mode=cmd2.CompletionMode.CUSTOM, choices_provider=cmd2.Cmd.get_all_commands
1964+
)
1965+
assert line == input_str
1966+
1967+
# custom completer
1968+
line = app.read_input(prompt_str, completion_mode=cmd2.CompletionMode.CUSTOM, completer=cmd2.Cmd.path_complete)
1969+
assert line == input_str
1970+
1971+
# custom parser
1972+
line = app.read_input(prompt_str, completion_mode=cmd2.CompletionMode.CUSTOM, parser=cmd2.Cmd2ArgumentParser())
1973+
assert line == input_str
1974+
1975+
# isatty is False
1976+
with mock.patch('sys.stdin.isatty', mock.MagicMock(name='isatty', return_value=False)):
1977+
# echo True
1978+
app.echo = True
1979+
line = app.read_input(prompt_str)
1980+
out, _err = capsys.readouterr()
1981+
assert line == input_str
1982+
assert out == f"{prompt_str}{input_str}\n"
1983+
1984+
# echo False
1985+
app.echo = False
1986+
line = app.read_input(prompt_str)
1987+
out, _err = capsys.readouterr()
1988+
assert line == input_str
1989+
assert not out
19851990

19861991

19871992
def test_read_input_rawinput_false(capsys, monkeypatch) -> None:
@@ -1999,42 +2004,49 @@ def make_app(isatty: bool, empty_input: bool = False):
19992004
new_app.use_rawinput = False
20002005
return new_app
20012006

2007+
def mock_pt_prompt(message='', **kwargs):
2008+
# Emulate prompt printing for isatty=True case
2009+
if message:
2010+
print(message, end='')
2011+
return input_str
2012+
20022013
# isatty True
20032014
app = make_app(isatty=True)
2004-
line = app.read_input(prompt_str)
2015+
with mock.patch('prompt_toolkit.prompt', side_effect=mock_pt_prompt):
2016+
line = app.read_input(prompt_str)
20052017
out, _err = capsys.readouterr()
20062018
assert line == input_str
20072019
assert out == prompt_str
20082020

20092021
# isatty True, empty input
20102022
app = make_app(isatty=True, empty_input=True)
2011-
line = app.read_input(prompt_str)
2023+
with mock.patch('prompt_toolkit.prompt', return_value=''), pytest.raises(EOFError):
2024+
app.read_input(prompt_str)
20122025
out, _err = capsys.readouterr()
2013-
assert line == 'eof'
2014-
assert out == prompt_str
20152026

20162027
# isatty is False, echo is True
20172028
app = make_app(isatty=False)
20182029
app.echo = True
2019-
line = app.read_input(prompt_str)
2030+
with mock.patch('prompt_toolkit.prompt', return_value=input_str):
2031+
line = app.read_input(prompt_str)
20202032
out, _err = capsys.readouterr()
20212033
assert line == input_str
20222034
assert out == f"{prompt_str}{input_str}\n"
20232035

20242036
# isatty is False, echo is False
20252037
app = make_app(isatty=False)
20262038
app.echo = False
2027-
line = app.read_input(prompt_str)
2039+
with mock.patch('prompt_toolkit.prompt', return_value=input_str):
2040+
line = app.read_input(prompt_str)
20282041
out, _err = capsys.readouterr()
20292042
assert line == input_str
20302043
assert not out
20312044

20322045
# isatty is False, empty input
20332046
app = make_app(isatty=False, empty_input=True)
2034-
line = app.read_input(prompt_str)
2047+
with mock.patch('prompt_toolkit.prompt', return_value=''), pytest.raises(EOFError):
2048+
app.read_input(prompt_str)
20352049
out, _err = capsys.readouterr()
2036-
assert line == 'eof'
2037-
assert not out
20382050

20392051

20402052
def test_custom_stdout() -> None:

0 commit comments

Comments
 (0)