Skip to content

Commit b7b179a

Browse files
committed
Updating unit tests
1 parent 42cc988 commit b7b179a

File tree

2 files changed

+76
-120
lines changed

2 files changed

+76
-120
lines changed

cmd2/cmd2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,14 +2031,14 @@ def enable_completion():
20312031
# Deal with the vagaries of readline and ANSI escape codes
20322032
safe_prompt = rl_make_safe_prompt(prompt)
20332033

2034-
# Check if tab completion should be disabled
20352034
with self.sigint_protection:
2035+
# Check if tab completion should be disabled
20362036
if not allow_completion:
20372037
disable_completion()
20382038
line = input(safe_prompt)
20392039
finally:
2040-
# Check if we need to reenable tab completion
20412040
with self.sigint_protection:
2041+
# Check if we need to re-enable tab completion
20422042
if not allow_completion:
20432043
enable_completion()
20442044
else:

tests/test_cmd2.py

Lines changed: 74 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,132 +1421,88 @@ def test_echo(capsys):
14211421
out, err = capsys.readouterr()
14221422
assert out.startswith('{}{}\n'.format(app.prompt, commands[0]) + HELP_HISTORY.split()[0])
14231423

1424-
def test_read_input_tty_rawinput_true():
1425-
# use context managers so original functions get put back when we are done
1426-
# we dont use decorators because we need m_input for the assertion
1427-
with mock.patch('sys.stdin.isatty', mock.MagicMock(name='isatty', return_value=True)):
1428-
with mock.patch('builtins.input', mock.MagicMock(name='input', side_effect=['set', EOFError])) as m_input:
1429-
# run the cmdloop, which should pull input from our mocks
1430-
app = cmd2.Cmd(allow_cli_args=False)
1431-
app.use_rawinput = True
1432-
app._cmdloop()
1433-
# because we mocked the input() call, we won't get the prompt
1434-
# or the name of the command in the output, so we can't check
1435-
# if its there. We assume that if input got called twice, once
1436-
# for the 'set' command, and once for the 'quit' command,
1437-
# that the rest of it worked
1438-
assert m_input.call_count == 2
1439-
1440-
def test_read_input_tty_rawinput_false():
1441-
# gin up some input like it's coming from a tty
1442-
fakein = io.StringIO(u'{}'.format('set\n'))
1443-
mtty = mock.MagicMock(name='isatty', return_value=True)
1444-
fakein.isatty = mtty
1445-
mreadline = mock.MagicMock(name='readline', wraps=fakein.readline)
1446-
fakein.readline = mreadline
1447-
1448-
# run the cmdloop, telling it where to get input from
1449-
app = cmd2.Cmd(stdin=fakein, allow_cli_args=False)
1450-
app.use_rawinput = False
1451-
app._cmdloop()
1452-
1453-
# because we mocked the readline() call, we won't get the prompt
1454-
# or the name of the command in the output, so we can't check
1455-
# if its there. We assume that if readline() got called twice, once
1456-
# for the 'set' command, and once for the 'quit' command,
1457-
# that the rest of it worked
1458-
assert mreadline.call_count == 2
1459-
1460-
# the next helper function and two tests check for piped
1461-
# input when use_rawinput is True.
1462-
def piped_rawinput_true(capsys, echo, command):
1463-
app = cmd2.Cmd(allow_cli_args=False)
1464-
app.use_rawinput = True
1465-
app.echo = echo
1466-
# run the cmdloop, which should pull input from our mock
1467-
app._cmdloop()
1468-
out, err = capsys.readouterr()
1469-
return app, out
1470-
1471-
# using the decorator puts the original input function back when this unit test returns
1472-
@mock.patch('builtins.input', mock.MagicMock(name='input', side_effect=['set', EOFError]))
1473-
def test_read_input_piped_rawinput_true_echo_true(capsys):
1474-
command = 'set'
1475-
app, out = piped_rawinput_true(capsys, True, command)
1476-
out = out.splitlines()
1477-
assert out[0] == '{}{}'.format(app.prompt, command)
1478-
assert out[1].startswith('allow_ansi:')
1479-
1480-
# using the decorator puts the original input function back when this unit test returns
1481-
@mock.patch('builtins.input', mock.MagicMock(name='input', side_effect=['set', EOFError]))
1482-
def test_read_input_piped_rawinput_true_echo_false(capsys):
1483-
command = 'set'
1484-
app, out = piped_rawinput_true(capsys, False, command)
1485-
firstline = out.splitlines()[0]
1486-
assert firstline.startswith('allow_ansi:')
1487-
assert not '{}{}'.format(app.prompt, command) in out
1488-
1489-
# the next helper function and two tests check for piped
1490-
# input when use_rawinput=False
1491-
def piped_rawinput_false(capsys, echo, command):
1492-
fakein = io.StringIO(u'{}'.format(command))
1493-
app = cmd2.Cmd(stdin=fakein, allow_cli_args=False)
1494-
app.use_rawinput = False
1495-
app.echo = echo
1496-
app._cmdloop()
1497-
out, err = capsys.readouterr()
1498-
return app, out
1499-
1500-
def test_read_input_piped_rawinput_false_echo_true(capsys):
1501-
command = 'set'
1502-
app, out = piped_rawinput_false(capsys, True, command)
1503-
out = out.splitlines()
1504-
assert out[0] == '{}{}'.format(app.prompt, command)
1505-
assert out[1].startswith('allow_ansi:')
1506-
1507-
def test_read_input_piped_rawinput_false_echo_false(capsys):
1508-
command = 'set'
1509-
app, out = piped_rawinput_false(capsys, False, command)
1510-
firstline = out.splitlines()[0]
1511-
assert firstline.startswith('allow_ansi:')
1512-
assert not '{}{}'.format(app.prompt, command) in out
1424+
def test_read_input_rawinput_true(capsys, monkeypatch):
1425+
prompt_str = 'the_prompt'
1426+
input_str = 'some input'
15131427

1514-
1515-
# other input tests
1516-
def test_raw_input(base_app):
1517-
base_app.use_raw_input = True
1518-
fake_input = 'quit'
1519-
1520-
# Mock out the input call so we don't actually wait for a user's response on stdin
1521-
m = mock.Mock(name='input', return_value=fake_input)
1522-
builtins.input = m
1523-
1524-
line = base_app.read_input('(cmd2)')
1525-
assert line == fake_input
1526-
1527-
def test_stdin_input():
15281428
app = cmd2.Cmd()
1529-
app.use_rawinput = False
1530-
fake_input = 'quit'
1429+
app.use_rawinput = True
15311430

1532-
# Mock out the readline call so we don't actually read from stdin
1533-
m = mock.Mock(name='readline', return_value=fake_input)
1534-
app.stdin.readline = m
1431+
# Mock out input() to return input_str
1432+
monkeypatch.setattr("builtins.input", lambda *args: input_str)
15351433

1536-
line = app.read_input('(cmd2)')
1537-
assert line == fake_input
1434+
# isatty is True
1435+
with mock.patch('sys.stdin.isatty', mock.MagicMock(name='isatty', return_value=True)):
1436+
line = app.read_input(prompt_str)
1437+
assert line == input_str
1438+
1439+
# isatty is False
1440+
with mock.patch('sys.stdin.isatty', mock.MagicMock(name='isatty', return_value=False)):
1441+
# echo True
1442+
app.echo = True
1443+
line = app.read_input(prompt_str)
1444+
out, err = capsys.readouterr()
1445+
assert line == input_str
1446+
assert out == "{}{}\n".format(prompt_str, input_str)
1447+
1448+
# echo False
1449+
app.echo = False
1450+
line = app.read_input(prompt_str)
1451+
out, err = capsys.readouterr()
1452+
assert line == input_str
1453+
assert not out
1454+
1455+
def test_read_input_rawinput_false(capsys, monkeypatch):
1456+
prompt_str = 'the_prompt'
1457+
input_str = 'some input'
1458+
1459+
def make_app(isatty: bool, empty_input: bool = False):
1460+
"""Make a cmd2 app with a custom stdin"""
1461+
app_input_str = '' if empty_input else input_str
1462+
1463+
fakein = io.StringIO('{}'.format(app_input_str))
1464+
fakein.isatty = mock.MagicMock(name='isatty', return_value=isatty)
1465+
1466+
new_app = cmd2.Cmd(stdin=fakein)
1467+
new_app.use_rawinput = False
1468+
return new_app
1469+
1470+
# isatty True
1471+
app = make_app(isatty=True)
1472+
line = app.read_input(prompt_str)
1473+
out, err = capsys.readouterr()
1474+
assert line == input_str
1475+
assert out == prompt_str
15381476

1539-
def test_empty_stdin_input():
1540-
app = cmd2.Cmd()
1541-
app.use_rawinput = False
1542-
fake_input = ''
1477+
# isatty True, empty input
1478+
app = make_app(isatty=True, empty_input=True)
1479+
line = app.read_input(prompt_str)
1480+
out, err = capsys.readouterr()
1481+
assert line == 'eof'
1482+
assert out == prompt_str
15431483

1544-
# Mock out the readline call so we don't actually read from stdin
1545-
m = mock.Mock(name='readline', return_value=fake_input)
1546-
app.stdin.readline = m
1484+
# isatty is False, echo is True
1485+
app = make_app(isatty=False)
1486+
app.echo = True
1487+
line = app.read_input(prompt_str)
1488+
out, err = capsys.readouterr()
1489+
assert line == input_str
1490+
assert out == "{}{}\n".format(prompt_str, input_str)
15471491

1548-
line = app.read_input('(cmd2)')
1492+
# isatty is False, echo is False
1493+
app = make_app(isatty=False)
1494+
app.echo = False
1495+
line = app.read_input(prompt_str)
1496+
out, err = capsys.readouterr()
1497+
assert line == input_str
1498+
assert not out
1499+
1500+
# isatty is False, empty input
1501+
app = make_app(isatty=False, empty_input=True)
1502+
line = app.read_input(prompt_str)
1503+
out, err = capsys.readouterr()
15491504
assert line == 'eof'
1505+
assert not out
15501506

15511507
def test_poutput_string(outsim_app):
15521508
msg = 'This is a test'

0 commit comments

Comments
 (0)