Skip to content
Myr

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

3 files+24 added−7 removed

modified:click/_bashcomplete.py+4 added-5 removed
36 unmodified lines
37 ctx = cmd.make_context(a[0], a[1:], parent=ctx, resilient_parsing=True)38 return ctx394041def get_choices(cli, prog_name, args, incomplete, cwords, cword):41def get_choices(cli, prog_name, args, incomplete):42 ctx = resolve_ctx(cli, prog_name, args)43 if ctx is None:44 return45
13 unmodified lines
59 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 pass
12 unmodified lines
81 incomplete = cwords[cword]82 except IndexError:83 incomplete = ''8486 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)8788 return True89
8 unmodified lines
modified:examples/bashcompletion/bashcompletion.py+1 added-1 removed
12 unmodified lines
13def cmd1(count, first, last):14 for c in range(count):15 click.echo('Name: %s %s' % (first, last))1617def get_env_vars(ctx, incomplete, cwords, cword):17def get_env_vars(ctx, args, incomplete):18 return os.environ.keys()1920@cli.command()21@click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)
18 unmodified lines
modified:tests/test_bashcomplete.py+19 added-1 removed
12 unmodified lines
13 @click.option('--local-opt')14 def sub(local_opt):15 pass1617 COLORS = ['red', 'green', 'blue']18 @cli.command()19 @click.argument('color', autocompletion=COLORS)20 def sub2(color):21 pass2223 def get_colors(ctx, args, incomplete):24 return COLORS2526 @cli.command()27 @click.argument('color', autocompletion=get_colors)28 def sub3(color):29 pass3017 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']