Skip to content
Myr

test_bashcomplete.py

This file

Path

tests/test_bashcomplete.py

409 lines · 14.9 KiB · Python

Last changed by

Merge pull request #1061 from stopthatcow/feature/1058David Lordbefore adoption752c5e4

History of this file

Take it

1# -*- coding: utf-8 -*-23import click4from click._bashcomplete import get_choices567def choices_without_help(cli, args, incomplete):8    completions = get_choices(cli, 'dummy', args, incomplete)9    return [c[0] for c in completions]101112def choices_with_help(cli, args, incomplete):13    return list(get_choices(cli, 'dummy', args, incomplete))141516def test_single_command():17    @click.command()18    @click.option('--local-opt')19    def cli(local_opt):20        pass2122    assert choices_without_help(cli, [], '-') == ['--local-opt']23    assert choices_without_help(cli, [], '') == []242526def test_boolean_flag():27    @click.command()28    @click.option('--shout/--no-shout', default=False)29    def cli(local_opt):30        pass3132    assert choices_without_help(cli, [], '-') == ['--shout', '--no-shout']333435def test_multi_value_option():36    @click.group()37    @click.option('--pos', nargs=2, type=float)38    def cli(local_opt):39        pass4041    @cli.command()42    @click.option('--local-opt')43    def sub(local_opt):44        pass4546    assert choices_without_help(cli, [], '-') == ['--pos']47    assert choices_without_help(cli, ['--pos'], '') == []48    assert choices_without_help(cli, ['--pos', '1.0'], '') == []49    assert choices_without_help(cli, ['--pos', '1.0', '1.0'], '') == ['sub']505152def test_multi_option():53    @click.command()54    @click.option('--message', '-m', multiple=True)55    def cli(local_opt):56        pass5758    assert choices_without_help(cli, [], '-') == ['--message', '-m']59    assert choices_without_help(cli, ['-m'], '') == []606162def test_small_chain():63    @click.group()64    @click.option('--global-opt')65    def cli(global_opt):66        pass6768    @cli.command()69    @click.option('--local-opt')70    def sub(local_opt):71        pass7273    assert choices_without_help(cli, [], '') == ['sub']74    assert choices_without_help(cli, [], '-') == ['--global-opt']75    assert choices_without_help(cli, ['sub'], '') == []76    assert choices_without_help(cli, ['sub'], '-') == ['--local-opt']777879def test_long_chain():80    @click.group('cli')81    @click.option('--cli-opt')82    def cli(cli_opt):83        pass8485    @cli.group('asub')86    @click.option('--asub-opt')87    def asub(asub_opt):88        pass8990    @asub.group('bsub')91    @click.option('--bsub-opt')92    def bsub(bsub_opt):93        pass9495    COLORS = ['red', 'green', 'blue']96    def get_colors(ctx, args, incomplete):97        for c in COLORS:98            if c.startswith(incomplete):99                yield c100101    def search_colors(ctx, args, incomplete):102        for c in COLORS:103            if incomplete in c:104                yield c105106    CSUB_OPT_CHOICES = ['foo', 'bar']107    CSUB_CHOICES = ['bar', 'baz']108    @bsub.command('csub')109    @click.option('--csub-opt', type=click.Choice(CSUB_OPT_CHOICES))110    @click.option('--csub', type=click.Choice(CSUB_CHOICES))111    @click.option('--search-color', autocompletion=search_colors)112    @click.argument('color', autocompletion=get_colors)113    def csub(csub_opt, color):114        pass115116    assert choices_without_help(cli, [], '-') == ['--cli-opt']117    assert choices_without_help(cli, [], '') == ['asub']118    assert choices_without_help(cli, ['asub'], '-') == ['--asub-opt']119    assert choices_without_help(cli, ['asub'], '') == ['bsub']120    assert choices_without_help(cli, ['asub', 'bsub'], '-') == ['--bsub-opt']121    assert choices_without_help(cli, ['asub', 'bsub'], '') == ['csub']122    assert choices_without_help(cli, ['asub', 'bsub', 'csub'], '-') == ['--csub-opt', '--csub', '--search-color']123    assert choices_without_help(cli, ['asub', 'bsub', 'csub', '--csub-opt'], '') == CSUB_OPT_CHOICES124    assert choices_without_help(cli, ['asub', 'bsub', 'csub'], '--csub') == ['--csub-opt', '--csub']125    assert choices_without_help(cli, ['asub', 'bsub', 'csub', '--csub'], '') == CSUB_CHOICES126    assert choices_without_help(cli, ['asub', 'bsub', 'csub', '--csub-opt'], 'f') == ['foo']127    assert choices_without_help(cli, ['asub', 'bsub', 'csub'], '') == COLORS128    assert choices_without_help(cli, ['asub', 'bsub', 'csub'], 'b') == ['blue']129    assert choices_without_help(cli, ['asub', 'bsub', 'csub', '--search-color'], 'een') == ['green']130131132def test_chaining():133    @click.group('cli', chain=True)134    @click.option('--cli-opt')135    @click.argument('arg', type=click.Choice(['cliarg1', 'cliarg2']))136    def cli(cli_opt, arg):137        pass138139    @cli.command()140    @click.option('--asub-opt')141    def asub(asub_opt):142        pass143144    @cli.command(help='bsub help')145    @click.option('--bsub-opt')146    @click.argument('arg', type=click.Choice(['arg1', 'arg2']))147    def bsub(bsub_opt, arg):148        pass149150    @cli.command()151    @click.option('--csub-opt')152    @click.argument('arg', type=click.Choice(['carg1', 'carg2']), default='carg1')153    def csub(csub_opt, arg):154        pass155156    assert choices_without_help(cli, [], '-') == ['--cli-opt']157    assert choices_without_help(cli, [], '') == ['cliarg1', 'cliarg2']158    assert choices_without_help(cli, ['cliarg1', 'asub'], '-') == ['--asub-opt']159    assert choices_without_help(cli, ['cliarg1', 'asub'], '') == ['bsub', 'csub']160    assert choices_without_help(cli, ['cliarg1', 'bsub'], '') == ['arg1', 'arg2']161    assert choices_without_help(cli, ['cliarg1', 'asub', '--asub-opt'], '') == []162    assert choices_without_help(cli, ['cliarg1', 'asub', '--asub-opt', '5', 'bsub'], '-') == ['--bsub-opt']163    assert choices_without_help(cli, ['cliarg1', 'asub', 'bsub'], '-') == ['--bsub-opt']164    assert choices_without_help(cli, ['cliarg1', 'asub', 'csub'], '') == ['carg1', 'carg2']165    assert choices_without_help(cli, ['cliarg1', 'bsub', 'arg1', 'csub'], '') == ['carg1', 'carg2']166    assert choices_without_help(cli, ['cliarg1', 'asub', 'csub'], '-') == ['--csub-opt']167    assert choices_with_help(cli, ['cliarg1', 'asub'], 'b') == [('bsub', 'bsub help')]168169170def test_argument_choice():171    @click.command()172    @click.argument('arg1', required=True, type=click.Choice(['arg11', 'arg12']))173    @click.argument('arg2', type=click.Choice(['arg21', 'arg22']), default='arg21')174    @click.argument('arg3', type=click.Choice(['arg', 'argument']), default='arg')175    def cli():176        pass177178    assert choices_without_help(cli, [], '') == ['arg11', 'arg12']179    assert choices_without_help(cli, [], 'arg') == ['arg11', 'arg12']180    assert choices_without_help(cli, ['arg11'], '') == ['arg21', 'arg22']181    assert choices_without_help(cli, ['arg12', 'arg21'], '') == ['arg', 'argument']182    assert choices_without_help(cli, ['arg12', 'arg21'], 'argu') == ['argument']183184185def test_option_choice():186    @click.command()187    @click.option('--opt1', type=click.Choice(['opt11', 'opt12']), help='opt1 help')188    @click.option('--opt2', type=click.Choice(['opt21', 'opt22']), default='opt21')189    @click.option('--opt3', type=click.Choice(['opt', 'option']))190    def cli():191        pass192193    assert choices_with_help(cli, [], '-') == [('--opt1', 'opt1 help'),194                                               ('--opt2', None),195                                               ('--opt3', None)]196    assert choices_without_help(cli, [], '--opt') == ['--opt1', '--opt2', '--opt3']197    assert choices_without_help(cli, [], '--opt1=') == ['opt11', 'opt12']198    assert choices_without_help(cli, [], '--opt2=') == ['opt21', 'opt22']199    assert choices_without_help(cli, ['--opt2'], '=') == ['opt21', 'opt22']200    assert choices_without_help(cli, ['--opt2', '='], 'opt') == ['opt21', 'opt22']201    assert choices_without_help(cli, ['--opt1'], '') == ['opt11', 'opt12']202    assert choices_without_help(cli, ['--opt2'], '') == ['opt21', 'opt22']203    assert choices_without_help(cli, ['--opt1', 'opt11', '--opt2'], '') == ['opt21', 'opt22']204    assert choices_without_help(cli, ['--opt2', 'opt21'], '-') == ['--opt1', '--opt3']205    assert choices_without_help(cli, ['--opt1', 'opt11'], '-') == ['--opt2', '--opt3']206    assert choices_without_help(cli, ['--opt1'], 'opt') == ['opt11', 'opt12']207    assert choices_without_help(cli, ['--opt3'], 'opti') == ['option']208209    assert choices_without_help(cli, ['--opt1', 'invalid_opt'], '-') == ['--opt2', '--opt3']210211212def test_option_and_arg_choice():213    @click.command()214    @click.option('--opt1', type=click.Choice(['opt11', 'opt12']))215    @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))216    @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))217    def cli():218        pass219220    assert choices_without_help(cli, ['--opt1'], '') == ['opt11', 'opt12']221    assert choices_without_help(cli, [''], '--opt1=') == ['opt11', 'opt12']222    assert choices_without_help(cli, [], '') == ['arg11', 'arg12']223    assert choices_without_help(cli, ['--opt2'], '') == ['opt21', 'opt22']224    assert choices_without_help(cli, ['arg11'], '--opt') == ['--opt1', '--opt2']225    assert choices_without_help(cli, [], '--opt') == ['--opt1', '--opt2']226227228def test_boolean_flag_choice():229    @click.command()230    @click.option('--shout/--no-shout', default=False)231    @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))232    def cli(local_opt):233        pass234235    assert choices_without_help(cli, [], '-') == ['--shout', '--no-shout']236    assert choices_without_help(cli, ['--shout'], '') == ['arg1', 'arg2']237238239def test_multi_value_option_choice():240    @click.command()241    @click.option('--pos', nargs=2, type=click.Choice(['pos1', 'pos2']))242    @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))243    def cli(local_opt):244        pass245246    assert choices_without_help(cli, ['--pos'], '') == ['pos1', 'pos2']247    assert choices_without_help(cli, ['--pos', 'pos1'], '') == ['pos1', 'pos2']248    assert choices_without_help(cli, ['--pos', 'pos1', 'pos2'], '') == ['arg1', 'arg2']249    assert choices_without_help(cli, ['--pos', 'pos1', 'pos2', 'arg1'], '') == []250251252def test_multi_option_choice():253    @click.command()254    @click.option('--message', '-m', multiple=True, type=click.Choice(['m1', 'm2']))255    @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))256    def cli(local_opt):257        pass258259    assert choices_without_help(cli, ['-m'], '') == ['m1', 'm2']260    assert choices_without_help(cli, ['-m', 'm1', '-m'], '') == ['m1', 'm2']261    assert choices_without_help(cli, ['-m', 'm1'], '') == ['arg1', 'arg2']262263264def test_variadic_argument_choice():265    @click.command()266    @click.option('--opt', type=click.Choice(['opt1', 'opt2']))267    @click.argument('src', nargs=-1, type=click.Choice(['src1', 'src2']))268    def cli(local_opt):269        pass270271    assert choices_without_help(cli, ['src1', 'src2'], '') == ['src1', 'src2']272    assert choices_without_help(cli, ['src1', 'src2'], '--o') == ['--opt']273    assert choices_without_help(cli, ['src1', 'src2', '--opt'], '') == ['opt1', 'opt2']274    assert choices_without_help(cli, ['src1', 'src2'], '') == ['src1', 'src2']275276277def test_variadic_argument_complete():278279    def _complete(ctx, args, incomplete):280        return ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz']281282    @click.group()283    def entrypoint():284        pass285286    @click.command()287    @click.option('--opt', autocompletion=_complete)288    @click.argument('arg', nargs=-1)289    def subcommand(opt, arg):290        pass291292    entrypoint.add_command(subcommand)293294    assert choices_without_help(entrypoint, ['subcommand', '--opt'], '') == _complete(0,0,0)295    assert choices_without_help(entrypoint, ['subcommand', 'whatever', '--opt'], '') == _complete(0,0,0)296    assert choices_without_help(entrypoint, ['subcommand', 'whatever', '--opt', 'abc'], '') == []297298299def test_long_chain_choice():300    @click.group()301    def cli():302        pass303304    @cli.group()305    @click.option('--sub-opt', type=click.Choice(['subopt1', 'subopt2']))306    @click.argument('sub-arg', required=False, type=click.Choice(['subarg1', 'subarg2']))307    def sub(sub_opt, sub_arg):308        pass309310    @sub.command(short_help='bsub help')311    @click.option('--bsub-opt', type=click.Choice(['bsubopt1', 'bsubopt2']))312    @click.argument('bsub-arg1', required=False, type=click.Choice(['bsubarg1', 'bsubarg2']))313    @click.argument('bbsub-arg2', required=False, type=click.Choice(['bbsubarg1', 'bbsubarg2']))314    def bsub(bsub_opt):315        pass316317    @sub.group('csub')318    def csub():319        pass320321    @csub.command()322    def dsub():323        pass324325    assert choices_with_help(cli, ['sub', 'subarg1'], '') == [('bsub', 'bsub help'), ('csub', '')]326    assert choices_without_help(cli, ['sub'], '') == ['subarg1', 'subarg2']327    assert choices_without_help(cli, ['sub', '--sub-opt'], '') == ['subopt1', 'subopt2']328    assert choices_without_help(cli, ['sub', '--sub-opt', 'subopt1'], '') == \329           ['subarg1', 'subarg2']330    assert choices_without_help(cli,331                                ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-') == ['--bsub-opt']332    assert choices_without_help(cli,333                                ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '') == ['bsubarg1', 'bsubarg2']334    assert choices_without_help(cli,335                                ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '') == \336           ['bsubopt1', 'bsubopt2']337    assert choices_without_help(cli,338                                ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1'],339                                '') == ['bbsubarg1', 'bbsubarg2']340    assert choices_without_help(cli,341                                ['sub', '--sub-opt', 'subopt1', 'subarg1', 'csub'],342                                '') == ['dsub']343344345def test_chained_multi():346    @click.group()347    def cli():348        pass349350    @cli.group()351    def sub():352        pass353354    @sub.group()355    def bsub():356        pass357358    @sub.group(chain=True)359    def csub():360        pass361362    @csub.command()363    def dsub():364        pass365366    @csub.command()367    def esub():368        pass369370    assert choices_without_help(cli, ['sub'], '') == ['bsub', 'csub']371    assert choices_without_help(cli, ['sub'], 'c') == ['csub']372    assert choices_without_help(cli, ['sub', 'csub'], '') == ['dsub', 'esub']373    assert choices_without_help(cli, ['sub', 'csub', 'dsub'], '') == ['esub']374375376def test_hidden():377    @click.group()378    @click.option('--name', hidden=True)379    @click.option('--choices', type=click.Choice([1, 2]), hidden=True)380    def cli(name):381        pass382383    @cli.group(hidden=True)384    def hgroup():385        pass386387    @hgroup.group()388    def hgroupsub():389        pass390391    @cli.command()392    def asub():393        pass394395    @cli.command(hidden=True)396    @click.option('--hname')397    def hsub():398        pass399400    assert choices_without_help(cli, [], '--n') == []401    assert choices_without_help(cli, [], '--c') == []402    # If the user exactly types out the hidden param, complete its options.403    assert choices_without_help(cli, ['--choices'], '') == [1, 2]404    assert choices_without_help(cli, [], '') == ['asub']405    assert choices_without_help(cli, [], '') == ['asub']406    assert choices_without_help(cli, [], 'h') == []407    # If the user exactly types out the hidden command, complete its subcommands.408    assert choices_without_help(cli, ['hgroup'], '') == ['hgroupsub']409    assert choices_without_help(cli, ['hsub'], '--h') == ['--hname']