|
9 | 9 | import pytest |
10 | 10 |
|
11 | 11 | import cmd2 |
12 | | -from cmd2 import with_argparser, Cmd2ArgumentParser, CompletionItem |
| 12 | +from cmd2 import with_argparser, Cmd2ArgumentParser, CompletionError, CompletionItem |
13 | 13 | from cmd2.utils import StdSim, basic_complete |
14 | 14 | from .conftest import run_cmd, complete_tester |
15 | 15 |
|
@@ -210,6 +210,27 @@ def do_nargs(self, args: argparse.Namespace) -> None: |
210 | 210 | def do_hint(self, args: argparse.Namespace) -> None: |
211 | 211 | pass |
212 | 212 |
|
| 213 | + ############################################################################################################ |
| 214 | + # Begin code related to CompletionError |
| 215 | + ############################################################################################################ |
| 216 | + def completer_raise_error(self, text: str, line: str, begidx: int, endidx: int) -> List[str]: |
| 217 | + """Raises CompletionError""" |
| 218 | + raise CompletionError('completer broke something') |
| 219 | + |
| 220 | + def choice_raise_error(self) -> List[str]: |
| 221 | + """Raises CompletionError""" |
| 222 | + raise CompletionError('choice broke something') |
| 223 | + |
| 224 | + comp_error_parser = Cmd2ArgumentParser() |
| 225 | + comp_error_parser.add_argument('completer', help='positional arg', |
| 226 | + completer_method=completer_raise_error) |
| 227 | + comp_error_parser.add_argument('--choice', help='flag arg', |
| 228 | + choices_method=choice_raise_error) |
| 229 | + |
| 230 | + @with_argparser(comp_error_parser) |
| 231 | + def do_raise_completion_error(self, args: argparse.Namespace) -> None: |
| 232 | + pass |
| 233 | + |
213 | 234 | ############################################################################################################ |
214 | 235 | # Begin code related to receiving arg_tokens |
215 | 236 | ############################################################################################################ |
@@ -723,6 +744,25 @@ def test_autocomp_hint_no_help_text(ac_app, capsys): |
723 | 744 | ''' |
724 | 745 |
|
725 | 746 |
|
| 747 | +@pytest.mark.parametrize('args, text', [ |
| 748 | + # Exercise a flag arg and choices function that raises a CompletionError |
| 749 | + ('--choice ', 'choice'), |
| 750 | +
|
| 751 | + # Exercise a positional arg and completer that raises a CompletionError |
| 752 | + ('', 'completer') |
| 753 | +]) |
| 754 | +def test_completion_error(ac_app, capsys, args, text): |
| 755 | + line = 'raise_completion_error {} {}'.format(args, text) |
| 756 | + endidx = len(line) |
| 757 | + begidx = endidx - len(text) |
| 758 | + |
| 759 | + first_match = complete_tester(text, line, begidx, endidx, ac_app) |
| 760 | + out, err = capsys.readouterr() |
| 761 | + |
| 762 | + assert first_match is None |
| 763 | + assert "{} broke something".format(text) in out |
| 764 | + |
| 765 | + |
726 | 766 | @pytest.mark.parametrize('command_and_args, completions', [ |
727 | 767 | # Exercise a choices function that receives arg_tokens dictionary |
728 | 768 | ('arg_tokens choice subcmd', ['choice', 'subcmd']), |
|
0 commit comments