summaryrefslogtreecommitdiff
path: root/tests/test_plugin.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-05-28 10:54:10 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-05-28 10:54:10 -0400
commit6f91bea16a00f48ff6be729ed12a5920ebac3baa (patch)
tree1fef77d75b0ee0dbde5158f01230ce07f48bbeb8 /tests/test_plugin.py
parent6460d5707fffc725c50e0642b1e1e2edafa18d87 (diff)
downloadcmd2-git-6f91bea16a00f48ff6be729ed12a5920ebac3baa.tar.gz
Removed pwarning() calls when command raises SystemExit
Added unit tests
Diffstat (limited to 'tests/test_plugin.py')
-rw-r--r--tests/test_plugin.py44
1 files changed, 43 insertions, 1 deletions
diff --git a/tests/test_plugin.py b/tests/test_plugin.py
index f7eb7e39..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
@@ -851,6 +864,35 @@ 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)