Skip to content
Myr

test_bashcomplete.py

This file

Path

tests/test_bashcomplete.py

499 lines · 15.8 KiB · Python

Last changed by

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

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"]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"]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"]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"], "-") == [125        "--csub-opt",126        "--csub",127        "--search-color",128    ]129    assert (130        choices_without_help(cli, ["asub", "bsub", "csub", "--csub-opt"], "")131        == CSUB_OPT_CHOICES132    )133    assert choices_without_help(cli, ["asub", "bsub", "csub"], "--csub") == [134        "--csub-opt",135        "--csub",136    ]137    assert (138        choices_without_help(cli, ["asub", "bsub", "csub", "--csub"], "")139        == CSUB_CHOICES140    )141    assert choices_without_help(cli, ["asub", "bsub", "csub", "--csub-opt"], "f") == [142        "foo"143    ]144    assert choices_without_help(cli, ["asub", "bsub", "csub"], "") == COLORS145    assert choices_without_help(cli, ["asub", "bsub", "csub"], "b") == ["blue"]146    assert choices_without_help(147        cli, ["asub", "bsub", "csub", "--search-color"], "een"148    ) == ["green"]149150151def test_chaining():152    @click.group("cli", chain=True)153    @click.option("--cli-opt")154    @click.argument("arg", type=click.Choice(["cliarg1", "cliarg2"]))155    def cli(cli_opt, arg):156        pass157158    @cli.command()159    @click.option("--asub-opt")160    def asub(asub_opt):161        pass162163    @cli.command(help="bsub help")164    @click.option("--bsub-opt")165    @click.argument("arg", type=click.Choice(["arg1", "arg2"]))166    def bsub(bsub_opt, arg):167        pass168169    @cli.command()170    @click.option("--csub-opt")171    @click.argument("arg", type=click.Choice(["carg1", "carg2"]), default="carg1")172    def csub(csub_opt, arg):173        pass174175    assert choices_without_help(cli, [], "-") == ["--cli-opt"]176    assert choices_without_help(cli, [], "") == ["cliarg1", "cliarg2"]177    assert choices_without_help(cli, ["cliarg1", "asub"], "-") == ["--asub-opt"]178    assert choices_without_help(cli, ["cliarg1", "asub"], "") == ["bsub", "csub"]179    assert choices_without_help(cli, ["cliarg1", "bsub"], "") == ["arg1", "arg2"]180    assert choices_without_help(cli, ["cliarg1", "asub", "--asub-opt"], "") == []181    assert choices_without_help(182        cli, ["cliarg1", "asub", "--asub-opt", "5", "bsub"], "-"183    ) == ["--bsub-opt"]184    assert choices_without_help(cli, ["cliarg1", "asub", "bsub"], "-") == ["--bsub-opt"]185    assert choices_without_help(cli, ["cliarg1", "asub", "csub"], "") == [186        "carg1",187        "carg2",188    ]189    assert choices_without_help(cli, ["cliarg1", "bsub", "arg1", "csub"], "") == [190        "carg1",191        "carg2",192    ]193    assert choices_without_help(cli, ["cliarg1", "asub", "csub"], "-") == ["--csub-opt"]194    assert choices_with_help(cli, ["cliarg1", "asub"], "b") == [("bsub", "bsub help")]195196197def test_argument_choice():198    @click.command()199    @click.argument("arg1", required=True, type=click.Choice(["arg11", "arg12"]))200    @click.argument("arg2", type=click.Choice(["arg21", "arg22"]), default="arg21")201    @click.argument("arg3", type=click.Choice(["arg", "argument"]), default="arg")202    def cli():203        pass204205    assert choices_without_help(cli, [], "") == ["arg11", "arg12"]206    assert choices_without_help(cli, [], "arg") == ["arg11", "arg12"]207    assert choices_without_help(cli, ["arg11"], "") == ["arg21", "arg22"]208    assert choices_without_help(cli, ["arg12", "arg21"], "") == ["arg", "argument"]209    assert choices_without_help(cli, ["arg12", "arg21"], "argu") == ["argument"]210211212def test_option_choice():213    @click.command()214    @click.option("--opt1", type=click.Choice(["opt11", "opt12"]), help="opt1 help")215    @click.option("--opt2", type=click.Choice(["opt21", "opt22"]), default="opt21")216    @click.option("--opt3", type=click.Choice(["opt", "option"]))217    def cli():218        pass219220    assert choices_with_help(cli, [], "-") == [221        ("--opt1", "opt1 help"),222        ("--opt2", None),223        ("--opt3", None),224    ]225    assert choices_without_help(cli, [], "--opt") == ["--opt1", "--opt2", "--opt3"]226    assert choices_without_help(cli, [], "--opt1=") == ["opt11", "opt12"]227    assert choices_without_help(cli, [], "--opt2=") == ["opt21", "opt22"]228    assert choices_without_help(cli, ["--opt2"], "=") == ["opt21", "opt22"]229    assert choices_without_help(cli, ["--opt2", "="], "opt") == ["opt21", "opt22"]230    assert choices_without_help(cli, ["--opt1"], "") == ["opt11", "opt12"]231    assert choices_without_help(cli, ["--opt2"], "") == ["opt21", "opt22"]232    assert choices_without_help(cli, ["--opt1", "opt11", "--opt2"], "") == [233        "opt21",234        "opt22",235    ]236    assert choices_without_help(cli, ["--opt2", "opt21"], "-") == ["--opt1", "--opt3"]237    assert choices_without_help(cli, ["--opt1", "opt11"], "-") == ["--opt2", "--opt3"]238    assert choices_without_help(cli, ["--opt1"], "opt") == ["opt11", "opt12"]239    assert choices_without_help(cli, ["--opt3"], "opti") == ["option"]240241    assert choices_without_help(cli, ["--opt1", "invalid_opt"], "-") == [242        "--opt2",243        "--opt3",244    ]245246247def test_option_and_arg_choice():248    @click.command()249    @click.option("--opt1", type=click.Choice(["opt11", "opt12"]))250    @click.argument("arg1", required=False, type=click.Choice(["arg11", "arg12"]))251    @click.option("--opt2", type=click.Choice(["opt21", "opt22"]))252    def cli():253        pass254255    assert choices_without_help(cli, ["--opt1"], "") == ["opt11", "opt12"]256    assert choices_without_help(cli, [""], "--opt1=") == ["opt11", "opt12"]257    assert choices_without_help(cli, [], "") == ["arg11", "arg12"]258    assert choices_without_help(cli, ["--opt2"], "") == ["opt21", "opt22"]259    assert choices_without_help(cli, ["arg11"], "--opt") == ["--opt1", "--opt2"]260    assert choices_without_help(cli, [], "--opt") == ["--opt1", "--opt2"]261262263def test_boolean_flag_choice():264    @click.command()265    @click.option("--shout/--no-shout", default=False)266    @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))267    def cli(local_opt):268        pass269270    assert choices_without_help(cli, [], "-") == ["--shout", "--no-shout"]271    assert choices_without_help(cli, ["--shout"], "") == ["arg1", "arg2"]272273274def test_multi_value_option_choice():275    @click.command()276    @click.option("--pos", nargs=2, type=click.Choice(["pos1", "pos2"]))277    @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))278    def cli(local_opt):279        pass280281    assert choices_without_help(cli, ["--pos"], "") == ["pos1", "pos2"]282    assert choices_without_help(cli, ["--pos", "pos1"], "") == ["pos1", "pos2"]283    assert choices_without_help(cli, ["--pos", "pos1", "pos2"], "") == ["arg1", "arg2"]284    assert choices_without_help(cli, ["--pos", "pos1", "pos2", "arg1"], "") == []285286287def test_multi_option_choice():288    @click.command()289    @click.option("--message", "-m", multiple=True, type=click.Choice(["m1", "m2"]))290    @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))291    def cli(local_opt):292        pass293294    assert choices_without_help(cli, ["-m"], "") == ["m1", "m2"]295    assert choices_without_help(cli, ["-m", "m1", "-m"], "") == ["m1", "m2"]296    assert choices_without_help(cli, ["-m", "m1"], "") == ["arg1", "arg2"]297298299def test_variadic_argument_choice():300    @click.command()301    @click.option("--opt", type=click.Choice(["opt1", "opt2"]))302    @click.argument("src", nargs=-1, type=click.Choice(["src1", "src2"]))303    def cli(local_opt):304        pass305306    assert choices_without_help(cli, ["src1", "src2"], "") == ["src1", "src2"]307    assert choices_without_help(cli, ["src1", "src2"], "--o") == ["--opt"]308    assert choices_without_help(cli, ["src1", "src2", "--opt"], "") == ["opt1", "opt2"]309    assert choices_without_help(cli, ["src1", "src2"], "") == ["src1", "src2"]310311312def test_variadic_argument_complete():313    def _complete(ctx, args, incomplete):314        return ["abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz"]315316    @click.group()317    def entrypoint():318        pass319320    @click.command()321    @click.option("--opt", autocompletion=_complete)322    @click.argument("arg", nargs=-1)323    def subcommand(opt, arg):324        pass325326    entrypoint.add_command(subcommand)327328    assert choices_without_help(entrypoint, ["subcommand", "--opt"], "") == _complete(329        0, 0, 0330    )331    assert choices_without_help(332        entrypoint, ["subcommand", "whatever", "--opt"], ""333    ) == _complete(0, 0, 0)334    assert (335        choices_without_help(entrypoint, ["subcommand", "whatever", "--opt", "abc"], "")336        == []337    )338339340def test_long_chain_choice():341    @click.group()342    def cli():343        pass344345    @cli.group()346    @click.option("--sub-opt", type=click.Choice(["subopt1", "subopt2"]))347    @click.argument(348        "sub-arg", required=False, type=click.Choice(["subarg1", "subarg2"])349    )350    def sub(sub_opt, sub_arg):351        pass352353    @sub.command(short_help="bsub help")354    @click.option("--bsub-opt", type=click.Choice(["bsubopt1", "bsubopt2"]))355    @click.argument(356        "bsub-arg1", required=False, type=click.Choice(["bsubarg1", "bsubarg2"])357    )358    @click.argument(359        "bbsub-arg2", required=False, type=click.Choice(["bbsubarg1", "bbsubarg2"])360    )361    def bsub(bsub_opt):362        pass363364    @sub.group("csub")365    def csub():366        pass367368    @csub.command()369    def dsub():370        pass371372    assert choices_with_help(cli, ["sub", "subarg1"], "") == [373        ("bsub", "bsub help"),374        ("csub", ""),375    ]376    assert choices_without_help(cli, ["sub"], "") == ["subarg1", "subarg2"]377    assert choices_without_help(cli, ["sub", "--sub-opt"], "") == ["subopt1", "subopt2"]378    assert choices_without_help(cli, ["sub", "--sub-opt", "subopt1"], "") == [379        "subarg1",380        "subarg2",381    ]382    assert choices_without_help(383        cli, ["sub", "--sub-opt", "subopt1", "subarg1", "bsub"], "-"384    ) == ["--bsub-opt"]385    assert choices_without_help(386        cli, ["sub", "--sub-opt", "subopt1", "subarg1", "bsub"], ""387    ) == ["bsubarg1", "bsubarg2"]388    assert choices_without_help(389        cli, ["sub", "--sub-opt", "subopt1", "subarg1", "bsub", "--bsub-opt"], ""390    ) == ["bsubopt1", "bsubopt2"]391    assert choices_without_help(392        cli,393        [394            "sub",395            "--sub-opt",396            "subopt1",397            "subarg1",398            "bsub",399            "--bsub-opt",400            "bsubopt1",401            "bsubarg1",402        ],403        "",404    ) == ["bbsubarg1", "bbsubarg2"]405    assert choices_without_help(406        cli, ["sub", "--sub-opt", "subopt1", "subarg1", "csub"], ""407    ) == ["dsub"]408409410def test_chained_multi():411    @click.group()412    def cli():413        pass414415    @cli.group()416    def sub():417        pass418419    @sub.group()420    def bsub():421        pass422423    @sub.group(chain=True)424    def csub():425        pass426427    @csub.command()428    def dsub():429        pass430431    @csub.command()432    def esub():433        pass434435    assert choices_without_help(cli, ["sub"], "") == ["bsub", "csub"]436    assert choices_without_help(cli, ["sub"], "c") == ["csub"]437    assert choices_without_help(cli, ["sub", "csub"], "") == ["dsub", "esub"]438    assert choices_without_help(cli, ["sub", "csub", "dsub"], "") == ["esub"]439440441def test_hidden():442    @click.group()443    @click.option("--name", hidden=True)444    @click.option("--choices", type=click.Choice([1, 2]), hidden=True)445    def cli(name):446        pass447448    @cli.group(hidden=True)449    def hgroup():450        pass451452    @hgroup.group()453    def hgroupsub():454        pass455456    @cli.command()457    def asub():458        pass459460    @cli.command(hidden=True)461    @click.option("--hname")462    def hsub():463        pass464465    assert choices_without_help(cli, [], "--n") == []466    assert choices_without_help(cli, [], "--c") == []467    # If the user exactly types out the hidden param, complete its options.468    assert choices_without_help(cli, ["--choices"], "") == [1, 2]469    assert choices_without_help(cli, [], "") == ["asub"]470    assert choices_without_help(cli, [], "") == ["asub"]471    assert choices_without_help(cli, [], "h") == []472    # If the user exactly types out the hidden command, complete its subcommands.473    assert choices_without_help(cli, ["hgroup"], "") == ["hgroupsub"]474    assert choices_without_help(cli, ["hsub"], "--h") == ["--hname"]475476477@pytest.mark.parametrize(478    ("args", "part", "expect"),479    [480        ([], "-", ["--opt"]),481        (["value"], "--", ["--opt"]),482        ([], "-o", []),483        (["--opt"], "-o", []),484        (["--"], "", ["name", "-o", "--opt", "--"]),485        (["--"], "--o", ["--opt"]),486    ],487)488def test_args_with_double_dash_complete(args, part, expect):489    def _complete(ctx, args, incomplete):490        values = ["name", "-o", "--opt", "--"]491        return [x for x in values if x.startswith(incomplete)]492493    @click.command()494    @click.option("--opt")495    @click.argument("args", nargs=-1, autocompletion=_complete)496    def cli(opt, args):497        pass498499    assert choices_without_help(cli, args, part) == expect