test_shell_completion.py
This file
Path
tests/test_shell_completion.py
344 lines · 11.0 KiB · Python
Last changed by
Merge pull request #2041 from spanglerco/shell-completion-option-valuesDavid Lordbefore adoptionef11be6
History of this fileTake it
1import pytest23from click.core import Argument4from click.core import Command5from click.core import Group6from click.core import Option7from click.shell_completion import CompletionItem8from click.shell_completion import ShellComplete9from click.types import Choice10from click.types import File11from click.types import Path121314def _get_completions(cli, args, incomplete):15 comp = ShellComplete(cli, {}, cli.name, "_CLICK_COMPLETE")16 return comp.get_completions(args, incomplete)171819def _get_words(cli, args, incomplete):20 return [c.value for c in _get_completions(cli, args, incomplete)]212223def test_command():24 cli = Command("cli", params=[Option(["-t", "--test"])])25 assert _get_words(cli, [], "") == []26 assert _get_words(cli, [], "-") == ["-t", "--test", "--help"]27 assert _get_words(cli, [], "--") == ["--test", "--help"]28 assert _get_words(cli, [], "--t") == ["--test"]29 # -t has been seen, so --test isn't suggested30 assert _get_words(cli, ["-t", "a"], "-") == ["--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_group_command_same_option():40 cli = Group(41 "cli", params=[Option(["-a"])], commands=[Command("x", params=[Option(["-a"])])]42 )43 assert _get_words(cli, [], "-") == ["-a", "--help"]44 assert _get_words(cli, ["-a", "a"], "-") == ["--help"]45 assert _get_words(cli, ["-a", "a", "x"], "-") == ["-a", "--help"]46 assert _get_words(cli, ["-a", "a", "x", "-a", "a"], "-") == ["--help"]474849def test_chained():50 cli = Group(51 "cli",52 chain=True,53 commands=[54 Command("set", params=[Option(["-y"])]),55 Command("start"),56 Group("get", commands=[Command("full")]),57 ],58 )59 assert _get_words(cli, [], "") == ["get", "set", "start"]60 assert _get_words(cli, [], "s") == ["set", "start"]61 assert _get_words(cli, ["set", "start"], "") == ["get"]62 # subcommands and parent subcommands63 assert _get_words(cli, ["get"], "") == ["full", "set", "start"]64 assert _get_words(cli, ["get", "full"], "") == ["set", "start"]65 assert _get_words(cli, ["get"], "s") == ["set", "start"]666768def test_help_option():69 cli = Group("cli", commands=[Command("with"), Command("no", add_help_option=False)])70 assert _get_words(cli, ["with"], "--") == ["--help"]71 assert _get_words(cli, ["no"], "--") == []727374def test_argument_order():75 cli = Command(76 "cli",77 params=[78 Argument(["plain"]),79 Argument(["c1"], type=Choice(["a1", "a2", "b"])),80 Argument(["c2"], type=Choice(["c1", "c2", "d"])),81 ],82 )83 # first argument has no completions84 assert _get_words(cli, [], "") == []85 assert _get_words(cli, [], "a") == []86 # first argument filled, now completion can happen87 assert _get_words(cli, ["x"], "a") == ["a1", "a2"]88 assert _get_words(cli, ["x", "b"], "d") == ["d"]899091def test_argument_default():92 cli = Command(93 "cli",94 add_help_option=False,95 params=[96 Argument(["a"], type=Choice(["a"]), default="a"),97 Argument(["b"], type=Choice(["b"]), default="b"),98 ],99 )100 assert _get_words(cli, [], "") == ["a"]101 assert _get_words(cli, ["a"], "b") == ["b"]102 # ignore type validation103 assert _get_words(cli, ["x"], "b") == ["b"]104105106def test_type_choice():107 cli = Command("cli", params=[Option(["-c"], type=Choice(["a1", "a2", "b"]))])108 assert _get_words(cli, ["-c"], "") == ["a1", "a2", "b"]109 assert _get_words(cli, ["-c"], "a") == ["a1", "a2"]110 assert _get_words(cli, ["-c"], "a2") == ["a2"]111112113def test_choice_special_characters():114 cli = Command("cli", params=[Option(["-c"], type=Choice(["!1", "!2", "+3"]))])115 assert _get_words(cli, ["-c"], "") == ["!1", "!2", "+3"]116 assert _get_words(cli, ["-c"], "!") == ["!1", "!2"]117 assert _get_words(cli, ["-c"], "!2") == ["!2"]118119120def test_choice_conflicting_prefix():121 cli = Command(122 "cli",123 params=[124 Option(["-c"], type=Choice(["!1", "!2", "+3"])),125 Option(["+p"], is_flag=True),126 ],127 )128 assert _get_words(cli, ["-c"], "") == ["!1", "!2", "+3"]129 assert _get_words(cli, ["-c"], "+") == ["+p"]130131132def test_option_count():133 cli = Command("cli", params=[Option(["-c"], count=True)])134 assert _get_words(cli, ["-c"], "") == []135 assert _get_words(cli, ["-c"], "-") == ["--help"]136137138def test_option_optional():139 cli = Command(140 "cli",141 add_help_option=False,142 params=[143 Option(["--name"], is_flag=False, flag_value="value"),144 Option(["--flag"], is_flag=True),145 ],146 )147 assert _get_words(cli, ["--name"], "") == []148 assert _get_words(cli, ["--name"], "-") == ["--flag"]149 assert _get_words(cli, ["--name", "--flag"], "-") == []150151152@pytest.mark.parametrize(153 ("type", "expect"),154 [(File(), "file"), (Path(), "file"), (Path(file_okay=False), "dir")],155)156def test_path_types(type, expect):157 cli = Command("cli", params=[Option(["-f"], type=type)])158 out = _get_completions(cli, ["-f"], "ab")159 assert len(out) == 1160 c = out[0]161 assert c.value == "ab"162 assert c.type == expect163164165def test_absolute_path():166 cli = Command("cli", params=[Option(["-f"], type=Path())])167 out = _get_completions(cli, ["-f"], "/ab")168 assert len(out) == 1169 c = out[0]170 assert c.value == "/ab"171172173def test_option_flag():174 cli = Command(175 "cli",176 add_help_option=False,177 params=[178 Option(["--on/--off"]),179 Argument(["a"], type=Choice(["a1", "a2", "b"])),180 ],181 )182 assert _get_words(cli, [], "--") == ["--on", "--off"]183 # flag option doesn't take value, use choice argument184 assert _get_words(cli, ["--on"], "a") == ["a1", "a2"]185186187def test_option_custom():188 def custom(ctx, param, incomplete):189 return [incomplete.upper()]190191 cli = Command(192 "cli",193 params=[194 Argument(["x"]),195 Argument(["y"]),196 Argument(["z"], shell_complete=custom),197 ],198 )199 assert _get_words(cli, ["a", "b"], "") == [""]200 assert _get_words(cli, ["a", "b"], "c") == ["C"]201202203def test_option_multiple():204 cli = Command(205 "type",206 params=[Option(["-m"], type=Choice(["a", "b"]), multiple=True), Option(["-f"])],207 )208 assert _get_words(cli, ["-m"], "") == ["a", "b"]209 assert "-m" in _get_words(cli, ["-m", "a"], "-")210 assert _get_words(cli, ["-m", "a", "-m"], "") == ["a", "b"]211 # used single options aren't suggested again212 assert "-c" not in _get_words(cli, ["-c", "f"], "-")213214215def test_option_nargs():216 cli = Command("cli", params=[Option(["-c"], type=Choice(["a", "b"]), nargs=2)])217 assert _get_words(cli, ["-c"], "") == ["a", "b"]218 assert _get_words(cli, ["-c", "a"], "") == ["a", "b"]219 assert _get_words(cli, ["-c", "a", "b"], "") == []220221222def test_argument_nargs():223 cli = Command(224 "cli",225 params=[226 Argument(["x"], type=Choice(["a", "b"]), nargs=2),227 Argument(["y"], type=Choice(["c", "d"]), nargs=-1),228 Option(["-z"]),229 ],230 )231 assert _get_words(cli, [], "") == ["a", "b"]232 assert _get_words(cli, ["a"], "") == ["a", "b"]233 assert _get_words(cli, ["a", "b"], "") == ["c", "d"]234 assert _get_words(cli, ["a", "b", "c"], "") == ["c", "d"]235 assert _get_words(cli, ["a", "b", "c", "d"], "") == ["c", "d"]236 assert _get_words(cli, ["a", "-z", "1"], "") == ["a", "b"]237 assert _get_words(cli, ["a", "-z", "1", "b"], "") == ["c", "d"]238239240def test_double_dash():241 cli = Command(242 "cli",243 add_help_option=False,244 params=[245 Option(["--opt"]),246 Argument(["name"], type=Choice(["name", "--", "-o", "--opt"])),247 ],248 )249 assert _get_words(cli, [], "-") == ["--opt"]250 assert _get_words(cli, ["value"], "-") == ["--opt"]251 assert _get_words(cli, [], "") == ["name", "--", "-o", "--opt"]252 assert _get_words(cli, ["--"], "") == ["name", "--", "-o", "--opt"]253254255def test_hidden():256 cli = Group(257 "cli",258 commands=[259 Command(260 "hidden",261 add_help_option=False,262 hidden=True,263 params=[264 Option(["-a"]),265 Option(["-b"], type=Choice(["a", "b"]), hidden=True),266 ],267 )268 ],269 )270 assert "hidden" not in _get_words(cli, [], "")271 assert "hidden" not in _get_words(cli, [], "hidden")272 assert _get_words(cli, ["hidden"], "-") == ["-a"]273 assert _get_words(cli, ["hidden", "-b"], "") == ["a", "b"]274275276def test_add_different_name():277 cli = Group("cli", commands={"renamed": Command("original")})278 words = _get_words(cli, [], "")279 assert "renamed" in words280 assert "original" not in words281282283def test_completion_item_data():284 c = CompletionItem("test", a=1)285 assert c.a == 1286 assert c.b is None287288289@pytest.fixture()290def _patch_for_completion(monkeypatch):291 monkeypatch.setattr(292 "click.shell_completion.BashComplete._check_version", lambda self: True293 )294295296@pytest.mark.parametrize("shell", ["bash", "zsh", "fish"])297@pytest.mark.usefixtures("_patch_for_completion")298def test_full_source(runner, shell):299 cli = Group("cli", commands=[Command("a"), Command("b")])300 result = runner.invoke(cli, env={"_CLI_COMPLETE": f"{shell}_source"})301 assert f"_CLI_COMPLETE={shell}_complete" in result.output302303304@pytest.mark.parametrize(305 ("shell", "env", "expect"),306 [307 ("bash", {"COMP_WORDS": "", "COMP_CWORD": "0"}, "plain,a\nplain,b\n"),308 ("bash", {"COMP_WORDS": "a b", "COMP_CWORD": "1"}, "plain,b\n"),309 ("zsh", {"COMP_WORDS": "", "COMP_CWORD": "0"}, "plain\na\n_\nplain\nb\nbee\n"),310 ("zsh", {"COMP_WORDS": "a b", "COMP_CWORD": "1"}, "plain\nb\nbee\n"),311 ("fish", {"COMP_WORDS": "", "COMP_CWORD": ""}, "plain,a\nplain,b\tbee\n"),312 ("fish", {"COMP_WORDS": "a b", "COMP_CWORD": "b"}, "plain,b\tbee\n"),313 ],314)315@pytest.mark.usefixtures("_patch_for_completion")316def test_full_complete(runner, shell, env, expect):317 cli = Group("cli", commands=[Command("a"), Command("b", help="bee")])318 env["_CLI_COMPLETE"] = f"{shell}_complete"319 result = runner.invoke(cli, env=env)320 assert result.output == expect321322323@pytest.mark.usefixtures("_patch_for_completion")324def test_context_settings(runner):325 def complete(ctx, param, incomplete):326 return ctx.obj["choices"]327328 cli = Command("cli", params=[Argument("x", shell_complete=complete)])329 result = runner.invoke(330 cli,331 obj={"choices": ["a", "b"]},332 env={"COMP_WORDS": "", "COMP_CWORD": "0", "_CLI_COMPLETE": "bash_complete"},333 )334 assert result.output == "plain,a\nplain,b\n"335336337@pytest.mark.parametrize(("value", "expect"), [(False, ["Au", "al"]), (True, ["al"])])338def test_choice_case_sensitive(value, expect):339 cli = Command(340 "cli",341 params=[Option(["-a"], type=Choice(["Au", "al", "Bc"], case_sensitive=value))],342 )343 completions = _get_words(cli, ["-a"], "a")344 assert completions == expect