| 1 |
"""Tests for bash completion script generation.""" |
| 2 |
|
| 3 |
from typing import Annotated, Literal |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from cyclopts import App, Parameter |
| 8 |
from cyclopts.completion.bash import generate_completion_script |
| 9 |
|
| 10 |
from .apps import ( |
| 11 |
app_basic, |
| 12 |
app_deploy, |
| 13 |
app_disabled_negative, |
| 14 |
app_enum, |
| 15 |
app_list_annotated_path, |
| 16 |
app_list_path, |
| 17 |
app_multiple_positionals, |
| 18 |
app_negative, |
| 19 |
app_nested, |
| 20 |
app_path, |
| 21 |
app_positional_literal, |
| 22 |
app_positional_path, |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
def test_generate_completion_script_invalid_prog_name(): |
| 27 |
"""Test that invalid prog_name raises ValueError.""" |
| 28 |
with pytest.raises(ValueError, match="Invalid prog_name"): |
| 29 |
generate_completion_script(app_basic, "invalid prog") |
| 30 |
|
| 31 |
with pytest.raises(ValueError, match="Invalid prog_name"): |
| 32 |
generate_completion_script(app_basic, "") |
| 33 |
|
| 34 |
with pytest.raises(ValueError, match="Invalid prog_name"): |
| 35 |
generate_completion_script(app_basic, "prog$name") |
| 36 |
|
| 37 |
|
| 38 |
def test_generate_completion_script_creates_script(): |
| 39 |
"""Test that bash completion generates a script.""" |
| 40 |
script = generate_completion_script(app_basic, "basic") |
| 41 |
assert script |
| 42 |
assert "# Bash completion for basic" in script |
| 43 |
assert "complete -F _basic basic" in script |
| 44 |
|
| 45 |
|
| 46 |
def test_basic_option_completion(bash_tester): |
| 47 |
"""Test basic option name completion.""" |
| 48 |
tester = bash_tester(app_basic, "basic") |
| 49 |
|
| 50 |
assert "--verbose" in tester.completion_script |
| 51 |
assert "--count" in tester.completion_script |
| 52 |
assert "--help" in tester.completion_script |
| 53 |
|
| 54 |
|
| 55 |
def test_command_completion(bash_tester): |
| 56 |
"""Test command completion.""" |
| 57 |
tester = bash_tester(app_basic, "basic") |
| 58 |
|
| 59 |
assert "deploy" in tester.completion_script |
| 60 |
|
| 61 |
|
| 62 |
def test_literal_value_completion(bash_tester): |
| 63 |
"""Test Literal type value completion.""" |
| 64 |
tester = bash_tester(app_basic, "basic") |
| 65 |
|
| 66 |
assert "dev" in tester.completion_script |
| 67 |
assert "staging" in tester.completion_script |
| 68 |
assert "prod" in tester.completion_script |
| 69 |
|
| 70 |
|
| 71 |
def test_enum_value_completion(bash_tester): |
| 72 |
"""Test Enum type value completion.""" |
| 73 |
tester = bash_tester(app_enum, "enumapp") |
| 74 |
|
| 75 |
assert "fast" in tester.completion_script |
| 76 |
assert "slow" in tester.completion_script |
| 77 |
|
| 78 |
|
| 79 |
def test_nested_subcommand_completion(bash_tester): |
| 80 |
"""Test nested subcommand completion.""" |
| 81 |
tester = bash_tester(app_nested, "nested") |
| 82 |
|
| 83 |
assert "config" in tester.completion_script |
| 84 |
assert "get" in tester.completion_script |
| 85 |
assert "set" in tester.completion_script |
| 86 |
|
| 87 |
|
| 88 |
def test_path_completion_action(bash_tester): |
| 89 |
"""Test that Path types trigger file completion.""" |
| 90 |
tester = bash_tester(app_path, "pathapp") |
| 91 |
|
| 92 |
assert tester.validate_script_syntax() |
| 93 |
|
| 94 |
|
| 95 |
def test_negative_flag_completion(bash_tester): |
| 96 |
"""Test negative flag handling.""" |
| 97 |
tester = bash_tester(app_negative, "negapp") |
| 98 |
|
| 99 |
assert "--verbose" in tester.completion_script |
| 100 |
assert "--no-verbose" in tester.completion_script |
| 101 |
assert "--colors" in tester.completion_script |
| 102 |
assert "--no-colors" in tester.completion_script |
| 103 |
|
| 104 |
|
| 105 |
def test_disabled_negative_flag_completion(bash_tester): |
| 106 |
"""Test that negative flags are not generated when disabled via App default_parameter.""" |
| 107 |
tester = bash_tester(app_disabled_negative, "disabledneg") |
| 108 |
|
| 109 |
assert "--param" in tester.completion_script |
| 110 |
assert "--empty-param" not in tester.completion_script |
| 111 |
|
| 112 |
|
| 113 |
def test_script_syntax_validation(bash_tester): |
| 114 |
"""Test that generated script has valid bash syntax.""" |
| 115 |
tester = bash_tester(app_basic, "basic") |
| 116 |
|
| 117 |
assert tester.validate_script_syntax() |
| 118 |
|
| 119 |
|
| 120 |
def test_completion_function_naming(bash_tester): |
| 121 |
"""Test that completion function uses correct naming convention.""" |
| 122 |
tester = bash_tester(app_basic, "myapp") |
| 123 |
|
| 124 |
assert "_myapp" in tester.completion_script or "_myapp_completion" in tester.completion_script |
| 125 |
assert "complete -F" in tester.completion_script |
| 126 |
|
| 127 |
|
| 128 |
def test_special_characters_in_choices(bash_tester): |
| 129 |
"""Test that special characters in choices are properly escaped.""" |
| 130 |
app = App(name="special") |
| 131 |
|
| 132 |
@app.default |
| 133 |
def main( |
| 134 |
value: Annotated[ |
| 135 |
Literal["normal", "with space", "with'quote", "with$dollar"], |
| 136 |
Parameter(help="Test value"), |
| 137 |
] = "normal", |
| 138 |
): |
| 139 |
"""Test app with special characters.""" |
| 140 |
pass |
| 141 |
|
| 142 |
tester = bash_tester(app, "special") |
| 143 |
assert tester.validate_script_syntax() |
| 144 |
|
| 145 |
|
| 146 |
def test_help_descriptions(bash_tester): |
| 147 |
"""Test that completion script contains relevant option and command names. |
| 148 |
|
| 149 |
Note: Bash completion uses a minimal format (compgen -W) without descriptions, |
| 150 |
unlike zsh/fish which support inline descriptions. |
| 151 |
""" |
| 152 |
tester = bash_tester(app_basic, "basic") |
| 153 |
|
| 154 |
assert "--verbose" in tester.completion_script |
| 155 |
assert "deploy" in tester.completion_script |
| 156 |
|
| 157 |
|
| 158 |
def test_description_escaping(bash_tester): |
| 159 |
"""Test that descriptions with special chars are properly escaped.""" |
| 160 |
app = App(name="escape_test") |
| 161 |
|
| 162 |
@app.default |
| 163 |
def main( |
| 164 |
param1: Annotated[str, Parameter(help="Test 'single' quotes")] = "", |
| 165 |
param2: Annotated[str, Parameter(help='Test "double" quotes')] = "", |
| 166 |
param3: Annotated[str, Parameter(help="Test $variable and `backticks`")] = "", |
| 167 |
): |
| 168 |
"""Test app.""" |
| 169 |
|
| 170 |
tester = bash_tester(app, "escape_test") |
| 171 |
assert tester.validate_script_syntax() |
| 172 |
|
| 173 |
|
| 174 |
def test_special_chars_in_literal_choices(bash_tester): |
| 175 |
"""Test that Literal choices with special characters are properly escaped.""" |
| 176 |
app = App(name="special_choices") |
| 177 |
|
| 178 |
@app.default |
| 179 |
def main( |
| 180 |
choice: Annotated[Literal["foo bar", "baz()", "test[1]"], Parameter()] = "foo bar", |
| 181 |
): |
| 182 |
"""Test app with special chars in choices.""" |
| 183 |
|
| 184 |
tester = bash_tester(app, "special_choices") |
| 185 |
assert tester.validate_script_syntax() |
| 186 |
|
| 187 |
|
| 188 |
def test_unicode_in_descriptions(bash_tester): |
| 189 |
"""Test that apps with Unicode descriptions generate valid bash syntax. |
| 190 |
|
| 191 |
Note: Bash completion doesn't include descriptions, so we only verify |
| 192 |
syntax validity and presence of the options. |
| 193 |
""" |
| 194 |
app = App(name="unicode_test") |
| 195 |
|
| 196 |
@app.default |
| 197 |
def main( |
| 198 |
emoji: Annotated[str, Parameter(help="Enable 🚀 rocket mode")] = "", |
| 199 |
chinese: Annotated[str, Parameter(help="中文描述")] = "", |
| 200 |
arabic: Annotated[str, Parameter(help="وصف بالعربية")] = "", |
| 201 |
): |
| 202 |
"""Test app with Unicode.""" |
| 203 |
|
| 204 |
tester = bash_tester(app, "unicode_test") |
| 205 |
assert "--emoji" in tester.completion_script |
| 206 |
assert tester.validate_script_syntax() |
| 207 |
|
| 208 |
|
| 209 |
def test_deeply_nested_commands(bash_tester): |
| 210 |
"""Test completion for deeply nested commands (3+ levels).""" |
| 211 |
root = App(name="root") |
| 212 |
level1 = App(name="level1") |
| 213 |
level2 = App(name="level2") |
| 214 |
level3 = App(name="level3") |
| 215 |
|
| 216 |
@level3.command |
| 217 |
def action(value: str): |
| 218 |
"""Perform action at level 3.""" |
| 219 |
pass |
| 220 |
|
| 221 |
level2.command(level3) |
| 222 |
level1.command(level2) |
| 223 |
root.command(level1) |
| 224 |
|
| 225 |
tester = bash_tester(root, "root") |
| 226 |
|
| 227 |
assert "level1" in tester.completion_script |
| 228 |
assert "level2" in tester.completion_script |
| 229 |
assert "level3" in tester.completion_script |
| 230 |
assert "action" in tester.completion_script |
| 231 |
assert tester.validate_script_syntax() |
| 232 |
|
| 233 |
|
| 234 |
def test_nested_command_disambiguation(bash_tester): |
| 235 |
"""Test that nested commands are properly disambiguated. |
| 236 |
|
| 237 |
This test verifies that the helper function correctly distinguishes between |
| 238 |
commands with overlapping names (e.g., 'config get' vs 'admin get'). |
| 239 |
""" |
| 240 |
root = App(name="myapp") |
| 241 |
|
| 242 |
config = App(name="config") |
| 243 |
admin = App(name="admin") |
| 244 |
|
| 245 |
@config.command(name="get") |
| 246 |
def config_get(key: Annotated[str, Parameter(help="Config key")] = ""): |
| 247 |
"""Get config value.""" |
| 248 |
pass |
| 249 |
|
| 250 |
@config.command(name="set") |
| 251 |
def config_set(key: str = "", value: str = ""): |
| 252 |
"""Set config value.""" |
| 253 |
pass |
| 254 |
|
| 255 |
@admin.command(name="get") |
| 256 |
def admin_get(user: Annotated[str, Parameter(help="Username")] = ""): |
| 257 |
"""Get admin user.""" |
| 258 |
pass |
| 259 |
|
| 260 |
root.command(config) |
| 261 |
root.command(admin) |
| 262 |
|
| 263 |
tester = bash_tester(root, "myapp") |
| 264 |
|
| 265 |
script = tester.completion_script |
| 266 |
|
| 267 |
assert "cmd_path" in script, "Should use command path detection" |
| 268 |
|
| 269 |
config_lines = [line for line in script.split("\n") if "config" in line.lower()] |
| 270 |
admin_lines = [line for line in script.split("\n") if "admin" in line.lower()] |
| 271 |
|
| 272 |
assert any("config" in line for line in config_lines), "Should have config-specific completions" |
| 273 |
assert any("admin" in line for line in admin_lines), "Should have admin-specific completions" |
| 274 |
|
| 275 |
assert tester.validate_script_syntax() |
| 276 |
|
| 277 |
|
| 278 |
def test_helper_function_generation(bash_tester): |
| 279 |
"""Test that command path detection logic works correctly. |
| 280 |
|
| 281 |
Note: Bash always generates cmd_path detection logic (even for root-only apps) |
| 282 |
unlike fish which conditionally generates helper functions. This is fine since |
| 283 |
the logic is minimal and doesn't hurt performance. |
| 284 |
""" |
| 285 |
root_only = App(name="rootonly") |
| 286 |
|
| 287 |
@root_only.default |
| 288 |
def main(verbose: bool = False): |
| 289 |
"""Root only app.""" |
| 290 |
pass |
| 291 |
|
| 292 |
tester_root = bash_tester(root_only, "rootonly") |
| 293 |
assert tester_root.validate_script_syntax() |
| 294 |
|
| 295 |
nested = App(name="nested") |
| 296 |
sub = App(name="sub") |
| 297 |
|
| 298 |
@sub.default |
| 299 |
def action(): |
| 300 |
"""Sub action.""" |
| 301 |
pass |
| 302 |
|
| 303 |
nested.command(sub) |
| 304 |
|
| 305 |
tester_nested = bash_tester(nested, "nested") |
| 306 |
assert "cmd_path" in tester_nested.completion_script |
| 307 |
assert tester_nested.validate_script_syntax() |
| 308 |
|
| 309 |
|
| 310 |
def test_optional_path_completion(bash_tester): |
| 311 |
"""Test that Optional[Path] and Path | None generate file completion.""" |
| 312 |
tester = bash_tester(app_path, "pathapp") |
| 313 |
|
| 314 |
assert "compgen -f" in tester.completion_script or "compgen -d" in tester.completion_script |
| 315 |
|
| 316 |
|
| 317 |
def test_no_file_completion_for_strings(bash_tester): |
| 318 |
"""Test that string options don't default to file completion.""" |
| 319 |
app = App(name="strtest") |
| 320 |
|
| 321 |
@app.default |
| 322 |
def main( |
| 323 |
name: Annotated[str, Parameter(help="Name")] = "default", |
| 324 |
): |
| 325 |
"""Test app.""" |
| 326 |
|
| 327 |
tester = bash_tester(app, "strtest") |
| 328 |
assert tester.validate_script_syntax() |
| 329 |
|
| 330 |
|
| 331 |
def test_empty_iterable_flag_completion(bash_tester): |
| 332 |
"""Test that --empty-* flags for list parameters are treated as flags. |
| 333 |
|
| 334 |
Regression test for issue where --empty-items on list[str] parameters |
| 335 |
would expect a value instead of being treated as a flag. |
| 336 |
""" |
| 337 |
app = App(name="listapp") |
| 338 |
|
| 339 |
@app.command |
| 340 |
def process( |
| 341 |
items: Annotated[list[str], Parameter(help="Items to process")], |
| 342 |
tags: Annotated[list[str] | None, Parameter(help="Optional tags")] = None, |
| 343 |
): |
| 344 |
"""Process items.""" |
| 345 |
pass |
| 346 |
|
| 347 |
tester = bash_tester(app, "listapp") |
| 348 |
|
| 349 |
assert "--items" in tester.completion_script |
| 350 |
assert "--empty-items" in tester.completion_script |
| 351 |
assert "--tags" in tester.completion_script |
| 352 |
assert "--empty-tags" in tester.completion_script |
| 353 |
|
| 354 |
lines = tester.completion_script.split("\n") |
| 355 |
options_line = None |
| 356 |
for line in lines: |
| 357 |
if "options_with_values" in line: |
| 358 |
options_line = line |
| 359 |
break |
| 360 |
|
| 361 |
if options_line: |
| 362 |
assert "--empty-items" not in options_line, "Empty flags should not be in options_with_values" |
| 363 |
assert "--empty-tags" not in options_line, "Empty flags should not be in options_with_values" |
| 364 |
|
| 365 |
assert tester.validate_script_syntax() |
| 366 |
|
| 367 |
|
| 368 |
def test_helper_function_skips_option_values(bash_tester): |
| 369 |
"""Test that helper function correctly identifies and skips option values. |
| 370 |
|
| 371 |
Critical bug fix test: ensures that when building command path, |
| 372 |
the helper function skips values for options that take arguments. |
| 373 |
Without this, 'myapp --config file.yaml subcommand' would incorrectly |
| 374 |
extract [file.yaml, subcommand] instead of [subcommand]. |
| 375 |
""" |
| 376 |
app = App(name="myapp") |
| 377 |
sub = App(name="sub") |
| 378 |
|
| 379 |
@sub.default |
| 380 |
def action(): |
| 381 |
"""Subcommand action.""" |
| 382 |
pass |
| 383 |
|
| 384 |
@app.default |
| 385 |
def main( |
| 386 |
config: Annotated[str, Parameter(help="Config file path")], |
| 387 |
verbose: bool = False, |
| 388 |
): |
| 389 |
"""Main app.""" |
| 390 |
pass |
| 391 |
|
| 392 |
app.command(sub) |
| 393 |
|
| 394 |
tester = bash_tester(app, "myapp") |
| 395 |
script = tester.completion_script |
| 396 |
|
| 397 |
assert "options_with_values" in script, "Helper should track options that take values" |
| 398 |
assert "--config" in script or "config" in script |
| 399 |
|
| 400 |
lines = script.split("\n") |
| 401 |
options_line = None |
| 402 |
for line in lines: |
| 403 |
if "options_with_values" in line and "local" in line: |
| 404 |
options_line = line |
| 405 |
break |
| 406 |
|
| 407 |
assert options_line is not None, "Should have a line setting options_with_values" |
| 408 |
assert "--config" in options_line or "config" in options_line, "Should include --config in options_with_values" |
| 409 |
assert "--verbose" not in options_line, "Flags should not be in options_with_values" |
| 410 |
|
| 411 |
skip_check = [line for line in lines if "skip_next" in line] |
| 412 |
assert len(skip_check) > 0, "Helper should use skip_next logic to skip option values" |
| 413 |
|
| 414 |
assert tester.validate_script_syntax() |
| 415 |
|
| 416 |
|
| 417 |
def test_positional_literal_completion(bash_tester): |
| 418 |
"""Test that positional Literal arguments generate completions. |
| 419 |
|
| 420 |
Regression test for issue #605: positional Literal arguments should |
| 421 |
suggest their choices in bash completion. |
| 422 |
""" |
| 423 |
tester = bash_tester(app_positional_literal, "poslit") |
| 424 |
|
| 425 |
assert "foo" in tester.completion_script |
| 426 |
assert "bar" in tester.completion_script |
| 427 |
assert "baz" in tester.completion_script |
| 428 |
assert tester.validate_script_syntax() |
| 429 |
|
| 430 |
|
| 431 |
def test_multiple_positional_literal_position_aware(bash_tester): |
| 432 |
"""Test that multiple positional Literals complete position-aware choices. |
| 433 |
|
| 434 |
When a command has multiple positional arguments with distinct choice sets, |
| 435 |
the completion should only suggest choices for the current position, not |
| 436 |
all choices from all positionals. |
| 437 |
|
| 438 |
This test validates that the generated bash script uses a case statement |
| 439 |
to provide position-aware completion rather than combining all choices. |
| 440 |
""" |
| 441 |
tester = bash_tester(app_multiple_positionals, "multipos") |
| 442 |
script = tester.completion_script |
| 443 |
|
| 444 |
|
| 445 |
assert "positional_count" in script |
| 446 |
|
| 447 |
|
| 448 |
assert "case ${positional_count} in" in script |
| 449 |
|
| 450 |
|
| 451 |
assert "0)" in script |
| 452 |
|
| 453 |
lines = script.split("\n") |
| 454 |
in_case_0 = False |
| 455 |
found_first_choices = False |
| 456 |
for i, line in enumerate(lines): |
| 457 |
if "case ${positional_count}" in line: |
| 458 |
in_case_0 = True |
| 459 |
elif in_case_0 and "0)" in line: |
| 460 |
|
| 461 |
case_content = [] |
| 462 |
for j in range(i + 1, min(i + 10, len(lines))): |
| 463 |
if ";;" in lines[j]: |
| 464 |
break |
| 465 |
case_content.append(lines[j]) |
| 466 |
case_str = "\n".join(case_content) |
| 467 |
|
| 468 |
if "red" in case_str and "blue" in case_str: |
| 469 |
found_first_choices = True |
| 470 |
|
| 471 |
assert "cat" not in case_str |
| 472 |
assert "dog" not in case_str |
| 473 |
break |
| 474 |
|
| 475 |
assert found_first_choices, "First positional choices not found in position 0" |
| 476 |
|
| 477 |
|
| 478 |
assert "1)" in script |
| 479 |
in_case_1 = False |
| 480 |
found_second_choices = False |
| 481 |
for i, line in enumerate(lines): |
| 482 |
if in_case_1 and "1)" in line: |
| 483 |
|
| 484 |
case_content = [] |
| 485 |
for j in range(i + 1, min(i + 10, len(lines))): |
| 486 |
if ";;" in lines[j]: |
| 487 |
break |
| 488 |
case_content.append(lines[j]) |
| 489 |
case_str = "\n".join(case_content) |
| 490 |
|
| 491 |
if "cat" in case_str and "dog" in case_str: |
| 492 |
found_second_choices = True |
| 493 |
|
| 494 |
assert "red" not in case_str |
| 495 |
assert "blue" not in case_str |
| 496 |
break |
| 497 |
elif "case ${positional_count}" in line: |
| 498 |
in_case_1 = True |
| 499 |
|
| 500 |
assert found_second_choices, "Second positional choices not found in position 1" |
| 501 |
|
| 502 |
assert tester.validate_script_syntax() |
| 503 |
|
| 504 |
|
| 505 |
def test_positional_with_keyword_options(bash_tester): |
| 506 |
"""Test positional completion when command also has keyword-only options. |
| 507 |
|
| 508 |
Regression test: When a command has both positional arguments and keyword-only |
| 509 |
options that take values (like --environment), completion after the command name |
| 510 |
should suggest the positional choices, not empty. |
| 511 |
|
| 512 |
For example: |
| 513 |
- 'deploy deploy <TAB>' should suggest ['web', 'api', 'worker'] |
| 514 |
- 'deploy deploy --environment <TAB>' should suggest ['dev', 'staging', 'prod'] |
| 515 |
|
| 516 |
This ensures the case "$prev" default (*) case handles positionals correctly. |
| 517 |
""" |
| 518 |
tester = bash_tester(app_deploy, "deploy") |
| 519 |
script = tester.completion_script |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
assert 'case "$_value_prev"' in script or 'case "${prev}"' in script |
| 526 |
|
| 527 |
|
| 528 |
assert "web" in script |
| 529 |
assert "api" in script |
| 530 |
assert "worker" in script |
| 531 |
|
| 532 |
|
| 533 |
assert "--environment" in script |
| 534 |
assert "dev" in script |
| 535 |
assert "staging" in script |
| 536 |
assert "prod" in script |
| 537 |
|
| 538 |
assert tester.validate_script_syntax() |
| 539 |
|
| 540 |
|
| 541 |
def test_positional_not_treated_as_command(bash_tester): |
| 542 |
"""Test that positional argument values are not mistaken for subcommands. |
| 543 |
|
| 544 |
Regression test: When a command takes positional arguments, those argument |
| 545 |
values should not be added to cmd_path (command hierarchy). For example, |
| 546 |
'deploy production us-east' should be recognized as the 'deploy' command |
| 547 |
with two positionals, NOT as command path ['deploy', 'production']. |
| 548 |
|
| 549 |
This was a critical bug where the second positional would fail to complete |
| 550 |
because 'production' was incorrectly treated as a subcommand. |
| 551 |
""" |
| 552 |
import subprocess |
| 553 |
from typing import Literal |
| 554 |
|
| 555 |
app = App(name="deploy") |
| 556 |
|
| 557 |
@app.command |
| 558 |
def deploy( |
| 559 |
environment: Literal["production", "staging"], |
| 560 |
region: Literal["us-east", "us-west", "eu"], |
| 561 |
/, |
| 562 |
): |
| 563 |
"""Deploy to environment and region.""" |
| 564 |
pass |
| 565 |
|
| 566 |
tester = bash_tester(app, "cyclopts-demo") |
| 567 |
script = tester.completion_script |
| 568 |
|
| 569 |
|
| 570 |
assert "all_commands" in script |
| 571 |
assert "deploy" in script |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
lines = script.split("\n") |
| 576 |
all_commands_line = None |
| 577 |
for line in lines: |
| 578 |
if "local all_commands=" in line: |
| 579 |
all_commands_line = line |
| 580 |
break |
| 581 |
|
| 582 |
assert all_commands_line is not None |
| 583 |
assert "production" not in all_commands_line |
| 584 |
assert "staging" not in all_commands_line |
| 585 |
assert "us-east" not in all_commands_line |
| 586 |
|
| 587 |
|
| 588 |
test_script = f""" |
| 589 |
source /dev/stdin << 'COMPLETION_SCRIPT' |
| 590 |
{script} |
| 591 |
COMPLETION_SCRIPT |
| 592 |
|
| 593 |
# Simulate: cyclopts-demo deploy production <TAB> |
| 594 |
COMP_WORDS=(cyclopts-demo deploy production "") |
| 595 |
COMP_CWORD=3 |
| 596 |
_cyclopts_demo |
| 597 |
|
| 598 |
# Should get region completions |
| 599 |
if [ ${{#COMPREPLY[@]}} -eq 0 ]; then |
| 600 |
echo "FAIL: No completions" |
| 601 |
exit 1 |
| 602 |
fi |
| 603 |
|
| 604 |
# Check that we got the expected completions |
| 605 |
found_useast=0 |
| 606 |
found_uswest=0 |
| 607 |
found_eu=0 |
| 608 |
for item in "${{COMPREPLY[@]}}"; do |
| 609 |
if [ "$item" = "us-east" ]; then found_useast=1; fi |
| 610 |
if [ "$item" = "us-west" ]; then found_uswest=1; fi |
| 611 |
if [ "$item" = "eu" ]; then found_eu=1; fi |
| 612 |
done |
| 613 |
|
| 614 |
if [ $found_useast -eq 1 ] && [ $found_uswest -eq 1 ] && [ $found_eu -eq 1 ]; then |
| 615 |
exit 0 |
| 616 |
else |
| 617 |
echo "FAIL: Missing expected completions" |
| 618 |
exit 1 |
| 619 |
fi |
| 620 |
""" |
| 621 |
|
| 622 |
result = subprocess.run( |
| 623 |
["bash", "-c", test_script], |
| 624 |
capture_output=True, |
| 625 |
text=True, |
| 626 |
) |
| 627 |
|
| 628 |
assert result.returncode == 0, f"Completion test failed: {result.stdout}\n{result.stderr}" |
| 629 |
assert tester.validate_script_syntax() |
| 630 |
|
| 631 |
|
| 632 |
def test_positional_path_completion(bash_tester): |
| 633 |
"""Test that positional Path arguments generate file completion. |
| 634 |
|
| 635 |
Regression test: positional Path arguments should use file completion |
| 636 |
(compgen -f) instead of empty completion. |
| 637 |
""" |
| 638 |
tester = bash_tester(app_positional_path, "pathpos") |
| 639 |
script = tester.completion_script |
| 640 |
|
| 641 |
|
| 642 |
assert "compgen -f" in script |
| 643 |
assert tester.validate_script_syntax() |
| 644 |
|
| 645 |
|
| 646 |
def test_literal_with_show_choices_false(bash_tester): |
| 647 |
"""Test that Literal with show_choices=False still provides completions. |
| 648 |
|
| 649 |
Regression test: When show_choices=False is set on a Literal parameter, |
| 650 |
the choices should still be available for shell completion, even though |
| 651 |
they are hidden from the help text. |
| 652 |
""" |
| 653 |
app = App(name="deploy") |
| 654 |
|
| 655 |
@app.default |
| 656 |
def main( |
| 657 |
env: Annotated[ |
| 658 |
Literal["dev", "staging", "prod"], |
| 659 |
Parameter(help="Environment to deploy to", show_choices=False), |
| 660 |
], |
| 661 |
): |
| 662 |
"""Deploy to environment.""" |
| 663 |
pass |
| 664 |
|
| 665 |
tester = bash_tester(app, "deploy") |
| 666 |
script = tester.completion_script |
| 667 |
|
| 668 |
|
| 669 |
assert "dev" in script |
| 670 |
assert "staging" in script |
| 671 |
assert "prod" in script |
| 672 |
|
| 673 |
|
| 674 |
def test_command_with_multiple_names_and_aliases(bash_tester): |
| 675 |
"""Test that commands registered with multiple names/aliases all appear in completions. |
| 676 |
|
| 677 |
Regression test for groups_from_app() deduplication - ensures all registered |
| 678 |
names are included in completion scripts. |
| 679 |
""" |
| 680 |
app = App(name="myapp") |
| 681 |
sub = App() |
| 682 |
|
| 683 |
@sub.default |
| 684 |
def action(value: str = ""): |
| 685 |
"""Perform an action.""" |
| 686 |
pass |
| 687 |
|
| 688 |
app.command(sub, name="foo", alias=["bar", "baz"]) |
| 689 |
|
| 690 |
tester = bash_tester(app, "myapp") |
| 691 |
script = tester.completion_script |
| 692 |
|
| 693 |
assert "foo" in script, "Primary name should be in completion script" |
| 694 |
assert "bar" in script, "First alias should be in completion script" |
| 695 |
assert "baz" in script, "Second alias should be in completion script" |
| 696 |
|
| 697 |
assert tester.validate_script_syntax() |
| 698 |
|
| 699 |
|
| 700 |
def test_list_path_completion(bash_tester): |
| 701 |
"""Test that list[Path] arguments generate file completion. |
| 702 |
|
| 703 |
Regression test for issue #654: list[Path] arguments should use |
| 704 |
file completion (compgen -f) just like Path arguments. |
| 705 |
""" |
| 706 |
tester = bash_tester(app_list_path, "listpath") |
| 707 |
script = tester.completion_script |
| 708 |
|
| 709 |
assert "compgen -f" in script, "list[Path] should generate file completion" |
| 710 |
assert tester.validate_script_syntax() |
| 711 |
|
| 712 |
|
| 713 |
def test_list_annotated_path_completion(bash_tester): |
| 714 |
"""Test that list[Annotated[Path, ...]] arguments generate file completion.""" |
| 715 |
tester = bash_tester(app_list_annotated_path, "listannotatedpath") |
| 716 |
script = tester.completion_script |
| 717 |
|
| 718 |
assert "compgen -f" in script, "list[ExistingFile] should generate file completion" |
| 719 |
assert tester.validate_script_syntax() |
| 720 |
|
| 721 |
|
| 722 |
def test_list_path_multi_positional_default_case(bash_tester): |
| 723 |
"""Test that list[Path] positional uses file completion at every later position. |
| 724 |
|
| 725 |
When a ``list[Path]`` follows a scalar positional, position 0 takes the |
| 726 |
scalar's completion and *every* later position is owned by the iterable |
| 727 |
(collapsed onto the ``*)`` default — there's no separate numbered case |
| 728 |
for the iterable). End-to-end: position 1, 2, … all offer files. |
| 729 |
""" |
| 730 |
import os |
| 731 |
import tempfile |
| 732 |
from pathlib import Path as _Path |
| 733 |
from typing import Literal |
| 734 |
|
| 735 |
app = App(name="testapp") |
| 736 |
|
| 737 |
@app.command |
| 738 |
def cmd(first: Literal["a", "b"], files: list[_Path], /): |
| 739 |
pass |
| 740 |
|
| 741 |
tester = bash_tester(app, "testapp") |
| 742 |
assert tester.validate_script_syntax() |
| 743 |
|
| 744 |
with tempfile.TemporaryDirectory() as td: |
| 745 |
(_Path(td) / "x.txt").write_text("x") |
| 746 |
cwd = _Path.cwd() |
| 747 |
try: |
| 748 |
os.chdir(td) |
| 749 |
pos0 = tester.get_completions("testapp cmd ") |
| 750 |
pos1 = tester.get_completions("testapp cmd a ") |
| 751 |
pos2 = tester.get_completions("testapp cmd a x.txt ") |
| 752 |
finally: |
| 753 |
os.chdir(cwd) |
| 754 |
|
| 755 |
assert {"a", "b"} <= set(pos0) |
| 756 |
assert pos1, f"expected file completion at position 1, got {pos1!r}" |
| 757 |
assert pos2, f"expected file completion at position 2, got {pos2!r}" |
| 758 |
|
| 759 |
|
| 760 |
def test_colon_in_command_name(bash_tester): |
| 761 |
"""Test that colons in command names work correctly in bash completion. |
| 762 |
|
| 763 |
Unlike zsh where colons are special in _describe format, bash handles |
| 764 |
colons without special escaping in compgen -W word lists. |
| 765 |
""" |
| 766 |
app = App(name="myapp") |
| 767 |
|
| 768 |
sub = App() |
| 769 |
|
| 770 |
@sub.default |
| 771 |
def action(value: str = ""): |
| 772 |
"""Perform an action.""" |
| 773 |
pass |
| 774 |
|
| 775 |
|
| 776 |
app.command(sub, name="utility:ping") |
| 777 |
|
| 778 |
tester = bash_tester(app, "myapp") |
| 779 |
script = tester.completion_script |
| 780 |
|
| 781 |
|
| 782 |
assert "utility:ping" in script, "Command name with colon should appear in script" |
| 783 |
|
| 784 |
assert tester.validate_script_syntax() |
| 785 |
|
| 786 |
|
| 787 |
def test_glob_chars_in_command_name(bash_tester): |
| 788 |
"""Test that glob characters in command names are properly escaped. |
| 789 |
|
| 790 |
Regression test: Command names containing glob characters like * ? [ ] |
| 791 |
should be escaped in case patterns to prevent glob matching. |
| 792 |
""" |
| 793 |
app = App(name="myapp") |
| 794 |
|
| 795 |
sub1 = App() |
| 796 |
|
| 797 |
@sub1.default |
| 798 |
def action1(): |
| 799 |
"""Action with brackets.""" |
| 800 |
pass |
| 801 |
|
| 802 |
|
| 803 |
app.command(sub1, name="test[1]") |
| 804 |
|
| 805 |
tester = bash_tester(app, "myapp") |
| 806 |
script = tester.completion_script |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
assert r"test\[1\]" in script, "Brackets in command name should be escaped in case patterns" |
| 811 |
|
| 812 |
assert tester.validate_script_syntax() |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
def test_choice_with_whitespace(bash_tester): |
| 825 |
"""A choice value containing a space stays a single completion.""" |
| 826 |
app = App(name="ws") |
| 827 |
|
| 828 |
@app.default |
| 829 |
def m(x: Literal["hello world", "normal"] = "normal", /): |
| 830 |
"""Whitespace in choice.""" |
| 831 |
|
| 832 |
tester = bash_tester(app, "ws") |
| 833 |
assert tester.validate_script_syntax() |
| 834 |
completions = tester.get_completions("ws ") |
| 835 |
assert "hello world" in completions |
| 836 |
assert "hello" not in completions |
| 837 |
assert "world" not in completions |
| 838 |
|
| 839 |
|
| 840 |
def test_choice_with_single_quote(bash_tester): |
| 841 |
"""A choice value containing a single quote round-trips intact.""" |
| 842 |
app = App(name="sq") |
| 843 |
|
| 844 |
@app.default |
| 845 |
def m(x: Literal["a'b", "normal"] = "normal", /): |
| 846 |
"""Single quote in choice.""" |
| 847 |
|
| 848 |
tester = bash_tester(app, "sq") |
| 849 |
assert tester.validate_script_syntax() |
| 850 |
completions = tester.get_completions("sq ") |
| 851 |
assert "a'b" in completions |
| 852 |
|
| 853 |
|
| 854 |
def test_choice_with_backtick(bash_tester): |
| 855 |
r"""A choice value containing a backtick does not break the script. |
| 856 |
|
| 857 |
Previously ``compgen -W 'a`b ...'`` inside ``$(...)`` raised |
| 858 |
``bad substitution: no closing "\`"`` because ``$(...)`` re-scans for |
| 859 |
backticks even inside single quotes. |
| 860 |
""" |
| 861 |
app = App(name="btk") |
| 862 |
|
| 863 |
@app.default |
| 864 |
def m(x: Literal["a`b", "normal"] = "normal", /): |
| 865 |
"""Backtick in choice.""" |
| 866 |
|
| 867 |
tester = bash_tester(app, "btk") |
| 868 |
assert tester.validate_script_syntax() |
| 869 |
completions = tester.get_completions("btk ") |
| 870 |
assert "a`b" in completions |
| 871 |
|
| 872 |
|
| 873 |
def test_choice_with_dollar(bash_tester): |
| 874 |
"""A choice value containing $ is not parameter-expanded.""" |
| 875 |
app = App(name="dol") |
| 876 |
|
| 877 |
@app.default |
| 878 |
def m(x: Literal["$home", "normal"] = "normal", /): |
| 879 |
"""Dollar in choice.""" |
| 880 |
|
| 881 |
tester = bash_tester(app, "dol") |
| 882 |
assert tester.validate_script_syntax() |
| 883 |
completions = tester.get_completions("dol ") |
| 884 |
assert "$home" in completions |
| 885 |
|
| 886 |
|
| 887 |
def test_choice_value_after_option(bash_tester): |
| 888 |
"""Tricky choices also survive the ``--opt <choice>`` value-completion path.""" |
| 889 |
app = App(name="optchoice") |
| 890 |
|
| 891 |
@app.default |
| 892 |
def m(env: Annotated[Literal["a b", "c'd", "e`f"], Parameter(help="env")] = "a b"): |
| 893 |
"""Tricky choices behind a keyword option.""" |
| 894 |
|
| 895 |
tester = bash_tester(app, "optchoice") |
| 896 |
assert tester.validate_script_syntax() |
| 897 |
completions = tester.get_completions("optchoice --env ") |
| 898 |
assert {"a b", "c'd", "e`f"} <= set(completions) |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
def test_eq_form_literal_choices(bash_tester): |
| 910 |
"""``--env=`` should offer the same Literal choices as ``--env ``.""" |
| 911 |
tester = bash_tester(app_basic, "basic") |
| 912 |
completions = tester.get_completions("basic deploy --env=") |
| 913 |
assert {"dev", "staging", "prod"} <= set(completions) |
| 914 |
|
| 915 |
|
| 916 |
def test_eq_form_literal_choices_partial(bash_tester): |
| 917 |
"""``--env=d`` should narrow the choices to those starting with ``d``.""" |
| 918 |
tester = bash_tester(app_basic, "basic") |
| 919 |
completions = tester.get_completions("basic deploy --env=d") |
| 920 |
assert "dev" in completions |
| 921 |
assert "staging" not in completions |
| 922 |
assert "prod" not in completions |
| 923 |
|
| 924 |
|
| 925 |
def test_eq_form_path_completion(bash_tester): |
| 926 |
"""``--input-file=`` should still offer file completion.""" |
| 927 |
import os |
| 928 |
import tempfile |
| 929 |
from pathlib import Path as _Path |
| 930 |
|
| 931 |
app = App(name="ekw") |
| 932 |
|
| 933 |
@app.default |
| 934 |
def m(input_file: Annotated[_Path, Parameter(help="In")] = _Path()): |
| 935 |
"""Path keyword.""" |
| 936 |
|
| 937 |
tester = bash_tester(app, "ekw") |
| 938 |
with tempfile.TemporaryDirectory() as td: |
| 939 |
(_Path(td) / "sample.txt").write_text("x") |
| 940 |
cwd = _Path.cwd() |
| 941 |
try: |
| 942 |
os.chdir(td) |
| 943 |
completions = tester.get_completions("ekw --input-file=") |
| 944 |
finally: |
| 945 |
os.chdir(cwd) |
| 946 |
assert completions, f"expected file completion, got {completions!r}" |
| 947 |
|
| 948 |
|
| 949 |
def test_eq_form_space_form_still_works(bash_tester): |
| 950 |
"""The ``--env <value>`` form must remain functional after the eq fix.""" |
| 951 |
tester = bash_tester(app_basic, "basic") |
| 952 |
completions = tester.get_completions("basic deploy --env ") |
| 953 |
assert {"dev", "staging", "prod"} <= set(completions) |
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
def test_value_option_repeatable_space_form(bash_tester): |
| 963 |
"""``--env dev --env<TAB>`` should still offer choices.""" |
| 964 |
tester = bash_tester(app_basic, "basic") |
| 965 |
completions = tester.get_completions("basic deploy --env dev --env ") |
| 966 |
assert {"dev", "staging", "prod"} <= set(completions) |
| 967 |
|
| 968 |
|
| 969 |
def test_value_option_repeatable_eq_form(bash_tester): |
| 970 |
"""``--env=dev --env=<TAB>`` should still offer choices.""" |
| 971 |
tester = bash_tester(app_basic, "basic") |
| 972 |
completions = tester.get_completions("basic deploy --env=dev --env=") |
| 973 |
assert {"dev", "staging", "prod"} <= set(completions) |
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
def test_naked_tab_at_no_arg_subcommand_returns_nothing(bash_tester): |
| 986 |
"""Pressing TAB after a no-arg subcommand + space should be empty.""" |
| 987 |
app = App(name="probe") |
| 988 |
|
| 989 |
@app.command |
| 990 |
def noarg(): |
| 991 |
"""No-arg subcommand.""" |
| 992 |
|
| 993 |
tester = bash_tester(app, "probe") |
| 994 |
completions = tester.get_completions("probe noarg ") |
| 995 |
assert completions == [] |
| 996 |
|
| 997 |
|
| 998 |
def test_dash_tab_at_no_arg_subcommand_offers_help(bash_tester): |
| 999 |
"""Typing ``-<TAB>`` at the same point still surfaces ``--help``.""" |
| 1000 |
app = App(name="probe") |
| 1001 |
|
| 1002 |
@app.command |
| 1003 |
def noarg(): |
| 1004 |
"""No-arg subcommand.""" |
| 1005 |
|
| 1006 |
tester = bash_tester(app, "probe") |
| 1007 |
completions = tester.get_completions("probe noarg -") |
| 1008 |
assert "--help" in completions |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
def test_multi_iterable_positionals_rest_owner_collapse(bash_tester): |
| 1023 |
"""Multiple iterable positionals collapse to a single rest-owner. |
| 1024 |
|
| 1025 |
The first iterable (``envs``) owns position 0 and the ``*)`` default; |
| 1026 |
the second iterable (``regions``) is keyword-only-via-default and must |
| 1027 |
not get its own ``1)`` case, otherwise position 1 would offer |
| 1028 |
``us / eu`` when ``envs`` has actually consumed it as ``dev / prod``. |
| 1029 |
""" |
| 1030 |
app = App(name="multi_iter") |
| 1031 |
|
| 1032 |
@app.command |
| 1033 |
def deploy( |
| 1034 |
envs: list[Literal["dev", "prod"]], |
| 1035 |
regions: list[Literal["us", "eu"]] | None = None, |
| 1036 |
): |
| 1037 |
"""Deploy. |
| 1038 |
|
| 1039 |
Parameters |
| 1040 |
---------- |
| 1041 |
envs |
| 1042 |
Environments. |
| 1043 |
regions |
| 1044 |
Regions. |
| 1045 |
""" |
| 1046 |
|
| 1047 |
tester = bash_tester(app, "multi_iter") |
| 1048 |
assert tester.validate_script_syntax() |
| 1049 |
|
| 1050 |
|
| 1051 |
0. |
| 1052 |
pos0 = tester.get_completions("multi_iter deploy ") |
| 1053 |
assert {"dev", "prod"} <= set(pos0) |
| 1054 |
pos1 = tester.get_completions("multi_iter deploy dev ") |
| 1055 |
assert {"dev", "prod"} <= set(pos1) |
| 1056 |
assert "us" not in pos1 and "eu" not in pos1 |
| 1057 |
|
| 1058 |
|
| 1059 |
via_kw = tester.get_completions("multi_iter deploy dev --regions ") |
| 1060 |
assert {"us", "eu"} <= set(via_kw) |
| 1061 |
|
| 1062 |
|
| 1063 |
def test_multi_iterable_after_scalar_positional(bash_tester): |
| 1064 |
"""Scalar positionals before the first iterable keep their own cases.""" |
| 1065 |
app = App(name="mixed_iter") |
| 1066 |
|
| 1067 |
@app.command |
| 1068 |
def cmd( |
| 1069 |
kind: Literal["a", "b"], |
| 1070 |
items: list[Literal["x", "y"]], |
| 1071 |
): |
| 1072 |
"""Mixed scalar + iterable. |
| 1073 |
|
| 1074 |
Parameters |
| 1075 |
---------- |
| 1076 |
kind |
| 1077 |
Kind. |
| 1078 |
items |
| 1079 |
Items. |
| 1080 |
""" |
| 1081 |
|
| 1082 |
tester = bash_tester(app, "mixed_iter") |
| 1083 |
assert tester.validate_script_syntax() |
| 1084 |
|
| 1085 |
pos0 = tester.get_completions("mixed_iter cmd ") |
| 1086 |
assert {"a", "b"} <= set(pos0) |
| 1087 |
pos1 = tester.get_completions("mixed_iter cmd a ") |
| 1088 |
assert {"x", "y"} <= set(pos1) |
| 1089 |
pos2 = tester.get_completions("mixed_iter cmd a x ") |
| 1090 |
assert {"x", "y"} <= set(pos2) |
| 1091 |
|
| 1092 |
|
| 1093 |
def test_bash_path_completion_reflects_caller_cwd(bash_tester): |
| 1094 |
"""Path completion must inspect the caller's cwd, not the harness tmpdir. |
| 1095 |
|
| 1096 |
Reproduces the Copilot finding: ``BashCompletionTester.get_completions()`` |
| 1097 |
forces ``cwd=`` to the temporary directory holding the comp-script, so a |
| 1098 |
test that ``os.chdir()``-es into a target directory before calling |
| 1099 |
``get_completions()`` has the chdir overridden by the driver. Path |
| 1100 |
completion then lists files from the comp-script's tmpdir instead of the |
| 1101 |
intended directory. The driver should inherit the caller's cwd. |
| 1102 |
""" |
| 1103 |
import os |
| 1104 |
import tempfile |
| 1105 |
from pathlib import Path as _Path |
| 1106 |
|
| 1107 |
app = App(name="pcwd") |
| 1108 |
|
| 1109 |
@app.default |
| 1110 |
def m(output: Annotated[_Path, Parameter(help="Out")] = _Path()): |
| 1111 |
"""Path keyword.""" |
| 1112 |
|
| 1113 |
tester = bash_tester(app, "pcwd") |
| 1114 |
with tempfile.TemporaryDirectory() as td: |
| 1115 |
(_Path(td) / "myfile.txt").write_text("x") |
| 1116 |
cwd = _Path.cwd() |
| 1117 |
try: |
| 1118 |
os.chdir(td) |
| 1119 |
completions = tester.get_completions("pcwd --output ") |
| 1120 |
finally: |
| 1121 |
os.chdir(cwd) |
| 1122 |
|
| 1123 |
assert "myfile.txt" in completions, ( |
| 1124 |
f"expected 'myfile.txt' from caller cwd, got {completions!r} (driver likely overrode cwd to its own tmpdir)" |
| 1125 |
) |