summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-08-28 01:27:05 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-08-28 01:27:05 -0400
commitdf703af969219665b7d672025bf9b26f75a76285 (patch)
tree4843360faf9677cfb0c22d95f1ae0bd100561616 /tests
parent6b5065e77715c6bab45f3a57eb5b97e3cbe4b86d (diff)
parent36b0b75265942fe375545beb7d2d8a2c5f6f63c4 (diff)
downloadcmd2-git-df703af969219665b7d672025bf9b26f75a76285.tar.gz
Merge branch 'master' into 2.0
Diffstat (limited to 'tests')
-rw-r--r--tests/test_argparse.py46
-rwxr-xr-xtests/test_cmd2.py17
2 files changed, 54 insertions, 9 deletions
diff --git a/tests/test_argparse.py b/tests/test_argparse.py
index 20d05bed..7059e9d3 100644
--- a/tests/test_argparse.py
+++ b/tests/test_argparse.py
@@ -289,24 +289,32 @@ class SubcommandApp(cmd2.Cmd):
func = getattr(args, 'func')
func(self, args)
- # Add a subcommand using as_subcommand_to decorator
- has_subcmd_parser = cmd2.Cmd2ArgumentParser(description="Tests as_subcmd_to decorator")
- has_subcmd_subparsers = has_subcmd_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND')
- has_subcmd_subparsers.required = True
+ # Add subcommands using as_subcommand_to decorator
+ has_subcmds_parser = cmd2.Cmd2ArgumentParser(description="Tests as_subcmd_to decorator")
+ has_subcmds_subparsers = has_subcmds_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND')
+ has_subcmds_subparsers.required = True
- @cmd2.with_argparser(has_subcmd_parser)
+ @cmd2.with_argparser(has_subcmds_parser)
def do_test_subcmd_decorator(self, args: argparse.Namespace):
handler = args.cmd2_handler.get()
handler(args)
- subcmd_parser = cmd2.Cmd2ArgumentParser(add_help=False, description="The subcommand")
+ subcmd_parser = cmd2.Cmd2ArgumentParser(description="A subcommand")
- @cmd2.as_subcommand_to('test_subcmd_decorator', 'subcmd', subcmd_parser, help='the subcommand')
+ @cmd2.as_subcommand_to('test_subcmd_decorator', 'subcmd', subcmd_parser, help=subcmd_parser.description.lower())
def subcmd_func(self, args: argparse.Namespace):
- # Make sure printing the Namespace works. The way we originally added get_hander()
- # to it resulted in a RecursionError when printing.
+ # Make sure printing the Namespace works. The way we originally added cmd2_hander to it resulted in a RecursionError.
self.poutput(args)
+ helpless_subcmd_parser = cmd2.Cmd2ArgumentParser(add_help=False, description="A subcommand with no help")
+
+ @cmd2.as_subcommand_to('test_subcmd_decorator', 'helpless_subcmd', helpless_subcmd_parser,
+ help=helpless_subcmd_parser.description.lower())
+ def helpless_subcmd_func(self, args: argparse.Namespace):
+ # Make sure vars(Namespace) works. The way we originally added cmd2_hander to it resulted in a RecursionError.
+ self.poutput(vars(args))
+
+
@pytest.fixture
def subcommand_app():
app = SubcommandApp()
@@ -391,9 +399,29 @@ def test_add_another_subcommand(subcommand_app):
def test_subcmd_decorator(subcommand_app):
+ # Test subcommand that has help option
out, err = run_cmd(subcommand_app, 'test_subcmd_decorator subcmd')
assert out[0].startswith('Namespace(')
+ out, err = run_cmd(subcommand_app, 'help test_subcmd_decorator subcmd')
+ assert out[0] == 'Usage: test_subcmd_decorator subcmd [-h]'
+
+ out, err = run_cmd(subcommand_app, 'test_subcmd_decorator subcmd -h')
+ assert out[0] == 'Usage: test_subcmd_decorator subcmd [-h]'
+
+ # Test subcommand that has no help option
+ out, err = run_cmd(subcommand_app, 'test_subcmd_decorator helpless_subcmd')
+ assert "'subcommand': 'helpless_subcmd'" in out[0]
+
+ out, err = run_cmd(subcommand_app, 'help test_subcmd_decorator helpless_subcmd')
+ assert out[0] == 'Usage: test_subcmd_decorator helpless_subcmd'
+ assert not err
+
+ out, err = run_cmd(subcommand_app, 'test_subcmd_decorator helpless_subcmd -h')
+ assert not out
+ assert err[0] == 'Usage: test_subcmd_decorator [-h] SUBCOMMAND ...'
+ assert err[1] == 'Error: unrecognized arguments: -h'
+
def test_unittest_mock():
from unittest import mock
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 8688e124..3b240e4e 100755
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -217,6 +217,23 @@ def test_shell_last_result(base_app):
run_cmd(base_app, 'shell fake')
assert base_app.last_result is not None
+
+def test_shell_manual_call(base_app):
+ # Verifies crash from Issue #986 doesn't happen
+ cmds = [
+ 'echo "hi"',
+ 'echo "there"',
+ 'echo "cmd2!"'
+ ]
+ cmd = ';'.join(cmds)
+
+ base_app.do_shell(cmd)
+
+ cmd = '&&'.join(cmds)
+
+ base_app.do_shell(cmd)
+
+
def test_base_py(base_app):
# Make sure py can't edit Cmd.py_locals. It used to be that cmd2 was passing its py_locals
# dictionary to the py environment instead of a shallow copy.