test_bashcomplete.py
This file
Path
tests/test_bashcomplete.py
436 lines · 15.6 KiB · Python
Last changed by
Merge pull request #1248 from bertolinocastro/masterDavid Lordbefore adoption29e7de5
History of this fileTake it
1# -*- coding: utf-8 -*-23import pytest45import click6from click._bashcomplete import get_choices789def choices_without_help(cli, args, incomplete):10 completions = get_choices(cli, 'dummy', args, incomplete)11 return [c[0] for c in completions]121314def choices_with_help(cli, args, incomplete):15 return list(get_choices(cli, 'dummy', args, incomplete))161718def test_single_command():19 @click.command()20 @click.option('--local-opt')21 def cli(local_opt):22 pass2324 assert choices_without_help(cli, [], '-') == ['--local-opt']25 assert choices_without_help(cli, [], '') == []262728def test_boolean_flag():29 @click.command()30 @click.option('--shout/--no-shout', default=False)31 def cli(local_opt):32 pass3334 assert choices_without_help(cli, [], '-') == ['--shout', '--no-shout']353637def test_multi_value_option():38 @click.group()39 @click.option('--pos', nargs=2, type=float)40 def cli(local_opt):41 pass4243 @cli.command()44 @click.option('--local-opt')45 def sub(local_opt):46 pass4748 assert choices_without_help(cli, [], '-') == ['--pos']49 assert choices_without_help(cli, ['--pos'], '') == []50 assert choices_without_help(cli, ['--pos', '1.0'], '') == []51 assert choices_without_help(cli, ['--pos', '1.0', '1.0'], '') == ['sub']525354def test_multi_option():55 @click.command()56 @click.option('--message', '-m', multiple=True)57 def cli(local_opt):58 pass5960 assert choices_without_help(cli, [], '-') == ['--message', '-m']61 assert choices_without_help(cli, ['-m'], '') == []626364def test_small_chain():65 @click.group()66 @click.option('--global-opt')67 def cli(global_opt):68 pass6970 @cli.command()71 @click.option('--local-opt')72 def sub(local_opt):73 pass7475 assert choices_without_help(cli, [], '') == ['sub']76 assert choices_without_help(cli, [], '-') == ['--global-opt']77 assert choices_without_help(cli, ['sub'], '') == []78 assert choices_without_help(cli, ['sub'], '-') == ['--local-opt']798081def test_long_chain():82 @click.group('cli')83 @click.option('--cli-opt')84 def cli(cli_opt):85 pass8687 @cli.group('asub')88 @click.option('--asub-opt')89 def asub(asub_opt):90 pass9192 @asub.group('bsub')93 @click.option('--bsub-opt')94 def bsub(bsub_opt):95 pass9697 COLORS = ['red', 'green', 'blue']98 def get_colors(ctx, args, incomplete):99 for c in COLORS:100 if c.startswith(incomplete):101 yield c102103 def search_colors(ctx, args, incomplete):104 for c in COLORS:105 if incomplete in c:106 yield c107108 CSUB_OPT_CHOICES = ['foo', 'bar']109 CSUB_CHOICES = ['bar', 'baz']110 @bsub.command('csub')111 @click.option('--csub-opt', type=click.Choice(CSUB_OPT_CHOICES))112 @click.option('--csub', type=click.Choice(CSUB_CHOICES))113 @click.option('--search-color', autocompletion=search_colors)114 @click.argument('color', autocompletion=get_colors)115 def csub(csub_opt, color):116 pass117118 assert choices_without_help(cli, [], '-') == ['--cli-opt']119 assert choices_without_help(cli, [], '') == ['asub']120 assert choices_without_help(cli, ['asub'], '-') == ['--asub-opt']121 assert choices_without_help(cli, ['asub'], '') == ['bsub']122 assert choices_without_help(cli, ['asub', 'bsub'], '-') == ['--bsub-opt']123 assert choices_without_help(cli, ['asub', 'bsub'], '') == ['csub']124 assert choices_without_help(cli, ['asub', 'bsub', 'csub'], '-') == ['--csub-opt', '--csub', '--search-color']125 assert choices_without_help(cli, ['asub', 'bsub', 'csub', '--csub-opt'], '') == CSUB_OPT_CHOICES126 assert choices_without_help(cli, ['asub', 'bsub', 'csub'], '--csub') == ['--csub-opt', '--csub']127 assert choices_without_help(cli, ['asub', 'bsub', 'csub', '--csub'], '') == CSUB_CHOICES128 assert choices_without_help(cli, ['asub', 'bsub', 'csub', '--csub-opt'], 'f') == ['foo']129 assert choices_without_help(cli, ['asub', 'bsub', 'csub'], '') == COLORS130 assert choices_without_help(cli, ['asub', 'bsub', 'csub'], 'b') == ['blue']131 assert choices_without_help(cli, ['asub', 'bsub', 'csub', '--search-color'], 'een') == ['green']132133134def test_chaining():135 @click.group('cli', chain=True)136 @click.option('--cli-opt')137 @click.argument('arg', type=click.Choice(['cliarg1', 'cliarg2']))138 def cli(cli_opt, arg):139 pass140141 @cli.command()142 @click.option('--asub-opt')143 def asub(asub_opt):144 pass145146 @cli.command(help='bsub help')147 @click.option('--bsub-opt')148 @click.argument('arg', type=click.Choice(['arg1', 'arg2']))149 def bsub(bsub_opt, arg):150 pass151152 @cli.command()153 @click.option('--csub-opt')154 @click.argument('arg', type=click.Choice(['carg1', 'carg2']), default='carg1')155 def csub(csub_opt, arg):156 pass157158 assert choices_without_help(cli, [], '-') == ['--cli-opt']159 assert choices_without_help(cli, [], '') == ['cliarg1', 'cliarg2']160 assert choices_without_help(cli, ['cliarg1', 'asub'], '-') == ['--asub-opt']161 assert choices_without_help(cli, ['cliarg1', 'asub'], '') == ['bsub', 'csub']162 assert choices_without_help(cli, ['cliarg1', 'bsub'], '') == ['arg1', 'arg2']163 assert choices_without_help(cli, ['cliarg1', 'asub', '--asub-opt'], '') == []164 assert choices_without_help(cli, ['cliarg1', 'asub', '--asub-opt', '5', 'bsub'], '-') == ['--bsub-opt']165 assert choices_without_help(cli, ['cliarg1', 'asub', 'bsub'], '-') == ['--bsub-opt']166 assert choices_without_help(cli, ['cliarg1', 'asub', 'csub'], '') == ['carg1', 'carg2']167 assert choices_without_help(cli, ['cliarg1', 'bsub', 'arg1', 'csub'], '') == ['carg1', 'carg2']168 assert choices_without_help(cli, ['cliarg1', 'asub', 'csub'], '-') == ['--csub-opt']169 assert choices_with_help(cli, ['cliarg1', 'asub'], 'b') == [('bsub', 'bsub help')]170171172def test_argument_choice():173 @click.command()174 @click.argument('arg1', required=True, type=click.Choice(['arg11', 'arg12']))175 @click.argument('arg2', type=click.Choice(['arg21', 'arg22']), default='arg21')176 @click.argument('arg3', type=click.Choice(['arg', 'argument']), default='arg')177 def cli():178 pass179180 assert choices_without_help(cli, [], '') == ['arg11', 'arg12']181 assert choices_without_help(cli, [], 'arg') == ['arg11', 'arg12']182 assert choices_without_help(cli, ['arg11'], '') == ['arg21', 'arg22']183 assert choices_without_help(cli, ['arg12', 'arg21'], '') == ['arg', 'argument']184 assert choices_without_help(cli, ['arg12', 'arg21'], 'argu') == ['argument']185186187def test_option_choice():188 @click.command()189 @click.option('--opt1', type=click.Choice(['opt11', 'opt12']), help='opt1 help')190 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']), default='opt21')191 @click.option('--opt3', type=click.Choice(['opt', 'option']))192 def cli():193 pass194195 assert choices_with_help(cli, [], '-') == [('--opt1', 'opt1 help'),196 ('--opt2', None),197 ('--opt3', None)]198 assert choices_without_help(cli, [], '--opt') == ['--opt1', '--opt2', '--opt3']199 assert choices_without_help(cli, [], '--opt1=') == ['opt11', 'opt12']200 assert choices_without_help(cli, [], '--opt2=') == ['opt21', 'opt22']201 assert choices_without_help(cli, ['--opt2'], '=') == ['opt21', 'opt22']202 assert choices_without_help(cli, ['--opt2', '='], 'opt') == ['opt21', 'opt22']203 assert choices_without_help(cli, ['--opt1'], '') == ['opt11', 'opt12']204 assert choices_without_help(cli, ['--opt2'], '') == ['opt21', 'opt22']205 assert choices_without_help(cli, ['--opt1', 'opt11', '--opt2'], '') == ['opt21', 'opt22']206 assert choices_without_help(cli, ['--opt2', 'opt21'], '-') == ['--opt1', '--opt3']207 assert choices_without_help(cli, ['--opt1', 'opt11'], '-') == ['--opt2', '--opt3']208 assert choices_without_help(cli, ['--opt1'], 'opt') == ['opt11', 'opt12']209 assert choices_without_help(cli, ['--opt3'], 'opti') == ['option']210211 assert choices_without_help(cli, ['--opt1', 'invalid_opt'], '-') == ['--opt2', '--opt3']212213214def test_option_and_arg_choice():215 @click.command()216 @click.option('--opt1', type=click.Choice(['opt11', 'opt12']))217 @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))218 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))219 def cli():220 pass221222 assert choices_without_help(cli, ['--opt1'], '') == ['opt11', 'opt12']223 assert choices_without_help(cli, [''], '--opt1=') == ['opt11', 'opt12']224 assert choices_without_help(cli, [], '') == ['arg11', 'arg12']225 assert choices_without_help(cli, ['--opt2'], '') == ['opt21', 'opt22']226 assert choices_without_help(cli, ['arg11'], '--opt') == ['--opt1', '--opt2']227 assert choices_without_help(cli, [], '--opt') == ['--opt1', '--opt2']228229230def test_boolean_flag_choice():231 @click.command()232 @click.option('--shout/--no-shout', default=False)233 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))234 def cli(local_opt):235 pass236237 assert choices_without_help(cli, [], '-') == ['--shout', '--no-shout']238 assert choices_without_help(cli, ['--shout'], '') == ['arg1', 'arg2']239240241def test_multi_value_option_choice():242 @click.command()243 @click.option('--pos', nargs=2, type=click.Choice(['pos1', 'pos2']))244 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))245 def cli(local_opt):246 pass247248 assert choices_without_help(cli, ['--pos'], '') == ['pos1', 'pos2']249 assert choices_without_help(cli, ['--pos', 'pos1'], '') == ['pos1', 'pos2']250 assert choices_without_help(cli, ['--pos', 'pos1', 'pos2'], '') == ['arg1', 'arg2']251 assert choices_without_help(cli, ['--pos', 'pos1', 'pos2', 'arg1'], '') == []252253254def test_multi_option_choice():255 @click.command()256 @click.option('--message', '-m', multiple=True, type=click.Choice(['m1', 'm2']))257 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))258 def cli(local_opt):259 pass260261 assert choices_without_help(cli, ['-m'], '') == ['m1', 'm2']262 assert choices_without_help(cli, ['-m', 'm1', '-m'], '') == ['m1', 'm2']263 assert choices_without_help(cli, ['-m', 'm1'], '') == ['arg1', 'arg2']264265266def test_variadic_argument_choice():267 @click.command()268 @click.option('--opt', type=click.Choice(['opt1', 'opt2']))269 @click.argument('src', nargs=-1, type=click.Choice(['src1', 'src2']))270 def cli(local_opt):271 pass272273 assert choices_without_help(cli, ['src1', 'src2'], '') == ['src1', 'src2']274 assert choices_without_help(cli, ['src1', 'src2'], '--o') == ['--opt']275 assert choices_without_help(cli, ['src1', 'src2', '--opt'], '') == ['opt1', 'opt2']276 assert choices_without_help(cli, ['src1', 'src2'], '') == ['src1', 'src2']277278279def test_variadic_argument_complete():280281 def _complete(ctx, args, incomplete):282 return ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz']283284 @click.group()285 def entrypoint():286 pass287288 @click.command()289 @click.option('--opt', autocompletion=_complete)290 @click.argument('arg', nargs=-1)291 def subcommand(opt, arg):292 pass293294 entrypoint.add_command(subcommand)295296 assert choices_without_help(entrypoint, ['subcommand', '--opt'], '') == _complete(0,0,0)297 assert choices_without_help(entrypoint, ['subcommand', 'whatever', '--opt'], '') == _complete(0,0,0)298 assert choices_without_help(entrypoint, ['subcommand', 'whatever', '--opt', 'abc'], '') == []299300301def test_long_chain_choice():302 @click.group()303 def cli():304 pass305306 @cli.group()307 @click.option('--sub-opt', type=click.Choice(['subopt1', 'subopt2']))308 @click.argument('sub-arg', required=False, type=click.Choice(['subarg1', 'subarg2']))309 def sub(sub_opt, sub_arg):310 pass311312 @sub.command(short_help='bsub help')313 @click.option('--bsub-opt', type=click.Choice(['bsubopt1', 'bsubopt2']))314 @click.argument('bsub-arg1', required=False, type=click.Choice(['bsubarg1', 'bsubarg2']))315 @click.argument('bbsub-arg2', required=False, type=click.Choice(['bbsubarg1', 'bbsubarg2']))316 def bsub(bsub_opt):317 pass318319 @sub.group('csub')320 def csub():321 pass322323 @csub.command()324 def dsub():325 pass326327 assert choices_with_help(cli, ['sub', 'subarg1'], '') == [('bsub', 'bsub help'), ('csub', '')]328 assert choices_without_help(cli, ['sub'], '') == ['subarg1', 'subarg2']329 assert choices_without_help(cli, ['sub', '--sub-opt'], '') == ['subopt1', 'subopt2']330 assert choices_without_help(cli, ['sub', '--sub-opt', 'subopt1'], '') == \331 ['subarg1', 'subarg2']332 assert choices_without_help(cli,333 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-') == ['--bsub-opt']334 assert choices_without_help(cli,335 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '') == ['bsubarg1', 'bsubarg2']336 assert choices_without_help(cli,337 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '') == \338 ['bsubopt1', 'bsubopt2']339 assert choices_without_help(cli,340 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1'],341 '') == ['bbsubarg1', 'bbsubarg2']342 assert choices_without_help(cli,343 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'csub'],344 '') == ['dsub']345346347def test_chained_multi():348 @click.group()349 def cli():350 pass351352 @cli.group()353 def sub():354 pass355356 @sub.group()357 def bsub():358 pass359360 @sub.group(chain=True)361 def csub():362 pass363364 @csub.command()365 def dsub():366 pass367368 @csub.command()369 def esub():370 pass371372 assert choices_without_help(cli, ['sub'], '') == ['bsub', 'csub']373 assert choices_without_help(cli, ['sub'], 'c') == ['csub']374 assert choices_without_help(cli, ['sub', 'csub'], '') == ['dsub', 'esub']375 assert choices_without_help(cli, ['sub', 'csub', 'dsub'], '') == ['esub']376377378def test_hidden():379 @click.group()380 @click.option('--name', hidden=True)381 @click.option('--choices', type=click.Choice([1, 2]), hidden=True)382 def cli(name):383 pass384385 @cli.group(hidden=True)386 def hgroup():387 pass388389 @hgroup.group()390 def hgroupsub():391 pass392393 @cli.command()394 def asub():395 pass396397 @cli.command(hidden=True)398 @click.option('--hname')399 def hsub():400 pass401402 assert choices_without_help(cli, [], '--n') == []403 assert choices_without_help(cli, [], '--c') == []404 # If the user exactly types out the hidden param, complete its options.405 assert choices_without_help(cli, ['--choices'], '') == [1, 2]406 assert choices_without_help(cli, [], '') == ['asub']407 assert choices_without_help(cli, [], '') == ['asub']408 assert choices_without_help(cli, [], 'h') == []409 # If the user exactly types out the hidden command, complete its subcommands.410 assert choices_without_help(cli, ['hgroup'], '') == ['hgroupsub']411 assert choices_without_help(cli, ['hsub'], '--h') == ['--hname']412413414@pytest.mark.parametrize(415 ("args", "part", "expect"),416 [417 ([], "-", ["--opt"]),418 (["value"], "--", ["--opt"]),419 ([], "-o", []),420 (["--opt"], "-o", []),421 (["--"], "", ["name", "-o", "--opt", "--"]),422 (["--"], "--o", ["--opt"]),423 ],424)425def test_args_with_double_dash_complete(args, part, expect):426 def _complete(ctx, args, incomplete):427 values = ["name", "-o", "--opt", "--"]428 return [x for x in values if x.startswith(incomplete)]429430 @click.command()431 @click.option("--opt")432 @click.argument("args", nargs=-1, autocompletion=_complete)433 def cli(opt, args):434 pass435436 assert choices_without_help(cli, args, part) == expect