summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-06-12 11:58:53 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-06-12 11:58:53 -0400
commitbc24624c93d80e64fba9d59455b7bc5a24eb4a1a (patch)
tree9bcff86b2b357ca9d7704e1ab453764705d80074 /tests
parentc0f92d1c3d3a00aa82f0ded091fde1e3a16022e8 (diff)
downloadcmd2-git-bc24624c93d80e64fba9d59455b7bc5a24eb4a1a.tar.gz
Fixed unit tests
Diffstat (limited to 'tests')
-rw-r--r--tests/test_cmd2.py85
-rw-r--r--tests/test_plugin.py44
-rw-r--r--tests/test_transcript.py38
3 files changed, 93 insertions, 74 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 795efcdb..0dc8c7c2 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -743,53 +743,57 @@ def test_base_cmdloop_with_startup_commands():
expected = intro + '\n'
with mock.patch.object(sys, 'argv', testargs):
- # Create a cmd2.Cmd() instance and make sure basic settings are like we want for test
app = CreateOutsimApp()
- app.use_rawinput = True
- # Run the command loop with custom intro
- app.cmdloop(intro=intro)
+ app.use_rawinput = True
+
+ # Run the command loop with custom intro
+ app.cmdloop(intro=intro)
out = app.stdout.getvalue()
assert out == expected
-def test_base_cmdloop_without_startup_commands(outsim_app):
- # Create a cmd2.Cmd() instance and make sure basic settings are like we want for test
- outsim_app.use_rawinput = True
- outsim_app.intro = 'Hello World, this is an intro ...'
+def test_base_cmdloop_without_startup_commands():
+ # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
+ testargs = ["prog"]
+ with mock.patch.object(sys, 'argv', testargs):
+ app = CreateOutsimApp()
+
+ app.use_rawinput = True
+ app.intro = 'Hello World, this is an intro ...'
# Mock out the input call so we don't actually wait for a user's response on stdin
m = mock.MagicMock(name='input', return_value='quit')
builtins.input = m
+ expected = app.intro + '\n'
+
+ # Run the command loop
+ app.cmdloop()
+ out = app.stdout.getvalue()
+ assert out == expected
+
+
+def test_cmdloop_without_rawinput():
# Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
testargs = ["prog"]
- expected = outsim_app.intro + '\n'
with mock.patch.object(sys, 'argv', testargs):
- # Run the command loop
- outsim_app.cmdloop()
- out = outsim_app.stdout.getvalue()
- assert out == expected
-
+ app = CreateOutsimApp()
-def test_cmdloop_without_rawinput(outsim_app):
- # Create a cmd2.Cmd() instance and make sure basic settings are like we want for test
- outsim_app.use_rawinput = False
- outsim_app.echo = False
- outsim_app.intro = 'Hello World, this is an intro ...'
+ app.use_rawinput = False
+ app.echo = False
+ app.intro = 'Hello World, this is an intro ...'
# Mock out the input call so we don't actually wait for a user's response on stdin
m = mock.MagicMock(name='input', return_value='quit')
builtins.input = m
- # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
- testargs = ["prog"]
- expected = outsim_app.intro + '\n'
- with mock.patch.object(sys, 'argv', testargs):
- with pytest.raises(OSError):
- outsim_app.cmdloop()
- out = outsim_app.stdout.getvalue()
+ expected = app.intro + '\n'
+
+ with pytest.raises(OSError):
+ app.cmdloop()
+ out = app.stdout.getvalue()
assert out == expected
class HookFailureApp(cmd2.Cmd):
@@ -1399,7 +1403,7 @@ def test_pseudo_raw_input_tty_rawinput_true():
with mock.patch('sys.stdin.isatty', mock.MagicMock(name='isatty', return_value=True)):
with mock.patch('builtins.input', mock.MagicMock(name='input', side_effect=['set', EOFError])) as m_input:
# run the cmdloop, which should pull input from our mocks
- app = cmd2.Cmd()
+ app = cmd2.Cmd(allow_cli_args=False)
app.use_rawinput = True
app._cmdloop()
# because we mocked the input() call, we won't get the prompt
@@ -1418,7 +1422,7 @@ def test_pseudo_raw_input_tty_rawinput_false():
fakein.readline = mreadline
# run the cmdloop, telling it where to get input from
- app = cmd2.Cmd(stdin=fakein)
+ app = cmd2.Cmd(stdin=fakein, allow_cli_args=False)
app.use_rawinput = False
app._cmdloop()
@@ -1432,7 +1436,7 @@ def test_pseudo_raw_input_tty_rawinput_false():
# the next helper function and two tests check for piped
# input when use_rawinput is True.
def piped_rawinput_true(capsys, echo, command):
- app = cmd2.Cmd()
+ app = cmd2.Cmd(allow_cli_args=False)
app.use_rawinput = True
app.echo = echo
# 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):
# input when use_rawinput=False
def piped_rawinput_false(capsys, echo, command):
fakein = io.StringIO(u'{}'.format(command))
- # run the cmdloop, telling it where to get input from
- app = cmd2.Cmd(stdin=fakein)
+ app = cmd2.Cmd(stdin=fakein, allow_cli_args=False)
app.use_rawinput = False
app.echo = echo
app._cmdloop()
@@ -1931,7 +1934,7 @@ class ReplWithExitCode(cmd2.Cmd):
""" Example cmd2 application where we can specify an exit code when existing."""
def __init__(self):
- super().__init__()
+ super().__init__(allow_cli_args=False)
@cmd2.with_argument_list
def do_exit(self, arg_list) -> bool:
@@ -1963,7 +1966,6 @@ def exit_code_repl():
return app
def test_exit_code_default(exit_code_repl):
- # Create a cmd2.Cmd() instance and make sure basic settings are like we want for test
app = exit_code_repl
app.use_rawinput = True
@@ -1971,17 +1973,14 @@ def test_exit_code_default(exit_code_repl):
m = mock.MagicMock(name='input', return_value='exit')
builtins.input = m
- # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
- testargs = ["prog"]
expected = 'exiting with code: 0\n'
- with mock.patch.object(sys, 'argv', testargs):
- # Run the command loop
- app.cmdloop()
+
+ # Run the command loop
+ app.cmdloop()
out = app.stdout.getvalue()
assert out == expected
def test_exit_code_nonzero(exit_code_repl):
- # Create a cmd2.Cmd() instance and make sure basic settings are like we want for test
app = exit_code_repl
app.use_rawinput = True
@@ -1989,12 +1988,10 @@ def test_exit_code_nonzero(exit_code_repl):
m = mock.MagicMock(name='input', return_value='exit 23')
builtins.input = m
- # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
- testargs = ["prog"]
expected = 'exiting with code: 23\n'
- with mock.patch.object(sys, 'argv', testargs):
- # Run the command loop
- app.cmdloop()
+
+ # Run the command loop
+ app.cmdloop()
out = app.stdout.getvalue()
assert out == expected
diff --git a/tests/test_plugin.py b/tests/test_plugin.py
index ce4eac2b..f7065db5 100644
--- a/tests/test_plugin.py
+++ b/tests/test_plugin.py
@@ -3,8 +3,16 @@
"""
Test plugin infrastructure and hooks.
"""
+import sys
+
import pytest
+# Python 3.5 had some regressions in the unitest.mock module, so use 3rd party mock if available
+try:
+ import mock
+except ImportError:
+ from unittest import mock
+
import cmd2
from cmd2 import plugin
@@ -262,21 +270,27 @@ def test_register_preloop_hook_with_return_annotation():
app.register_preloop_hook(app.prepost_hook_with_wrong_return_annotation)
def test_preloop_hook(capsys):
- app = PluggedApp()
+ # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
+ testargs = ["prog", "say hello", 'quit']
+
+ with mock.patch.object(sys, 'argv', testargs):
+ app = PluggedApp()
+
app.register_preloop_hook(app.prepost_hook_one)
- app._startup_commands.append('say hello')
- app._startup_commands.append('quit')
app.cmdloop()
out, err = capsys.readouterr()
assert out == 'one\nhello\n'
assert not err
def test_preloop_hooks(capsys):
- app = PluggedApp()
+ # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
+ testargs = ["prog", "say hello", 'quit']
+
+ with mock.patch.object(sys, 'argv', testargs):
+ app = PluggedApp()
+
app.register_preloop_hook(app.prepost_hook_one)
app.register_preloop_hook(app.prepost_hook_two)
- app._startup_commands.append('say hello')
- app._startup_commands.append('quit')
app.cmdloop()
out, err = capsys.readouterr()
assert out == 'one\ntwo\nhello\n'
@@ -293,21 +307,27 @@ def test_register_postloop_hook_with_wrong_return_annotation():
app.register_postloop_hook(app.prepost_hook_with_wrong_return_annotation)
def test_postloop_hook(capsys):
- app = PluggedApp()
+ # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
+ testargs = ["prog", "say hello", 'quit']
+
+ with mock.patch.object(sys, 'argv', testargs):
+ app = PluggedApp()
+
app.register_postloop_hook(app.prepost_hook_one)
- app._startup_commands.append('say hello')
- app._startup_commands.append('quit')
app.cmdloop()
out, err = capsys.readouterr()
assert out == 'hello\none\n'
assert not err
def test_postloop_hooks(capsys):
- app = PluggedApp()
+ # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
+ testargs = ["prog", "say hello", 'quit']
+
+ with mock.patch.object(sys, 'argv', testargs):
+ app = PluggedApp()
+
app.register_postloop_hook(app.prepost_hook_one)
app.register_postloop_hook(app.prepost_hook_two)
- app._startup_commands.append('say hello')
- app._startup_commands.append('quit')
app.cmdloop()
out, err = capsys.readouterr()
assert out == 'hello\none\ntwo\n'
diff --git a/tests/test_transcript.py b/tests/test_transcript.py
index 214ef87b..4142e386 100644
--- a/tests/test_transcript.py
+++ b/tests/test_transcript.py
@@ -87,10 +87,11 @@ def test_commands_at_invocation():
expected = "This is an intro banner ...\nhello\nGracie\n"
with mock.patch.object(sys, 'argv', testargs):
app = CmdLineApp()
- app.stdout = StdSim(app.stdout)
- app.cmdloop()
- out = app.stdout.getvalue()
- assert out == expected
+
+ app.stdout = StdSim(app.stdout)
+ app.cmdloop()
+ out = app.stdout.getvalue()
+ assert out == expected
@pytest.mark.parametrize('filename,feedback_to_output', [
('bol_eol.txt', False),
@@ -121,11 +122,12 @@ def test_transcript(request, capsys, filename, feedback_to_output):
# Create a cmd2.Cmd() instance and make sure basic settings are
# like we want for test
app = CmdLineApp()
- app.feedback_to_output = feedback_to_output
- # Run the command loop
- sys_exit_code = app.cmdloop()
- assert sys_exit_code == 0
+ app.feedback_to_output = feedback_to_output
+
+ # Run the command loop
+ sys_exit_code = app.cmdloop()
+ assert sys_exit_code == 0
# Check for the unittest "OK" condition for the 1 test which ran
expected_start = ".\n----------------------------------------------------------------------\nRan 1 test in"
@@ -280,11 +282,12 @@ def test_transcript_failure(request, capsys):
# Create a cmd2.Cmd() instance and make sure basic settings are
# like we want for test
app = CmdLineApp()
- app.feedback_to_output = False
- # Run the command loop
- sys_exit_code = app.cmdloop()
- assert sys_exit_code != 0
+ app.feedback_to_output = False
+
+ # Run the command loop
+ sys_exit_code = app.cmdloop()
+ assert sys_exit_code != 0
expected_start = "File "
expected_end = "s\n\nFAILED (failures=1)\n\n"
@@ -298,14 +301,13 @@ def test_transcript_no_file(request, capsys):
# arguments equal to the py.test args
testargs = ['prog', '-t']
with mock.patch.object(sys, 'argv', testargs):
- # Create a cmd2.Cmd() instance and make sure basic settings are
- # like we want for test
app = CmdLineApp()
- app.feedback_to_output = False
- # Run the command loop
- sys_exit_code = app.cmdloop()
- assert sys_exit_code != 0
+ app.feedback_to_output = False
+
+ # Run the command loop
+ sys_exit_code = app.cmdloop()
+ assert sys_exit_code != 0
# Check for the unittest "OK" condition for the 1 test which ran
expected = 'No test files found - nothing to test\n'