@@ -376,6 +376,28 @@ def test_runcmds_plus_hooks(base_app, request):
376376 out , err = run_cmd (base_app , 'history -s' )
377377 assert out == normalize (expected )
378378
379+ def test_runcmds_plus_hooks_ctrl_c (base_app , capsys ):
380+ """Test Ctrl-C while in runcmds_plus_hooks"""
381+ import types
382+
383+ def do_keyboard_interrupt (self , _ ):
384+ raise KeyboardInterrupt ('Interrupting this command' )
385+ setattr (base_app , 'do_keyboard_interrupt' , types .MethodType (do_keyboard_interrupt , base_app ))
386+
387+ # Default behavior is to stop command loop on Ctrl-C
388+ base_app .history .clear ()
389+ base_app .runcmds_plus_hooks (['help' , 'keyboard_interrupt' , 'shortcuts' ])
390+ out , err = capsys .readouterr ()
391+ assert err .startswith ("Interrupting this command" )
392+ assert len (base_app .history ) == 2
393+
394+ # Ctrl-C should not stop command loop in this case
395+ base_app .history .clear ()
396+ base_app .runcmds_plus_hooks (['help' , 'keyboard_interrupt' , 'shortcuts' ], stop_on_keyboard_interrupt = False )
397+ out , err = capsys .readouterr ()
398+ assert not err
399+ assert len (base_app .history ) == 3
400+
379401def test_relative_run_script (base_app , request ):
380402 test_dir = os .path .dirname (request .module .__file__ )
381403 filename = os .path .join (test_dir , 'script.txt' )
0 commit comments