summaryrefslogtreecommitdiff
path: root/tests/test_cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-09-04 22:50:09 -0700
committerTodd Leonhardt <todd.leonhardt@gmail.com>2018-09-04 22:50:09 -0700
commit59969dd42694d4b1d11cd1be11adf2d6db41df55 (patch)
tree6f09b671b0f8af98684ca008a0ff8aeceeeb9ea1 /tests/test_cmd2.py
parent1daf5d2cb63145408520f39c799425a82b2ec948 (diff)
parent1641f54458686eace59bb195fda0e78fb2ecb83f (diff)
downloadcmd2-git-59969dd42694d4b1d11cd1be11adf2d6db41df55.tar.gz
Merged master in and resolved conflicts
@kotfu - I hope this doesn't screw you up, but I wanted to minimize some of the merge pain that will likely be coming up soon
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r--tests/test_cmd2.py96
1 files changed, 91 insertions, 5 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index efdfee7e..85e6c2f8 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -63,7 +63,7 @@ def test_base_argparse_help(base_app, capsys):
out2 = run_cmd(base_app, 'help set')
assert out1 == out2
- assert out1[0].startswith('usage: set')
+ assert out1[0].startswith('Usage: set')
assert out1[1] == ''
assert out1[2].startswith('Sets a settable parameter')
@@ -72,10 +72,8 @@ def test_base_invalid_option(base_app, capsys):
out, err = capsys.readouterr()
out = normalize(out)
err = normalize(err)
- assert len(err) == 3
- assert len(out) == 15
assert 'Error: unrecognized arguments: -z' in err[0]
- assert out[0] == 'usage: set [-h] [-a] [-l] [settable [settable ...]]'
+ assert out[0] == 'Usage: set settable{0..2} [-h] [-a] [-l]'
def test_base_shortcuts(base_app):
out = run_cmd(base_app, 'shortcuts')
@@ -1252,7 +1250,7 @@ load Runs commands in script file that is encoded as either ASCII
py Invoke python command, shell, or script
pyscript Runs a python script file inside the console
quit Exits this application.
-set Sets a settable parameter or shows current settings of parameters.
+set Sets a settable parameter or shows current settings of parameters
shell Execute a command as if at the OS prompt.
shortcuts Lists shortcuts (aliases) available.
unalias Unsets aliases
@@ -1918,6 +1916,94 @@ def test_bad_history_file_path(capsys, request):
assert 'readline cannot read' in err
+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', 'py', 'pyscript',
+ 'quit', 'set', 'shell', 'shortcuts', 'unalias']
+ assert commands == expected_commands
+
+def test_get_help_topics(base_app):
+ # Verify that the base app has no additional help_foo methods
+ custom_help = base_app.get_help_topics()
+ assert len(custom_help) == 0
+
+
+class ReplWithExitCode(cmd2.Cmd):
+ """ Example cmd2 application where we can specify an exit code when existing."""
+
+ def __init__(self):
+ super().__init__()
+
+ @cmd2.with_argument_list
+ def do_exit(self, arg_list) -> bool:
+ """Exit the application with an optional exit code.
+
+Usage: exit [exit_code]
+ Where:
+ * exit_code - integer exit code to return to the shell
+"""
+ # If an argument was provided
+ if arg_list:
+ try:
+ self.exit_code = int(arg_list[0])
+ except ValueError:
+ 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
+
+ def postloop(self) -> None:
+ """Hook method executed once when the cmdloop() method is about to return."""
+ code = self.exit_code if self.exit_code is not None else 0
+ self.poutput('exiting with code: {}'.format(code))
+
+@pytest.fixture
+def exit_code_repl():
+ app = ReplWithExitCode()
+ 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
+ app.stdout = StdOut()
+
+ # 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='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()
+ out = app.stdout.buffer
+ 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
+ app.stdout = StdOut()
+
+ # 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='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
+ with pytest.raises(SystemExit):
+ app.cmdloop()
+ out = app.stdout.buffer
+ assert out == expected
+
+
class ColorsApp(cmd2.Cmd):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)