summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-06-01 00:45:15 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-06-01 00:45:15 -0400
commiteb41d46c1f6c6281e57e8b864fa4500d3509ed55 (patch)
tree0b515e33f78b45f7eefa9dd2dcbe5bcc14baa0ec /tests
parent19a142782bc1272af870cd893fd25fc7f19c9940 (diff)
parent8d9405a1fcc2169aa039172a8e2891b839a59e6c (diff)
downloadcmd2-git-eb41d46c1f6c6281e57e8b864fa4500d3509ed55.tar.gz
Merge branch 'master' into hint_bug
Diffstat (limited to 'tests')
-rwxr-xr-xtests/test_cmd2.py11
-rw-r--r--tests/test_plugin.py59
2 files changed, 69 insertions, 1 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 33f75c9e..bc0e0a94 100755
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -466,6 +466,17 @@ def test_in_script(request):
assert "WE ARE IN SCRIPT" in out[-1]
+def test_system_exit_in_command(base_app, capsys):
+ """Test raising SystemExit from a command"""
+ import types
+
+ def do_system_exit(self, _):
+ raise SystemExit
+ setattr(base_app, 'do_system_exit', types.MethodType(do_system_exit, base_app))
+
+ stop = base_app.onecmd_plus_hooks('system_exit')
+ assert stop
+
def test_output_redirection(base_app):
fd, filename = tempfile.mkstemp(prefix='cmd2_test', suffix='.txt')
os.close(fd)
diff --git a/tests/test_plugin.py b/tests/test_plugin.py
index 132361a6..e49cbbfc 100644
--- a/tests/test_plugin.py
+++ b/tests/test_plugin.py
@@ -222,11 +222,24 @@ class Plugin:
self.called_cmdfinalization += 1
raise ValueError
+ def cmdfinalization_hook_system_exit(self, data: cmd2.plugin.CommandFinalizationData) -> \
+ cmd2.plugin.CommandFinalizationData:
+ """A command finalization hook which raises a SystemExit"""
+ self.called_cmdfinalization += 1
+ raise SystemExit
+
+ def cmdfinalization_hook_keyboard_interrupt(self, data: cmd2.plugin.CommandFinalizationData) -> \
+ cmd2.plugin.CommandFinalizationData:
+ """A command finalization hook which raises a KeyboardInterrupt"""
+ self.called_cmdfinalization += 1
+ raise KeyboardInterrupt
+
def cmdfinalization_hook_not_enough_parameters(self) -> plugin.CommandFinalizationData:
"""A command finalization hook with no parameters."""
pass
- def cmdfinalization_hook_too_many_parameters(self, one: plugin.CommandFinalizationData, two: str) -> plugin.CommandFinalizationData:
+ def cmdfinalization_hook_too_many_parameters(self, one: plugin.CommandFinalizationData, two: str) -> \
+ plugin.CommandFinalizationData:
"""A command finalization hook with too many parameters."""
return one
@@ -256,6 +269,10 @@ class PluggedApp(Plugin, cmd2.Cmd):
"""Repeat back the arguments"""
self.poutput(statement)
+ def do_skip_postcmd_hooks(self, _):
+ self.poutput("In do_skip_postcmd_hooks")
+ raise exceptions.SkipPostcommandHooks
+
parser = Cmd2ArgumentParser(description="Test parser")
parser.add_argument("my_arg", help="some help text")
@@ -847,6 +864,46 @@ def test_cmdfinalization_hook_exception(capsys):
assert err
assert app.called_cmdfinalization == 1
+def test_cmdfinalization_hook_system_exit(capsys):
+ app = PluggedApp()
+ app.register_cmdfinalization_hook(app.cmdfinalization_hook_system_exit)
+ stop = app.onecmd_plus_hooks('say hello')
+ assert stop
+ assert app.called_cmdfinalization == 1
+
+def test_cmdfinalization_hook_keyboard_interrupt(capsys):
+ app = PluggedApp()
+ app.register_cmdfinalization_hook(app.cmdfinalization_hook_keyboard_interrupt)
+
+ # First make sure KeyboardInterrupt isn't raised unless told to
+ stop = app.onecmd_plus_hooks('say hello', raise_keyboard_interrupt=False)
+ assert not stop
+ assert app.called_cmdfinalization == 1
+
+ # Now enable raising the KeyboardInterrupt
+ app.reset_counters()
+ with pytest.raises(KeyboardInterrupt):
+ stop = app.onecmd_plus_hooks('say hello', raise_keyboard_interrupt=True)
+ assert not stop
+ assert app.called_cmdfinalization == 1
+
+ # Now make sure KeyboardInterrupt isn't raised if stop is already True
+ app.reset_counters()
+ stop = app.onecmd_plus_hooks('quit', raise_keyboard_interrupt=True)
+ assert stop
+ assert app.called_cmdfinalization == 1
+
+def test_skip_postcmd_hooks(capsys):
+ app = PluggedApp()
+ app.register_postcmd_hook(app.postcmd_hook)
+ app.register_cmdfinalization_hook(app.cmdfinalization_hook)
+
+ # Cause a SkipPostcommandHooks exception and verify no postcmd stuff runs but cmdfinalization_hook still does
+ app.onecmd_plus_hooks('skip_postcmd_hooks')
+ out, err = capsys.readouterr()
+ assert "In do_skip_postcmd_hooks" in out
+ assert app.called_postcmd == 0
+ assert app.called_cmdfinalization == 1
def test_cmd2_argparse_exception(capsys):
"""