test_bashcomplete.py
This file
Path
tests/test_bashcomplete.py
297 lines · 11.2 KiB · Python
Last changed by
Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
History of this fileTake 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 def cli(cli_opt):136 pass137138 @cli.command()139 @click.option('--asub-opt')140 def asub(asub_opt):141 pass142143 @cli.command(help='bsub help')144 @click.option('--bsub-opt')145 @click.argument('arg', type=click.Choice(['arg1', 'arg2']), required=True)146 def bsub(bsub_opt, arg):147 pass148149 @cli.command()150 @click.option('--csub-opt')151 @click.argument('arg', type=click.Choice(['carg1', 'carg2']), required=False)152 def csub(csub_opt, arg):153 pass154155 assert choices_without_help(cli, [], '-') == ['--cli-opt']156 assert choices_without_help(cli, [], '') == ['asub', 'bsub', 'csub']157 assert choices_without_help(cli, ['asub'], '-') == ['--asub-opt']158 assert choices_without_help(cli, ['asub'], '') == ['bsub', 'csub']159 assert choices_without_help(cli, ['bsub'], '') == ['arg1', 'arg2']160 assert choices_without_help(cli, ['asub', '--asub-opt'], '') == []161 assert choices_without_help(cli, ['asub', '--asub-opt', '5', 'bsub'], '-') == ['--bsub-opt']162 assert choices_without_help(cli, ['asub', 'bsub'], '-') == ['--bsub-opt']163 assert choices_with_help(cli, ['asub'], 'b') == [('bsub', 'bsub help')]164 assert choices_without_help(cli, ['asub', 'csub'], '-') == ['--csub-opt']165166167def test_argument_choice():168 @click.command()169 @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))170 @click.argument('arg2', required=False, type=click.Choice(['arg21', 'arg22']))171 @click.argument('arg3', required=False, type=click.Choice(['arg', 'argument']))172 def cli():173 pass174175 assert choices_without_help(cli, [], '') == ['arg11', 'arg12']176 assert choices_without_help(cli, [], 'arg') == ['arg11', 'arg12']177 assert choices_without_help(cli, ['arg11'], '') == ['arg21', 'arg22']178 assert choices_without_help(cli, ['arg12', 'arg21'], '') == ['arg', 'argument']179 assert choices_without_help(cli, ['arg12', 'arg21'], 'argu') == ['argument']180181182def test_option_choice():183 @click.command()184 @click.option('--opt1', type=click.Choice(['opt11', 'opt12']), help='opt1 help')185 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))186 @click.option('--opt3', type=click.Choice(['opt', 'option']))187 def cli():188 pass189190 assert choices_with_help(cli, [], '-') == [('--opt1', 'opt1 help'),191 ('--opt2', None),192 ('--opt3', None)]193 assert choices_without_help(cli, [], '--opt') == ['--opt1', '--opt2', '--opt3']194 assert choices_without_help(cli, [], '--opt1=') == ['opt11', 'opt12']195 assert choices_without_help(cli, [], '--opt2=') == ['opt21', 'opt22']196 assert choices_without_help(cli, ['--opt2'], '=') == ['opt21', 'opt22']197 assert choices_without_help(cli, ['--opt2', '='], 'opt') == ['opt21', 'opt22']198 assert choices_without_help(cli, ['--opt1'], '') == ['opt11', 'opt12']199 assert choices_without_help(cli, ['--opt2'], '') == ['opt21', 'opt22']200 assert choices_without_help(cli, ['--opt1', 'opt11', '--opt2'], '') == ['opt21', 'opt22']201 assert choices_without_help(cli, ['--opt2', 'opt21'], '-') == ['--opt1', '--opt3']202 assert choices_without_help(cli, ['--opt1', 'opt11'], '-') == ['--opt2', '--opt3']203 assert choices_without_help(cli, ['--opt1'], 'opt') == ['opt11', 'opt12']204 assert choices_without_help(cli, ['--opt3'], 'opti') == ['option']205206 assert choices_without_help(cli, ['--opt1', 'invalid_opt'], '-') == ['--opt2', '--opt3']207208209def test_option_and_arg_choice():210 @click.command()211 @click.option('--opt1', type=click.Choice(['opt11', 'opt12']))212 @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))213 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))214 def cli():215 pass216217 assert choices_without_help(cli, ['--opt1'], '') == ['opt11', 'opt12']218 assert choices_without_help(cli, [''], '--opt1=') == ['opt11', 'opt12']219 assert choices_without_help(cli, [], '') == ['arg11', 'arg12']220 assert choices_without_help(cli, ['--opt2'], '') == ['opt21', 'opt22']221222223def test_boolean_flag_choice():224 @click.command()225 @click.option('--shout/--no-shout', default=False)226 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))227 def cli(local_opt):228 pass229230 assert choices_without_help(cli, [], '-') == ['--shout', '--no-shout']231 assert choices_without_help(cli, ['--shout'], '') == ['arg1', 'arg2']232233234def test_multi_value_option_choice():235 @click.command()236 @click.option('--pos', nargs=2, type=click.Choice(['pos1', 'pos2']))237 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))238 def cli(local_opt):239 pass240241 assert choices_without_help(cli, ['--pos'], '') == ['pos1', 'pos2']242 assert choices_without_help(cli, ['--pos', 'pos1'], '') == ['pos1', 'pos2']243 assert choices_without_help(cli, ['--pos', 'pos1', 'pos2'], '') == ['arg1', 'arg2']244 assert choices_without_help(cli, ['--pos', 'pos1', 'pos2', 'arg1'], '') == []245246247def test_multi_option_choice():248 @click.command()249 @click.option('--message', '-m', multiple=True, type=click.Choice(['m1', 'm2']))250 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))251 def cli(local_opt):252 pass253254 assert choices_without_help(cli, ['-m'], '') == ['m1', 'm2']255 assert choices_without_help(cli, ['-m', 'm1', '-m'], '') == ['m1', 'm2']256 assert choices_without_help(cli, ['-m', 'm1'], '') == ['arg1', 'arg2']257258259def test_variadic_argument_choice():260 @click.command()261 @click.argument('src', nargs=-1, type=click.Choice(['src1', 'src2']))262 def cli(local_opt):263 pass264265 assert choices_without_help(cli, ['src1', 'src2'], '') == ['src1', 'src2']266267268def test_long_chain_choice():269 @click.group()270 def cli():271 pass272273 @cli.group()274 @click.option('--sub-opt', type=click.Choice(['subopt1', 'subopt2']))275 @click.argument('sub-arg', required=False, type=click.Choice(['subarg1', 'subarg2']))276 def sub(sub_opt):277 pass278279 @sub.command(short_help='bsub help')280 @click.option('--bsub-opt', type=click.Choice(['bsubopt1', 'bsubopt2']))281 @click.argument('bsub-arg1', required=False, type=click.Choice(['bsubarg1', 'bsubarg2']))282 @click.argument('bbsub-arg2', required=False, type=click.Choice(['bbsubarg1', 'bbsubarg2']))283 def bsub(bsub_opt):284 pass285286 assert choices_with_help(cli, ['sub'], '') == [('subarg1', None), ('subarg2', None), ('bsub', 'bsub help')]287 assert choices_without_help(cli, ['sub', '--sub-opt'], '') == ['subopt1', 'subopt2']288 assert choices_without_help(cli, ['sub', '--sub-opt', 'subopt1'], '') == \289 ['subarg1', 'subarg2', 'bsub']290 assert choices_without_help(cli,291 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-') == ['--bsub-opt']292 assert choices_without_help(cli,293 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '') == \294 ['bsubopt1', 'bsubopt2']295 assert choices_without_help(cli,296 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1'],297 '') == ['bbsubarg1', 'bbsubarg2']