1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
import pytest
import asyncclick as click
import asyncclick.shell_completion
from asyncclick.core import Argument
from asyncclick.core import Command
from asyncclick.core import Group
from asyncclick.core import Option
from asyncclick.shell_completion import add_completion_class
from asyncclick.shell_completion import CompletionItem
from asyncclick.shell_completion import ShellComplete
from asyncclick.types import Choice
from asyncclick.types import File
from asyncclick.types import Path
async def _get_completions(cli, args, incomplete):
comp = ShellComplete(cli, {}, cli.name, "_CLICK_COMPLETE")
return await comp.get_completions(args, incomplete)
async def _get_words(cli, args, incomplete):
return [c.value for c in await _get_completions(cli, args, incomplete)]
@pytest.mark.anyio
async def test_command():
cli = Command("cli", params=[Option(["-t", "--test"])])
assert await _get_words(cli, [], "") == []
assert await _get_words(cli, [], "-") == ["-t", "--test", "--help"]
assert await _get_words(cli, [], "--") == ["--test", "--help"]
assert await _get_words(cli, [], "--t") == ["--test"]
# -t has been seen, so --test isn't suggested
assert await _get_words(cli, ["-t", "a"], "-") == ["--help"]
@pytest.mark.anyio
async def test_group():
cli = Group("cli", params=[Option(["-a"])], commands=[Command("x"), Command("y")])
assert await _get_words(cli, [], "") == ["x", "y"]
assert await _get_words(cli, [], "-") == ["-a", "--help"]
@pytest.mark.anyio
async def test_group_command_same_option():
cli = Group(
"cli", params=[Option(["-a"])], commands=[Command("x", params=[Option(["-a"])])]
)
assert await _get_words(cli, [], "-") == ["-a", "--help"]
assert await _get_words(cli, ["-a", "a"], "-") == ["--help"]
assert await _get_words(cli, ["-a", "a", "x"], "-") == ["-a", "--help"]
assert await _get_words(cli, ["-a", "a", "x", "-a", "a"], "-") == ["--help"]
@pytest.mark.anyio
async def test_chained():
cli = Group(
"cli",
chain=True,
commands=[
Command("set", params=[Option(["-y"])]),
Command("start"),
Group("get", commands=[Command("full")]),
],
)
assert await _get_words(cli, [], "") == ["get", "set", "start"]
assert await _get_words(cli, [], "s") == ["set", "start"]
assert await _get_words(cli, ["set", "start"], "") == ["get"]
# subcommands and parent subcommands
assert await _get_words(cli, ["get"], "") == ["full", "set", "start"]
assert await _get_words(cli, ["get", "full"], "") == ["set", "start"]
assert await _get_words(cli, ["get"], "s") == ["set", "start"]
@pytest.mark.anyio
async def test_help_option():
cli = Group("cli", commands=[Command("with"), Command("no", add_help_option=False)])
assert await _get_words(cli, ["with"], "--") == ["--help"]
assert await _get_words(cli, ["no"], "--") == []
@pytest.mark.anyio
async def test_argument_order():
cli = Command(
"cli",
params=[
Argument(["plain"]),
Argument(["c1"], type=Choice(["a1", "a2", "b"])),
Argument(["c2"], type=Choice(["c1", "c2", "d"])),
],
)
# first argument has no completions
assert await _get_words(cli, [], "") == []
assert await _get_words(cli, [], "a") == []
# first argument filled, now completion can happen
assert await _get_words(cli, ["x"], "a") == ["a1", "a2"]
assert await _get_words(cli, ["x", "b"], "d") == ["d"]
@pytest.mark.anyio
async def test_argument_default():
cli = Command(
"cli",
add_help_option=False,
params=[
Argument(["a"], type=Choice(["a"]), default="a"),
Argument(["b"], type=Choice(["b"]), default="b"),
],
)
assert await _get_words(cli, [], "") == ["a"]
assert await _get_words(cli, ["a"], "b") == ["b"]
# ignore type validation
assert await _get_words(cli, ["x"], "b") == ["b"]
@pytest.mark.anyio
async def test_type_choice():
cli = Command("cli", params=[Option(["-c"], type=Choice(["a1", "a2", "b"]))])
assert await _get_words(cli, ["-c"], "") == ["a1", "a2", "b"]
assert await _get_words(cli, ["-c"], "a") == ["a1", "a2"]
assert await _get_words(cli, ["-c"], "a2") == ["a2"]
@pytest.mark.anyio
async def test_choice_special_characters():
cli = Command("cli", params=[Option(["-c"], type=Choice(["!1", "!2", "+3"]))])
assert await _get_words(cli, ["-c"], "") == ["!1", "!2", "+3"]
assert await _get_words(cli, ["-c"], "!") == ["!1", "!2"]
assert await _get_words(cli, ["-c"], "!2") == ["!2"]
@pytest.mark.anyio
async def test_choice_conflicting_prefix():
cli = Command(
"cli",
params=[
Option(["-c"], type=Choice(["!1", "!2", "+3"])),
Option(["+p"], is_flag=True),
],
)
assert await _get_words(cli, ["-c"], "") == ["!1", "!2", "+3"]
assert await _get_words(cli, ["-c"], "+") == ["+p"]
@pytest.mark.anyio
async def test_option_count():
cli = Command("cli", params=[Option(["-c"], count=True)])
assert await _get_words(cli, ["-c"], "") == []
assert await _get_words(cli, ["-c"], "-") == ["--help"]
@pytest.mark.anyio
async def test_option_optional():
cli = Command(
"cli",
add_help_option=False,
params=[
Option(["--name"], is_flag=False, flag_value="value"),
Option(["--flag"], is_flag=True),
],
)
assert await _get_words(cli, ["--name"], "") == []
assert await _get_words(cli, ["--name"], "-") == ["--flag"]
assert await _get_words(cli, ["--name", "--flag"], "-") == []
@pytest.mark.parametrize(
("type", "expect"),
[(File(), "file"), (Path(), "file"), (Path(file_okay=False), "dir")],
)
@pytest.mark.anyio
async def test_path_types(type, expect):
cli = Command("cli", params=[Option(["-f"], type=type)])
out = await _get_completions(cli, ["-f"], "ab")
assert len(out) == 1
c = out[0]
assert c.value == "ab"
assert c.type == expect
@pytest.mark.anyio
async def test_absolute_path():
cli = Command("cli", params=[Option(["-f"], type=Path())])
out = await _get_completions(cli, ["-f"], "/ab")
assert len(out) == 1
c = out[0]
assert c.value == "/ab"
@pytest.mark.anyio
async def test_option_flag():
cli = Command(
"cli",
add_help_option=False,
params=[
Option(["--on/--off"]),
Argument(["a"], type=Choice(["a1", "a2", "b"])),
],
)
assert await _get_words(cli, [], "--") == ["--on", "--off"]
# flag option doesn't take value, use choice argument
assert await _get_words(cli, ["--on"], "a") == ["a1", "a2"]
@pytest.mark.anyio
async def test_option_custom():
def custom(ctx, param, incomplete):
return [incomplete.upper()]
cli = Command(
"cli",
params=[
Argument(["x"]),
Argument(["y"]),
Argument(["z"], shell_complete=custom),
],
)
assert await _get_words(cli, ["a", "b"], "") == [""]
assert await _get_words(cli, ["a", "b"], "c") == ["C"]
@pytest.mark.anyio
async def test_option_multiple():
cli = Command(
"type",
params=[Option(["-m"], type=Choice(["a", "b"]), multiple=True), Option(["-f"])],
)
assert await _get_words(cli, ["-m"], "") == ["a", "b"]
assert "-m" in await _get_words(cli, ["-m", "a"], "-")
assert await _get_words(cli, ["-m", "a", "-m"], "") == ["a", "b"]
# used single options aren't suggested again
assert "-c" not in await _get_words(cli, ["-c", "f"], "-")
@pytest.mark.anyio
async def test_option_nargs():
cli = Command("cli", params=[Option(["-c"], type=Choice(["a", "b"]), nargs=2)])
assert await _get_words(cli, ["-c"], "") == ["a", "b"]
assert await _get_words(cli, ["-c", "a"], "") == ["a", "b"]
assert await _get_words(cli, ["-c", "a", "b"], "") == []
@pytest.mark.anyio
async def test_argument_nargs():
cli = Command(
"cli",
params=[
Argument(["x"], type=Choice(["a", "b"]), nargs=2),
Argument(["y"], type=Choice(["c", "d"]), nargs=-1),
Option(["-z"]),
],
)
assert await _get_words(cli, [], "") == ["a", "b"]
assert await _get_words(cli, ["a"], "") == ["a", "b"]
assert await _get_words(cli, ["a", "b"], "") == ["c", "d"]
assert await _get_words(cli, ["a", "b", "c"], "") == ["c", "d"]
assert await _get_words(cli, ["a", "b", "c", "d"], "") == ["c", "d"]
assert await _get_words(cli, ["a", "-z", "1"], "") == ["a", "b"]
assert await _get_words(cli, ["a", "-z", "1", "b"], "") == ["c", "d"]
@pytest.mark.anyio
async def test_double_dash():
cli = Command(
"cli",
add_help_option=False,
params=[
Option(["--opt"]),
Argument(["name"], type=Choice(["name", "--", "-o", "--opt"])),
],
)
assert await _get_words(cli, [], "-") == ["--opt"]
assert await _get_words(cli, ["value"], "-") == ["--opt"]
assert await _get_words(cli, [], "") == ["name", "--", "-o", "--opt"]
assert await _get_words(cli, ["--"], "") == ["name", "--", "-o", "--opt"]
@pytest.mark.anyio
async def test_hidden():
cli = Group(
"cli",
commands=[
Command(
"hidden",
add_help_option=False,
hidden=True,
params=[
Option(["-a"]),
Option(["-b"], type=Choice(["a", "b"]), hidden=True),
],
)
],
)
assert "hidden" not in await _get_words(cli, [], "")
assert "hidden" not in await _get_words(cli, [], "hidden")
assert await _get_words(cli, ["hidden"], "-") == ["-a"]
assert await _get_words(cli, ["hidden", "-b"], "") == ["a", "b"]
@pytest.mark.anyio
async def test_add_different_name():
cli = Group("cli", commands={"renamed": Command("original")})
words = await _get_words(cli, [], "")
assert "renamed" in words
assert "original" not in words
def test_completion_item_data():
c = CompletionItem("test", a=1)
assert c.a == 1
assert c.b is None
@pytest.fixture()
def _patch_for_completion(monkeypatch):
monkeypatch.setattr(
"asyncclick.shell_completion.BashComplete._check_version", lambda self: True
)
@pytest.mark.parametrize("shell", ["bash", "zsh", "fish"])
@pytest.mark.usefixtures("_patch_for_completion")
def test_full_source(runner, shell):
cli = Group("cli", commands=[Command("a"), Command("b")])
result = runner.invoke(cli, env={"_CLI_COMPLETE": f"{shell}_source"})
assert f"_CLI_COMPLETE={shell}_complete" in result.output
@pytest.mark.parametrize(
("shell", "env", "expect"),
[
("bash", {"COMP_WORDS": "", "COMP_CWORD": "0"}, "plain,a\nplain,b\n"),
("bash", {"COMP_WORDS": "a b", "COMP_CWORD": "1"}, "plain,b\n"),
("zsh", {"COMP_WORDS": "", "COMP_CWORD": "0"}, "plain\na\n_\nplain\nb\nbee\n"),
("zsh", {"COMP_WORDS": "a b", "COMP_CWORD": "1"}, "plain\nb\nbee\n"),
("fish", {"COMP_WORDS": "", "COMP_CWORD": ""}, "plain,a\nplain,b\tbee\n"),
("fish", {"COMP_WORDS": "a b", "COMP_CWORD": "b"}, "plain,b\tbee\n"),
],
)
@pytest.mark.usefixtures("_patch_for_completion")
def test_full_complete(runner, shell, env, expect):
cli = Group("cli", commands=[Command("a"), Command("b", help="bee")])
env["_CLI_COMPLETE"] = f"{shell}_complete"
result = runner.invoke(cli, env=env)
assert result.output == expect
@pytest.mark.usefixtures("_patch_for_completion")
def test_context_settings(runner):
def complete(ctx, param, incomplete):
return ctx.obj["choices"]
cli = Command("cli", params=[Argument("x", shell_complete=complete)])
result = runner.invoke(
cli,
obj={"choices": ["a", "b"]},
env={"COMP_WORDS": "", "COMP_CWORD": "0", "_CLI_COMPLETE": "bash_complete"},
)
assert result.output == "plain,a\nplain,b\n"
@pytest.mark.parametrize(("value", "expect"), [(False, ["Au", "al"]), (True, ["al"])])
@pytest.mark.anyio
async def test_choice_case_sensitive(value, expect):
cli = Command(
"cli",
params=[Option(["-a"], type=Choice(["Au", "al", "Bc"], case_sensitive=value))],
)
completions = await _get_words(cli, ["-a"], "a")
assert completions == expect
@pytest.fixture()
def _restore_available_shells(tmpdir):
prev_available_shells = click.shell_completion._available_shells.copy()
click.shell_completion._available_shells.clear()
yield
click.shell_completion._available_shells.clear()
click.shell_completion._available_shells.update(prev_available_shells)
@pytest.mark.usefixtures("_restore_available_shells")
def test_add_completion_class():
# At first, "mysh" is not in available shells
assert "mysh" not in click.shell_completion._available_shells
class MyshComplete(ShellComplete):
name = "mysh"
source_template = "dummy source"
# "mysh" still not in available shells because it is not registered
assert "mysh" not in click.shell_completion._available_shells
# Adding a completion class should return that class
assert add_completion_class(MyshComplete) is MyshComplete
# Now, "mysh" is finally in available shells
assert "mysh" in click.shell_completion._available_shells
assert click.shell_completion._available_shells["mysh"] is MyshComplete
@pytest.mark.usefixtures("_restore_available_shells")
def test_add_completion_class_with_name():
# At first, "mysh" is not in available shells
assert "mysh" not in click.shell_completion._available_shells
assert "not_mysh" not in click.shell_completion._available_shells
class MyshComplete(ShellComplete):
name = "not_mysh"
source_template = "dummy source"
# "mysh" and "not_mysh" are still not in available shells because
# it is not registered yet
assert "mysh" not in click.shell_completion._available_shells
assert "not_mysh" not in click.shell_completion._available_shells
# Adding a completion class should return that class.
# Because we are using the "name" parameter, the name isn't taken
# from the class.
assert add_completion_class(MyshComplete, name="mysh") is MyshComplete
# Now, "mysh" is finally in available shells
assert "mysh" in click.shell_completion._available_shells
assert "not_mysh" not in click.shell_completion._available_shells
assert click.shell_completion._available_shells["mysh"] is MyshComplete
@pytest.mark.usefixtures("_restore_available_shells")
def test_add_completion_class_decorator():
# At first, "mysh" is not in available shells
assert "mysh" not in click.shell_completion._available_shells
@add_completion_class
class MyshComplete(ShellComplete):
name = "mysh"
source_template = "dummy source"
# Using `add_completion_class` as a decorator adds the new shell immediately
assert "mysh" in click.shell_completion._available_shells
assert click.shell_completion._available_shells["mysh"] is MyshComplete
|