Skip to content

Commit bc24624

Browse files
committed
Fixed unit tests
1 parent c0f92d1 commit bc24624

File tree

3 files changed

+93
-74
lines changed

3 files changed

+93
-74
lines changed

tests/test_cmd2.py

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -743,53 +743,57 @@ def test_base_cmdloop_with_startup_commands():
743743
expected = intro + '\n'
744744

745745
with mock.patch.object(sys, 'argv', testargs):
746-
# Create a cmd2.Cmd() instance and make sure basic settings are like we want for test
747746
app = CreateOutsimApp()
748-
app.use_rawinput = True
749747

750-
# Run the command loop with custom intro
751-
app.cmdloop(intro=intro)
748+
app.use_rawinput = True
749+
750+
# Run the command loop with custom intro
751+
app.cmdloop(intro=intro)
752752

753753
out = app.stdout.getvalue()
754754
assert out == expected
755755

756756

757-
def test_base_cmdloop_without_startup_commands(outsim_app):
758-
# Create a cmd2.Cmd() instance and make sure basic settings are like we want for test
759-
outsim_app.use_rawinput = True
760-
outsim_app.intro = 'Hello World, this is an intro ...'
757+
def test_base_cmdloop_without_startup_commands():
758+
# Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
759+
testargs = ["prog"]
760+
with mock.patch.object(sys, 'argv', testargs):
761+
app = CreateOutsimApp()
762+
763+
app.use_rawinput = True
764+
app.intro = 'Hello World, this is an intro ...'
761765

762766
# Mock out the input call so we don't actually wait for a user's response on stdin
763767
m = mock.MagicMock(name='input', return_value='quit')
764768
builtins.input = m
765769

770+
expected = app.intro + '\n'
771+
772+
# Run the command loop
773+
app.cmdloop()
774+
out = app.stdout.getvalue()
775+
assert out == expected
776+
777+
778+
def test_cmdloop_without_rawinput():
766779
# Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
767780
testargs = ["prog"]
768-
expected = outsim_app.intro + '\n'
769781
with mock.patch.object(sys, 'argv', testargs):
770-
# Run the command loop
771-
outsim_app.cmdloop()
772-
out = outsim_app.stdout.getvalue()
773-
assert out == expected
774-
782+
app = CreateOutsimApp()
775783

776-
def test_cmdloop_without_rawinput(outsim_app):
777-
# Create a cmd2.Cmd() instance and make sure basic settings are like we want for test
778-
outsim_app.use_rawinput = False
779-
outsim_app.echo = False
780-
outsim_app.intro = 'Hello World, this is an intro ...'
784+
app.use_rawinput = False
785+
app.echo = False
786+
app.intro = 'Hello World, this is an intro ...'
781787

782788
# Mock out the input call so we don't actually wait for a user's response on stdin
783789
m = mock.MagicMock(name='input', return_value='quit')
784790
builtins.input = m
785791

786-
# Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
787-
testargs = ["prog"]
788-
expected = outsim_app.intro + '\n'
789-
with mock.patch.object(sys, 'argv', testargs):
790-
with pytest.raises(OSError):
791-
outsim_app.cmdloop()
792-
out = outsim_app.stdout.getvalue()
792+
expected = app.intro + '\n'
793+
794+
with pytest.raises(OSError):
795+
app.cmdloop()
796+
out = app.stdout.getvalue()
793797
assert out == expected
794798

795799
class HookFailureApp(cmd2.Cmd):
@@ -1399,7 +1403,7 @@ def test_pseudo_raw_input_tty_rawinput_true():
13991403
with mock.patch('sys.stdin.isatty', mock.MagicMock(name='isatty', return_value=True)):
14001404
with mock.patch('builtins.input', mock.MagicMock(name='input', side_effect=['set', EOFError])) as m_input:
14011405
# run the cmdloop, which should pull input from our mocks
1402-
app = cmd2.Cmd()
1406+
app = cmd2.Cmd(allow_cli_args=False)
14031407
app.use_rawinput = True
14041408
app._cmdloop()
14051409
# because we mocked the input() call, we won't get the prompt
@@ -1418,7 +1422,7 @@ def test_pseudo_raw_input_tty_rawinput_false():
14181422
fakein.readline = mreadline
14191423

14201424
# run the cmdloop, telling it where to get input from
1421-
app = cmd2.Cmd(stdin=fakein)
1425+
app = cmd2.Cmd(stdin=fakein, allow_cli_args=False)
14221426
app.use_rawinput = False
14231427
app._cmdloop()
14241428

@@ -1432,7 +1436,7 @@ def test_pseudo_raw_input_tty_rawinput_false():
14321436
# the next helper function and two tests check for piped
14331437
# input when use_rawinput is True.
14341438
def piped_rawinput_true(capsys, echo, command):
1435-
app = cmd2.Cmd()
1439+
app = cmd2.Cmd(allow_cli_args=False)
14361440
app.use_rawinput = True
14371441
app.echo = echo
14381442
# run the cmdloop, which should pull input from our mock
@@ -1462,8 +1466,7 @@ def test_pseudo_raw_input_piped_rawinput_true_echo_false(capsys):
14621466
# input when use_rawinput=False
14631467
def piped_rawinput_false(capsys, echo, command):
14641468
fakein = io.StringIO(u'{}'.format(command))
1465-
# run the cmdloop, telling it where to get input from
1466-
app = cmd2.Cmd(stdin=fakein)
1469+
app = cmd2.Cmd(stdin=fakein, allow_cli_args=False)
14671470
app.use_rawinput = False
14681471
app.echo = echo
14691472
app._cmdloop()
@@ -1931,7 +1934,7 @@ class ReplWithExitCode(cmd2.Cmd):
19311934
""" Example cmd2 application where we can specify an exit code when existing."""
19321935

19331936
def __init__(self):
1934-
super().__init__()
1937+
super().__init__(allow_cli_args=False)
19351938

19361939
@cmd2.with_argument_list
19371940
def do_exit(self, arg_list) -> bool:
@@ -1963,38 +1966,32 @@ def exit_code_repl():
19631966
return app
19641967

19651968
def test_exit_code_default(exit_code_repl):
1966-
# Create a cmd2.Cmd() instance and make sure basic settings are like we want for test
19671969
app = exit_code_repl
19681970
app.use_rawinput = True
19691971

19701972
# Mock out the input call so we don't actually wait for a user's response on stdin
19711973
m = mock.MagicMock(name='input', return_value='exit')
19721974
builtins.input = m
19731975

1974-
# Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
1975-
testargs = ["prog"]
19761976
expected = 'exiting with code: 0\n'
1977-
with mock.patch.object(sys, 'argv', testargs):
1978-
# Run the command loop
1979-
app.cmdloop()
1977+
1978+
# Run the command loop
1979+
app.cmdloop()
19801980
out = app.stdout.getvalue()
19811981
assert out == expected
19821982

19831983
def test_exit_code_nonzero(exit_code_repl):
1984-
# Create a cmd2.Cmd() instance and make sure basic settings are like we want for test
19851984
app = exit_code_repl
19861985
app.use_rawinput = True
19871986

19881987
# Mock out the input call so we don't actually wait for a user's response on stdin
19891988
m = mock.MagicMock(name='input', return_value='exit 23')
19901989
builtins.input = m
19911990

1992-
# Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
1993-
testargs = ["prog"]
19941991
expected = 'exiting with code: 23\n'
1995-
with mock.patch.object(sys, 'argv', testargs):
1996-
# Run the command loop
1997-
app.cmdloop()
1992+
1993+
# Run the command loop
1994+
app.cmdloop()
19981995
out = app.stdout.getvalue()
19991996
assert out == expected
20001997

tests/test_plugin.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
"""
44
Test plugin infrastructure and hooks.
55
"""
6+
import sys
7+
68
import pytest
79

10+
# Python 3.5 had some regressions in the unitest.mock module, so use 3rd party mock if available
11+
try:
12+
import mock
13+
except ImportError:
14+
from unittest import mock
15+
816
import cmd2
917
from cmd2 import plugin
1018

@@ -262,21 +270,27 @@ def test_register_preloop_hook_with_return_annotation():
262270
app.register_preloop_hook(app.prepost_hook_with_wrong_return_annotation)
263271

264272
def test_preloop_hook(capsys):
265-
app = PluggedApp()
273+
# Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
274+
testargs = ["prog", "say hello", 'quit']
275+
276+
with mock.patch.object(sys, 'argv', testargs):
277+
app = PluggedApp()
278+
266279
app.register_preloop_hook(app.prepost_hook_one)
267-
app._startup_commands.append('say hello')
268-
app._startup_commands.append('quit')
269280
app.cmdloop()
270281
out, err = capsys.readouterr()
271282
assert out == 'one\nhello\n'
272283
assert not err
273284

274285
def test_preloop_hooks(capsys):
275-
app = PluggedApp()
286+
# Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
287+
testargs = ["prog", "say hello", 'quit']
288+
289+
with mock.patch.object(sys, 'argv', testargs):
290+
app = PluggedApp()
291+
276292
app.register_preloop_hook(app.prepost_hook_one)
277293
app.register_preloop_hook(app.prepost_hook_two)
278-
app._startup_commands.append('say hello')
279-
app._startup_commands.append('quit')
280294
app.cmdloop()
281295
out, err = capsys.readouterr()
282296
assert out == 'one\ntwo\nhello\n'
@@ -293,21 +307,27 @@ def test_register_postloop_hook_with_wrong_return_annotation():
293307
app.register_postloop_hook(app.prepost_hook_with_wrong_return_annotation)
294308

295309
def test_postloop_hook(capsys):
296-
app = PluggedApp()
310+
# Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
311+
testargs = ["prog", "say hello", 'quit']
312+
313+
with mock.patch.object(sys, 'argv', testargs):
314+
app = PluggedApp()
315+
297316
app.register_postloop_hook(app.prepost_hook_one)
298-
app._startup_commands.append('say hello')
299-
app._startup_commands.append('quit')
300317
app.cmdloop()
301318
out, err = capsys.readouterr()
302319
assert out == 'hello\none\n'
303320
assert not err
304321

305322
def test_postloop_hooks(capsys):
306-
app = PluggedApp()
323+
# Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
324+
testargs = ["prog", "say hello", 'quit']
325+
326+
with mock.patch.object(sys, 'argv', testargs):
327+
app = PluggedApp()
328+
307329
app.register_postloop_hook(app.prepost_hook_one)
308330
app.register_postloop_hook(app.prepost_hook_two)
309-
app._startup_commands.append('say hello')
310-
app._startup_commands.append('quit')
311331
app.cmdloop()
312332
out, err = capsys.readouterr()
313333
assert out == 'hello\none\ntwo\n'

tests/test_transcript.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ def test_commands_at_invocation():
8787
expected = "This is an intro banner ...\nhello\nGracie\n"
8888
with mock.patch.object(sys, 'argv', testargs):
8989
app = CmdLineApp()
90-
app.stdout = StdSim(app.stdout)
91-
app.cmdloop()
92-
out = app.stdout.getvalue()
93-
assert out == expected
90+
91+
app.stdout = StdSim(app.stdout)
92+
app.cmdloop()
93+
out = app.stdout.getvalue()
94+
assert out == expected
9495

9596
@pytest.mark.parametrize('filename,feedback_to_output', [
9697
('bol_eol.txt', False),
@@ -121,11 +122,12 @@ def test_transcript(request, capsys, filename, feedback_to_output):
121122
# Create a cmd2.Cmd() instance and make sure basic settings are
122123
# like we want for test
123124
app = CmdLineApp()
124-
app.feedback_to_output = feedback_to_output
125125

126-
# Run the command loop
127-
sys_exit_code = app.cmdloop()
128-
assert sys_exit_code == 0
126+
app.feedback_to_output = feedback_to_output
127+
128+
# Run the command loop
129+
sys_exit_code = app.cmdloop()
130+
assert sys_exit_code == 0
129131

130132
# Check for the unittest "OK" condition for the 1 test which ran
131133
expected_start = ".\n----------------------------------------------------------------------\nRan 1 test in"
@@ -280,11 +282,12 @@ def test_transcript_failure(request, capsys):
280282
# Create a cmd2.Cmd() instance and make sure basic settings are
281283
# like we want for test
282284
app = CmdLineApp()
283-
app.feedback_to_output = False
284285

285-
# Run the command loop
286-
sys_exit_code = app.cmdloop()
287-
assert sys_exit_code != 0
286+
app.feedback_to_output = False
287+
288+
# Run the command loop
289+
sys_exit_code = app.cmdloop()
290+
assert sys_exit_code != 0
288291

289292
expected_start = "File "
290293
expected_end = "s\n\nFAILED (failures=1)\n\n"
@@ -298,14 +301,13 @@ def test_transcript_no_file(request, capsys):
298301
# arguments equal to the py.test args
299302
testargs = ['prog', '-t']
300303
with mock.patch.object(sys, 'argv', testargs):
301-
# Create a cmd2.Cmd() instance and make sure basic settings are
302-
# like we want for test
303304
app = CmdLineApp()
304-
app.feedback_to_output = False
305305

306-
# Run the command loop
307-
sys_exit_code = app.cmdloop()
308-
assert sys_exit_code != 0
306+
app.feedback_to_output = False
307+
308+
# Run the command loop
309+
sys_exit_code = app.cmdloop()
310+
assert sys_exit_code != 0
309311

310312
# Check for the unittest "OK" condition for the 1 test which ran
311313
expected = 'No test files found - nothing to test\n'

0 commit comments

Comments
 (0)