From c50db52da00f4e544a6b3a19ee5b0f54e8503914 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 7 May 2020 14:32:29 -0400 Subject: Added SkipPostcommandHooks exception and made Cmd2ArgparseError inherit from it. Both exception classes have been added to the public API. --- tests/test_plugin.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'tests') diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 132361a6..f7eb7e39 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -256,6 +256,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 +851,17 @@ def test_cmdfinalization_hook_exception(capsys): assert err 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): """ -- cgit v1.2.1 From 6f91bea16a00f48ff6be729ed12a5920ebac3baa Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 28 May 2020 10:54:10 -0400 Subject: Removed pwarning() calls when command raises SystemExit Added unit tests --- tests/test_cmd2.py | 11 +++++++++++ tests/test_plugin.py | 44 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) (limited to 'tests') 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) -- cgit v1.2.1