diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-03-26 15:25:16 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-26 15:25:16 -0400 |
commit | 274a57b7aa56cd6b5dabcdc73326dd4d0e45aae3 (patch) | |
tree | cfd3150073e9f0fce61fdc18d17fb49496e66b8a /tests/test_cmd2.py | |
parent | 990ec45e087aed2a9a6309db893c8a25cb3a89fd (diff) | |
parent | 38b37a98f3affe8632866177195c2c16a3ef88ed (diff) | |
download | cmd2-git-274a57b7aa56cd6b5dabcdc73326dd4d0e45aae3.tar.gz |
Merge pull request #910 from python-cmd2/ctrl-c-script
Ctrl-C now stops a running text script instead of just the current script command
Diffstat (limited to 'tests/test_cmd2.py')
-rwxr-xr-x | tests/test_cmd2.py | 22 |
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') |