Skip to content

Commit 027f7d8

Browse files
committed
Updating unit tests
1 parent 4202135 commit 027f7d8

File tree

2 files changed

+46
-44
lines changed

2 files changed

+46
-44
lines changed

cmd2/cmd2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1993,7 +1993,7 @@ def default(self, statement: Statement) -> Optional[bool]:
19931993
# Set apply_style to False so default_error's style is not overridden
19941994
self.perror(err_msg, apply_style=False)
19951995

1996-
def read_input(self, prompt: str, allow_completion: bool = False) -> str:
1996+
def read_input(self, prompt: str, *, allow_completion: bool = False) -> str:
19971997
"""
19981998
Read input from appropriate stdin value. Also allows you to disable tab completion while input is being read.
19991999

tests/test_cmd2.py

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,10 +1092,10 @@ def select_app():
10921092
app = SelectApp()
10931093
return app
10941094

1095-
def test_select_options(select_app):
1096-
# Mock out the input call so we don't actually wait for a user's response on stdin
1097-
m = mock.MagicMock(name='input', return_value='2')
1098-
builtins.input = m
1095+
def test_select_options(select_app, monkeypatch):
1096+
# Mock out the read_input call so we don't actually wait for a user's response on stdin
1097+
read_input_mock = mock.MagicMock(name='read_input', return_value='2')
1098+
monkeypatch.setattr("cmd2.Cmd.read_input", read_input_mock)
10991099

11001100
food = 'bacon'
11011101
out, err = run_cmd(select_app, "eat {}".format(food))
@@ -1106,17 +1106,18 @@ def test_select_options(select_app):
11061106
""".format(food))
11071107

11081108
# Make sure our mock was called with the expected arguments
1109-
m.assert_called_once_with('Sauce? ')
1109+
read_input_mock.assert_called_once_with('Sauce? ')
11101110

11111111
# And verify the expected output to stdout
11121112
assert out == expected
11131113

1114-
def test_select_invalid_option_too_big(select_app):
1114+
def test_select_invalid_option_too_big(select_app, monkeypatch):
11151115
# Mock out the input call so we don't actually wait for a user's response on stdin
1116-
m = mock.MagicMock(name='input')
1116+
read_input_mock = mock.MagicMock(name='read_input')
1117+
11171118
# If side_effect is an iterable then each call to the mock will return the next value from the iterable.
1118-
m.side_effect = ['3', '1'] # First pass an invalid selection, then pass a valid one
1119-
builtins.input = m
1119+
read_input_mock.side_effect = ['3', '1'] # First pass an invalid selection, then pass a valid one
1120+
monkeypatch.setattr("cmd2.Cmd.read_input", read_input_mock)
11201121

11211122
food = 'fish'
11221123
out, err = run_cmd(select_app, "eat {}".format(food))
@@ -1130,18 +1131,19 @@ def test_select_invalid_option_too_big(select_app):
11301131
# Make sure our mock was called exactly twice with the expected arguments
11311132
arg = 'Sauce? '
11321133
calls = [mock.call(arg), mock.call(arg)]
1133-
m.assert_has_calls(calls)
1134-
assert m.call_count == 2
1134+
read_input_mock.assert_has_calls(calls)
1135+
assert read_input_mock.call_count == 2
11351136

11361137
# And verify the expected output to stdout
11371138
assert out == expected
11381139

1139-
def test_select_invalid_option_too_small(select_app):
1140+
def test_select_invalid_option_too_small(select_app, monkeypatch):
11401141
# Mock out the input call so we don't actually wait for a user's response on stdin
1141-
m = mock.MagicMock(name='input')
1142+
read_input_mock = mock.MagicMock(name='read_input')
1143+
11421144
# If side_effect is an iterable then each call to the mock will return the next value from the iterable.
1143-
m.side_effect = ['0', '1'] # First pass an invalid selection, then pass a valid one
1144-
builtins.input = m
1145+
read_input_mock.side_effect = ['0', '1'] # First pass an invalid selection, then pass a valid one
1146+
monkeypatch.setattr("cmd2.Cmd.read_input", read_input_mock)
11451147

11461148
food = 'fish'
11471149
out, err = run_cmd(select_app, "eat {}".format(food))
@@ -1155,16 +1157,16 @@ def test_select_invalid_option_too_small(select_app):
11551157
# Make sure our mock was called exactly twice with the expected arguments
11561158
arg = 'Sauce? '
11571159
calls = [mock.call(arg), mock.call(arg)]
1158-
m.assert_has_calls(calls)
1159-
assert m.call_count == 2
1160+
read_input_mock.assert_has_calls(calls)
1161+
assert read_input_mock.call_count == 2
11601162

11611163
# And verify the expected output to stdout
11621164
assert out == expected
11631165

1164-
def test_select_list_of_strings(select_app):
1166+
def test_select_list_of_strings(select_app, monkeypatch):
11651167
# Mock out the input call so we don't actually wait for a user's response on stdin
1166-
m = mock.MagicMock(name='input', return_value='2')
1167-
builtins.input = m
1168+
read_input_mock = mock.MagicMock(name='read_input', return_value='2')
1169+
monkeypatch.setattr("cmd2.Cmd.read_input", read_input_mock)
11681170

11691171
out, err = run_cmd(select_app, "study")
11701172
expected = normalize("""
@@ -1174,15 +1176,15 @@ def test_select_list_of_strings(select_app):
11741176
""".format('science'))
11751177

11761178
# Make sure our mock was called with the expected arguments
1177-
m.assert_called_once_with('Subject? ')
1179+
read_input_mock.assert_called_once_with('Subject? ')
11781180

11791181
# And verify the expected output to stdout
11801182
assert out == expected
11811183

1182-
def test_select_list_of_tuples(select_app):
1184+
def test_select_list_of_tuples(select_app, monkeypatch):
11831185
# Mock out the input call so we don't actually wait for a user's response on stdin
1184-
m = mock.MagicMock(name='input', return_value='2')
1185-
builtins.input = m
1186+
read_input_mock = mock.MagicMock(name='read_input', return_value='2')
1187+
monkeypatch.setattr("cmd2.Cmd.read_input", read_input_mock)
11861188

11871189
out, err = run_cmd(select_app, "procrastinate")
11881190
expected = normalize("""
@@ -1192,16 +1194,16 @@ def test_select_list_of_tuples(select_app):
11921194
""".format('YouTube'))
11931195

11941196
# Make sure our mock was called with the expected arguments
1195-
m.assert_called_once_with('How would you like to procrastinate? ')
1197+
read_input_mock.assert_called_once_with('How would you like to procrastinate? ')
11961198

11971199
# And verify the expected output to stdout
11981200
assert out == expected
11991201

12001202

1201-
def test_select_uneven_list_of_tuples(select_app):
1203+
def test_select_uneven_list_of_tuples(select_app, monkeypatch):
12021204
# Mock out the input call so we don't actually wait for a user's response on stdin
1203-
m = mock.MagicMock(name='input', return_value='2')
1204-
builtins.input = m
1205+
read_input_mock = mock.MagicMock(name='read_input', return_value='2')
1206+
monkeypatch.setattr("cmd2.Cmd.read_input", read_input_mock)
12051207

12061208
out, err = run_cmd(select_app, "play")
12071209
expected = normalize("""
@@ -1211,24 +1213,24 @@ def test_select_uneven_list_of_tuples(select_app):
12111213
""".format('Drums'))
12121214

12131215
# Make sure our mock was called with the expected arguments
1214-
m.assert_called_once_with('Instrument? ')
1216+
read_input_mock.assert_called_once_with('Instrument? ')
12151217

12161218
# And verify the expected output to stdout
12171219
assert out == expected
12181220

1219-
def test_select_eof(select_app):
1221+
def test_select_eof(select_app, monkeypatch):
12201222
# Ctrl-D during select causes an EOFError that just reprompts the user
1221-
m = mock.MagicMock(name='input', side_effect=[EOFError, 2])
1222-
builtins.input = m
1223+
read_input_mock = mock.MagicMock(name='read_input', side_effect=[EOFError, 2])
1224+
monkeypatch.setattr("cmd2.Cmd.read_input", read_input_mock)
12231225

12241226
food = 'fish'
12251227
out, err = run_cmd(select_app, "eat {}".format(food))
12261228

12271229
# Make sure our mock was called exactly twice with the expected arguments
12281230
arg = 'Sauce? '
12291231
calls = [mock.call(arg), mock.call(arg)]
1230-
m.assert_has_calls(calls)
1231-
assert m.call_count == 2
1232+
read_input_mock.assert_has_calls(calls)
1233+
assert read_input_mock.call_count == 2
12321234

12331235
class HelpNoDocstringApp(cmd2.Cmd):
12341236
greet_parser = argparse.ArgumentParser()
@@ -1419,7 +1421,7 @@ def test_echo(capsys):
14191421
out, err = capsys.readouterr()
14201422
assert out.startswith('{}{}\n'.format(app.prompt, commands[0]) + HELP_HISTORY.split()[0])
14211423

1422-
def test_pseudo_raw_input_tty_rawinput_true():
1424+
def test_read_input_tty_rawinput_true():
14231425
# use context managers so original functions get put back when we are done
14241426
# we dont use decorators because we need m_input for the assertion
14251427
with mock.patch('sys.stdin.isatty', mock.MagicMock(name='isatty', return_value=True)):
@@ -1435,7 +1437,7 @@ def test_pseudo_raw_input_tty_rawinput_true():
14351437
# that the rest of it worked
14361438
assert m_input.call_count == 2
14371439

1438-
def test_pseudo_raw_input_tty_rawinput_false():
1440+
def test_read_input_tty_rawinput_false():
14391441
# gin up some input like it's coming from a tty
14401442
fakein = io.StringIO(u'{}'.format('set\n'))
14411443
mtty = mock.MagicMock(name='isatty', return_value=True)
@@ -1468,7 +1470,7 @@ def piped_rawinput_true(capsys, echo, command):
14681470

14691471
# using the decorator puts the original input function back when this unit test returns
14701472
@mock.patch('builtins.input', mock.MagicMock(name='input', side_effect=['set', EOFError]))
1471-
def test_pseudo_raw_input_piped_rawinput_true_echo_true(capsys):
1473+
def test_read_input_piped_rawinput_true_echo_true(capsys):
14721474
command = 'set'
14731475
app, out = piped_rawinput_true(capsys, True, command)
14741476
out = out.splitlines()
@@ -1477,7 +1479,7 @@ def test_pseudo_raw_input_piped_rawinput_true_echo_true(capsys):
14771479

14781480
# using the decorator puts the original input function back when this unit test returns
14791481
@mock.patch('builtins.input', mock.MagicMock(name='input', side_effect=['set', EOFError]))
1480-
def test_pseudo_raw_input_piped_rawinput_true_echo_false(capsys):
1482+
def test_read_input_piped_rawinput_true_echo_false(capsys):
14811483
command = 'set'
14821484
app, out = piped_rawinput_true(capsys, False, command)
14831485
firstline = out.splitlines()[0]
@@ -1495,14 +1497,14 @@ def piped_rawinput_false(capsys, echo, command):
14951497
out, err = capsys.readouterr()
14961498
return app, out
14971499

1498-
def test_pseudo_raw_input_piped_rawinput_false_echo_true(capsys):
1500+
def test_read_input_piped_rawinput_false_echo_true(capsys):
14991501
command = 'set'
15001502
app, out = piped_rawinput_false(capsys, True, command)
15011503
out = out.splitlines()
15021504
assert out[0] == '{}{}'.format(app.prompt, command)
15031505
assert out[1].startswith('allow_ansi:')
15041506

1505-
def test_pseudo_raw_input_piped_rawinput_false_echo_false(capsys):
1507+
def test_read_input_piped_rawinput_false_echo_false(capsys):
15061508
command = 'set'
15071509
app, out = piped_rawinput_false(capsys, False, command)
15081510
firstline = out.splitlines()[0]
@@ -1519,7 +1521,7 @@ def test_raw_input(base_app):
15191521
m = mock.Mock(name='input', return_value=fake_input)
15201522
builtins.input = m
15211523

1522-
line = base_app._pseudo_raw_input('(cmd2)')
1524+
line = base_app.read_input('(cmd2)')
15231525
assert line == fake_input
15241526

15251527
def test_stdin_input():
@@ -1531,7 +1533,7 @@ def test_stdin_input():
15311533
m = mock.Mock(name='readline', return_value=fake_input)
15321534
app.stdin.readline = m
15331535

1534-
line = app._pseudo_raw_input('(cmd2)')
1536+
line = app.read_input('(cmd2)')
15351537
assert line == fake_input
15361538

15371539
def test_empty_stdin_input():
@@ -1543,7 +1545,7 @@ def test_empty_stdin_input():
15431545
m = mock.Mock(name='readline', return_value=fake_input)
15441546
app.stdin.readline = m
15451547

1546-
line = app._pseudo_raw_input('(cmd2)')
1548+
line = app.read_input('(cmd2)')
15471549
assert line == 'eof'
15481550

15491551
def test_poutput_string(outsim_app):

0 commit comments

Comments
 (0)