diff options
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/test_cmd2.py | 11 | ||||
-rw-r--r-- | tests/test_plugin.py | 44 |
2 files changed, 54 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 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) |