Updated tests for autocompletion
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nicholas Wiles · committed by Nicholas Wiles · parent 1d0b394 · Browse files · 3 files·+24 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+24 added−7 removed
- tests:+19 added−1 removed
- other:+5 added−6 removed
Provenance
- Commit
dcc0719- Tree
c47d60b- Author
Nicholas Wiles <nwiles@google.com>- Committer
Nicholas Wiles <nwiles@google.com>- Parent
1d0b394
3 files changed+24 added−7 removed
modified:click/_bashcomplete.py+4 added-5 removed
36 unmodified lines37 ctx = cmd.make_context(a[0], a[1:], parent=ctx, resilient_parsing=True)38 return ctx39 40 41-def get_choices(cli, prog_name, args, incomplete, cwords, cword):41+def get_choices(cli, prog_name, args, incomplete):42 ctx = resolve_ctx(cli, prog_name, args)43 if ctx is None:44 return45 13 unmodified lines59 if (value == None) or (isinstance(value, tuple) and (len(value) < param.nargs or param.nargs == -1)):60 try:61 if callable(param.autocompletion):62 choices.extend(param.autocompletion(ctx=ctx,63- incomplete=incomplete,64- cwords=cwords,65- cword=cword))63+ args=args,64+ incomplete=incomplete))65 else:66 choices.extend(param.autocompletion)67 except AttributeError:68 pass12 unmodified lines81 incomplete = cwords[cword]82 except IndexError:83 incomplete = ''84 86- for item in get_choices(cli, prog_name, args, incomplete, cwords, cword):85+ for item in get_choices(cli, prog_name, args, incomplete):86 echo(item)87 88 return True89 8 unmodified linesmodified:examples/bashcompletion/bashcompletion.py+1 added-1 removed
12 unmodified lines13 def cmd1(count, first, last):14 for c in range(count):15 click.echo('Name: %s %s' % (first, last))16 17-def get_env_vars(ctx, incomplete, cwords, cword):17+def get_env_vars(ctx, args, incomplete):18 return os.environ.keys()19 20 @cli.command()21 @click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)18 unmodified linesmodified:tests/test_bashcomplete.py+19 added-1 removed
12 unmodified lines13 @click.option('--local-opt')14 def sub(local_opt):15 pass16 17+ COLORS = ['red', 'green', 'blue']18+ @cli.command()19+ @click.argument('color', autocompletion=COLORS)20+ def sub2(color):21+ pass22+23+ def get_colors(ctx, args, incomplete):24+ return COLORS25+26+ @cli.command()27+ @click.argument('color', autocompletion=get_colors)28+ def sub3(color):29+ pass30+17- assert list(get_choices(cli, 'lol', [], '')) == ['sub']31+ assert list(get_choices(cli, 'lol', [], '')) == ['sub', 'sub2', 'sub3']32 assert list(get_choices(cli, 'lol', [], '-')) == ['--global-opt']33 assert list(get_choices(cli, 'lol', ['sub'], '')) == []34 assert list(get_choices(cli, 'lol', ['sub'], '-')) == ['--local-opt']35+ assert list(get_choices(cli, 'lol', ['sub2'], '')) == COLORS36+ assert list(get_choices(cli, 'lol', ['sub2'], 'g')) == ['green']37+ assert list(get_choices(cli, 'lol', ['sub3'], '')) == COLORS38+ assert list(get_choices(cli, 'lol', ['sub2'], 'b')) == ['blue']