Skip to content
Myr

test_shell_completion.py

This file

Path

tests/test_shell_completion.py

277 lines · 9.0 KiB · Python

Last changed by

Merge pull request #1622 from MLH-Fellowship/1484-redesign-completionDavid Lordbefore adoption6c8301e

History of this file

Take it

Merge pull request #1622 from MLH-Fellowship/1484-redesign-completionDavid Lordbefore adoption6c8301e

lines 1–277why, one step back
1import sys
23import pytest45from click.core import Argument6from click.core import Command7from click.core import Group8from click.core import Option9from click.shell_completion import CompletionItem10from click.shell_completion import ShellComplete11from click.types import Choice12from click.types import File13from click.types import Path141516def _get_completions(cli, args, incomplete):17 comp = ShellComplete(cli, cli.name, "_CLICK_COMPLETE")18 return comp.get_completions(args, incomplete)192021def _get_words(cli, args, incomplete):22 return [c.value for c in _get_completions(cli, args, incomplete)]232425def test_command():26 cli = Command("cli", params=[Option(["-t", "--test"])])27 assert _get_words(cli, [], "") == []28 assert _get_words(cli, [], "-") == ["-t", "--test", "--help"]29 assert _get_words(cli, [], "--") == ["--test", "--help"]30 assert _get_words(cli, [], "--t") == ["--test"]31 # -t has been seen, so --test isn't suggested32 assert _get_words(cli, ["-t", "a"], "-") == ["--help"]333435def test_group():36 cli = Group("cli", params=[Option(["-a"])], commands=[Command("x"), Command("y")])37 assert _get_words(cli, [], "") == ["x", "y"]38 assert _get_words(cli, [], "-") == ["-a", "--help"]394041def test_group_command_same_option():42 cli = Group(43 "cli", params=[Option(["-a"])], commands=[Command("x", params=[Option(["-a"])])]44 )45 assert _get_words(cli, [], "-") == ["-a", "--help"]46 assert _get_words(cli, ["-a", "a"], "-") == ["--help"]47 assert _get_words(cli, ["-a", "a", "x"], "-") == ["-a", "--help"]48 assert _get_words(cli, ["-a", "a", "x", "-a", "a"], "-") == ["--help"]495051def test_chained():52 cli = Group(53 "cli",54 chain=True,55 commands=[56 Command("set", params=[Option(["-y"])]),57 Command("start"),58 Group("get", commands=[Command("full")]),59 ],60 )61 assert _get_words(cli, [], "") == ["get", "set", "start"]62 assert _get_words(cli, [], "s") == ["set", "start"]63 assert _get_words(cli, ["set", "start"], "") == ["get"]64 # subcommands and parent subcommands65 assert _get_words(cli, ["get"], "") == ["full", "set", "start"]66 assert _get_words(cli, ["get", "full"], "") == ["set", "start"]67 assert _get_words(cli, ["get"], "s") == ["set", "start"]686970def test_help_option():71 cli = Group("cli", commands=[Command("with"), Command("no", add_help_option=False)])72 assert _get_words(cli, ["with"], "--") == ["--help"]73 assert _get_words(cli, ["no"], "--") == []747576def test_argument_order():77 cli = Command(78 "cli",79 params=[80 Argument(["plain"]),81 Argument(["c1"], type=Choice(["a1", "a2", "b"])),82 Argument(["c2"], type=Choice(["c1", "c2", "d"])),83 ],84 )85 # first argument has no completions86 assert _get_words(cli, [], "") == []87 assert _get_words(cli, [], "a") == []88 # first argument filled, now completion can happen89 assert _get_words(cli, ["x"], "a") == ["a1", "a2"]90 assert _get_words(cli, ["x", "b"], "d") == ["d"]919293def test_type_choice():94 cli = Command("cli", params=[Option(["-c"], type=Choice(["a1", "a2", "b"]))])95 assert _get_words(cli, ["-c"], "") == ["a1", "a2", "b"]96 assert _get_words(cli, ["-c"], "a") == ["a1", "a2"]97 assert _get_words(cli, ["-c"], "a2") == ["a2"]9899100@pytest.mark.parametrize(101 ("type", "expect"),102 [(File(), "file"), (Path(), "file"), (Path(file_okay=False), "dir")],103)104def test_path_types(type, expect):105 cli = Command("cli", params=[Option(["-f"], type=type)])106 out = _get_completions(cli, ["-f"], "ab")107 assert len(out) == 1108 c = out[0]109 assert c.value == "ab"110 assert c.type == expect111112113def test_option_flag():114 cli = Command(115 "cli",116 add_help_option=False,117 params=[118 Option(["--on/--off"]),119 Argument(["a"], type=Choice(["a1", "a2", "b"])),120 ],121 )122 assert _get_words(cli, ["type"], "--") == ["--on", "--off"]123 # flag option doesn't take value, use choice argument124 assert _get_words(cli, ["x", "--on"], "a") == ["a1", "a2"]125126127def test_option_custom():128 def custom(ctx, param, incomplete):129 return [incomplete.upper()]130131 cli = Command(132 "cli",133 params=[134 Argument(["x"]),135 Argument(["y"]),136 Argument(["z"], shell_complete=custom),137 ],138 )139 assert _get_words(cli, ["a", "b"], "") == [""]140 assert _get_words(cli, ["a", "b"], "c") == ["C"]141142143def test_autocompletion_deprecated():144 # old function takes args and not param, returns all values, can mix145 # strings and tuples146 def custom(ctx, args, incomplete):147 assert isinstance(args, list)148 return [("art", "x"), "bat", "cat"]149150 with pytest.deprecated_call():151 cli = Command("cli", params=[Argument(["x"], autocompletion=custom)])152153 assert _get_words(cli, [], "") == ["art", "bat", "cat"]154 assert _get_words(cli, [], "c") == ["cat"]155156157def test_option_multiple():158 cli = Command(159 "type",160 params=[Option(["-m"], type=Choice(["a", "b"]), multiple=True), Option(["-f"])],161 )162 assert _get_words(cli, ["-m"], "") == ["a", "b"]163 assert "-m" in _get_words(cli, ["-m", "a"], "-")164 assert _get_words(cli, ["-m", "a", "-m"], "") == ["a", "b"]165 # used single options aren't suggested again166 assert "-c" not in _get_words(cli, ["-c", "f"], "-")167168169def test_option_nargs():170 cli = Command("cli", params=[Option(["-c"], type=Choice(["a", "b"]), nargs=2)])171 assert _get_words(cli, ["-c"], "") == ["a", "b"]172 assert _get_words(cli, ["-c", "a"], "") == ["a", "b"]173 assert _get_words(cli, ["-c", "a", "b"], "") == []174175176def test_argument_nargs():177 cli = Command(178 "cli",179 params=[180 Argument(["x"], type=Choice(["a", "b"]), nargs=2),181 Argument(["y"], type=Choice(["c", "d"]), nargs=-1),182 Option(["-z"]),183 ],184 )185 assert _get_words(cli, [], "") == ["a", "b"]186 assert _get_words(cli, ["a"], "") == ["a", "b"]187 assert _get_words(cli, ["a", "b"], "") == ["c", "d"]188 assert _get_words(cli, ["a", "b", "c"], "") == ["c", "d"]189 assert _get_words(cli, ["a", "b", "c", "d"], "") == ["c", "d"]190 assert _get_words(cli, ["a", "-z", "1"], "") == ["a", "b"]191 assert _get_words(cli, ["a", "-z", "1", "b"], "") == ["c", "d"]192193194def test_double_dash():195 cli = Command(196 "cli",197 add_help_option=False,198 params=[199 Option(["--opt"]),200 Argument(["name"], type=Choice(["name", "--", "-o", "--opt"])),201 ],202 )203 assert _get_words(cli, [], "-") == ["--opt"]204 assert _get_words(cli, ["value"], "-") == ["--opt"]205 assert _get_words(cli, [], "") == ["name", "--", "-o", "--opt"]206 assert _get_words(cli, ["--"], "") == ["name", "--", "-o", "--opt"]207208209def test_hidden():210 cli = Group(211 "cli",212 commands=[213 Command(214 "hidden",215 add_help_option=False,216 hidden=True,217 params=[218 Option(["-a"]),219 Option(["-b"], type=Choice(["a", "b"]), hidden=True),220 ],221 )222 ],223 )224 assert "hidden" not in _get_words(cli, [], "")225 assert "hidden" not in _get_words(cli, [], "hidden")226 assert _get_words(cli, ["hidden"], "-") == ["-a"]227 assert _get_words(cli, ["hidden", "-b"], "") == ["a", "b"]228229230def test_add_different_name():231 cli = Group("cli", commands={"renamed": Command("original")})232 words = _get_words(cli, [], "")233 assert "renamed" in words234 assert "original" not in words235236237def test_completion_item_data():238 c = CompletionItem("test", a=1)239 assert c.a == 1240 assert c.b is None241242243@pytest.fixture()244def _patch_for_completion(monkeypatch):245 monkeypatch.setattr("click.core._fast_exit", sys.exit)246 monkeypatch.setattr(247 "click.shell_completion.BashComplete._check_version", lambda self: True248 )249250251@pytest.mark.parametrize(252 "shell", ["bash", "zsh", "fish"],253)254@pytest.mark.usefixtures("_patch_for_completion")255def test_full_source(runner, shell):256 cli = Group("cli", commands=[Command("a"), Command("b")])257 result = runner.invoke(cli, env={"_CLI_COMPLETE": f"source_{shell}"})258 assert f"_CLI_COMPLETE=complete_{shell}" in result.output259260261@pytest.mark.parametrize(262 ("shell", "env", "expect"),263 [264 ("bash", {"COMP_WORDS": "", "COMP_CWORD": "0"}, "plain,a\nplain,b\n"),265 ("bash", {"COMP_WORDS": "a b", "COMP_CWORD": "1"}, "plain,b\n"),266 ("zsh", {"COMP_WORDS": "", "COMP_CWORD": "0"}, "plain\na\n_\nplain\nb\nbee\n"),267 ("zsh", {"COMP_WORDS": "a b", "COMP_CWORD": "1"}, "plain\nb\nbee\n"),268 ("fish", {"COMP_WORDS": "", "COMP_CWORD": ""}, "plain,a\nplain,b\tbee\n"),269 ("fish", {"COMP_WORDS": "a b", "COMP_CWORD": "b"}, "plain,b\tbee\n"),270 ],271)272@pytest.mark.usefixtures("_patch_for_completion")273def test_full_complete(runner, shell, env, expect):274 cli = Group("cli", commands=[Command("a"), Command("b", help="bee")])275 env["_CLI_COMPLETE"] = f"complete_{shell}"276 result = runner.invoke(cli, env=env)277 assert result.output == expect