Skip to content

Commit 6f91bea

Browse files
committed
Removed pwarning() calls when command raises SystemExit
Added unit tests
1 parent 6460d57 commit 6f91bea

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

cmd2/cmd2.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,6 @@ def onecmd_plus_hooks(self, line: str, *, add_to_history: bool = True,
16781678
if raise_keyboard_interrupt and not stop:
16791679
raise ex
16801680
except SystemExit:
1681-
self.pwarning("Caught SystemExit. Attempting to stop command loop...")
16821681
stop = True
16831682
except Exception as ex:
16841683
self.pexcept(ex)
@@ -1689,7 +1688,6 @@ def onecmd_plus_hooks(self, line: str, *, add_to_history: bool = True,
16891688
if raise_keyboard_interrupt and not stop:
16901689
raise ex
16911690
except SystemExit:
1692-
self.pwarning("Caught SystemExit. Attempting to stop command loop...")
16931691
stop = True
16941692
except Exception as ex:
16951693
self.pexcept(ex)

tests/test_cmd2.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,17 @@ def hook(self: cmd2.Cmd, data: plugin.CommandFinalizationData) -> plugin.Command
466466

467467
assert "WE ARE IN SCRIPT" in out[-1]
468468

469+
def test_system_exit_in_command(base_app, capsys):
470+
"""Test raising SystemExit from a command"""
471+
import types
472+
473+
def do_system_exit(self, _):
474+
raise SystemExit
475+
setattr(base_app, 'do_system_exit', types.MethodType(do_system_exit, base_app))
476+
477+
stop = base_app.onecmd_plus_hooks('system_exit')
478+
assert stop
479+
469480
def test_output_redirection(base_app):
470481
fd, filename = tempfile.mkstemp(prefix='cmd2_test', suffix='.txt')
471482
os.close(fd)

tests/test_plugin.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,24 @@ def cmdfinalization_hook_exception(self, data: cmd2.plugin.CommandFinalizationDa
222222
self.called_cmdfinalization += 1
223223
raise ValueError
224224

225+
def cmdfinalization_hook_system_exit(self, data: cmd2.plugin.CommandFinalizationData) -> \
226+
cmd2.plugin.CommandFinalizationData:
227+
"""A command finalization hook which raises a SystemExit"""
228+
self.called_cmdfinalization += 1
229+
raise SystemExit
230+
231+
def cmdfinalization_hook_keyboard_interrupt(self, data: cmd2.plugin.CommandFinalizationData) -> \
232+
cmd2.plugin.CommandFinalizationData:
233+
"""A command finalization hook which raises a KeyboardInterrupt"""
234+
self.called_cmdfinalization += 1
235+
raise KeyboardInterrupt
236+
225237
def cmdfinalization_hook_not_enough_parameters(self) -> plugin.CommandFinalizationData:
226238
"""A command finalization hook with no parameters."""
227239
pass
228240

229-
def cmdfinalization_hook_too_many_parameters(self, one: plugin.CommandFinalizationData, two: str) -> plugin.CommandFinalizationData:
241+
def cmdfinalization_hook_too_many_parameters(self, one: plugin.CommandFinalizationData, two: str) -> \
242+
plugin.CommandFinalizationData:
230243
"""A command finalization hook with too many parameters."""
231244
return one
232245

@@ -851,6 +864,35 @@ def test_cmdfinalization_hook_exception(capsys):
851864
assert err
852865
assert app.called_cmdfinalization == 1
853866

867+
def test_cmdfinalization_hook_system_exit(capsys):
868+
app = PluggedApp()
869+
app.register_cmdfinalization_hook(app.cmdfinalization_hook_system_exit)
870+
stop = app.onecmd_plus_hooks('say hello')
871+
assert stop
872+
assert app.called_cmdfinalization == 1
873+
874+
def test_cmdfinalization_hook_keyboard_interrupt(capsys):
875+
app = PluggedApp()
876+
app.register_cmdfinalization_hook(app.cmdfinalization_hook_keyboard_interrupt)
877+
878+
# First make sure KeyboardInterrupt isn't raised unless told to
879+
stop = app.onecmd_plus_hooks('say hello', raise_keyboard_interrupt=False)
880+
assert not stop
881+
assert app.called_cmdfinalization == 1
882+
883+
# Now enable raising the KeyboardInterrupt
884+
app.reset_counters()
885+
with pytest.raises(KeyboardInterrupt):
886+
stop = app.onecmd_plus_hooks('say hello', raise_keyboard_interrupt=True)
887+
assert not stop
888+
assert app.called_cmdfinalization == 1
889+
890+
# Now make sure KeyboardInterrupt isn't raised if stop is already True
891+
app.reset_counters()
892+
stop = app.onecmd_plus_hooks('quit', raise_keyboard_interrupt=True)
893+
assert stop
894+
assert app.called_cmdfinalization == 1
895+
854896
def test_skip_postcmd_hooks(capsys):
855897
app = PluggedApp()
856898
app.register_postcmd_hook(app.postcmd_hook)

0 commit comments

Comments
 (0)