summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2020-05-29 17:43:02 -0400
committerGitHub <noreply@github.com>2020-05-29 17:43:02 -0400
commit8d9405a1fcc2169aa039172a8e2891b839a59e6c (patch)
tree53d94a01e7421f8ffba8c8df3ef640c16413b008 /tests
parentd4653e6fccf0bc15d04075110769c11befb22819 (diff)
parent9b91116ec523f1bb27ae8c353a2a7a1dc7975888 (diff)
downloadcmd2-git-8d9405a1fcc2169aa039172a8e2891b839a59e6c.tar.gz
Merge pull request #937 from python-cmd2/exceptions
Exception handling
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):
"""