Skip to content
Myr

test_shell_completion.py

This file

Path

tests/test_shell_completion.py

240 lines · 7.8 KiB · Python

Last changed by

tests for new shell completion systemKai Chenbefore adoption7029307

History of this file

Take it

tests for new shell completion systemKai Chenbefore adoption7029307

lines 1–240why, 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 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