|
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import argparse |
| 8 | +import signal |
8 | 9 | from typing import ( |
9 | 10 | List, |
10 | 11 | ) |
@@ -197,6 +198,9 @@ def test_load_commands(command_sets_manual, capsys): |
197 | 198 | assert command_sets_manual.find_commandsets(CommandSetA)[0] is cmd_set |
198 | 199 | assert command_sets_manual.find_commandset_for_command('elderberry') is cmd_set |
199 | 200 |
|
| 201 | + out = command_sets_manual.app_cmd('apple') |
| 202 | + assert 'Apple!' in out.stdout |
| 203 | + |
200 | 204 | # Make sure registration callbacks ran |
201 | 205 | out, err = capsys.readouterr() |
202 | 206 | assert "in on_register now" in out |
@@ -573,6 +577,38 @@ def test_subcommands(command_sets_manual): |
573 | 577 | command_sets_manual.unregister_command_set(base_cmds) |
574 | 578 |
|
575 | 579 |
|
| 580 | +def test_commandset_sigint(command_sets_manual): |
| 581 | + # shows that the command is able to continue execution if the sigint_handler |
| 582 | + # returns True that we've handled interrupting the command. |
| 583 | + class SigintHandledCommandSet(cmd2.CommandSet): |
| 584 | + def do_foo(self, _): |
| 585 | + self._cmd.poutput('in foo') |
| 586 | + self._cmd.sigint_handler(signal.SIGINT, None) |
| 587 | + self._cmd.poutput('end of foo') |
| 588 | + |
| 589 | + def sigint_handler(self) -> bool: |
| 590 | + return True |
| 591 | + |
| 592 | + cs1 = SigintHandledCommandSet() |
| 593 | + command_sets_manual.register_command_set(cs1) |
| 594 | + out = command_sets_manual.app_cmd('foo') |
| 595 | + assert 'in foo' in out.stdout |
| 596 | + assert 'end of foo' in out.stdout |
| 597 | + |
| 598 | + # shows that the command is interrupted if we don't report we've handled the sigint |
| 599 | + class SigintUnhandledCommandSet(cmd2.CommandSet): |
| 600 | + def do_bar(self, _): |
| 601 | + self._cmd.poutput('in do bar') |
| 602 | + self._cmd.sigint_handler(signal.SIGINT, None) |
| 603 | + self._cmd.poutput('end of do bar') |
| 604 | + |
| 605 | + cs2 = SigintUnhandledCommandSet() |
| 606 | + command_sets_manual.register_command_set(cs2) |
| 607 | + out = command_sets_manual.app_cmd('bar') |
| 608 | + assert 'in do bar' in out.stdout |
| 609 | + assert 'end of do bar' not in out.stdout |
| 610 | + |
| 611 | + |
576 | 612 | def test_nested_subcommands(command_sets_manual): |
577 | 613 | base_cmds = LoadableBase(1) |
578 | 614 | pasta_cmds = LoadablePastaStir(1) |
|
0 commit comments