test_bashcomplete.py
This file
Path
tests/test_bashcomplete.py
500 lines · 15.8 KiB · Python
Last changed by
apply blackDavid Lordbefore adoption93ba3ba
History of this fileTake it
1# -*- coding: utf-8 -*-2import pytest34import click5from click._bashcomplete import get_choices678def choices_without_help(cli, args, incomplete):9 completions = get_choices(cli, "dummy", args, incomplete)10 return [c[0] for c in completions]111213def choices_with_help(cli, args, incomplete):14 return list(get_choices(cli, "dummy", args, incomplete))151617def test_single_command():18 @click.command()19 @click.option("--local-opt")20 def cli(local_opt):21 pass2223 assert choices_without_help(cli, [], "-") == ["--local-opt"]24 assert choices_without_help(cli, [], "") == []252627def test_boolean_flag():28 @click.command()29 @click.option("--shout/--no-shout", default=False)30 def cli(local_opt):31 pass3233 assert choices_without_help(cli, [], "-") == ["--shout", "--no-shout"]343536def test_multi_value_option():37 @click.group()38 @click.option("--pos", nargs=2, type=float)39 def cli(local_opt):40 pass4142 @cli.command()43 @click.option("--local-opt")44 def sub(local_opt):45 pass4647 assert choices_without_help(cli, [], "-") == ["--pos"]48 assert choices_without_help(cli, ["--pos"], "") == []49 assert choices_without_help(cli, ["--pos", "1.0"], "") == []50 assert choices_without_help(cli, ["--pos", "1.0", "1.0"], "") == ["sub"]515253def test_multi_option():54 @click.command()55 @click.option("--message", "-m", multiple=True)56 def cli(local_opt):57 pass5859 assert choices_without_help(cli, [], "-") == ["--message", "-m"]60 assert choices_without_help(cli, ["-m"], "") == []616263def test_small_chain():64 @click.group()65 @click.option("--global-opt")66 def cli(global_opt):67 pass6869 @cli.command()70 @click.option("--local-opt")71 def sub(local_opt):72 pass7374 assert choices_without_help(cli, [], "") == ["sub"]75 assert choices_without_help(cli, [], "-") == ["--global-opt"]76 assert choices_without_help(cli, ["sub"], "") == []77 assert choices_without_help(cli, ["sub"], "-") == ["--local-opt"]787980def test_long_chain():81 @click.group("cli")82 @click.option("--cli-opt")83 def cli(cli_opt):84 pass8586 @cli.group("asub")87 @click.option("--asub-opt")88 def asub(asub_opt):89 pass9091 @asub.group("bsub")92 @click.option("--bsub-opt")93 def bsub(bsub_opt):94 pass9596 COLORS = ["red", "green", "blue"]9798 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"]110111 @bsub.command("csub")112 @click.option("--csub-opt", type=click.Choice(CSUB_OPT_CHOICES))113 @click.option("--csub", type=click.Choice(CSUB_CHOICES))114 @click.option("--search-color", autocompletion=search_colors)115 @click.argument("color", autocompletion=get_colors)116 def csub(csub_opt, color):117 pass118119 assert choices_without_help(cli, [], "-") == ["--cli-opt"]120 assert choices_without_help(cli, [], "") == ["asub"]121 assert choices_without_help(cli, ["asub"], "-") == ["--asub-opt"]122 assert choices_without_help(cli, ["asub"], "") == ["bsub"]123 assert choices_without_help(cli, ["asub", "bsub"], "-") == ["--bsub-opt"]124 assert choices_without_help(cli, ["asub", "bsub"], "") == ["csub"]125 assert choices_without_help(cli, ["asub", "bsub", "csub"], "-") == [126 "--csub-opt",127 "--csub",128 "--search-color",129 ]130 assert (131 choices_without_help(cli, ["asub", "bsub", "csub", "--csub-opt"], "")132 == CSUB_OPT_CHOICES133 )134 assert choices_without_help(cli, ["asub", "bsub", "csub"], "--csub") == [135 "--csub-opt",136 "--csub",137 ]138 assert (139 choices_without_help(cli, ["asub", "bsub", "csub", "--csub"], "")140 == CSUB_CHOICES141 )142 assert choices_without_help(cli, ["asub", "bsub", "csub", "--csub-opt"], "f") == [143 "foo"144 ]145 assert choices_without_help(cli, ["asub", "bsub", "csub"], "") == COLORS146 assert choices_without_help(cli, ["asub", "bsub", "csub"], "b") == ["blue"]147 assert choices_without_help(148 cli, ["asub", "bsub", "csub", "--search-color"], "een"149 ) == ["green"]150151152def test_chaining():153 @click.group("cli", chain=True)154 @click.option("--cli-opt")155 @click.argument("arg", type=click.Choice(["cliarg1", "cliarg2"]))156 def cli(cli_opt, arg):157 pass158159 @cli.command()160 @click.option("--asub-opt")161 def asub(asub_opt):162 pass163164 @cli.command(help="bsub help")165 @click.option("--bsub-opt")166 @click.argument("arg", type=click.Choice(["arg1", "arg2"]))167 def bsub(bsub_opt, arg):168 pass169170 @cli.command()171 @click.option("--csub-opt")172 @click.argument("arg", type=click.Choice(["carg1", "carg2"]), default="carg1")173 def csub(csub_opt, arg):174 pass175176 assert choices_without_help(cli, [], "-") == ["--cli-opt"]177 assert choices_without_help(cli, [], "") == ["cliarg1", "cliarg2"]178 assert choices_without_help(cli, ["cliarg1", "asub"], "-") == ["--asub-opt"]179 assert choices_without_help(cli, ["cliarg1", "asub"], "") == ["bsub", "csub"]180 assert choices_without_help(cli, ["cliarg1", "bsub"], "") == ["arg1", "arg2"]181 assert choices_without_help(cli, ["cliarg1", "asub", "--asub-opt"], "") == []182 assert choices_without_help(183 cli, ["cliarg1", "asub", "--asub-opt", "5", "bsub"], "-"184 ) == ["--bsub-opt"]185 assert choices_without_help(cli, ["cliarg1", "asub", "bsub"], "-") == ["--bsub-opt"]186 assert choices_without_help(cli, ["cliarg1", "asub", "csub"], "") == [187 "carg1",188 "carg2",189 ]190 assert choices_without_help(cli, ["cliarg1", "bsub", "arg1", "csub"], "") == [191 "carg1",192 "carg2",193 ]194 assert choices_without_help(cli, ["cliarg1", "asub", "csub"], "-") == ["--csub-opt"]195 assert choices_with_help(cli, ["cliarg1", "asub"], "b") == [("bsub", "bsub help")]196197198def test_argument_choice():199 @click.command()200 @click.argument("arg1", required=True, type=click.Choice(["arg11", "arg12"]))201 @click.argument("arg2", type=click.Choice(["arg21", "arg22"]), default="arg21")202 @click.argument("arg3", type=click.Choice(["arg", "argument"]), default="arg")203 def cli():204 pass205206 assert choices_without_help(cli, [], "") == ["arg11", "arg12"]207 assert choices_without_help(cli, [], "arg") == ["arg11", "arg12"]208 assert choices_without_help(cli, ["arg11"], "") == ["arg21", "arg22"]209 assert choices_without_help(cli, ["arg12", "arg21"], "") == ["arg", "argument"]210 assert choices_without_help(cli, ["arg12", "arg21"], "argu") == ["argument"]211212213def test_option_choice():214 @click.command()215 @click.option("--opt1", type=click.Choice(["opt11", "opt12"]), help="opt1 help")216 @click.option("--opt2", type=click.Choice(["opt21", "opt22"]), default="opt21")217 @click.option("--opt3", type=click.Choice(["opt", "option"]))218 def cli():219 pass220221 assert choices_with_help(cli, [], "-") == [222 ("--opt1", "opt1 help"),223 ("--opt2", None),224 ("--opt3", None),225 ]226 assert choices_without_help(cli, [], "--opt") == ["--opt1", "--opt2", "--opt3"]227 assert choices_without_help(cli, [], "--opt1=") == ["opt11", "opt12"]228 assert choices_without_help(cli, [], "--opt2=") == ["opt21", "opt22"]229 assert choices_without_help(cli, ["--opt2"], "=") == ["opt21", "opt22"]230 assert choices_without_help(cli, ["--opt2", "="], "opt") == ["opt21", "opt22"]231 assert choices_without_help(cli, ["--opt1"], "") == ["opt11", "opt12"]232 assert choices_without_help(cli, ["--opt2"], "") == ["opt21", "opt22"]233 assert choices_without_help(cli, ["--opt1", "opt11", "--opt2"], "") == [234 "opt21",235 "opt22",236 ]237 assert choices_without_help(cli, ["--opt2", "opt21"], "-") == ["--opt1", "--opt3"]238 assert choices_without_help(cli, ["--opt1", "opt11"], "-") == ["--opt2", "--opt3"]239 assert choices_without_help(cli, ["--opt1"], "opt") == ["opt11", "opt12"]240 assert choices_without_help(cli, ["--opt3"], "opti") == ["option"]241242 assert choices_without_help(cli, ["--opt1", "invalid_opt"], "-") == [243 "--opt2",244 "--opt3",245 ]246247248def test_option_and_arg_choice():249 @click.command()250 @click.option("--opt1", type=click.Choice(["opt11", "opt12"]))251 @click.argument("arg1", required=False, type=click.Choice(["arg11", "arg12"]))252 @click.option("--opt2", type=click.Choice(["opt21", "opt22"]))253 def cli():254 pass255256 assert choices_without_help(cli, ["--opt1"], "") == ["opt11", "opt12"]257 assert choices_without_help(cli, [""], "--opt1=") == ["opt11", "opt12"]258 assert choices_without_help(cli, [], "") == ["arg11", "arg12"]259 assert choices_without_help(cli, ["--opt2"], "") == ["opt21", "opt22"]260 assert choices_without_help(cli, ["arg11"], "--opt") == ["--opt1", "--opt2"]261 assert choices_without_help(cli, [], "--opt") == ["--opt1", "--opt2"]262263264def test_boolean_flag_choice():265 @click.command()266 @click.option("--shout/--no-shout", default=False)267 @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))268 def cli(local_opt):269 pass270271 assert choices_without_help(cli, [], "-") == ["--shout", "--no-shout"]272 assert choices_without_help(cli, ["--shout"], "") == ["arg1", "arg2"]273274275def test_multi_value_option_choice():276 @click.command()277 @click.option("--pos", nargs=2, type=click.Choice(["pos1", "pos2"]))278 @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))279 def cli(local_opt):280 pass281282 assert choices_without_help(cli, ["--pos"], "") == ["pos1", "pos2"]283 assert choices_without_help(cli, ["--pos", "pos1"], "") == ["pos1", "pos2"]284 assert choices_without_help(cli, ["--pos", "pos1", "pos2"], "") == ["arg1", "arg2"]285 assert choices_without_help(cli, ["--pos", "pos1", "pos2", "arg1"], "") == []286287288def test_multi_option_choice():289 @click.command()290 @click.option("--message", "-m", multiple=True, type=click.Choice(["m1", "m2"]))291 @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))292 def cli(local_opt):293 pass294295 assert choices_without_help(cli, ["-m"], "") == ["m1", "m2"]296 assert choices_without_help(cli, ["-m", "m1", "-m"], "") == ["m1", "m2"]297 assert choices_without_help(cli, ["-m", "m1"], "") == ["arg1", "arg2"]298299300def test_variadic_argument_choice():301 @click.command()302 @click.option("--opt", type=click.Choice(["opt1", "opt2"]))303 @click.argument("src", nargs=-1, type=click.Choice(["src1", "src2"]))304 def cli(local_opt):305 pass306307 assert choices_without_help(cli, ["src1", "src2"], "") == ["src1", "src2"]308 assert choices_without_help(cli, ["src1", "src2"], "--o") == ["--opt"]309 assert choices_without_help(cli, ["src1", "src2", "--opt"], "") == ["opt1", "opt2"]310 assert choices_without_help(cli, ["src1", "src2"], "") == ["src1", "src2"]311312313def test_variadic_argument_complete():314 def _complete(ctx, args, incomplete):315 return ["abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz"]316317 @click.group()318 def entrypoint():319 pass320321 @click.command()322 @click.option("--opt", autocompletion=_complete)323 @click.argument("arg", nargs=-1)324 def subcommand(opt, arg):325 pass326327 entrypoint.add_command(subcommand)328329 assert choices_without_help(entrypoint, ["subcommand", "--opt"], "") == _complete(330 0, 0, 0331 )332 assert choices_without_help(333 entrypoint, ["subcommand", "whatever", "--opt"], ""334 ) == _complete(0, 0, 0)335 assert (336 choices_without_help(entrypoint, ["subcommand", "whatever", "--opt", "abc"], "")337 == []338 )339340341def test_long_chain_choice():342 @click.group()343 def cli():344 pass345346 @cli.group()347 @click.option("--sub-opt", type=click.Choice(["subopt1", "subopt2"]))348 @click.argument(349 "sub-arg", required=False, type=click.Choice(["subarg1", "subarg2"])350 )351 def sub(sub_opt, sub_arg):352 pass353354 @sub.command(short_help="bsub help")355 @click.option("--bsub-opt", type=click.Choice(["bsubopt1", "bsubopt2"]))356 @click.argument(357 "bsub-arg1", required=False, type=click.Choice(["bsubarg1", "bsubarg2"])358 )359 @click.argument(360 "bbsub-arg2", required=False, type=click.Choice(["bbsubarg1", "bbsubarg2"])361 )362 def bsub(bsub_opt):363 pass364365 @sub.group("csub")366 def csub():367 pass368369 @csub.command()370 def dsub():371 pass372373 assert choices_with_help(cli, ["sub", "subarg1"], "") == [374 ("bsub", "bsub help"),375 ("csub", ""),376 ]377 assert choices_without_help(cli, ["sub"], "") == ["subarg1", "subarg2"]378 assert choices_without_help(cli, ["sub", "--sub-opt"], "") == ["subopt1", "subopt2"]379 assert choices_without_help(cli, ["sub", "--sub-opt", "subopt1"], "") == [380 "subarg1",381 "subarg2",382 ]383 assert choices_without_help(384 cli, ["sub", "--sub-opt", "subopt1", "subarg1", "bsub"], "-"385 ) == ["--bsub-opt"]386 assert choices_without_help(387 cli, ["sub", "--sub-opt", "subopt1", "subarg1", "bsub"], ""388 ) == ["bsubarg1", "bsubarg2"]389 assert choices_without_help(390 cli, ["sub", "--sub-opt", "subopt1", "subarg1", "bsub", "--bsub-opt"], ""391 ) == ["bsubopt1", "bsubopt2"]392 assert choices_without_help(393 cli,394 [395 "sub",396 "--sub-opt",397 "subopt1",398 "subarg1",399 "bsub",400 "--bsub-opt",401 "bsubopt1",402 "bsubarg1",403 ],404 "",405 ) == ["bbsubarg1", "bbsubarg2"]406 assert choices_without_help(407 cli, ["sub", "--sub-opt", "subopt1", "subarg1", "csub"], ""408 ) == ["dsub"]409410411def test_chained_multi():412 @click.group()413 def cli():414 pass415416 @cli.group()417 def sub():418 pass419420 @sub.group()421 def bsub():422 pass423424 @sub.group(chain=True)425 def csub():426 pass427428 @csub.command()429 def dsub():430 pass431432 @csub.command()433 def esub():434 pass435436 assert choices_without_help(cli, ["sub"], "") == ["bsub", "csub"]437 assert choices_without_help(cli, ["sub"], "c") == ["csub"]438 assert choices_without_help(cli, ["sub", "csub"], "") == ["dsub", "esub"]439 assert choices_without_help(cli, ["sub", "csub", "dsub"], "") == ["esub"]440441442def test_hidden():443 @click.group()444 @click.option("--name", hidden=True)445 @click.option("--choices", type=click.Choice([1, 2]), hidden=True)446 def cli(name):447 pass448449 @cli.group(hidden=True)450 def hgroup():451 pass452453 @hgroup.group()454 def hgroupsub():455 pass456457 @cli.command()458 def asub():459 pass460461 @cli.command(hidden=True)462 @click.option("--hname")463 def hsub():464 pass465466 assert choices_without_help(cli, [], "--n") == []467 assert choices_without_help(cli, [], "--c") == []468 # If the user exactly types out the hidden param, complete its options.469 assert choices_without_help(cli, ["--choices"], "") == [1, 2]470 assert choices_without_help(cli, [], "") == ["asub"]471 assert choices_without_help(cli, [], "") == ["asub"]472 assert choices_without_help(cli, [], "h") == []473 # If the user exactly types out the hidden command, complete its subcommands.474 assert choices_without_help(cli, ["hgroup"], "") == ["hgroupsub"]475 assert choices_without_help(cli, ["hsub"], "--h") == ["--hname"]476477478@pytest.mark.parametrize(479 ("args", "part", "expect"),480 [481 ([], "-", ["--opt"]),482 (["value"], "--", ["--opt"]),483 ([], "-o", []),484 (["--opt"], "-o", []),485 (["--"], "", ["name", "-o", "--opt", "--"]),486 (["--"], "--o", ["--opt"]),487 ],488)489def test_args_with_double_dash_complete(args, part, expect):490 def _complete(ctx, args, incomplete):491 values = ["name", "-o", "--opt", "--"]492 return [x for x in values if x.startswith(incomplete)]493494 @click.command()495 @click.option("--opt")496 @click.argument("args", nargs=-1, autocompletion=_complete)497 def cli(opt, args):498 pass499500 assert choices_without_help(cli, args, part) == expect