summaryrefslogtreecommitdiff
path: root/tests/test_cmd2.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2018-09-01 11:50:37 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2018-09-01 11:50:37 -0400
commit610aad33eb8fe1772480e98af8b255bd56dfe78c (patch)
treeb9dfb60fa0a60b438c8b8e31679f6df768f24665 /tests/test_cmd2.py
parent20f4a52399c5c1ee87ae57b9f082113663b20060 (diff)
parent93d40a4a486ae6121858f9fb7369ed272a768672 (diff)
downloadcmd2-git-610aad33eb8fe1772480e98af8b255bd56dfe78c.tar.gz
Merge branch 'quoted_args' of github.com:python-cmd2/cmd2 into quoted_args
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r--tests/test_cmd2.py83
1 files changed, 78 insertions, 5 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 13b53224..3aeb9959 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -62,7 +62,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')
@@ -71,10 +71,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
@@ -1939,3 +1937,78 @@ 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