| 1 |
"""Unit/functional testing for prompt-toolkit tab completion functions in the cmd2.py module. |
| 2 |
|
| 3 |
These are primarily tests related to prompt-toolkit completer functions which handle tab completion of cmd2/cmd commands, |
| 4 |
file system paths, and shell commands. |
| 5 |
""" |
| 6 |
|
| 7 |
import argparse |
| 8 |
import copy |
| 9 |
import dataclasses |
| 10 |
import enum |
| 11 |
import os |
| 12 |
import sys |
| 13 |
from typing import NoReturn |
| 14 |
from unittest import mock |
| 15 |
|
| 16 |
import pytest |
| 17 |
|
| 18 |
import cmd2 |
| 19 |
from cmd2 import ( |
| 20 |
Choices, |
| 21 |
CompletionItem, |
| 22 |
Completions, |
| 23 |
utils, |
| 24 |
) |
| 25 |
|
| 26 |
from .conftest import ( |
| 27 |
normalize, |
| 28 |
run_cmd, |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
class SubcommandsExample(cmd2.Cmd): |
| 33 |
"""Example cmd2 application where we a base command which has a couple subcommands |
| 34 |
and the "sport" subcommand has tab completion enabled. |
| 35 |
""" |
| 36 |
|
| 37 |
sport_item_strs = ("Bat", "Basket", "Basketball", "Football", "Space Ball") |
| 38 |
|
| 39 |
|
| 40 |
base_parser = cmd2.Cmd2ArgumentParser() |
| 41 |
base_subparsers = base_parser.add_subparsers(title="subcommands", help="subcommand help") |
| 42 |
|
| 43 |
|
| 44 |
parser_foo = base_subparsers.add_parser("foo", help="foo help") |
| 45 |
parser_foo.add_argument("-x", type=int, default=1, help="integer") |
| 46 |
parser_foo.add_argument("y", type=float, help="float") |
| 47 |
parser_foo.add_argument("input_file", type=str, help="Input File") |
| 48 |
|
| 49 |
|
| 50 |
parser_bar = base_subparsers.add_parser("bar", help="bar help") |
| 51 |
|
| 52 |
bar_subparsers = parser_bar.add_subparsers(title="layer3", help="help for 3rd layer of commands") |
| 53 |
parser_bar.add_argument("z", help="string") |
| 54 |
|
| 55 |
bar_subparsers.add_parser("apple", help="apple help") |
| 56 |
bar_subparsers.add_parser("artichoke", help="artichoke help") |
| 57 |
bar_subparsers.add_parser("cranberries", help="cranberries help") |
| 58 |
|
| 59 |
|
| 60 |
parser_sport = base_subparsers.add_parser("sport", help="sport help") |
| 61 |
sport_arg = parser_sport.add_argument("sport", help="Enter name of a sport", choices=sport_item_strs) |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
base2_parser = cmd2.Cmd2ArgumentParser(add_help=False) |
| 66 |
base2_subparsers = base2_parser.add_subparsers(title="subcommands", help="subcommand help") |
| 67 |
|
| 68 |
|
| 69 |
parser_foo2 = base2_subparsers.add_parser("foo", help="foo help") |
| 70 |
parser_foo2.add_argument("-x", type=int, default=1, help="integer") |
| 71 |
parser_foo2.add_argument("y", type=float, help="float") |
| 72 |
parser_foo2.add_argument("input_file", type=str, help="Input File") |
| 73 |
|
| 74 |
|
| 75 |
parser_bar2 = base2_subparsers.add_parser("bar", help="bar help") |
| 76 |
|
| 77 |
bar2_subparsers = parser_bar2.add_subparsers(title="layer3", help="help for 3rd layer of commands") |
| 78 |
parser_bar2.add_argument("z", help="string") |
| 79 |
|
| 80 |
bar2_subparsers.add_parser("apple", help="apple help") |
| 81 |
bar2_subparsers.add_parser("artichoke", help="artichoke help") |
| 82 |
bar2_subparsers.add_parser("cranberries", help="cranberries help") |
| 83 |
|
| 84 |
|
| 85 |
parser_sport2 = base2_subparsers.add_parser("sport", help="sport help") |
| 86 |
sport2_arg = parser_sport2.add_argument("sport", help="Enter name of a sport", choices=sport_item_strs) |
| 87 |
|
| 88 |
def __init__(self) -> None: |
| 89 |
super().__init__() |
| 90 |
|
| 91 |
|
| 92 |
def base_foo(self, args) -> None: |
| 93 |
"""Foo subcommand of base command.""" |
| 94 |
self.poutput(args.x * args.y) |
| 95 |
|
| 96 |
def base_bar(self, args) -> None: |
| 97 |
"""Bar subcommand of base command.""" |
| 98 |
self.poutput(f"(({args.z}))") |
| 99 |
|
| 100 |
def base_sport(self, args) -> None: |
| 101 |
"""Sport subcommand of base command.""" |
| 102 |
self.poutput(f"Sport is {args.sport}") |
| 103 |
|
| 104 |
|
| 105 |
parser_foo.set_defaults(func=base_foo) |
| 106 |
parser_bar.set_defaults(func=base_bar) |
| 107 |
parser_sport.set_defaults(func=base_sport) |
| 108 |
|
| 109 |
@cmd2.with_argparser(base_parser) |
| 110 |
def do_base(self, args) -> None: |
| 111 |
"""Base command help.""" |
| 112 |
func = getattr(args, "func", None) |
| 113 |
if func is not None: |
| 114 |
|
| 115 |
func(self, args) |
| 116 |
else: |
| 117 |
|
| 118 |
self.do_help("base") |
| 119 |
|
| 120 |
@cmd2.with_argparser(base2_parser) |
| 121 |
def do_alternate(self, args) -> None: |
| 122 |
"""Alternate command help.""" |
| 123 |
func = getattr(args, "func", None) |
| 124 |
if func is not None: |
| 125 |
|
| 126 |
func(self, args) |
| 127 |
else: |
| 128 |
|
| 129 |
self.do_help("alternate") |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
food_item_strs = ["Pizza", "Ham", "Ham Sandwich", "Potato", 'Cheese "Pizza"'] |
| 134 |
sport_item_strs = ["Bat", "Basket", "Basketball", "Football", "Space Ball"] |
| 135 |
delimited_strs = [ |
| 136 |
"/home/user/file.txt", |
| 137 |
"/home/user/file space.txt", |
| 138 |
"/home/user/prog.c", |
| 139 |
"/home/other user/maps", |
| 140 |
"/home/other user/tests", |
| 141 |
] |
| 142 |
|
| 143 |
|
| 144 |
class CompletionsExample(cmd2.Cmd): |
| 145 |
"""Example cmd2 application used to exercise tab completion tests""" |
| 146 |
|
| 147 |
def __init__(self) -> None: |
| 148 |
cmd2.Cmd.__init__(self, multiline_commands=["test_multiline"]) |
| 149 |
self.foo = "bar" |
| 150 |
self.add_settable( |
| 151 |
utils.Settable( |
| 152 |
"foo", |
| 153 |
str, |
| 154 |
description="a test settable param", |
| 155 |
settable_object=self, |
| 156 |
completer=CompletionsExample.complete_foo_val, |
| 157 |
) |
| 158 |
) |
| 159 |
|
| 160 |
def do_test_basic(self, args) -> None: |
| 161 |
pass |
| 162 |
|
| 163 |
def complete_test_basic(self, text, line, begidx, endidx) -> Completions: |
| 164 |
return self.basic_complete(text, line, begidx, endidx, food_item_strs) |
| 165 |
|
| 166 |
def do_test_delimited(self, args) -> None: |
| 167 |
pass |
| 168 |
|
| 169 |
def complete_test_delimited(self, text, line, begidx, endidx) -> Completions: |
| 170 |
return self.delimiter_complete(text, line, begidx, endidx, delimited_strs, "/") |
| 171 |
|
| 172 |
def do_test_sort_key(self, args) -> None: |
| 173 |
pass |
| 174 |
|
| 175 |
def complete_test_sort_key(self, text, line, begidx, endidx) -> Completions: |
| 176 |
num_strs = ["file2", "file11", "file1"] |
| 177 |
return self.basic_complete(text, line, begidx, endidx, num_strs) |
| 178 |
|
| 179 |
def do_test_raise_exception(self, args) -> None: |
| 180 |
pass |
| 181 |
|
| 182 |
def complete_test_raise_exception(self, text, line, begidx, endidx) -> NoReturn: |
| 183 |
raise IndexError("You are out of bounds!!") |
| 184 |
|
| 185 |
def do_test_multiline(self, args) -> None: |
| 186 |
pass |
| 187 |
|
| 188 |
def complete_test_multiline(self, text, line, begidx, endidx) -> Completions: |
| 189 |
return self.basic_complete(text, line, begidx, endidx, sport_item_strs) |
| 190 |
|
| 191 |
def do_test_no_completer(self, args) -> None: |
| 192 |
"""Completing this should result in completedefault() being called""" |
| 193 |
|
| 194 |
def complete_foo_val(self, text, line, begidx, endidx, arg_tokens) -> Completions: |
| 195 |
"""Supports unit testing cmd2.Cmd2.complete_set_val to confirm it passes all tokens in the set command""" |
| 196 |
value = "SUCCESS" if "param" in arg_tokens else "FAIL" |
| 197 |
return Completions.from_values([value]) |
| 198 |
|
| 199 |
def completedefault(self, *ignored) -> Completions: |
| 200 |
"""Method called to complete an input line when no command-specific |
| 201 |
complete_*() method is available. |
| 202 |
|
| 203 |
""" |
| 204 |
return Completions.from_values(["default"]) |
| 205 |
|
| 206 |
|
| 207 |
@pytest.fixture |
| 208 |
def cmd_app(): |
| 209 |
return CompletionsExample() |
| 210 |
|
| 211 |
|
| 212 |
def test_command_completion(cmd_app) -> None: |
| 213 |
text = "run" |
| 214 |
line = text |
| 215 |
endidx = len(line) |
| 216 |
begidx = endidx - len(text) |
| 217 |
|
| 218 |
expected = ["run_pyscript", "run_script"] |
| 219 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 220 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 221 |
|
| 222 |
|
| 223 |
def test_command_completion_nomatch(cmd_app) -> None: |
| 224 |
text = "fakecommand" |
| 225 |
line = text |
| 226 |
endidx = len(line) |
| 227 |
begidx = endidx - len(text) |
| 228 |
|
| 229 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 230 |
assert not completions |
| 231 |
|
| 232 |
|
| 233 |
assert "Hint" in completions.error |
| 234 |
|
| 235 |
|
| 236 |
def test_complete_bogus_command(cmd_app) -> None: |
| 237 |
text = "" |
| 238 |
line = f"fizbuzz {text}" |
| 239 |
endidx = len(line) |
| 240 |
begidx = endidx - len(text) |
| 241 |
|
| 242 |
expected = ["default"] |
| 243 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 244 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 245 |
|
| 246 |
|
| 247 |
def test_complete_exception(cmd_app) -> None: |
| 248 |
text = "" |
| 249 |
line = f"test_raise_exception {text}" |
| 250 |
endidx = len(line) |
| 251 |
begidx = endidx - len(text) |
| 252 |
|
| 253 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 254 |
|
| 255 |
assert not completions |
| 256 |
assert "IndexError" in completions.error |
| 257 |
|
| 258 |
|
| 259 |
def test_complete_macro(base_app, request) -> None: |
| 260 |
|
| 261 |
out, _err = run_cmd(base_app, "macro create fake run_pyscript {1}") |
| 262 |
assert out == normalize("Macro 'fake' created") |
| 263 |
|
| 264 |
|
| 265 |
test_dir = os.path.dirname(request.module.__file__) |
| 266 |
|
| 267 |
text = os.path.join(test_dir, "s") |
| 268 |
line = f"fake {text}" |
| 269 |
|
| 270 |
endidx = len(line) |
| 271 |
begidx = endidx - len(text) |
| 272 |
|
| 273 |
expected = [text + "cript.py", text + "cript.txt", text + "cripts" + os.path.sep] |
| 274 |
completions = base_app.complete(text, line, begidx, endidx) |
| 275 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 276 |
|
| 277 |
|
| 278 |
def test_default_str_sort_key(cmd_app) -> None: |
| 279 |
text = "" |
| 280 |
line = f"test_sort_key {text}" |
| 281 |
endidx = len(line) |
| 282 |
begidx = endidx - len(text) |
| 283 |
|
| 284 |
saved_sort_key = utils.DEFAULT_STR_SORT_KEY |
| 285 |
|
| 286 |
try: |
| 287 |
|
| 288 |
utils.set_default_str_sort_key(utils.ALPHABETICAL_SORT_KEY) |
| 289 |
expected = ["file1", "file11", "file2"] |
| 290 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 291 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 292 |
|
| 293 |
|
| 294 |
utils.set_default_str_sort_key(utils.NATURAL_SORT_KEY) |
| 295 |
expected = ["file1", "file2", "file11"] |
| 296 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 297 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 298 |
finally: |
| 299 |
utils.set_default_str_sort_key(saved_sort_key) |
| 300 |
|
| 301 |
|
| 302 |
def test_help_completion(cmd_app) -> None: |
| 303 |
text = "h" |
| 304 |
line = f"help {text}" |
| 305 |
endidx = len(line) |
| 306 |
begidx = endidx - len(text) |
| 307 |
|
| 308 |
expected = ["help", "history"] |
| 309 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 310 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 311 |
|
| 312 |
|
| 313 |
def test_help_completion_empty_arg(cmd_app) -> None: |
| 314 |
text = "" |
| 315 |
line = f"help {text}" |
| 316 |
endidx = len(line) |
| 317 |
begidx = endidx - len(text) |
| 318 |
|
| 319 |
expected = cmd_app.get_visible_commands() |
| 320 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 321 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 322 |
|
| 323 |
|
| 324 |
def test_help_completion_nomatch(cmd_app) -> None: |
| 325 |
text = "fakecommand" |
| 326 |
line = f"help {text}" |
| 327 |
endidx = len(line) |
| 328 |
begidx = endidx - len(text) |
| 329 |
|
| 330 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 331 |
assert not completions |
| 332 |
|
| 333 |
|
| 334 |
def test_set_allow_style_completion(cmd_app) -> None: |
| 335 |
"""Confirm that completing allow_style presents AllowStyle strings""" |
| 336 |
text = "" |
| 337 |
line = "set allow_style" |
| 338 |
endidx = len(line) |
| 339 |
begidx = endidx - len(text) |
| 340 |
|
| 341 |
expected = [val.name.lower() for val in cmd2.rich_utils.AllowStyle] |
| 342 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 343 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 344 |
|
| 345 |
|
| 346 |
def test_set_bool_completion(cmd_app) -> None: |
| 347 |
"""Confirm that completing a boolean Settable presents true and false strings""" |
| 348 |
text = "" |
| 349 |
line = "set debug" |
| 350 |
endidx = len(line) |
| 351 |
begidx = endidx - len(text) |
| 352 |
|
| 353 |
expected = ["false", "true"] |
| 354 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 355 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 356 |
|
| 357 |
|
| 358 |
def test_shell_command_completion_shortcut(cmd_app) -> None: |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
if sys.platform == "win32": |
| 363 |
text = "!calc" |
| 364 |
expected_item = CompletionItem("!calc.exe", display="calc.exe") |
| 365 |
else: |
| 366 |
text = "!egr" |
| 367 |
expected_item = CompletionItem("!egrep", display="egrep") |
| 368 |
|
| 369 |
expected_completions = Completions([expected_item]) |
| 370 |
|
| 371 |
line = text |
| 372 |
endidx = len(line) |
| 373 |
begidx = 0 |
| 374 |
|
| 375 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 376 |
assert completions.to_strings() == expected_completions.to_strings() |
| 377 |
assert [item.display for item in completions] == [item.display for item in expected_completions] |
| 378 |
|
| 379 |
|
| 380 |
def test_shell_command_completion_does_not_match_wildcards(cmd_app) -> None: |
| 381 |
if sys.platform == "win32": |
| 382 |
text = "c*" |
| 383 |
else: |
| 384 |
text = "e*" |
| 385 |
|
| 386 |
line = f"shell {text}" |
| 387 |
endidx = len(line) |
| 388 |
begidx = endidx - len(text) |
| 389 |
|
| 390 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 391 |
assert not completions |
| 392 |
|
| 393 |
|
| 394 |
def test_shell_command_complete(cmd_app) -> None: |
| 395 |
if sys.platform == "win32": |
| 396 |
text = "c" |
| 397 |
expected = "calc.exe" |
| 398 |
else: |
| 399 |
text = "l" |
| 400 |
expected = "ls" |
| 401 |
|
| 402 |
line = f"shell {text}" |
| 403 |
endidx = len(line) |
| 404 |
begidx = endidx - len(text) |
| 405 |
|
| 406 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 407 |
assert expected in completions.to_strings() |
| 408 |
|
| 409 |
|
| 410 |
def test_shell_command_completion_nomatch(cmd_app) -> None: |
| 411 |
text = "zzzz" |
| 412 |
line = f"shell {text}" |
| 413 |
endidx = len(line) |
| 414 |
begidx = endidx - len(text) |
| 415 |
|
| 416 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 417 |
assert not completions |
| 418 |
|
| 419 |
|
| 420 |
def test_shell_command_completion_does_not_complete_when_just_shell(cmd_app) -> None: |
| 421 |
text = "" |
| 422 |
line = f"shell {text}" |
| 423 |
endidx = len(line) |
| 424 |
begidx = endidx - len(text) |
| 425 |
|
| 426 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 427 |
assert not completions |
| 428 |
|
| 429 |
|
| 430 |
def test_shell_command_completion_does_path_completion_when_after_command(cmd_app, request) -> None: |
| 431 |
test_dir = os.path.dirname(request.module.__file__) |
| 432 |
|
| 433 |
text = os.path.join(test_dir, "conftest") |
| 434 |
line = f"shell cat {text}" |
| 435 |
|
| 436 |
endidx = len(line) |
| 437 |
begidx = endidx - len(text) |
| 438 |
|
| 439 |
expected = [text + ".py"] |
| 440 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 441 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 442 |
|
| 443 |
|
| 444 |
def test_shell_command_complete_in_path(cmd_app, request) -> None: |
| 445 |
test_dir = os.path.dirname(request.module.__file__) |
| 446 |
|
| 447 |
text = os.path.join(test_dir, "s") |
| 448 |
line = f"shell {text}" |
| 449 |
|
| 450 |
endidx = len(line) |
| 451 |
begidx = endidx - len(text) |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
expected = os.path.join(test_dir, "scripts" + os.path.sep) |
| 456 |
|
| 457 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 458 |
assert expected in completions.to_strings() |
| 459 |
|
| 460 |
|
| 461 |
def test_path_completion_files_and_directories(cmd_app, request) -> None: |
| 462 |
"""Test that directories include an ending slash and files do not.""" |
| 463 |
test_dir = os.path.dirname(request.module.__file__) |
| 464 |
|
| 465 |
text = os.path.join(test_dir, "s") |
| 466 |
line = f"shell cat {text}" |
| 467 |
|
| 468 |
endidx = len(line) |
| 469 |
begidx = endidx - len(text) |
| 470 |
|
| 471 |
expected = [text + "cript.py", text + "cript.txt", text + "cripts" + os.path.sep] |
| 472 |
completions = cmd_app.path_complete(text, line, begidx, endidx) |
| 473 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 474 |
|
| 475 |
|
| 476 |
def test_path_completion_nomatch(cmd_app, request) -> None: |
| 477 |
test_dir = os.path.dirname(request.module.__file__) |
| 478 |
|
| 479 |
text = os.path.join(test_dir, "fakepath") |
| 480 |
line = f"shell cat {text}" |
| 481 |
|
| 482 |
endidx = len(line) |
| 483 |
begidx = endidx - len(text) |
| 484 |
|
| 485 |
completions = cmd_app.path_complete(text, line, begidx, endidx) |
| 486 |
assert not completions |
| 487 |
|
| 488 |
|
| 489 |
def test_path_completion_no_text(cmd_app) -> None: |
| 490 |
|
| 491 |
text = "" |
| 492 |
line = f"shell ls {text}" |
| 493 |
endidx = len(line) |
| 494 |
begidx = endidx - len(text) |
| 495 |
completions_no_text = cmd_app.path_complete(text, line, begidx, endidx) |
| 496 |
|
| 497 |
|
| 498 |
text = os.getcwd() + os.path.sep |
| 499 |
line = f"shell ls {text}" |
| 500 |
endidx = len(line) |
| 501 |
begidx = endidx - len(text) |
| 502 |
completions_cwd = cmd_app.path_complete(text, line, begidx, endidx) |
| 503 |
|
| 504 |
|
| 505 |
stripped_paths = [CompletionItem(value=item.text.replace(text, "", 1)) for item in completions_cwd] |
| 506 |
completions_cwd = dataclasses.replace(completions_cwd, items=stripped_paths) |
| 507 |
|
| 508 |
|
| 509 |
assert completions_no_text == completions_cwd |
| 510 |
assert completions_cwd |
| 511 |
|
| 512 |
|
| 513 |
def test_path_completion_no_path(cmd_app) -> None: |
| 514 |
|
| 515 |
text = "p" |
| 516 |
line = f"shell ls {text}" |
| 517 |
endidx = len(line) |
| 518 |
begidx = endidx - len(text) |
| 519 |
completions_no_text = cmd_app.path_complete(text, line, begidx, endidx) |
| 520 |
|
| 521 |
|
| 522 |
text = os.getcwd() + os.path.sep + text |
| 523 |
line = f"shell ls {text}" |
| 524 |
endidx = len(line) |
| 525 |
begidx = endidx - len(text) |
| 526 |
completions_cwd = cmd_app.path_complete(text, line, begidx, endidx) |
| 527 |
|
| 528 |
|
| 529 |
stripped_paths = [CompletionItem(value=item.text.replace(text[:-1], "", 1)) for item in completions_cwd] |
| 530 |
completions_cwd = dataclasses.replace(completions_cwd, items=stripped_paths) |
| 531 |
|
| 532 |
|
| 533 |
assert completions_no_text == completions_cwd |
| 534 |
assert completions_cwd |
| 535 |
|
| 536 |
|
| 537 |
@pytest.mark.skipif(sys.platform == "win32", reason="this only applies on systems where the root directory is a slash") |
| 538 |
def test_path_completion_cwd_is_root_dir(cmd_app) -> None: |
| 539 |
|
| 540 |
cwd = os.getcwd() |
| 541 |
try: |
| 542 |
os.chdir(os.path.sep) |
| 543 |
|
| 544 |
text = "" |
| 545 |
line = f"shell ls {text}" |
| 546 |
endidx = len(line) |
| 547 |
begidx = endidx - len(text) |
| 548 |
completions = cmd_app.path_complete(text, line, begidx, endidx) |
| 549 |
|
| 550 |
|
| 551 |
assert not any(item.text.startswith(os.path.sep) for item in completions) |
| 552 |
finally: |
| 553 |
|
| 554 |
os.chdir(cwd) |
| 555 |
|
| 556 |
|
| 557 |
def test_path_completion_does_not_match_wildcards(cmd_app, request) -> None: |
| 558 |
test_dir = os.path.dirname(request.module.__file__) |
| 559 |
|
| 560 |
text = os.path.join(test_dir, "c*") |
| 561 |
line = f"shell cat {text}" |
| 562 |
|
| 563 |
endidx = len(line) |
| 564 |
begidx = endidx - len(text) |
| 565 |
|
| 566 |
|
| 567 |
completions = cmd_app.path_complete(text, line, begidx, endidx) |
| 568 |
assert not completions |
| 569 |
|
| 570 |
|
| 571 |
def test_path_completion_complete_user(cmd_app) -> None: |
| 572 |
import getpass |
| 573 |
|
| 574 |
user = getpass.getuser() |
| 575 |
|
| 576 |
text = f"~{user}" |
| 577 |
line = f"shell fake {text}" |
| 578 |
endidx = len(line) |
| 579 |
begidx = endidx - len(text) |
| 580 |
|
| 581 |
expected = text + os.path.sep |
| 582 |
completions = cmd_app.path_complete(text, line, begidx, endidx) |
| 583 |
assert expected in completions.to_strings() |
| 584 |
|
| 585 |
|
| 586 |
def test_path_completion_user_path_expansion(cmd_app) -> None: |
| 587 |
|
| 588 |
if sys.platform.startswith("win"): |
| 589 |
cmd = "dir" |
| 590 |
else: |
| 591 |
cmd = "ls" |
| 592 |
|
| 593 |
|
| 594 |
text = f"~{os.path.sep}" |
| 595 |
line = f"shell {cmd} {text}" |
| 596 |
endidx = len(line) |
| 597 |
begidx = endidx - len(text) |
| 598 |
completions_tilde_slash = cmd_app.path_complete(text, line, begidx, endidx) |
| 599 |
|
| 600 |
|
| 601 |
stripped_paths = [CompletionItem(value=item.text.replace(text, "", 1)) for item in completions_tilde_slash] |
| 602 |
completions_tilde_slash = dataclasses.replace(completions_tilde_slash, items=stripped_paths) |
| 603 |
|
| 604 |
|
| 605 |
text = os.path.expanduser("~") + os.path.sep |
| 606 |
line = f"shell {cmd} {text}" |
| 607 |
endidx = len(line) |
| 608 |
begidx = endidx - len(text) |
| 609 |
completions_home = cmd_app.path_complete(text, line, begidx, endidx) |
| 610 |
|
| 611 |
|
| 612 |
stripped_paths = [CompletionItem(value=item.text.replace(text, "", 1)) for item in completions_home] |
| 613 |
completions_home = dataclasses.replace(completions_home, items=stripped_paths) |
| 614 |
|
| 615 |
assert completions_tilde_slash == completions_home |
| 616 |
|
| 617 |
|
| 618 |
def test_basic_completion(cmd_app) -> None: |
| 619 |
text = "P" |
| 620 |
line = f"list_food -f {text}" |
| 621 |
endidx = len(line) |
| 622 |
begidx = endidx - len(text) |
| 623 |
|
| 624 |
expected = ["Pizza", "Potato"] |
| 625 |
completions = cmd_app.basic_complete(text, line, begidx, endidx, food_item_strs) |
| 626 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 627 |
|
| 628 |
|
| 629 |
def test_basic_completion_nomatch(cmd_app) -> None: |
| 630 |
text = "q" |
| 631 |
line = f"list_food -f {text}" |
| 632 |
endidx = len(line) |
| 633 |
begidx = endidx - len(text) |
| 634 |
|
| 635 |
completions = cmd_app.basic_complete(text, line, begidx, endidx, food_item_strs) |
| 636 |
assert not completions |
| 637 |
|
| 638 |
|
| 639 |
def test_delimiter_completion_partial(cmd_app) -> None: |
| 640 |
"""Test that a delimiter is added when an item has not been fully completed""" |
| 641 |
text = "/home/" |
| 642 |
line = f"command {text}" |
| 643 |
endidx = len(line) |
| 644 |
begidx = endidx - len(text) |
| 645 |
|
| 646 |
|
| 647 |
expected_items = [ |
| 648 |
CompletionItem("/home/other user/", display="other user/"), |
| 649 |
CompletionItem("/home/user/", display="user/"), |
| 650 |
] |
| 651 |
expected_completions = Completions(expected_items) |
| 652 |
completions = cmd_app.delimiter_complete(text, line, begidx, endidx, delimited_strs, "/") |
| 653 |
|
| 654 |
assert completions.to_strings() == expected_completions.to_strings() |
| 655 |
assert [item.display for item in completions] == [item.display for item in expected_completions] |
| 656 |
|
| 657 |
|
| 658 |
def test_delimiter_completion_full(cmd_app) -> None: |
| 659 |
"""Test that no delimiter is added when an item has been fully completed""" |
| 660 |
text = "/home/other user/" |
| 661 |
line = f"command {text}" |
| 662 |
endidx = len(line) |
| 663 |
begidx = endidx - len(text) |
| 664 |
|
| 665 |
|
| 666 |
expected_items = [ |
| 667 |
CompletionItem("/home/other user/maps", display="maps"), |
| 668 |
CompletionItem("/home/other user/tests", display="tests"), |
| 669 |
] |
| 670 |
expected_completions = Completions(expected_items) |
| 671 |
completions = cmd_app.delimiter_complete(text, line, begidx, endidx, delimited_strs, "/") |
| 672 |
|
| 673 |
assert completions.to_strings() == expected_completions.to_strings() |
| 674 |
assert [item.display for item in completions] == [item.display for item in expected_completions] |
| 675 |
|
| 676 |
|
| 677 |
def test_delimiter_completion_nomatch(cmd_app) -> None: |
| 678 |
text = "/nothing_to_see" |
| 679 |
line = f"command {text}" |
| 680 |
endidx = len(line) |
| 681 |
begidx = endidx - len(text) |
| 682 |
|
| 683 |
completions = cmd_app.delimiter_complete(text, line, begidx, endidx, delimited_strs, "/") |
| 684 |
assert not completions |
| 685 |
|
| 686 |
|
| 687 |
def test_tokens_for_completion_quoted(cmd_app) -> None: |
| 688 |
text = "Pi" |
| 689 |
line = f'list_food "{text}"' |
| 690 |
endidx = len(line) |
| 691 |
begidx = endidx |
| 692 |
|
| 693 |
expected_tokens = ["list_food", "Pi", ""] |
| 694 |
expected_raw_tokens = ["list_food", '"Pi"', ""] |
| 695 |
|
| 696 |
tokens, raw_tokens = cmd_app.tokens_for_completion(line, begidx, endidx) |
| 697 |
assert expected_tokens == tokens |
| 698 |
assert expected_raw_tokens == raw_tokens |
| 699 |
|
| 700 |
|
| 701 |
def test_tokens_for_completion_unclosed_quote(cmd_app) -> None: |
| 702 |
text = "Pi" |
| 703 |
line = f'list_food "{text}' |
| 704 |
endidx = len(line) |
| 705 |
begidx = endidx - len(text) |
| 706 |
|
| 707 |
expected_tokens = ["list_food", "Pi"] |
| 708 |
expected_raw_tokens = ["list_food", '"Pi'] |
| 709 |
|
| 710 |
tokens, raw_tokens = cmd_app.tokens_for_completion(line, begidx, endidx) |
| 711 |
assert expected_tokens == tokens |
| 712 |
assert expected_raw_tokens == raw_tokens |
| 713 |
|
| 714 |
|
| 715 |
def test_tokens_for_completion_punctuation(cmd_app) -> None: |
| 716 |
"""Test that redirectors and terminators are word delimiters""" |
| 717 |
text = "file" |
| 718 |
line = f"command | < ;>>{text}" |
| 719 |
endidx = len(line) |
| 720 |
begidx = endidx - len(text) |
| 721 |
|
| 722 |
expected_tokens = ["command", "|", "<", ";", ">>", "file"] |
| 723 |
expected_raw_tokens = ["command", "|", "<", ";", ">>", "file"] |
| 724 |
|
| 725 |
tokens, raw_tokens = cmd_app.tokens_for_completion(line, begidx, endidx) |
| 726 |
assert expected_tokens == tokens |
| 727 |
assert expected_raw_tokens == raw_tokens |
| 728 |
|
| 729 |
|
| 730 |
def test_tokens_for_completion_quoted_punctuation(cmd_app) -> None: |
| 731 |
"""Test that quoted punctuation characters are not word delimiters""" |
| 732 |
text = ">file" |
| 733 |
line = f'command "{text}' |
| 734 |
endidx = len(line) |
| 735 |
begidx = endidx - len(text) |
| 736 |
|
| 737 |
expected_tokens = ["command", ">file"] |
| 738 |
expected_raw_tokens = ["command", '">file'] |
| 739 |
|
| 740 |
tokens, raw_tokens = cmd_app.tokens_for_completion(line, begidx, endidx) |
| 741 |
assert expected_tokens == tokens |
| 742 |
assert expected_raw_tokens == raw_tokens |
| 743 |
|
| 744 |
|
| 745 |
def test_add_opening_quote_double_quote_added(cmd_app) -> None: |
| 746 |
text = "Ha" |
| 747 |
line = f"test_basic {text}" |
| 748 |
endidx = len(line) |
| 749 |
begidx = endidx - len(text) |
| 750 |
|
| 751 |
|
| 752 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 753 |
assert completions._add_opening_quote |
| 754 |
assert completions._quote_char == '"' |
| 755 |
|
| 756 |
|
| 757 |
def test_add_opening_quote_single_quote_added(cmd_app) -> None: |
| 758 |
text = "Ch" |
| 759 |
line = f"test_basic {text}" |
| 760 |
endidx = len(line) |
| 761 |
begidx = endidx - len(text) |
| 762 |
|
| 763 |
|
| 764 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 765 |
assert completions._add_opening_quote |
| 766 |
assert completions._quote_char == "'" |
| 767 |
|
| 768 |
|
| 769 |
def test_add_opening_quote_nothing_added(cmd_app) -> None: |
| 770 |
text = "P" |
| 771 |
line = f"test_basic {text}" |
| 772 |
endidx = len(line) |
| 773 |
begidx = endidx - len(text) |
| 774 |
|
| 775 |
|
| 776 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 777 |
assert not completions._add_opening_quote |
| 778 |
assert not completions._quote_char |
| 779 |
|
| 780 |
|
| 781 |
def test_word_break_in_quote(cmd_app) -> None: |
| 782 |
"""Test case where search text has a space and is in a quote.""" |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
text = "S" |
| 788 |
line = 'test_basic "Ham S' |
| 789 |
endidx = len(line) |
| 790 |
begidx = endidx - len(text) |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
expected = ["Sandwich"] |
| 798 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 799 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 800 |
|
| 801 |
|
| 802 |
def test_no_completer(cmd_app) -> None: |
| 803 |
text = "" |
| 804 |
line = f"test_no_completer {text}" |
| 805 |
endidx = len(line) |
| 806 |
begidx = endidx - len(text) |
| 807 |
|
| 808 |
expected = ["default"] |
| 809 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 810 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 811 |
|
| 812 |
|
| 813 |
def test_word_break_in_command(cmd_app) -> None: |
| 814 |
text = "" |
| 815 |
line = f'"{text}' |
| 816 |
endidx = len(line) |
| 817 |
begidx = endidx - len(text) |
| 818 |
|
| 819 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 820 |
assert not completions |
| 821 |
|
| 822 |
|
| 823 |
def test_complete_multiline_on_single_line(cmd_app) -> None: |
| 824 |
text = "" |
| 825 |
line = f"test_multiline {text}" |
| 826 |
endidx = len(line) |
| 827 |
begidx = endidx - len(text) |
| 828 |
|
| 829 |
expected = ["Basket", "Basketball", "Bat", "Football", "Space Ball"] |
| 830 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 831 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 832 |
|
| 833 |
|
| 834 |
def test_complete_multiline_on_multiple_lines(cmd_app) -> None: |
| 835 |
text = "Ba" |
| 836 |
line = f"test_multiline\n{text}" |
| 837 |
endidx = len(line) |
| 838 |
begidx = endidx - len(text) |
| 839 |
|
| 840 |
expected = ["Bat", "Basket", "Basketball"] |
| 841 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 842 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 843 |
|
| 844 |
|
| 845 |
def test_completions_iteration() -> None: |
| 846 |
items = [CompletionItem(1), CompletionItem(2)] |
| 847 |
completions = Completions(items) |
| 848 |
|
| 849 |
|
| 850 |
assert list(completions) == items |
| 851 |
|
| 852 |
|
| 853 |
assert list(reversed(completions)) == items[::-1] |
| 854 |
|
| 855 |
|
| 856 |
def test_numeric_sorting() -> None: |
| 857 |
"""Test that numbers and numeric strings are sorted numerically.""" |
| 858 |
numbers = [5, 6, 4, 3, 7.2, 9.1] |
| 859 |
completions = Completions.from_values(numbers) |
| 860 |
assert [item.value for item in completions] == sorted(numbers) |
| 861 |
|
| 862 |
number_strs = ["5", "6", "4", "3", "7.2", "9.1"] |
| 863 |
completions = Completions.from_values(number_strs) |
| 864 |
assert list(completions.to_strings()) == sorted(number_strs, key=float) |
| 865 |
|
| 866 |
mixed = ["5", "6", "4", 3, "7.2", 9.1] |
| 867 |
completions = Completions.from_values(mixed) |
| 868 |
assert list(completions.to_strings()) == [str(v) for v in sorted(number_strs, key=float)] |
| 869 |
|
| 870 |
|
| 871 |
def test_is_sorted() -> None: |
| 872 |
"""Test that already sorted results are not re-sorted.""" |
| 873 |
values = [5, 6, 4, 3] |
| 874 |
already_sorted = Completions.from_values(values, is_sorted=True) |
| 875 |
sorted_on_creation = Completions.from_values(values, is_sorted=False) |
| 876 |
|
| 877 |
assert already_sorted.to_strings() != sorted_on_creation.to_strings() |
| 878 |
assert [item.value for item in already_sorted] == values |
| 879 |
|
| 880 |
|
| 881 |
@pytest.mark.parametrize( |
| 882 |
("values", "numeric_display"), |
| 883 |
[ |
| 884 |
([2, 3], True), |
| 885 |
([2, 3.7], True), |
| 886 |
([2, "3"], True), |
| 887 |
([2.2, "3.4"], True), |
| 888 |
([2, "3g"], False), |
| 889 |
|
| 890 |
(["\x1b[31m5\x1b[0m", "\x1b[32m9.2\x1b[0m"], True), |
| 891 |
(["\x1b[31mNOT_STRING\x1b[0m", "\x1b[32m9.2\x1b[0m"], False), |
| 892 |
], |
| 893 |
) |
| 894 |
def test_numeric_display(values: list[int | float | str], numeric_display: bool) -> None: |
| 895 |
"""Test setting of the Completions.numeric_display field.""" |
| 896 |
completions = Completions.from_values(values) |
| 897 |
assert completions.numeric_display == numeric_display |
| 898 |
|
| 899 |
|
| 900 |
def test_remove_duplicates() -> None: |
| 901 |
"""Test that duplicate CompletionItems are removed.""" |
| 902 |
|
| 903 |
|
| 904 |
orig_item = CompletionItem(value="orig item", display="orig display", display_meta="orig meta") |
| 905 |
new_value = dataclasses.replace(orig_item, value="new value") |
| 906 |
new_text = dataclasses.replace(orig_item, text="new text") |
| 907 |
new_display = dataclasses.replace(orig_item, display="new display") |
| 908 |
new_meta = dataclasses.replace(orig_item, display_meta="new meta") |
| 909 |
|
| 910 |
|
| 911 |
items = [orig_item, orig_item, new_value, new_value, new_text, new_text, new_display, new_display, new_meta, new_meta] |
| 912 |
completions = Completions(items) |
| 913 |
|
| 914 |
|
| 915 |
assert len(completions) == 5 |
| 916 |
assert orig_item in completions |
| 917 |
assert new_value in completions |
| 918 |
assert new_text in completions |
| 919 |
assert new_display in completions |
| 920 |
assert new_meta in completions |
| 921 |
|
| 922 |
|
| 923 |
def test_completion_item_deepcopy() -> None: |
| 924 |
"""Test that deepcopy of a CompletionItem preserves identity of its members.""" |
| 925 |
|
| 926 |
class ComplexValue: |
| 927 |
pass |
| 928 |
|
| 929 |
value = ComplexValue() |
| 930 |
table_data = (ComplexValue(),) |
| 931 |
orig_item = CompletionItem( |
| 932 |
value=value, |
| 933 |
text="my_text", |
| 934 |
display="my_display", |
| 935 |
display_meta="my_meta", |
| 936 |
table_data=table_data, |
| 937 |
) |
| 938 |
|
| 939 |
|
| 940 |
copied_item = copy.deepcopy(orig_item) |
| 941 |
|
| 942 |
|
| 943 |
assert copied_item is not orig_item |
| 944 |
|
| 945 |
|
| 946 |
assert copied_item.value is orig_item.value |
| 947 |
assert copied_item.text is orig_item.text |
| 948 |
assert copied_item.display is orig_item.display |
| 949 |
assert copied_item.display_meta is orig_item.display_meta |
| 950 |
assert copied_item.table_data is orig_item.table_data |
| 951 |
|
| 952 |
|
| 953 |
def test_plain_fields() -> None: |
| 954 |
"""Test the plain text fields in CompletionItem.""" |
| 955 |
display = "\x1b[31mApple\x1b[0m" |
| 956 |
display_meta = "\x1b[32mA tasty apple\x1b[0m" |
| 957 |
|
| 958 |
|
| 959 |
completion_item = CompletionItem("apple", display=display, display_meta=display_meta) |
| 960 |
assert completion_item.display == display |
| 961 |
assert completion_item.display_plain == "Apple" |
| 962 |
assert completion_item.display_meta == display_meta |
| 963 |
assert completion_item.display_meta_plain == "A tasty apple" |
| 964 |
|
| 965 |
|
| 966 |
def test_clean_display() -> None: |
| 967 |
"""Test display string cleaning in CompletionItem.""" |
| 968 |
|
| 969 |
|
| 970 |
display = "str1\r\nstr2\nstr3\rstr4\tstr5\fstr6\vstr7" |
| 971 |
expected = "str1 str2 str3 str4 str5 str6 str7" |
| 972 |
|
| 973 |
|
| 974 |
completion_item = CompletionItem("item", display=display, display_meta=display) |
| 975 |
assert completion_item.display == expected |
| 976 |
assert completion_item.display_meta == expected |
| 977 |
|
| 978 |
|
| 979 |
text = "item\nwith\nnewlines" |
| 980 |
expected_text_display = "item with newlines" |
| 981 |
completion_item = CompletionItem(text) |
| 982 |
assert completion_item.display == expected_text_display |
| 983 |
|
| 984 |
|
| 985 |
def test_styled_completion_sort() -> None: |
| 986 |
"""Test that sorting is done with the display_plain field.""" |
| 987 |
|
| 988 |
|
| 989 |
red_apple = "\x1b[31mApple\x1b[0m" |
| 990 |
green_cherry = "\x1b[32mCherry\x1b[0m" |
| 991 |
blue_banana = "\x1b[34mBanana\x1b[0m" |
| 992 |
|
| 993 |
|
| 994 |
unsorted_strs = [blue_banana, red_apple, green_cherry] |
| 995 |
sorted_strs = sorted(unsorted_strs, key=utils.DEFAULT_STR_SORT_KEY) |
| 996 |
assert sorted_strs == [red_apple, green_cherry, blue_banana] |
| 997 |
|
| 998 |
|
| 999 |
unsorted_items = [ |
| 1000 |
CompletionItem("banana", display=blue_banana), |
| 1001 |
CompletionItem("cherry", display=green_cherry), |
| 1002 |
CompletionItem("apple", display=red_apple), |
| 1003 |
] |
| 1004 |
|
| 1005 |
completions = Completions(unsorted_items) |
| 1006 |
|
| 1007 |
|
| 1008 |
expected_plain = ["Apple", "Banana", "Cherry"] |
| 1009 |
expected_styled = [red_apple, blue_banana, green_cherry] |
| 1010 |
|
| 1011 |
for index, item in enumerate(completions): |
| 1012 |
|
| 1013 |
assert item.display_plain == expected_plain[index] |
| 1014 |
|
| 1015 |
|
| 1016 |
assert item.display == expected_styled[index] |
| 1017 |
|
| 1018 |
|
| 1019 |
completion_displays = [item.display for item in completions] |
| 1020 |
assert completion_displays != sorted_strs |
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
class RedirCompType(enum.Enum): |
| 1025 |
SHELL_CMD = (1,) |
| 1026 |
PATH = (2,) |
| 1027 |
DEFAULT = (3,) |
| 1028 |
NONE = 4 |
| 1029 |
|
| 1030 |
|
| 1031 |
@pytest.mark.parametrize( |
| 1032 |
("line", "comp_type"), |
| 1033 |
[ |
| 1034 |
("fake", RedirCompType.DEFAULT), |
| 1035 |
("fake arg", RedirCompType.DEFAULT), |
| 1036 |
("fake |", RedirCompType.SHELL_CMD), |
| 1037 |
("fake | grep", RedirCompType.PATH), |
| 1038 |
("fake | grep arg", RedirCompType.PATH), |
| 1039 |
("fake | grep >", RedirCompType.PATH), |
| 1040 |
("fake | grep > >", RedirCompType.NONE), |
| 1041 |
("fake | grep > file", RedirCompType.NONE), |
| 1042 |
("fake | grep > file >", RedirCompType.NONE), |
| 1043 |
("fake | grep > file |", RedirCompType.SHELL_CMD), |
| 1044 |
("fake | grep > file | grep", RedirCompType.PATH), |
| 1045 |
("fake | |", RedirCompType.NONE), |
| 1046 |
("fake | >", RedirCompType.NONE), |
| 1047 |
("fake >", RedirCompType.PATH), |
| 1048 |
("fake >>", RedirCompType.PATH), |
| 1049 |
("fake > >", RedirCompType.NONE), |
| 1050 |
("fake > |", RedirCompType.SHELL_CMD), |
| 1051 |
("fake >> file |", RedirCompType.SHELL_CMD), |
| 1052 |
("fake >> file | grep", RedirCompType.PATH), |
| 1053 |
("fake > file", RedirCompType.NONE), |
| 1054 |
("fake > file >", RedirCompType.NONE), |
| 1055 |
("fake > file >>", RedirCompType.NONE), |
| 1056 |
], |
| 1057 |
) |
| 1058 |
def test_redirect_complete(cmd_app, monkeypatch, line, comp_type) -> None: |
| 1059 |
|
| 1060 |
cmd_app.allow_redirection = True |
| 1061 |
for _ in range(2): |
| 1062 |
shell_cmd_complete_mock = mock.MagicMock(name="shell_cmd_complete") |
| 1063 |
monkeypatch.setattr("cmd2.Cmd.shell_cmd_complete", shell_cmd_complete_mock) |
| 1064 |
|
| 1065 |
path_complete_mock = mock.MagicMock(name="path_complete") |
| 1066 |
monkeypatch.setattr("cmd2.Cmd.path_complete", path_complete_mock) |
| 1067 |
|
| 1068 |
default_complete_mock = mock.MagicMock(name="fake_completer") |
| 1069 |
|
| 1070 |
text = "" |
| 1071 |
line = f"{line} {text}" |
| 1072 |
endidx = len(line) |
| 1073 |
begidx = endidx - len(text) |
| 1074 |
|
| 1075 |
cmd_app._redirect_complete(text, line, begidx, endidx, default_complete_mock) |
| 1076 |
|
| 1077 |
if comp_type == RedirCompType.SHELL_CMD: |
| 1078 |
shell_cmd_complete_mock.assert_called_once() |
| 1079 |
elif comp_type == RedirCompType.PATH: |
| 1080 |
path_complete_mock.assert_called_once() |
| 1081 |
elif comp_type == RedirCompType.DEFAULT: |
| 1082 |
default_complete_mock.assert_called_once() |
| 1083 |
else: |
| 1084 |
shell_cmd_complete_mock.assert_not_called() |
| 1085 |
path_complete_mock.assert_not_called() |
| 1086 |
default_complete_mock.assert_not_called() |
| 1087 |
|
| 1088 |
|
| 1089 |
cmd_app.allow_redirection = False |
| 1090 |
if comp_type != RedirCompType.DEFAULT: |
| 1091 |
comp_type = RedirCompType.NONE |
| 1092 |
|
| 1093 |
|
| 1094 |
def test_complete_set_value(cmd_app) -> None: |
| 1095 |
text = "" |
| 1096 |
line = f"set foo {text}" |
| 1097 |
endidx = len(line) |
| 1098 |
begidx = endidx - len(text) |
| 1099 |
|
| 1100 |
expected = ["SUCCESS"] |
| 1101 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 1102 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 1103 |
assert completions.hint.strip() == "" |
| 1104 |
|
| 1105 |
|
| 1106 |
def test_complete_set_value_invalid_settable(cmd_app) -> None: |
| 1107 |
text = "" |
| 1108 |
line = f"set fake {text}" |
| 1109 |
endidx = len(line) |
| 1110 |
begidx = endidx - len(text) |
| 1111 |
|
| 1112 |
completions = cmd_app.complete(text, line, begidx, endidx) |
| 1113 |
assert not completions |
| 1114 |
assert "fake is not a settable parameter" in completions.error |
| 1115 |
|
| 1116 |
|
| 1117 |
@pytest.fixture |
| 1118 |
def sc_app(): |
| 1119 |
c = SubcommandsExample() |
| 1120 |
c.stdout = utils.StdSim(c.stdout) |
| 1121 |
return c |
| 1122 |
|
| 1123 |
|
| 1124 |
def test_cmd2_subcommand_completion(sc_app) -> None: |
| 1125 |
text = "" |
| 1126 |
line = f"base {text}" |
| 1127 |
endidx = len(line) |
| 1128 |
begidx = endidx - len(text) |
| 1129 |
|
| 1130 |
expected = ["bar", "foo", "sport"] |
| 1131 |
completions = sc_app.complete(text, line, begidx, endidx) |
| 1132 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 1133 |
|
| 1134 |
|
| 1135 |
def test_cmd2_subcommand_completion_nomatch(sc_app) -> None: |
| 1136 |
text = "z" |
| 1137 |
line = f"base {text}" |
| 1138 |
endidx = len(line) |
| 1139 |
begidx = endidx - len(text) |
| 1140 |
|
| 1141 |
completions = sc_app.complete(text, line, begidx, endidx) |
| 1142 |
assert not completions |
| 1143 |
|
| 1144 |
|
| 1145 |
def test_help_subcommand_completion_multiple(sc_app) -> None: |
| 1146 |
text = "" |
| 1147 |
line = f"help base {text}" |
| 1148 |
endidx = len(line) |
| 1149 |
begidx = endidx - len(text) |
| 1150 |
|
| 1151 |
expected = ["bar", "foo", "sport"] |
| 1152 |
completions = sc_app.complete(text, line, begidx, endidx) |
| 1153 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 1154 |
|
| 1155 |
|
| 1156 |
def test_help_subcommand_completion_nomatch(sc_app) -> None: |
| 1157 |
text = "z" |
| 1158 |
line = f"help base {text}" |
| 1159 |
endidx = len(line) |
| 1160 |
begidx = endidx - len(text) |
| 1161 |
|
| 1162 |
completions = sc_app.complete(text, line, begidx, endidx) |
| 1163 |
assert not completions |
| 1164 |
|
| 1165 |
|
| 1166 |
def test_subcommand_tab_completion(sc_app) -> None: |
| 1167 |
|
| 1168 |
text = "Foot" |
| 1169 |
line = f"base sport {text}" |
| 1170 |
endidx = len(line) |
| 1171 |
begidx = endidx - len(text) |
| 1172 |
|
| 1173 |
expected = ["Football"] |
| 1174 |
completions = sc_app.complete(text, line, begidx, endidx) |
| 1175 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 1176 |
|
| 1177 |
|
| 1178 |
def test_subcommand_tab_completion_with_no_completer(sc_app) -> None: |
| 1179 |
|
| 1180 |
|
| 1181 |
text = "Foot" |
| 1182 |
line = f"base foo {text}" |
| 1183 |
endidx = len(line) |
| 1184 |
begidx = endidx - len(text) |
| 1185 |
|
| 1186 |
completions = sc_app.complete(text, line, begidx, endidx) |
| 1187 |
assert not completions |
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
class SubcommandsWithUnknownExample(cmd2.Cmd): |
| 1194 |
"""Example cmd2 application where we a base command which has a couple subcommands |
| 1195 |
and the "sport" subcommand has tab completion enabled. |
| 1196 |
""" |
| 1197 |
|
| 1198 |
def __init__(self) -> None: |
| 1199 |
cmd2.Cmd.__init__(self) |
| 1200 |
|
| 1201 |
|
| 1202 |
def base_foo(self, args) -> None: |
| 1203 |
"""Foo subcommand of base command""" |
| 1204 |
self.poutput(args.x * args.y) |
| 1205 |
|
| 1206 |
def base_bar(self, args) -> None: |
| 1207 |
"""Bar subcommand of base command""" |
| 1208 |
self.poutput(f"(({args.z}))") |
| 1209 |
|
| 1210 |
def base_sport(self, args) -> None: |
| 1211 |
"""Sport subcommand of base command""" |
| 1212 |
self.poutput(f"Sport is {args.sport}") |
| 1213 |
|
| 1214 |
|
| 1215 |
base_parser = cmd2.Cmd2ArgumentParser() |
| 1216 |
base_subparsers = base_parser.add_subparsers(title="subcommands", help="subcommand help") |
| 1217 |
|
| 1218 |
|
| 1219 |
parser_foo = base_subparsers.add_parser("foo", help="foo help") |
| 1220 |
parser_foo.add_argument("-x", type=int, default=1, help="integer") |
| 1221 |
parser_foo.add_argument("y", type=float, help="float") |
| 1222 |
parser_foo.set_defaults(func=base_foo) |
| 1223 |
|
| 1224 |
|
| 1225 |
parser_bar = base_subparsers.add_parser("bar", help="bar help") |
| 1226 |
parser_bar.add_argument("z", help="string") |
| 1227 |
parser_bar.set_defaults(func=base_bar) |
| 1228 |
|
| 1229 |
|
| 1230 |
parser_sport = base_subparsers.add_parser("sport", help="sport help") |
| 1231 |
sport_arg = parser_sport.add_argument("sport", help="Enter name of a sport", choices=sport_item_strs) |
| 1232 |
|
| 1233 |
@cmd2.with_argparser(base_parser, with_unknown_args=True) |
| 1234 |
def do_base(self, args) -> None: |
| 1235 |
"""Base command help""" |
| 1236 |
func = getattr(args, "func", None) |
| 1237 |
if func is not None: |
| 1238 |
|
| 1239 |
func(self, args) |
| 1240 |
else: |
| 1241 |
|
| 1242 |
self.do_help("base") |
| 1243 |
|
| 1244 |
|
| 1245 |
@pytest.fixture |
| 1246 |
def scu_app(): |
| 1247 |
"""Declare test fixture for with_argparser decorator""" |
| 1248 |
return SubcommandsWithUnknownExample() |
| 1249 |
|
| 1250 |
|
| 1251 |
def test_subcmd_with_unknown_completion(scu_app) -> None: |
| 1252 |
text = "" |
| 1253 |
line = f"base {text}" |
| 1254 |
endidx = len(line) |
| 1255 |
begidx = endidx - len(text) |
| 1256 |
|
| 1257 |
expected = ["bar", "foo", "sport"] |
| 1258 |
completions = scu_app.complete(text, line, begidx, endidx) |
| 1259 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 1260 |
|
| 1261 |
|
| 1262 |
def test_subcmd_with_unknown_completion_nomatch(scu_app) -> None: |
| 1263 |
text = "z" |
| 1264 |
line = f"base {text}" |
| 1265 |
endidx = len(line) |
| 1266 |
begidx = endidx - len(text) |
| 1267 |
|
| 1268 |
completions = scu_app.complete(text, line, begidx, endidx) |
| 1269 |
assert not completions |
| 1270 |
|
| 1271 |
|
| 1272 |
def test_help_subcommand_completion_scu(scu_app) -> None: |
| 1273 |
text = "" |
| 1274 |
line = f"help base {text}" |
| 1275 |
endidx = len(line) |
| 1276 |
begidx = endidx - len(text) |
| 1277 |
|
| 1278 |
expected = ["bar", "foo", "sport"] |
| 1279 |
completions = scu_app.complete(text, line, begidx, endidx) |
| 1280 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 1281 |
|
| 1282 |
|
| 1283 |
def test_help_subcommand_completion_with_flags_before_command(scu_app) -> None: |
| 1284 |
text = "" |
| 1285 |
line = f"help -h -v base {text}" |
| 1286 |
endidx = len(line) |
| 1287 |
begidx = endidx - len(text) |
| 1288 |
|
| 1289 |
expected = ["bar", "foo", "sport"] |
| 1290 |
completions = scu_app.complete(text, line, begidx, endidx) |
| 1291 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 1292 |
|
| 1293 |
|
| 1294 |
def test_complete_help_subcommands_with_blank_command(scu_app) -> None: |
| 1295 |
text = "" |
| 1296 |
line = f'help "" {text}' |
| 1297 |
endidx = len(line) |
| 1298 |
begidx = endidx - len(text) |
| 1299 |
|
| 1300 |
completions = scu_app.complete(text, line, begidx, endidx) |
| 1301 |
assert not completions |
| 1302 |
|
| 1303 |
|
| 1304 |
def test_help_subcommand_completion_nomatch_scu(scu_app) -> None: |
| 1305 |
text = "z" |
| 1306 |
line = f"help base {text}" |
| 1307 |
endidx = len(line) |
| 1308 |
begidx = endidx - len(text) |
| 1309 |
|
| 1310 |
completions = scu_app.complete(text, line, begidx, endidx) |
| 1311 |
assert not completions |
| 1312 |
|
| 1313 |
|
| 1314 |
def test_subcommand_tab_completion_scu(scu_app) -> None: |
| 1315 |
|
| 1316 |
text = "Foot" |
| 1317 |
line = f"base sport {text}" |
| 1318 |
endidx = len(line) |
| 1319 |
begidx = endidx - len(text) |
| 1320 |
|
| 1321 |
expected = ["Football"] |
| 1322 |
completions = scu_app.complete(text, line, begidx, endidx) |
| 1323 |
assert completions.to_strings() == Completions.from_values(expected).to_strings() |
| 1324 |
|
| 1325 |
|
| 1326 |
def test_subcommand_tab_completion_with_no_completer_scu(scu_app) -> None: |
| 1327 |
|
| 1328 |
|
| 1329 |
text = "Foot" |
| 1330 |
line = f"base foo {text}" |
| 1331 |
endidx = len(line) |
| 1332 |
begidx = endidx - len(text) |
| 1333 |
|
| 1334 |
completions = scu_app.complete(text, line, begidx, endidx) |
| 1335 |
assert not completions |
| 1336 |
|
| 1337 |
|
| 1338 |
def test_set_completion_item_text() -> None: |
| 1339 |
"""Test setting CompletionItem.text and how it affects CompletionItem.display.""" |
| 1340 |
value = 5 |
| 1341 |
|
| 1342 |
|
| 1343 |
item = CompletionItem(value=value) |
| 1344 |
assert item.text == str(value) |
| 1345 |
|
| 1346 |
|
| 1347 |
item = CompletionItem(value=value, text="my_text") |
| 1348 |
assert item.text == "my_text" |
| 1349 |
|
| 1350 |
|
| 1351 |
item = CompletionItem(value=value, text="") |
| 1352 |
assert item.text == "" |
| 1353 |
|
| 1354 |
|
| 1355 |
def test_replace_completion_item_text() -> None: |
| 1356 |
"""Test replacing the value of CompletionItem.text""" |
| 1357 |
value = 5 |
| 1358 |
|
| 1359 |
|
| 1360 |
item = CompletionItem(value=value, text="my_text") |
| 1361 |
updated_item = dataclasses.replace(item, text="new_text") |
| 1362 |
assert item.text == "my_text" |
| 1363 |
assert item.display == "my_text" |
| 1364 |
|
| 1365 |
|
| 1366 |
assert updated_item.text == "new_text" |
| 1367 |
assert updated_item.display == "my_text" |
| 1368 |
|
| 1369 |
|
| 1370 |
item = CompletionItem(value=value, text="my_text") |
| 1371 |
updated_item = dataclasses.replace(item, text="") |
| 1372 |
assert item.text == "my_text" |
| 1373 |
assert item.display == "my_text" |
| 1374 |
|
| 1375 |
|
| 1376 |
assert updated_item.text == "" |
| 1377 |
assert updated_item.display == "my_text" |
| 1378 |
|
| 1379 |
|
| 1380 |
def test_set_completion_item_display() -> None: |
| 1381 |
"""Test setting CompletionItem.display and how it is affected by CompletionItem.text.""" |
| 1382 |
value = 5 |
| 1383 |
|
| 1384 |
|
| 1385 |
value = 5 |
| 1386 |
item = CompletionItem(value=value) |
| 1387 |
assert item.text == str(value) |
| 1388 |
assert item.display == item.text |
| 1389 |
|
| 1390 |
|
| 1391 |
item = CompletionItem(value=value, text="my_text") |
| 1392 |
assert item.text == "my_text" |
| 1393 |
assert item.display == item.text |
| 1394 |
|
| 1395 |
|
| 1396 |
item = CompletionItem(value=value, text="my_text", display="my_display") |
| 1397 |
assert item.text == "my_text" |
| 1398 |
assert item.display == "my_display" |
| 1399 |
|
| 1400 |
|
| 1401 |
item = CompletionItem(value=value, text="my_text", display="") |
| 1402 |
assert item.text == "my_text" |
| 1403 |
assert item.display == "" |
| 1404 |
|
| 1405 |
|
| 1406 |
def test_replace_completion_item_display() -> None: |
| 1407 |
"""Test replacing the value of CompletionItem.display""" |
| 1408 |
value = 5 |
| 1409 |
|
| 1410 |
|
| 1411 |
item = CompletionItem(value=value, display="my_display") |
| 1412 |
updated_item = dataclasses.replace(item, display="new_display") |
| 1413 |
|
| 1414 |
assert item.display == "my_display" |
| 1415 |
assert updated_item.display == "new_display" |
| 1416 |
|
| 1417 |
|
| 1418 |
item = CompletionItem(value=value, display="my_display") |
| 1419 |
updated_item = dataclasses.replace(item, display="") |
| 1420 |
|
| 1421 |
assert item.display == "my_display" |
| 1422 |
assert updated_item.display == "" |
| 1423 |
|
| 1424 |
|
| 1425 |
def test_full_prefix_removal() -> None: |
| 1426 |
"""Verify that Cmd._perform_completion() can clear item.text when |
| 1427 |
text_to_remove matches item.text exactly. This occurs when completing |
| 1428 |
a nested quoted string where the command line already contains the |
| 1429 |
full unquoted content of the completion match. |
| 1430 |
""" |
| 1431 |
|
| 1432 |
class TestApp(cmd2.Cmd): |
| 1433 |
def get_choices(self) -> Choices: |
| 1434 |
"""Return choices.""" |
| 1435 |
choices = [ |
| 1436 |
"'This is a single-quoted item'", |
| 1437 |
'"This is a double-quoted item"', |
| 1438 |
] |
| 1439 |
return cmd2.Choices.from_values(choices) |
| 1440 |
|
| 1441 |
parser = cmd2.Cmd2ArgumentParser() |
| 1442 |
parser.add_argument("arg", choices_provider=get_choices) |
| 1443 |
|
| 1444 |
@cmd2.with_argparser(parser) |
| 1445 |
def do_command(self, args: argparse.Namespace) -> None: |
| 1446 |
"""Test stuff.""" |
| 1447 |
|
| 1448 |
|
| 1449 |
text = "" |
| 1450 |
line = "command \"'This is a single-quoted item'" |
| 1451 |
endidx = len(line) |
| 1452 |
begidx = endidx |
| 1453 |
|
| 1454 |
app = TestApp() |
| 1455 |
completions = app.complete(text, line, begidx, endidx) |
| 1456 |
assert len(completions) == 1 |
| 1457 |
|
| 1458 |
item = completions[0] |
| 1459 |
assert item.text == "" |
| 1460 |
|
| 1461 |
|
| 1462 |
text = "" |
| 1463 |
line = 'command \'"This is a double-quoted item"' |
| 1464 |
endidx = len(line) |
| 1465 |
begidx = endidx |
| 1466 |
|
| 1467 |
app = TestApp() |
| 1468 |
completions = app.complete(text, line, begidx, endidx) |
| 1469 |
assert len(completions) == 1 |
| 1470 |
|
| 1471 |
item = completions[0] |
| 1472 |
assert item.text == "" |