def test_fail_alt_stderr(parser1, capfd, mock): completer = CompletionFinder() comp_line = 'media movies add ' exp_out = '\013\013 ' exp_err = ''' Hint: TITLE Movie Title''' mock.patch.dict( os.environ, { '_ARGCOMPLETE': '1', '_ARGCOMPLETE_IFS': '\013', 'COMP_TYPE': '63', 'COMP_LINE': comp_line, 'COMP_POINT': str(len(comp_line)) }) mock.patch.object(os, 'fdopen', fdopen_fail_9) with pytest.raises(SystemExit): choices = { 'actor': query_actors, # function } autocompleter = AutoCompleter(parser1, arg_choices=choices) completer(parser1, autocompleter, exit_method=sys.exit) out, err = capfd.readouterr() assert out == exp_out assert err == exp_err
def test_invalid_ifs(parser1, mocker): completer = CompletionFinder() mocker.patch.dict(os.environ, { '_ARGCOMPLETE': '1', '_ARGCOMPLETE_IFS': '\013\013' }) mocker.patch.object(os, 'fdopen', my_fdopen) with pytest.raises(SystemExit): completer(parser1, AutoCompleter(parser1), exit_method=sys.exit)
def test_fail_alt_stdout(parser1, mocker): completer = CompletionFinder() comp_line = 'media movies list ' mocker.patch.dict( os.environ, { '_ARGCOMPLETE': '1', '_ARGCOMPLETE_IFS': '\013', 'COMP_TYPE': '63', 'COMP_LINE': comp_line, 'COMP_POINT': str(len(comp_line)) }) mocker.patch.object(os, 'fdopen', fdopen_fail_8) try: choices = { 'actor': query_actors, # function } autocompleter = AutoCompleter(parser1, arg_choices=choices) completer(parser1, autocompleter, exit_method=sys.exit) except SystemExit as err: assert err.code == 1
def test_commands(parser1, capfd, mock, comp_line, exp_out, exp_err): mock.patch.dict( os.environ, { '_ARGCOMPLETE': '1', '_ARGCOMPLETE_IFS': '\013', 'COMP_TYPE': '63', 'COMP_LINE': comp_line, 'COMP_POINT': str(len(comp_line)) }) mock.patch.object(os, 'fdopen', my_fdopen) with pytest.raises(SystemExit): completer = CompletionFinder() choices = { 'actor': query_actors, # function } autocompleter = AutoCompleter(parser1, arg_choices=choices) completer(parser1, autocompleter, exit_method=sys.exit) out, err = capfd.readouterr() assert out == exp_out assert err == exp_err
def test_bash_nocomplete(parser1): completer = CompletionFinder() result = completer(parser1, AutoCompleter(parser1)) assert result is None
bar_subparsers.add_parser('apple', help='apple help') bar_subparsers.add_parser('artichoke', help='artichoke help') bar_subparsers.add_parser('cranberries', help='cranberries help') # create the parser for the "sport" subcommand parser_sport = base_subparsers.add_parser('sport', help='sport help') sport_arg = parser_sport.add_argument('sport', help='Enter name of a sport') setattr(sport_arg, 'arg_choices', sport_item_strs) # Handle bash completion if it's installed try: # only move forward if we can import CompletionFinder and AutoCompleter from cmd2.argcomplete_bridge import CompletionFinder from cmd2.argparse_completer import AutoCompleter if __name__ == '__main__': completer = CompletionFinder() completer(base_parser, AutoCompleter(base_parser)) except ImportError: pass # Intentionally below the bash completion code to reduce tab completion lag import cmd2 class SubcommandsExample(cmd2.Cmd): """ Example cmd2 application where we a base command which has a couple subcommands and the "sport" subcommand has tab completion enabled. """ def __init__(self): super().__init__()