summaryrefslogtreecommitdiff
path: root/tests/test_cmd2.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-06-14 18:12:39 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-06-14 18:12:39 -0400
commit85dbe035114330a15abd5af6e098ef6948fa6cb0 (patch)
treee087dfa160eba882dcbd623609cab763efbd1ebc /tests/test_cmd2.py
parent3329e5fec21f2cc4479bb3358170e2226885ab3b (diff)
parentddd07f9cd6d72baca1232ae98856cf3b3d564706 (diff)
downloadcmd2-git-85dbe035114330a15abd5af6e098ef6948fa6cb0.tar.gz
Merge branch 'master' into history_fixes
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r--tests/test_cmd2.py259
1 files changed, 130 insertions, 129 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index d5546f93..72643308 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -2,9 +2,6 @@
# flake8: noqa E302
"""
Cmd2 unit/functional testing
-
-Copyright 2016 Federico Ceratto <federico.ceratto@gmail.com>
-Released under MIT license, see LICENSE file
"""
import argparse
import builtins
@@ -28,13 +25,15 @@ from cmd2 import clipboard, constants, utils
from .conftest import run_cmd, normalize, BASE_HELP, BASE_HELP_VERBOSE, \
HELP_HISTORY, SHORTCUTS_TXT, SHOW_TXT, SHOW_LONG
-
-@pytest.fixture
-def outsim_app():
+def CreateOutsimApp():
c = cmd2.Cmd()
c.stdout = utils.StdSim(c.stdout)
return c
+@pytest.fixture
+def outsim_app():
+ return CreateOutsimApp()
+
def test_version(base_app):
assert cmd2.__version__
@@ -112,9 +111,8 @@ def test_base_show_readonly(base_app):
out, err = run_cmd(base_app, 'set -a')
expected = normalize(SHOW_TXT + '\nRead only settings:' + """
Commands may be terminated with: {}
- Arguments at invocation allowed: {}
Output redirection and pipes allowed: {}
-""".format(base_app.statement_parser.terminators, base_app.allow_cli_args, base_app.allow_redirection))
+""".format(base_app.statement_parser.terminators, base_app.allow_redirection))
assert out == expected
@@ -297,17 +295,28 @@ def test_base_load(base_app, request):
test_dir = os.path.dirname(request.module.__file__)
filename = os.path.join(test_dir, 'script.txt')
- assert base_app.cmdqueue == []
assert base_app._script_dir == []
assert base_app._current_script_dir is None
- # Run the load command, which populates the command queue and sets the script directory
- run_cmd(base_app, 'load {}'.format(filename))
+ # Get output out the script
+ script_out, script_err = run_cmd(base_app, 'load {}'.format(filename))
+
+ assert base_app._script_dir == []
+ assert base_app._current_script_dir is None
- assert base_app.cmdqueue == ['help history', 'eos']
- sdir = os.path.dirname(filename)
- assert base_app._script_dir == [sdir]
- assert base_app._current_script_dir == sdir
+ # Now run the commands manually and compare their output to script's
+ with open(filename, encoding='utf-8') as file:
+ script_commands = file.read().splitlines()
+
+ manual_out = []
+ manual_err = []
+ for cmdline in script_commands:
+ out, err = run_cmd(base_app, cmdline)
+ manual_out.extend(out)
+ manual_err.extend(err)
+
+ assert script_out == manual_out
+ assert script_err == manual_err
def test_load_with_empty_args(base_app):
# The way the load command works, we can't directly capture its stdout or stderr
@@ -315,7 +324,6 @@ def test_load_with_empty_args(base_app):
# The load command requires a file path argument, so we should get an error message
assert "the following arguments are required" in err[1]
- assert base_app.cmdqueue == []
def test_load_with_nonexistent_file(base_app, capsys):
@@ -324,7 +332,6 @@ def test_load_with_nonexistent_file(base_app, capsys):
# The load command requires a path to an existing file
assert "does not exist" in err[0]
- assert base_app.cmdqueue == []
def test_load_with_directory(base_app, request):
test_dir = os.path.dirname(request.module.__file__)
@@ -333,7 +340,6 @@ def test_load_with_directory(base_app, request):
out, err = run_cmd(base_app, 'load {}'.format(test_dir))
assert "is not a file" in err[0]
- assert base_app.cmdqueue == []
def test_load_with_empty_file(base_app, request):
test_dir = os.path.dirname(request.module.__file__)
@@ -344,7 +350,6 @@ def test_load_with_empty_file(base_app, request):
# The load command requires non-empty script files
assert "is empty" in err[0]
- assert base_app.cmdqueue == []
def test_load_with_binary_file(base_app, request):
@@ -356,42 +361,45 @@ def test_load_with_binary_file(base_app, request):
# The load command requires non-empty scripts files
assert "is not an ASCII or UTF-8 encoded text file" in err[0]
- assert base_app.cmdqueue == []
def test_load_with_utf8_file(base_app, request):
test_dir = os.path.dirname(request.module.__file__)
filename = os.path.join(test_dir, 'scripts', 'utf8.txt')
- assert base_app.cmdqueue == []
assert base_app._script_dir == []
assert base_app._current_script_dir is None
- # Run the load command, which populates the command queue and sets the script directory
- run_cmd(base_app, 'load {}'.format(filename))
+ # Get output out the script
+ script_out, script_err = run_cmd(base_app, 'load {}'.format(filename))
+
+ assert base_app._script_dir == []
+ assert base_app._current_script_dir is None
- assert base_app.cmdqueue == ['!echo γνωρίζω', 'eos']
- sdir = os.path.dirname(filename)
- assert base_app._script_dir == [sdir]
- assert base_app._current_script_dir == sdir
+ # Now run the commands manually and compare their output to script's
+ with open(filename, encoding='utf-8') as file:
+ script_commands = file.read().splitlines()
+
+ manual_out = []
+ manual_err = []
+ for cmdline in script_commands:
+ out, err = run_cmd(base_app, cmdline)
+ manual_out.extend(out)
+ manual_err.extend(err)
+
+ assert script_out == manual_out
+ assert script_err == manual_err
def test_load_nested_loads(base_app, request):
# Verify that loading a script with nested load commands works correctly,
- # and loads the nested script commands in the correct order. The recursive
- # loads don't happen all at once, but as the commands are interpreted. So,
- # we will need to drain the cmdqueue and inspect the stdout to see if all
- # steps were executed in the expected order.
+ # and loads the nested script commands in the correct order.
test_dir = os.path.dirname(request.module.__file__)
filename = os.path.join(test_dir, 'scripts', 'nested.txt')
- assert base_app.cmdqueue == []
- # Load the top level script and then run the command queue until all
- # commands have been exhausted.
+ # Load the top level script
initial_load = 'load ' + filename
run_cmd(base_app, initial_load)
- while base_app.cmdqueue:
- base_app.onecmd_plus_hooks(base_app.cmdqueue.pop(0))
# Check that the right commands were executed.
expected = """
@@ -407,12 +415,9 @@ set colors Never""" % initial_load
def test_base_runcmds_plus_hooks(base_app, request):
- # Make sure that runcmds_plus_hooks works as intended. I.E. to run multiple
- # commands and process any commands added, by them, to the command queue.
test_dir = os.path.dirname(request.module.__file__)
prefilepath = os.path.join(test_dir, 'scripts', 'precmds.txt')
postfilepath = os.path.join(test_dir, 'scripts', 'postcmds.txt')
- assert base_app.cmdqueue == []
base_app.runcmds_plus_hooks(['load ' + prefilepath,
'help',
@@ -429,27 +434,36 @@ set colors Never""" % (prefilepath, postfilepath)
out, err = run_cmd(base_app, 'history -s')
assert out == normalize(expected)
-
def test_base_relative_load(base_app, request):
test_dir = os.path.dirname(request.module.__file__)
filename = os.path.join(test_dir, 'script.txt')
- assert base_app.cmdqueue == []
assert base_app._script_dir == []
assert base_app._current_script_dir is None
- # Run the load command, which populates the command queue and sets the script directory
- run_cmd(base_app, '_relative_load {}'.format(filename))
+ # Get output out the script
+ script_out, script_err = run_cmd(base_app, 'load {}'.format(filename))
+
+ assert base_app._script_dir == []
+ assert base_app._current_script_dir is None
+
+ # Now run the commands manually and compare their output to script's
+ with open(filename, encoding='utf-8') as file:
+ script_commands = file.read().splitlines()
- assert base_app.cmdqueue == ['help history', 'eos']
- sdir = os.path.dirname(filename)
- assert base_app._script_dir == [sdir]
- assert base_app._current_script_dir == sdir
+ manual_out = []
+ manual_err = []
+ for cmdline in script_commands:
+ out, err = run_cmd(base_app, cmdline)
+ manual_out.extend(out)
+ manual_err.extend(err)
+
+ assert script_out == manual_out
+ assert script_err == manual_err
def test_relative_load_requires_an_argument(base_app):
out, err = run_cmd(base_app, '_relative_load')
assert 'Error: the following arguments' in err[1]
- assert base_app.cmdqueue == []
def test_output_redirection(base_app):
@@ -495,7 +509,11 @@ def test_output_redirection_to_nonexistent_directory(base_app):
assert content == expected
def test_output_redirection_to_too_long_filename(base_app):
- filename = '~/sdkfhksdjfhkjdshfkjsdhfkjsdhfkjdshfkjdshfkjshdfkhdsfkjhewfuihewiufhweiufhiweufhiuewhiuewhfiuwehfiuewhfiuewhfiuewhfiuewhiuewhfiuewhfiuewfhiuwehewiufhewiuhfiweuhfiuwehfiuewfhiuwehiuewfhiuewhiewuhfiuewhfiuwefhewiuhewiufhewiufhewiufhewiufhewiufhewiufhewiufhewiuhewiufhewiufhewiuheiufhiuewheiwufhewiufheiufheiufhieuwhfewiuhfeiufhiuewfhiuewheiwuhfiuewhfiuewhfeiuwfhewiufhiuewhiuewhfeiuwhfiuwehfuiwehfiuehiuewhfieuwfhieufhiuewhfeiuwfhiuefhueiwhfw'
+ filename = '~/sdkfhksdjfhkjdshfkjsdhfkjsdhfkjdshfkjdshfkjshdfkhdsfkjhewfuihewiufhweiufhiweufhiuewhiuewhfiuwehfia' \
+ 'ewhfiuewhfiuewhfiuewhiuewhfiuewhfiuewfhiuwehewiufhewiuhfiweuhfiuwehfiuewfhiuwehiuewfhiuewhiewuhfiueh' \
+ 'fiuwefhewiuhewiufhewiufhewiufhewiufhewiufhewiufhewiufhewiuhewiufhewiufhewiuheiufhiuewheiwufhewiufheu' \
+ 'fheiufhieuwhfewiuhfeiufhiuewfhiuewheiwuhfiuewhfiuewhfeiuwfhewiufhiuewhiuewhfeiuwhfiuwehfuiwehfiuehie' \
+ 'whfieuwfhieufhiuewhfeiuwfhiuefhueiwhfw'
# Verify that writing to a file in a non-existent directory doesn't work
run_cmd(base_app, 'help > {}'.format(filename))
@@ -717,60 +735,66 @@ def test_base_py_interactive(base_app):
m.assert_called_once()
-def test_base_cmdloop_with_queue(outsim_app):
- # Create a cmd2.Cmd() instance and make sure basic settings are like we want for test
- outsim_app.use_rawinput = True
+def test_base_cmdloop_with_startup_commands():
intro = 'Hello World, this is an intro ...'
- outsim_app.cmdqueue.append('quit\n')
# Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
- testargs = ["prog"]
+ testargs = ["prog", 'quit']
expected = intro + '\n'
+
with mock.patch.object(sys, 'argv', testargs):
- # Run the command loop with custom intro
- outsim_app.cmdloop(intro=intro)
- out = outsim_app.stdout.getvalue()
+ app = CreateOutsimApp()
+
+ 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_queue(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):
- # Run the command loop
- outsim_app.cmdloop()
- out = outsim_app.stdout.getvalue()
- assert out == expected
+ expected = app.intro + '\n'
+ with pytest.raises(OSError):
+ app.cmdloop()
+ out = app.stdout.getvalue()
+ assert out == expected
class HookFailureApp(cmd2.Cmd):
def __init__(self, *args, **kwargs):
@@ -807,8 +831,7 @@ class SayApp(cmd2.Cmd):
@pytest.fixture
def say_app():
- app = SayApp()
- app.allow_cli_args = False
+ app = SayApp(allow_cli_args=False)
app.stdout = utils.StdSim(app.stdout)
return app
@@ -820,7 +843,10 @@ def test_interrupt_quit(say_app):
m.side_effect = ['say hello', KeyboardInterrupt(), 'say goodbye', 'eof']
builtins.input = m
- say_app.cmdloop()
+ try:
+ say_app.cmdloop()
+ except KeyboardInterrupt:
+ pass
# And verify the expected output to stdout
out = say_app.stdout.getvalue()
@@ -834,7 +860,10 @@ def test_interrupt_noquit(say_app):
m.side_effect = ['say hello', KeyboardInterrupt(), 'say goodbye', 'eof']
builtins.input = m
- say_app.cmdloop()
+ try:
+ say_app.cmdloop()
+ except KeyboardInterrupt:
+ pass
# And verify the expected output to stdout
out = say_app.stdout.getvalue()
@@ -1372,36 +1401,15 @@ def test_eof(base_app):
# Only thing to verify is that it returns True
assert base_app.do_eof('')
-def test_eos(base_app):
- sdir = 'dummy_dir'
- base_app._script_dir.append(sdir)
- assert len(base_app._script_dir) == 1
-
- # Assert that it does NOT return true
- assert not base_app.do_eos('')
-
- # And make sure it reduced the length of the script dir list
- assert len(base_app._script_dir) == 0
-
def test_echo(capsys):
app = cmd2.Cmd()
- # Turn echo on and pre-stage some commands in the queue, simulating like we are in the middle of a script
app.echo = True
- command = 'help history'
- app.cmdqueue = [command, 'quit', 'eos']
- app._script_dir.append('some_dir')
-
- assert app._current_script_dir is not None
+ commands = ['help history']
- # Run the inner _cmdloop
- app._cmdloop()
+ app.runcmds_plus_hooks(commands)
out, err = capsys.readouterr()
-
- # Check the output
- assert app.cmdqueue == []
- assert app._current_script_dir is None
- assert out.startswith('{}{}\n'.format(app.prompt, command) + HELP_HISTORY.split()[0])
+ assert out.startswith('{}{}\n'.format(app.prompt, commands[0]) + HELP_HISTORY.split()[0])
def test_pseudo_raw_input_tty_rawinput_true():
# use context managers so original functions get put back when we are done
@@ -1409,7 +1417,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
@@ -1428,7 +1436,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()
@@ -1442,7 +1450,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
@@ -1472,8 +1480,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()
@@ -1927,7 +1934,7 @@ def test_onecmd_raw_str_quit(outsim_app):
def test_get_all_commands(base_app):
# Verify that the base app has the expected commands
commands = base_app.get_all_commands()
- expected_commands = ['_relative_load', 'alias', 'edit', 'eof', 'eos', 'help', 'history', 'load', 'macro',
+ expected_commands = ['_relative_load', 'alias', 'edit', 'eof', 'help', 'history', 'load', 'macro',
'py', 'pyscript', 'quit', 'set', 'shell', 'shortcuts']
assert commands == expected_commands
@@ -1941,7 +1948,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:
@@ -1959,8 +1966,8 @@ Usage: exit [exit_code]
self.perror("{} isn't a valid integer exit code".format(arg_list[0]))
self.exit_code = -1
- self._should_quit = True
- return self._STOP_AND_EXIT
+ # Return True to stop the command loop
+ return True
def postloop(self) -> None:
"""Hook method executed once when the cmdloop() method is about to return."""
@@ -1973,7 +1980,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
@@ -1981,17 +1987,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
@@ -1999,12 +2002,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