Skip to content
Myr

test_bashcomplete.py

This file

Path

tests/test_bashcomplete.py

519 lines · 16.2 KiB · Python

Last changed by

Merge branch '7.x'David Lordbefore adoptionf634bd0

History of this file

Take it

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