summaryrefslogtreecommitdiff
path: root/tests/test_cmd2.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-03-26 14:19:09 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-03-26 14:19:09 -0400
commit8d611e0a90dc1cca4601fcfa7fe88561162e3f26 (patch)
tree0e16e6dbe6b8790aadf9fc5cc9de654fbe23386d /tests/test_cmd2.py
parent662a7842f93c3c89b95697e9a81f277f729e132d (diff)
downloadcmd2-git-8d611e0a90dc1cca4601fcfa7fe88561162e3f26.tar.gz
Add unit tests for Ctrl-C stopping scripts/transcripts
Diffstat (limited to 'tests/test_cmd2.py')
-rwxr-xr-xtests/test_cmd2.py22
1 files changed, 22 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')