Skip to content
Myr

tests for new shell completion system

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kai Chen · committed by David Lord · parent 3c4cacb · Browse files · 4 files+243 added−537 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Amy <leiamy12@gmail.com>
  • Co-authored-by: David Lord <davidism@gmail.com>

Diff stats

4 files+243 added−537 removed

  • tests:+240 added−536 removed
  • other:+3 added−1 removed

4 files+243 added−537 removed

modified:src/click/shell_completion.py+3 added-1 removed
240 unmodified lines
241242 name = "bash"243 source_template = _SOURCE_BASH244245 def source(self):245 def _check_version(self):246 import subprocess247248 output = subprocess.run(["bash", "--version"], stdout=subprocess.PIPE)249 match = re.search(r"version (\d)\.(\d)\.\d", output.stdout.decode())
10 unmodified lines
260 raise RuntimeError(261 "Couldn't detect Bash version, shell completion is not supported."262 )263264 def source(self):265 self._check_version()266 return super().source()267268 def get_completion_args(self):269 cwords = split_arg_string(os.environ["COMP_WORDS"])
240 unmodified lines
deleted:tests/test_bashcomplete.py-519 removed
1import pytest23import 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", "--help"]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", "--help"]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", "--help"]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", "--help"]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", "--help"]75 assert choices_without_help(cli, ["sub"], "") == []76 assert choices_without_help(cli, ["sub"], "-") == ["--local-opt", "--help"]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"]9697 def get_colors(ctx, args, incomplete):98 for c in COLORS:99 if c.startswith(incomplete):100 yield c101102 def search_colors(ctx, args, incomplete):103 for c in COLORS:104 if incomplete in c:105 yield c106107 CSUB_OPT_CHOICES = ["foo", "bar"]108 CSUB_CHOICES = ["bar", "baz"]109110 @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", "--help"]119 assert choices_without_help(cli, [], "") == ["asub"]120 assert choices_without_help(cli, ["asub"], "-") == ["--asub-opt", "--help"]121 assert choices_without_help(cli, ["asub"], "") == ["bsub"]122 assert choices_without_help(cli, ["asub", "bsub"], "-") == ["--bsub-opt", "--help"]123 assert choices_without_help(cli, ["asub", "bsub"], "") == ["csub"]124 assert choices_without_help(cli, ["asub", "bsub", "csub"], "-") == [125 "--csub-opt",126 "--csub",127 "--search-color",128 "--help",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", "--help"]177 assert choices_without_help(cli, [], "") == ["cliarg1", "cliarg2"]178 assert choices_without_help(cli, ["cliarg1", "asub"], "-") == [179 "--asub-opt",180 "--help",181 ]182 assert choices_without_help(cli, ["cliarg1", "asub"], "") == ["bsub", "csub"]183 assert choices_without_help(cli, ["cliarg1", "bsub"], "") == ["arg1", "arg2"]184 assert choices_without_help(cli, ["cliarg1", "asub", "--asub-opt"], "") == []185 assert choices_without_help(186 cli, ["cliarg1", "asub", "--asub-opt", "5", "bsub"], "-"187 ) == ["--bsub-opt", "--help"]188 assert choices_without_help(cli, ["cliarg1", "asub", "bsub"], "-") == [189 "--bsub-opt",190 "--help",191 ]192 assert choices_without_help(cli, ["cliarg1", "asub", "csub"], "") == [193 "carg1",194 "carg2",195 ]196 assert choices_without_help(cli, ["cliarg1", "bsub", "arg1", "csub"], "") == [197 "carg1",198 "carg2",199 ]200 assert choices_without_help(cli, ["cliarg1", "asub", "csub"], "-") == [201 "--csub-opt",202 "--help",203 ]204 assert choices_with_help(cli, ["cliarg1", "asub"], "b") == [("bsub", "bsub help")]205206207def test_argument_choice():208 @click.command()209 @click.argument("arg1", required=True, type=click.Choice(["arg11", "arg12"]))210 @click.argument("arg2", type=click.Choice(["arg21", "arg22"]), default="arg21")211 @click.argument("arg3", type=click.Choice(["arg", "argument"]), default="arg")212 def cli():213 pass214215 assert choices_without_help(cli, [], "") == ["arg11", "arg12"]216 assert choices_without_help(cli, [], "arg") == ["arg11", "arg12"]217 assert choices_without_help(cli, ["arg11"], "") == ["arg21", "arg22"]218 assert choices_without_help(cli, ["arg12", "arg21"], "") == ["arg", "argument"]219 assert choices_without_help(cli, ["arg12", "arg21"], "argu") == ["argument"]220221222def test_option_choice():223 @click.command()224 @click.option("--opt1", type=click.Choice(["opt11", "opt12"]), help="opt1 help")225 @click.option("--opt2", type=click.Choice(["opt21", "opt22"]), default="opt21")226 @click.option("--opt3", type=click.Choice(["opt", "option"]))227 def cli():228 pass229230 assert choices_with_help(cli, [], "-") == [231 ("--opt1", "opt1 help"),232 ("--opt2", None),233 ("--opt3", None),234 ("--help", "Show this message and exit."),235 ]236 assert choices_without_help(cli, [], "--opt") == ["--opt1", "--opt2", "--opt3"]237 assert choices_without_help(cli, [], "--opt1=") == ["opt11", "opt12"]238 assert choices_without_help(cli, [], "--opt2=") == ["opt21", "opt22"]239 assert choices_without_help(cli, ["--opt2"], "=") == ["opt21", "opt22"]240 assert choices_without_help(cli, ["--opt2", "="], "opt") == ["opt21", "opt22"]241 assert choices_without_help(cli, ["--opt1"], "") == ["opt11", "opt12"]242 assert choices_without_help(cli, ["--opt2"], "") == ["opt21", "opt22"]243 assert choices_without_help(cli, ["--opt1", "opt11", "--opt2"], "") == [244 "opt21",245 "opt22",246 ]247 assert choices_without_help(cli, ["--opt2", "opt21"], "-") == [248 "--opt1",249 "--opt3",250 "--help",251 ]252 assert choices_without_help(cli, ["--opt1", "opt11"], "-") == [253 "--opt2",254 "--opt3",255 "--help",256 ]257 assert choices_without_help(cli, ["--opt1"], "opt") == ["opt11", "opt12"]258 assert choices_without_help(cli, ["--opt3"], "opti") == ["option"]259260 assert choices_without_help(cli, ["--opt1", "invalid_opt"], "-") == [261 "--opt2",262 "--opt3",263 "--help",264 ]265266267def test_option_and_arg_choice():268 @click.command()269 @click.option("--opt1", type=click.Choice(["opt11", "opt12"]))270 @click.argument("arg1", required=False, type=click.Choice(["arg11", "arg12"]))271 @click.option("--opt2", type=click.Choice(["opt21", "opt22"]))272 def cli():273 pass274275 assert choices_without_help(cli, ["--opt1"], "") == ["opt11", "opt12"]276 assert choices_without_help(cli, [""], "--opt1=") == ["opt11", "opt12"]277 assert choices_without_help(cli, [], "") == ["arg11", "arg12"]278 assert choices_without_help(cli, ["--opt2"], "") == ["opt21", "opt22"]279 assert choices_without_help(cli, ["arg11"], "--opt") == ["--opt1", "--opt2"]280 assert choices_without_help(cli, [], "--opt") == ["--opt1", "--opt2"]281282283def test_boolean_flag_choice():284 @click.command()285 @click.option("--shout/--no-shout", default=False)286 @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))287 def cli(local_opt):288 pass289290 assert choices_without_help(cli, [], "-") == ["--shout", "--no-shout", "--help"]291 assert choices_without_help(cli, ["--shout"], "") == ["arg1", "arg2"]292293294def test_multi_value_option_choice():295 @click.command()296 @click.option("--pos", nargs=2, type=click.Choice(["pos1", "pos2"]))297 @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))298 def cli(local_opt):299 pass300301 assert choices_without_help(cli, ["--pos"], "") == ["pos1", "pos2"]302 assert choices_without_help(cli, ["--pos", "pos1"], "") == ["pos1", "pos2"]303 assert choices_without_help(cli, ["--pos", "pos1", "pos2"], "") == ["arg1", "arg2"]304 assert choices_without_help(cli, ["--pos", "pos1", "pos2", "arg1"], "") == []305306307def test_multi_option_choice():308 @click.command()309 @click.option("--message", "-m", multiple=True, type=click.Choice(["m1", "m2"]))310 @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))311 def cli(local_opt):312 pass313314 assert choices_without_help(cli, ["-m"], "") == ["m1", "m2"]315 assert choices_without_help(cli, ["-m", "m1", "-m"], "") == ["m1", "m2"]316 assert choices_without_help(cli, ["-m", "m1"], "") == ["arg1", "arg2"]317318319def test_variadic_argument_choice():320 @click.command()321 @click.option("--opt", type=click.Choice(["opt1", "opt2"]))322 @click.argument("src", nargs=-1, type=click.Choice(["src1", "src2"]))323 def cli(local_opt):324 pass325326 assert choices_without_help(cli, ["src1", "src2"], "") == ["src1", "src2"]327 assert choices_without_help(cli, ["src1", "src2"], "--o") == ["--opt"]328 assert choices_without_help(cli, ["src1", "src2", "--opt"], "") == ["opt1", "opt2"]329 assert choices_without_help(cli, ["src1", "src2"], "") == ["src1", "src2"]330331332def test_variadic_argument_complete():333 def _complete(ctx, args, incomplete):334 return ["abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz"]335336 @click.group()337 def entrypoint():338 pass339340 @click.command()341 @click.option("--opt", autocompletion=_complete)342 @click.argument("arg", nargs=-1)343 def subcommand(opt, arg):344 pass345346 entrypoint.add_command(subcommand)347348 assert choices_without_help(entrypoint, ["subcommand", "--opt"], "") == _complete(349 0, 0, 0350 )351 assert choices_without_help(352 entrypoint, ["subcommand", "whatever", "--opt"], ""353 ) == _complete(0, 0, 0)354 assert (355 choices_without_help(entrypoint, ["subcommand", "whatever", "--opt", "abc"], "")356 == []357 )358359360def test_long_chain_choice():361 @click.group()362 def cli():363 pass364365 @cli.group()366 @click.option("--sub-opt", type=click.Choice(["subopt1", "subopt2"]))367 @click.argument(368 "sub-arg", required=False, type=click.Choice(["subarg1", "subarg2"])369 )370 def sub(sub_opt, sub_arg):371 pass372373 @sub.command(short_help="bsub help")374 @click.option("--bsub-opt", type=click.Choice(["bsubopt1", "bsubopt2"]))375 @click.argument(376 "bsub-arg1", required=False, type=click.Choice(["bsubarg1", "bsubarg2"])377 )378 @click.argument(379 "bbsub-arg2", required=False, type=click.Choice(["bbsubarg1", "bbsubarg2"])380 )381 def bsub(bsub_opt):382 pass383384 @sub.group("csub")385 def csub():386 pass387388 @csub.command()389 def dsub():390 pass391392 assert choices_with_help(cli, ["sub", "subarg1"], "") == [393 ("bsub", "bsub help"),394 ("csub", ""),395 ]396 assert choices_without_help(cli, ["sub"], "") == ["subarg1", "subarg2"]397 assert choices_without_help(cli, ["sub", "--sub-opt"], "") == ["subopt1", "subopt2"]398 assert choices_without_help(cli, ["sub", "--sub-opt", "subopt1"], "") == [399 "subarg1",400 "subarg2",401 ]402 assert choices_without_help(403 cli, ["sub", "--sub-opt", "subopt1", "subarg1", "bsub"], "-"404 ) == ["--bsub-opt", "--help"]405 assert choices_without_help(406 cli, ["sub", "--sub-opt", "subopt1", "subarg1", "bsub"], ""407 ) == ["bsubarg1", "bsubarg2"]408 assert choices_without_help(409 cli, ["sub", "--sub-opt", "subopt1", "subarg1", "bsub", "--bsub-opt"], ""410 ) == ["bsubopt1", "bsubopt2"]411 assert choices_without_help(412 cli,413 [414 "sub",415 "--sub-opt",416 "subopt1",417 "subarg1",418 "bsub",419 "--bsub-opt",420 "bsubopt1",421 "bsubarg1",422 ],423 "",424 ) == ["bbsubarg1", "bbsubarg2"]425 assert choices_without_help(426 cli, ["sub", "--sub-opt", "subopt1", "subarg1", "csub"], ""427 ) == ["dsub"]428429430def test_chained_multi():431 @click.group()432 def cli():433 pass434435 @cli.group()436 def sub():437 pass438439 @sub.group()440 def bsub():441 pass442443 @sub.group(chain=True)444 def csub():445 pass446447 @csub.command()448 def dsub():449 pass450451 @csub.command()452 def esub():453 pass454455 assert choices_without_help(cli, ["sub"], "") == ["bsub", "csub"]456 assert choices_without_help(cli, ["sub"], "c") == ["csub"]457 assert choices_without_help(cli, ["sub", "csub"], "") == ["dsub", "esub"]458 assert choices_without_help(cli, ["sub", "csub", "dsub"], "") == ["esub"]459460461def test_hidden():462 @click.group()463 @click.option("--name", hidden=True)464 @click.option("--choices", type=click.Choice([1, 2]), hidden=True)465 def cli(name):466 pass467468 @cli.group(hidden=True)469 def hgroup():470 pass471472 @hgroup.group()473 def hgroupsub():474 pass475476 @cli.command()477 def asub():478 pass479480 @cli.command(hidden=True)481 @click.option("--hname")482 def hsub():483 pass484485 assert choices_without_help(cli, [], "--n") == []486 assert choices_without_help(cli, [], "--c") == []487 # If the user exactly types out the hidden param, complete its options.488 assert choices_without_help(cli, ["--choices"], "") == [1, 2]489 assert choices_without_help(cli, [], "") == ["asub"]490 assert choices_without_help(cli, [], "") == ["asub"]491 assert choices_without_help(cli, [], "h") == []492 # If the user exactly types out the hidden command, complete its subcommands.493 assert choices_without_help(cli, ["hgroup"], "") == ["hgroupsub"]494 assert choices_without_help(cli, ["hsub"], "--h") == ["--hname", "--help"]495496497@pytest.mark.parametrize(498 ("args", "part", "expect"),499 [500 ([], "-", ["--opt", "--help"]),501 (["value"], "--", ["--opt", "--help"]),502 ([], "-o", []),503 (["--opt"], "-o", []),504 (["--"], "", ["name", "-o", "--opt", "--"]),505 (["--"], "--o", ["--opt"]),506 ],507)508def test_args_with_double_dash_complete(args, part, expect):509 def _complete(ctx, args, incomplete):510 values = ["name", "-o", "--opt", "--"]511 return [x for x in values if x.startswith(incomplete)]512513 @click.command()514 @click.option("--opt")515 @click.argument("args", nargs=-1, autocompletion=_complete)516 def cli(opt, args):517 pass518519 assert choices_without_help(cli, args, part) == expect
modified:tests/test_compat.py-17 removed
2 unmodified lines
3from click._compat import should_strip_ansi4from click._compat import WIN567def test_bash_func_name():8 from click._bashcomplete import get_completion_script910 script = get_completion_script("foo-bar baz_blah", "_COMPLETE_VAR", "bash").strip()11 assert script.startswith("_foo_barbaz_blah_completion()")12 assert "_COMPLETE_VAR=complete $1" in script131415def test_zsh_func_name():16 from click._bashcomplete import get_completion_script1718 script = get_completion_script("foo-bar", "_COMPLETE_VAR", "zsh").strip()19 assert script.startswith("#compdef foo-bar")20 assert "compdef _foo_bar_completion foo-bar;" in script21 assert "(( ! $+commands[foo-bar] )) && return 1" in script22237@pytest.mark.xfail(WIN, reason="Jupyter not tested/supported on Windows")8def test_is_jupyter_kernel_output():9 class JupyterKernelFakeStream:10 pass
4 unmodified lines
added:tests/test_shell_completion.py+240 added
1import sys23import pytest45from click.core import Argument6from click.core import Command7from click.core import Group8from click.core import Option9from click.shell_completion import ShellComplete10from click.types import Choice11from click.types import File12from click.types import Path131415def _get_completions(cli, args, incomplete):16 comp = ShellComplete(cli, cli.name, "_CLICK_COMPLETE")17 return comp.get_completions(args, incomplete)181920def _get_words(cli, args, incomplete):21 return [c[1] for c in _get_completions(cli, args, incomplete)]222324def test_command():25 cli = Command("cli", params=[Option(["-t", "--test"])])26 assert _get_words(cli, [], "") == []27 assert _get_words(cli, [], "-") == ["-t", "--test", "--help"]28 assert _get_words(cli, [], "--") == ["--test", "--help"]29 assert _get_words(cli, [], "--t") == ["--test"]30 assert _get_words(cli, ["-t", "a"], "-") == ["--test", "--help"]313233def test_group():34 cli = Group("cli", params=[Option(["-a"])], commands=[Command("x"), Command("y")])35 assert _get_words(cli, [], "") == ["x", "y"]36 assert _get_words(cli, [], "-") == ["-a", "--help"]373839def test_chained():40 cli = Group(41 "cli",42 chain=True,43 commands=[44 Command("set", params=[Option(["-y"])]),45 Command("start"),46 Group("get", commands=[Command("full")]),47 ],48 )49 assert _get_words(cli, [], "") == ["get", "set", "start"]50 assert _get_words(cli, [], "s") == ["set", "start"]51 assert _get_words(cli, ["set", "start"], "") == ["get"]52 # subcommands and parent subcommands53 assert _get_words(cli, ["get"], "") == ["full", "set", "start"]54 assert _get_words(cli, ["get", "full"], "") == ["set", "start"]55 assert _get_words(cli, ["get"], "s") == ["set", "start"]565758def test_help_option():59 cli = Group("cli", commands=[Command("with"), Command("no", add_help_option=False)])60 assert _get_words(cli, ["with"], "--") == ["--help"]61 assert _get_words(cli, ["no"], "--") == []626364def test_argument_order():65 cli = Command(66 "cli",67 params=[68 Argument(["plain"]),69 Argument(["c1"], type=Choice(["a1", "a2", "b"])),70 Argument(["c2"], type=Choice(["c1", "c2", "d"])),71 ],72 )73 # first argument has no completions74 assert _get_words(cli, [], "") == []75 assert _get_words(cli, [], "a") == []76 # first argument filled, now completion can happen77 assert _get_words(cli, ["x"], "a") == ["a1", "a2"]78 assert _get_words(cli, ["x", "b"], "d") == ["d"]798081def test_type_choice():82 cli = Command("cli", params=[Option(["-c"], type=Choice(["a1", "a2", "b"]))])83 assert _get_words(cli, ["-c"], "") == ["a1", "a2", "b"]84 assert _get_words(cli, ["-c"], "a") == ["a1", "a2"]85 assert _get_words(cli, ["-c"], "a2") == ["a2"]868788def test_type_file():89 cli = Command("cli", params=[Option(["-f"], type=File())])90 assert _get_completions(cli, ["-f"], "ab") == [("file", "ab", None)]919293def test_type_path_file():94 cli = Command("cli", params=[Option(["-p"], type=Path())])95 assert _get_completions(cli, ["-p"], "ab") == [("file", "ab", None)]969798def test_type_path_dir():99 cli = Command("cli", params=[Option(["-d"], type=Path(file_okay=False))])100 assert _get_completions(cli, ["-d"], "ab") == [("dir", "ab", None)]101102103def test_option_flag():104 cli = Command(105 "cli",106 add_help_option=False,107 params=[108 Option(["--on/--off"]),109 Argument(["a"], type=Choice(["a1", "a2", "b"])),110 ],111 )112 assert _get_words(cli, ["type"], "--") == ["--on", "--off"]113 # flag option doesn't take value, use choice argument114 assert _get_words(cli, ["x", "--on"], "a") == ["a1", "a2"]115116117def test_option_custom():118 def custom(ctx, args, incomplete):119 return [incomplete.upper()]120121 cli = Command(122 "cli",123 params=[124 Argument(["x"]),125 Argument(["y"]),126 Argument(["z"], autocompletion=custom),127 ],128 )129 assert _get_words(cli, ["a", "b"], "") == [""]130 assert _get_words(cli, ["a", "b"], "c") == ["C"]131132133def test_option_multiple():134 cli = Command(135 "type",136 params=[Option(["-m"], type=Choice(["a", "b"]), multiple=True), Option(["-f"])],137 )138 assert _get_words(cli, ["-m"], "") == ["a", "b"]139 assert "-m" in _get_words(cli, ["-m", "a"], "-")140 assert _get_words(cli, ["-m", "a", "-m"], "") == ["a", "b"]141 # used single options aren't suggested again142 assert "-c" not in _get_words(cli, ["-c", "f"], "-")143144145def test_option_nargs():146 cli = Command("cli", params=[Option(["-c"], type=Choice(["a", "b"]), nargs=2)])147 assert _get_words(cli, ["-c"], "") == ["a", "b"]148 assert _get_words(cli, ["-c", "a"], "") == ["a", "b"]149 assert _get_words(cli, ["-c", "a", "b"], "") == []150151152def test_argument_nargs():153 cli = Command(154 "cli",155 params=[156 Argument(["x"], type=Choice(["a", "b"]), nargs=2),157 Argument(["y"], type=Choice(["c", "d"]), nargs=-1),158 Option(["-z"]),159 ],160 )161 assert _get_words(cli, [], "") == ["a", "b"]162 assert _get_words(cli, ["a"], "") == ["a", "b"]163 assert _get_words(cli, ["a", "b"], "") == ["c", "d"]164 assert _get_words(cli, ["a", "b", "c"], "") == ["c", "d"]165 assert _get_words(cli, ["a", "b", "c", "d"], "") == ["c", "d"]166 assert _get_words(cli, ["a", "-z", "1"], "") == ["a", "b"]167 assert _get_words(cli, ["a", "-z", "1", "b"], "") == ["c", "d"]168169170def test_double_dash():171 cli = Command(172 "cli",173 add_help_option=False,174 params=[175 Option(["--opt"]),176 Argument(["name"], type=Choice(["name", "--", "-o", "--opt"])),177 ],178 )179 assert _get_words(cli, [], "-") == ["--opt"]180 assert _get_words(cli, ["value"], "-") == ["--opt"]181 assert _get_words(cli, [], "") == ["name", "--", "-o", "--opt"]182 assert _get_words(cli, ["--"], "") == ["name", "--", "-o", "--opt"]183184185def test_hidden():186 cli = Group(187 "cli",188 commands=[189 Command(190 "hidden",191 add_help_option=False,192 hidden=True,193 params=[194 Option(["-a"]),195 Option(["-b"], type=Choice(["a", "b"]), hidden=True),196 ],197 )198 ],199 )200 assert "hidden" not in _get_words(cli, [], "")201 assert "hidden" not in _get_words(cli, [], "hidden")202 assert _get_words(cli, ["hidden"], "-") == ["-a"]203 assert _get_words(cli, ["hidden", "-b"], "") == ["a", "b"]204205206@pytest.fixture()207def _patch_for_completion(monkeypatch):208 monkeypatch.setattr("click.core._fast_exit", sys.exit)209 monkeypatch.setattr(210 "click.shell_completion.BashComplete._check_version", lambda self: True211 )212213214@pytest.mark.parametrize(215 "shell", ["bash", "zsh", "fish"],216)217@pytest.mark.usefixtures("_patch_for_completion")218def test_full_source(runner, shell):219 cli = Group("cli", commands=[Command("a"), Command("b")])220 result = runner.invoke(cli, env={"_CLI_COMPLETE": f"source_{shell}"})221 assert f"_CLI_COMPLETE=complete_{shell}" in result.output222223224@pytest.mark.parametrize(225 ("shell", "env", "expect"),226 [227 ("bash", {"COMP_WORDS": "", "COMP_CWORD": "0"}, "plain,a\nplain,b\n"),228 ("bash", {"COMP_WORDS": "a b", "COMP_CWORD": "1"}, "plain,b\n"),229 ("zsh", {"COMP_WORDS": "", "COMP_CWORD": "0"}, "plain\na\n_\nplain\nb\nbee\n"),230 ("zsh", {"COMP_WORDS": "a b", "COMP_CWORD": "1"}, "plain\nb\nbee\n"),231 ("fish", {"COMP_WORDS": "", "COMP_CWORD": ""}, "plain,a\nplain,b\tbee\n"),232 ("fish", {"COMP_WORDS": "a b", "COMP_CWORD": "b"}, "plain,b\tbee\n"),233 ],234)235@pytest.mark.usefixtures("_patch_for_completion")236def test_full_complete(runner, shell, env, expect):237 cli = Group("cli", commands=[Command("a"), Command("b", help="bee")])238 env["_CLI_COMPLETE"] = f"complete_{shell}"239 result = runner.invoke(cli, env=env)240 assert result.output == expect