summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rwxr-xr-xtests/test_cmd2.py22
-rw-r--r--tests/test_transcript.py9
2 files changed, 31 insertions, 0 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 66ef33de..b86ddfa6 100755
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -376,6 +376,28 @@ set allow_style Never""" % (prefilepath, postfilepath)
out, err = run_cmd(base_app, 'history -s')
assert out == normalize(expected)
+def test_runcmds_plus_hooks_ctrl_c(base_app, capsys):
+ """Test Ctrl-C while in runcmds_plus_hooks"""
+ import types
+
+ def do_keyboard_interrupt(self, _):
+ raise KeyboardInterrupt('Interrupting this command')
+ setattr(base_app, 'do_keyboard_interrupt', types.MethodType(do_keyboard_interrupt, base_app))
+
+ # Default behavior is to stop command loop on Ctrl-C
+ base_app.history.clear()
+ base_app.runcmds_plus_hooks(['help', 'keyboard_interrupt', 'shortcuts'])
+ out, err = capsys.readouterr()
+ assert err.startswith("Interrupting this command")
+ assert len(base_app.history) == 2
+
+ # Ctrl-C should not stop command loop in this case
+ base_app.history.clear()
+ base_app.runcmds_plus_hooks(['help', 'keyboard_interrupt', 'shortcuts'], stop_on_keyboard_interrupt=False)
+ out, err = capsys.readouterr()
+ assert not err
+ assert len(base_app.history) == 3
+
def test_relative_run_script(base_app, request):
test_dir = os.path.dirname(request.module.__file__)
filename = os.path.join(test_dir, 'script.txt')
diff --git a/tests/test_transcript.py b/tests/test_transcript.py
index 64c95b30..aa6f8c4e 100644
--- a/tests/test_transcript.py
+++ b/tests/test_transcript.py
@@ -81,6 +81,9 @@ class CmdLineApp(cmd2.Cmd):
"""Do nothing and output nothing"""
pass
+ def do_keyboard_interrupt(self, _):
+ raise KeyboardInterrupt('Interrupting this command')
+
def test_commands_at_invocation():
testargs = ["prog", "say hello", "say Gracie", "quit"]
@@ -235,6 +238,12 @@ def test_generate_transcript_stop(capsys):
_, err = capsys.readouterr()
assert err.startswith("Command 2 triggered a stop")
+ # keyboard_interrupt command should stop the loop and not run the third command
+ commands = ['help', 'keyboard_interrupt', 'set']
+ app._generate_transcript(commands, transcript_fname)
+ _, err = capsys.readouterr()
+ assert err.startswith("Interrupting this command\nCommand 2 triggered a stop")
+
@pytest.mark.parametrize('expected, transformed', [
# strings with zero or one slash or with escaped slashes means no regular