| 1 |
"""Tests for lazy command loading via import path strings.""" |
| 2 |
|
| 3 |
import sys |
| 4 |
import textwrap |
| 5 |
from types import ModuleType |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from cyclopts import App, Group |
| 10 |
from cyclopts.command_spec import CommandSpec |
| 11 |
from cyclopts.completion.bash import generate_completion_script |
| 12 |
from cyclopts.exceptions import CommandCollisionError |
| 13 |
|
| 14 |
|
| 15 |
@pytest.fixture |
| 16 |
def lazy_module(): |
| 17 |
"""Fixture that provides a helper to create lazy-loadable test modules. |
| 18 |
|
| 19 |
Automatically cleans up all created modules after the test. |
| 20 |
|
| 21 |
Usage: |
| 22 |
def test_example(lazy_module): |
| 23 |
module = lazy_module() # creates "test_lazy_module" |
| 24 |
module.cmd = lambda: "result" |
| 25 |
# ... test code ... |
| 26 |
# cleanup happens automatically |
| 27 |
""" |
| 28 |
created_modules: list[str] = [] |
| 29 |
|
| 30 |
def create(name: str = "test_lazy_module") -> ModuleType: |
| 31 |
module = ModuleType(name) |
| 32 |
sys.modules[name] = module |
| 33 |
created_modules.append(name) |
| 34 |
return module |
| 35 |
|
| 36 |
yield create |
| 37 |
|
| 38 |
for name in created_modules: |
| 39 |
if name in sys.modules: |
| 40 |
del sys.modules[name] |
| 41 |
|
| 42 |
|
| 43 |
def test_command_spec_basic_function(app): |
| 44 |
"""Test CommandSpec can resolve a basic function.""" |
| 45 |
spec = CommandSpec(import_path="os.path:join") |
| 46 |
resolved = spec.resolve(app) |
| 47 |
|
| 48 |
assert isinstance(resolved, App) |
| 49 |
assert resolved.default_command is not None |
| 50 |
assert resolved.default_command.__name__ == "join" |
| 51 |
|
| 52 |
|
| 53 |
def test_command_spec_invalid_format(app): |
| 54 |
"""Test CommandSpec raises error for invalid import path format.""" |
| 55 |
spec = CommandSpec(import_path="invalid_format_no_colon") |
| 56 |
|
| 57 |
with pytest.raises(ValueError, match="Invalid import path"): |
| 58 |
spec.resolve(app) |
| 59 |
|
| 60 |
|
| 61 |
def test_command_spec_module_not_found(app): |
| 62 |
"""Test CommandSpec raises ImportError for non-existent module.""" |
| 63 |
spec = CommandSpec(import_path="nonexistent_module:func") |
| 64 |
|
| 65 |
with pytest.raises(ImportError, match="Cannot import module"): |
| 66 |
spec.resolve(app) |
| 67 |
|
| 68 |
|
| 69 |
def test_command_spec_attribute_not_found(app): |
| 70 |
"""Test CommandSpec raises AttributeError for non-existent attribute.""" |
| 71 |
spec = CommandSpec(import_path="os:nonexistent_function") |
| 72 |
|
| 73 |
with pytest.raises(AttributeError, match="has no attribute"): |
| 74 |
spec.resolve(app) |
| 75 |
|
| 76 |
|
| 77 |
def test_command_spec_caching(app): |
| 78 |
"""Test CommandSpec caches the resolved App.""" |
| 79 |
spec = CommandSpec(import_path="os.path:join") |
| 80 |
|
| 81 |
resolved1 = spec.resolve(app) |
| 82 |
resolved2 = spec.resolve(app) |
| 83 |
|
| 84 |
assert resolved1 is resolved2 |
| 85 |
|
| 86 |
|
| 87 |
def test_lazy_command_registration(): |
| 88 |
"""Test registering a lazy command via import path.""" |
| 89 |
app = App() |
| 90 |
|
| 91 |
|
| 92 |
app.command("os.path:join", name="join-path") |
| 93 |
|
| 94 |
|
| 95 |
assert "join-path" in app |
| 96 |
|
| 97 |
|
| 98 |
assert isinstance(app._commands["join-path"], CommandSpec) |
| 99 |
|
| 100 |
|
| 101 |
def test_lazy_command_execution(app, lazy_module): |
| 102 |
"""Test executing a lazy command.""" |
| 103 |
test_module = lazy_module() |
| 104 |
test_module.test_command = lambda x: f"executed with {x}" |
| 105 |
|
| 106 |
app.command("test_lazy_module:test_command", name="lazy") |
| 107 |
|
| 108 |
|
| 109 |
resolved = app["lazy"] |
| 110 |
assert isinstance(resolved, App) |
| 111 |
assert resolved.default_command is not None |
| 112 |
|
| 113 |
|
| 114 |
result = app(["lazy", "--x", "value"]) |
| 115 |
assert result == "executed with value" |
| 116 |
|
| 117 |
|
| 118 |
def test_lazy_command_name_transform(app, lazy_module): |
| 119 |
"""Test that name transform is applied to lazy command names.""" |
| 120 |
test_module = lazy_module() |
| 121 |
test_module.create_user = lambda: "created" |
| 122 |
|
| 123 |
|
| 124 |
app.command("test_lazy_module:create_user") |
| 125 |
|
| 126 |
|
| 127 |
assert "create-user" in app |
| 128 |
assert "create_user" not in app |
| 129 |
|
| 130 |
|
| 131 |
def test_lazy_command_inherits_name_transform(lazy_module): |
| 132 |
"""Test that a lazily-resolved sub-app inherits name_transform from parent.""" |
| 133 |
|
| 134 |
def upper_transform(s: str) -> str: |
| 135 |
return s.upper() |
| 136 |
|
| 137 |
parent = App(name="parent", name_transform=upper_transform) |
| 138 |
|
| 139 |
|
| 140 |
test_module = lazy_module("test_nt_module") |
| 141 |
child = App(name="CHILD") |
| 142 |
test_module.child = child |
| 143 |
|
| 144 |
parent.command("test_nt_module:child", name="CHILD") |
| 145 |
resolved = parent["CHILD"] |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
@resolved.command |
| 150 |
def sub_command(): |
| 151 |
pass |
| 152 |
|
| 153 |
assert "SUB_COMMAND" in resolved |
| 154 |
|
| 155 |
|
| 156 |
def test_lazy_command_with_alias(app, lazy_module): |
| 157 |
"""Test lazy command with alias.""" |
| 158 |
test_module = lazy_module() |
| 159 |
test_module.cmd = lambda: "result" |
| 160 |
|
| 161 |
app.command("test_lazy_module:cmd", name="command", alias=["c", "cmd-alias"]) |
| 162 |
|
| 163 |
assert "command" in app |
| 164 |
assert "c" in app |
| 165 |
assert "cmd-alias" in app |
| 166 |
|
| 167 |
|
| 168 |
assert app["command"] is app["c"] is app["cmd-alias"] |
| 169 |
|
| 170 |
|
| 171 |
def test_lazy_command_collision(app): |
| 172 |
"""Test that lazy commands detect collisions.""" |
| 173 |
|
| 174 |
@app.command |
| 175 |
def existing(): |
| 176 |
pass |
| 177 |
|
| 178 |
with pytest.raises(CommandCollisionError): |
| 179 |
app.command("os.path:join", name="existing") |
| 180 |
|
| 181 |
|
| 182 |
def test_lazy_command_help(app, console, lazy_module): |
| 183 |
"""Test that help generation works with lazy commands. |
| 184 |
|
| 185 |
When help= is provided at registration, it appears in the parent's |
| 186 |
--help without resolving the lazy command. When no help= is provided, |
| 187 |
the command still appears but with no description. |
| 188 |
""" |
| 189 |
test_module = lazy_module() |
| 190 |
|
| 191 |
def cmd(x): |
| 192 |
"""Test command documentation.""" |
| 193 |
return x |
| 194 |
|
| 195 |
test_module.cmd = cmd |
| 196 |
|
| 197 |
app.command("test_lazy_module:cmd", name="lazy-cmd", help="Test command documentation.") |
| 198 |
|
| 199 |
nd |
| 200 |
with console.capture() as capture: |
| 201 |
app(["--help"], console=console) |
| 202 |
|
| 203 |
output = capture.get() |
| 204 |
|
| 205 |
assert "lazy-cmd" in output |
| 206 |
|
| 207 |
assert "Test command documentation" in output |
| 208 |
|
| 209 |
assert not app._commands["lazy-cmd"].is_resolved |
| 210 |
|
| 211 |
|
| 212 |
def test_lazy_and_immediate_commands_mixed(app, lazy_module): |
| 213 |
"""Test mixing lazy and immediate command registration.""" |
| 214 |
test_module = lazy_module() |
| 215 |
test_module.lazy_func = lambda: "lazy" |
| 216 |
|
| 217 |
|
| 218 |
@app.command |
| 219 |
def immediate(): |
| 220 |
return "immediate" |
| 221 |
|
| 222 |
|
| 223 |
app.command("test_lazy_module:lazy_func", name="lazy") |
| 224 |
|
| 225 |
|
| 226 |
assert app(["immediate"]) == "immediate" |
| 227 |
assert app(["lazy"]) == "lazy" |
| 228 |
|
| 229 |
|
| 230 |
def test_lazy_command_not_resolved_until_accessed(app): |
| 231 |
"""Test that lazy commands are not resolved until they are accessed.""" |
| 232 |
|
| 233 |
|
| 234 |
app.command("os.path:join", name="lazy") |
| 235 |
|
| 236 |
|
| 237 |
assert isinstance(app._commands["lazy"], CommandSpec) |
| 238 |
assert not app._commands["lazy"].is_resolved |
| 239 |
|
| 240 |
|
| 241 |
_ = app["lazy"] |
| 242 |
|
| 243 |
|
| 244 |
assert app._commands["lazy"].is_resolved |
| 245 |
|
| 246 |
|
| 247 |
def test_lazy_command_app_kwargs(app, lazy_module): |
| 248 |
"""Test that app_kwargs are passed when wrapping a function. |
| 249 |
|
| 250 |
Note: help= and show= are stored on the CommandSpec for pre-resolution |
| 251 |
display, not forwarded into app_kwargs. After resolution the App uses |
| 252 |
the function's docstring for its own help text. |
| 253 |
""" |
| 254 |
test_module = lazy_module() |
| 255 |
|
| 256 |
def test_func(x: int): |
| 257 |
"""Test function.""" |
| 258 |
return x * 2 |
| 259 |
|
| 260 |
test_module.test_func = test_func |
| 261 |
|
| 262 |
|
| 263 |
app.command( |
| 264 |
"test_lazy_module:test_func", |
| 265 |
name="custom", |
| 266 |
help="Custom help text", |
| 267 |
show=True, |
| 268 |
) |
| 269 |
|
| 270 |
|
| 271 |
spec = app._commands["custom"] |
| 272 |
assert isinstance(spec, CommandSpec) |
| 273 |
assert spec.help == "Custom help text" |
| 274 |
assert spec.show is True |
| 275 |
|
| 276 |
|
| 277 |
resolved = app["custom"] |
| 278 |
assert resolved.help == "Custom help text" |
| 279 |
|
| 280 |
|
| 281 |
def test_lazy_command_parse_commands(app, lazy_module): |
| 282 |
"""Test that parse_commands works with lazy commands.""" |
| 283 |
test_module = lazy_module() |
| 284 |
test_module.cmd = lambda: "result" |
| 285 |
|
| 286 |
app.command("test_lazy_module:cmd", name="lazy") |
| 287 |
|
| 288 |
|
| 289 |
command_chain, apps, unused = app.parse_commands(["lazy", "arg1", "arg2"]) |
| 290 |
|
| 291 |
assert command_chain == ("lazy",) |
| 292 |
assert len(apps) == 2 |
| 293 |
assert isinstance(apps[-1], App) |
| 294 |
assert unused == ["arg1", "arg2"] |
| 295 |
|
| 296 |
|
| 297 |
def test_lazy_command_iteration(app, lazy_module): |
| 298 |
"""Test that iterating over commands includes lazy commands.""" |
| 299 |
test_module = lazy_module() |
| 300 |
test_module.cmd = lambda: None |
| 301 |
|
| 302 |
app.command("test_lazy_module:cmd", name="lazy") |
| 303 |
|
| 304 |
@app.command |
| 305 |
def immediate(): |
| 306 |
pass |
| 307 |
|
| 308 |
|
| 309 |
commands = list(app) |
| 310 |
assert "lazy" in commands |
| 311 |
assert "immediate" in commands |
| 312 |
|
| 313 |
|
| 314 |
def test_lazy_command_contains(app, lazy_module): |
| 315 |
"""Test that 'in' operator works with lazy commands.""" |
| 316 |
test_module = lazy_module() |
| 317 |
test_module.cmd = lambda: None |
| 318 |
|
| 319 |
app.command("test_lazy_module:cmd", name="lazy") |
| 320 |
|
| 321 |
|
| 322 |
assert "lazy" in app |
| 323 |
assert "nonexistent" not in app |
| 324 |
|
| 325 |
|
| 326 |
def test_lazy_command_resolves_to_app_instance(lazy_module): |
| 327 |
"""Test that if the import path points to an App, it's used directly.""" |
| 328 |
test_module = lazy_module() |
| 329 |
test_module.my_app = App(name="subapp", help="Subapp help") |
| 330 |
|
| 331 |
app = App() |
| 332 |
|
| 333 |
app.command("test_lazy_module:my_app", name="subapp") |
| 334 |
|
| 335 |
|
| 336 |
resolved = app["subapp"] |
| 337 |
|
| 338 |
|
| 339 |
assert resolved is test_module.my_app |
| 340 |
assert resolved.help == "Subapp help" |
| 341 |
|
| 342 |
|
| 343 |
def test_lazy_subcommands(app, lazy_module): |
| 344 |
"""Test lazy loading in nested command structures.""" |
| 345 |
test_module = lazy_module() |
| 346 |
test_module.subcmd = lambda: "sub result" |
| 347 |
|
| 348 |
|
| 349 |
user_app = App(name="user") |
| 350 |
user_app.command("test_lazy_module:subcmd", name="create") |
| 351 |
app.command(user_app) |
| 352 |
|
| 353 |
|
| 354 |
result = app(["user", "create"]) |
| 355 |
assert result == "sub result" |
| 356 |
|
| 357 |
|
| 358 |
def test_lazy_command_custom_name_in_help(app, console, lazy_module): |
| 359 |
"""Test that custom name (not function name) appears in help. |
| 360 |
|
| 361 |
With help= provided, the stub shows the custom name and help text |
| 362 |
without resolving the lazy command. |
| 363 |
""" |
| 364 |
test_module = lazy_module() |
| 365 |
|
| 366 |
def list_users(limit: int = 10): |
| 367 |
"""List all user accounts.""" |
| 368 |
return f"listing {limit} users" |
| 369 |
|
| 370 |
test_module.list_users = list_users |
| 371 |
|
| 372 |
|
| 373 |
app.command("test_lazy_module:list_users", name="list", help="List all user accounts.") |
| 374 |
|
| 375 |
|
| 376 |
with console.capture() as capture: |
| 377 |
app(["--help"], console=console) |
| 378 |
|
| 379 |
output = capture.get() |
| 380 |
|
| 381 |
|
| 382 |
assert "list" in output.lower() |
| 383 |
|
| 384 |
assert "list-users" not in output |
| 385 |
|
| 386 |
assert "List all user accounts" in output |
| 387 |
|
| 388 |
assert not app._commands["list"].is_resolved |
| 389 |
|
| 390 |
|
| 391 |
def test_lazy_command_custom_name_execution(app, lazy_module): |
| 392 |
"""Test executing a lazy command by its custom name. |
| 393 |
|
| 394 |
Verifies the documented Name vs Function Name behavior: a lazy command |
| 395 |
registered with ``name="list"`` for function ``list_users`` must be |
| 396 |
invocable by the custom name and resolve to the correct function. |
| 397 |
""" |
| 398 |
test_module = lazy_module() |
| 399 |
|
| 400 |
def list_users(limit: int = 10): |
| 401 |
"""List all user accounts.""" |
| 402 |
return f"listing {limit} users" |
| 403 |
|
| 404 |
test_module.list_users = list_users |
| 405 |
|
| 406 |
app.command("test_lazy_module:list_users", name="list") |
| 407 |
|
| 408 |
assert not app._commands["list"].is_resolved |
| 409 |
|
| 410 |
on |
| 411 |
result = app(["list", "--limit", "5"]) |
| 412 |
assert result == "listing 5 users" |
| 413 |
assert app._commands["list"].is_resolved |
| 414 |
|
| 415 |
|
| 416 |
def test_lazy_command_non_leaf_help_does_not_resolve(console, lazy_module): |
| 417 |
"""Test that --help on a non-leaf command lists lazy children without resolving them. |
| 418 |
|
| 419 |
Running ``myapp user --help`` should show lazy child commands in the |
| 420 |
commands panel (as stubs) but not trigger resolution/import. |
| 421 |
""" |
| 422 |
mod_create = lazy_module("test_lazy_create") |
| 423 |
mod_delete = lazy_module("test_lazy_delete") |
| 424 |
|
| 425 |
def create_user(name: str): |
| 426 |
"""Create a new user.""" |
| 427 |
return f"creating {name}" |
| 428 |
|
| 429 |
def delete_user(name: str): |
| 430 |
"""Delete a user.""" |
| 431 |
return f"deleting {name}" |
| 432 |
|
| 433 |
mod_create.create_user = create_user |
| 434 |
mod_delete.delete_user = delete_user |
| 435 |
|
| 436 |
app = App(name="myapp", result_action="return_value") |
| 437 |
user_app = app.command(App(name="user", help="Manage users.")) |
| 438 |
user_app.command("test_lazy_create:create_user", name="create", help="Create a new user.") |
| 439 |
user_app.command("test_lazy_delete:delete_user", name="delete", help="Delete a user.") |
| 440 |
|
| 441 |
assert isinstance(user_app._commands["create"], CommandSpec) |
| 442 |
assert isinstance(user_app._commands["delete"], CommandSpec) |
| 443 |
assert not user_app._commands["create"].is_resolved |
| 444 |
assert not user_app._commands["delete"].is_resolved |
| 445 |
|
| 446 |
with console.capture() as capture: |
| 447 |
app(["user", "--help"], console=console) |
| 448 |
|
| 449 |
actual = capture.get() |
| 450 |
expected = textwrap.dedent( |
| 451 |
"""\ |
| 452 |
Usage: myapp user COMMAND |
| 453 |
|
| 454 |
Manage users. |
| 455 |
|
| 456 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 457 |
│ create Create a new user. │ |
| 458 |
│ delete Delete a user. │ |
| 459 |
╰────────────────────────────────────────────────────────────────────╯ |
| 460 |
""" |
| 461 |
) |
| 462 |
assert actual == expected |
| 463 |
assert not user_app._commands["create"].is_resolved |
| 464 |
assert not user_app._commands["delete"].is_resolved |
| 465 |
|
| 466 |
|
| 467 |
def test_lazy_command_app_with_explicit_help(console, lazy_module): |
| 468 |
"""Test lazily registering an App object with explicit help at registration. |
| 469 |
|
| 470 |
When a lazy import resolves to an App, the explicit ``help`` provided |
| 471 |
at registration time should be used (both before and after resolution). |
| 472 |
""" |
| 473 |
test_module = lazy_module() |
| 474 |
|
| 475 |
sub_app = App(name="deploy", help="App's own help.") |
| 476 |
|
| 477 |
@sub_app.default |
| 478 |
def deploy_handler(target: str): |
| 479 |
"""Deploy handler docstring.""" |
| 480 |
return f"deploying {target}" |
| 481 |
|
| 482 |
test_module.deploy_app = sub_app |
| 483 |
|
| 484 |
app = App(name="myapp", result_action="return_value") |
| 485 |
app.command("test_lazy_module:deploy_app", name="deploy", help="Deploy the application.") |
| 486 |
|
| 487 |
expected = textwrap.dedent( |
| 488 |
"""\ |
| 489 |
Usage: myapp COMMAND |
| 490 |
|
| 491 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 492 |
│ deploy Deploy the application. │ |
| 493 |
│ --help (-h) Display this message and exit. │ |
| 494 |
│ --version Display application version. │ |
| 495 |
╰────────────────────────────────────────────────────────────────────╯ |
| 496 |
""" |
| 497 |
) |
| 498 |
|
| 499 |
assert isinstance(app._commands["deploy"], CommandSpec) |
| 500 |
|
| 501 |
|
| 502 |
with console.capture() as capture: |
| 503 |
app(["--help"], console=console) |
| 504 |
|
| 505 |
assert capture.get() == expected |
| 506 |
assert not app._commands["deploy"].is_resolved |
| 507 |
|
| 508 |
|
| 509 |
result = app(["deploy", "--target", "prod"]) |
| 510 |
assert result == "deploying prod" |
| 511 |
assert app._commands["deploy"].is_resolved |
| 512 |
|
| 513 |
|
| 514 |
with console.capture() as capture: |
| 515 |
app(["--help"], console=console) |
| 516 |
|
| 517 |
assert capture.get() == expected |
| 518 |
|
| 519 |
|
| 520 |
def test_lazy_command_custom_name_subcommand_help(console, lazy_module): |
| 521 |
"""Test that subcommand --help resolves and shows the custom name. |
| 522 |
|
| 523 |
Parent --help does NOT resolve lazy commands, but targeting a specific |
| 524 |
lazy subcommand (e.g., ``app subcmd --help``) resolves only that command. |
| 525 |
The custom name (not the function name) should appear in the output. |
| 526 |
""" |
| 527 |
test_module = lazy_module() |
| 528 |
|
| 529 |
def list_users(limit: int = 10): |
| 530 |
"""List all user accounts.""" |
| 531 |
return f"listing {limit} users" |
| 532 |
|
| 533 |
test_module.list_users = list_users |
| 534 |
|
| 535 |
app = App(name="myapp", result_action="return_value") |
| 536 |
app.command("test_lazy_module:list_users", name="list") |
| 537 |
|
| 538 |
assert isinstance(app._commands["list"], CommandSpec) |
| 539 |
assert not app._commands["list"].is_resolved |
| 540 |
|
| 541 |
|
| 542 |
with console.capture() as capture: |
| 543 |
app(["list", "--help"], console=console) |
| 544 |
|
| 545 |
expected = textwrap.dedent( |
| 546 |
"""\ |
| 547 |
Usage: myapp list [ARGS] |
| 548 |
|
| 549 |
List all user accounts. |
| 550 |
|
| 551 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 552 |
│ LIMIT --limit [default: 10] │ |
| 553 |
╰────────────────────────────────────────────────────────────────────╯ |
| 554 |
""" |
| 555 |
) |
| 556 |
assert capture.get() == expected |
| 557 |
assert app._commands["list"].is_resolved |
| 558 |
|
| 559 |
|
| 560 |
def test_lazy_function_inherits_parent_help_flags(lazy_module): |
| 561 |
"""Test that lazy function commands inherit parent's help_flags. |
| 562 |
|
| 563 |
This proves behavioral equivalence with direct function registration. |
| 564 |
""" |
| 565 |
test_module = lazy_module() |
| 566 |
test_module.cmd = lambda: "result" |
| 567 |
|
| 568 |
|
| 569 |
parent = App(name="parent", help_flags=["--custom-help", "-ch"]) |
| 570 |
|
| 571 |
|
| 572 |
def direct_func(): |
| 573 |
return "direct" |
| 574 |
|
| 575 |
parent.command(direct_func, name="direct") |
| 576 |
direct_app = parent["direct"] |
| 577 |
assert direct_app.help_flags == ("--custom-help", "-ch") |
| 578 |
|
| 579 |
|
| 580 |
parent.command("test_lazy_module:cmd", name="lazy") |
| 581 |
lazy_app = parent["lazy"] |
| 582 |
assert lazy_app.help_flags == ("--custom-help", "-ch") |
| 583 |
|
| 584 |
|
| 585 |
assert direct_app.help_flags == lazy_app.help_flags |
| 586 |
|
| 587 |
|
| 588 |
def test_lazy_function_inherits_parent_version_flags(lazy_module): |
| 589 |
"""Test that lazy function commands inherit parent's version_flags. |
| 590 |
|
| 591 |
This proves behavioral equivalence with direct function registration. |
| 592 |
""" |
| 593 |
test_module = lazy_module() |
| 594 |
test_module.cmd = lambda: "result" |
| 595 |
|
| 596 |
|
| 597 |
parent = App(name="parent", version_flags=["--custom-version", "-cv"]) |
| 598 |
|
| 599 |
|
| 600 |
def direct_func(): |
| 601 |
return "direct" |
| 602 |
|
| 603 |
parent.command(direct_func, name="direct") |
| 604 |
direct_app = parent["direct"] |
| 605 |
assert direct_app.version_flags == ("--custom-version", "-cv") |
| 606 |
|
| 607 |
|
| 608 |
parent.command("test_lazy_module:cmd", name="lazy") |
| 609 |
lazy_app = parent["lazy"] |
| 610 |
assert lazy_app.version_flags == ("--custom-version", "-cv") |
| 611 |
|
| 612 |
|
| 613 |
assert direct_app.version_flags == lazy_app.version_flags |
| 614 |
|
| 615 |
|
| 616 |
def test_lazy_function_inherits_parent_groups(lazy_module): |
| 617 |
"""Test that lazy function commands inherit parent's group settings. |
| 618 |
|
| 619 |
This proves behavioral equivalence with direct function registration. |
| 620 |
""" |
| 621 |
test_module = lazy_module() |
| 622 |
test_module.cmd = lambda: "result" |
| 623 |
|
| 624 |
|
| 625 |
custom_cmd_group = Group("Custom Commands", sort_key=1) |
| 626 |
custom_param_group = Group("Custom Parameters", sort_key=2) |
| 627 |
custom_arg_group = Group("Custom Arguments", sort_key=3) |
| 628 |
|
| 629 |
parent = App( |
| 630 |
name="parent", |
| 631 |
group_commands=custom_cmd_group, |
| 632 |
group_parameters=custom_param_group, |
| 633 |
group_arguments=custom_arg_group, |
| 634 |
) |
| 635 |
|
| 636 |
|
| 637 |
direct_app = App(name="direct") |
| 638 |
parent.command(direct_app) |
| 639 |
assert direct_app._group_commands == custom_cmd_group |
| 640 |
assert direct_app._group_parameters == custom_param_group |
| 641 |
assert direct_app._group_arguments == custom_arg_group |
| 642 |
|
| 643 |
|
| 644 |
parent.command("test_lazy_module:cmd", name="lazy") |
| 645 |
lazy_app = parent["lazy"] |
| 646 |
assert lazy_app._group_commands == custom_cmd_group |
| 647 |
assert lazy_app._group_parameters == custom_param_group |
| 648 |
assert lazy_app._group_arguments == custom_arg_group |
| 649 |
|
| 650 |
|
| 651 |
assert direct_app._group_commands == lazy_app._group_commands |
| 652 |
assert direct_app._group_parameters == lazy_app._group_parameters |
| 653 |
assert direct_app._group_arguments == lazy_app._group_arguments |
| 654 |
|
| 655 |
|
| 656 |
def test_lazy_app_inherits_parent_groups_when_unset(lazy_module): |
| 657 |
"""Test that lazy App imports inherit parent's unset groups. |
| 658 |
|
| 659 |
This proves behavioral equivalence with direct App registration. |
| 660 |
""" |
| 661 |
test_module = lazy_module() |
| 662 |
|
| 663 |
test_module.subapp = App(name="subapp", help="Subapp help") |
| 664 |
|
| 665 |
|
| 666 |
custom_cmd_group = Group("Parent Commands", sort_key=1) |
| 667 |
custom_param_group = Group("Parent Parameters", sort_key=2) |
| 668 |
|
| 669 |
parent = App( |
| 670 |
name="parent", |
| 671 |
group_commands=custom_cmd_group, |
| 672 |
group_parameters=custom_param_group, |
| 673 |
) |
| 674 |
|
| 675 |
|
| 676 |
direct_subapp = App(name="direct-subapp", help="Direct subapp") |
| 677 |
parent.command(direct_subapp) |
| 678 |
assert direct_subapp._group_commands == custom_cmd_group |
| 679 |
assert direct_subapp._group_parameters == custom_param_group |
| 680 |
|
| 681 |
|
| 682 |
parent.command("test_lazy_module:subapp", name="subapp") |
| 683 |
lazy_subapp = parent["subapp"] |
| 684 |
assert lazy_subapp._group_commands == custom_cmd_group |
| 685 |
assert lazy_subapp._group_parameters == custom_param_group |
| 686 |
|
| 687 |
|
| 688 |
assert direct_subapp._group_commands == lazy_subapp._group_commands |
| 689 |
assert direct_subapp._group_parameters == lazy_subapp._group_parameters |
| 690 |
|
| 691 |
|
| 692 |
def test_lazy_app_doesnt_override_existing_groups(lazy_module): |
| 693 |
"""Test that lazy App imports don't override their own groups. |
| 694 |
|
| 695 |
An imported App with its own groups should keep them, not inherit parent's. |
| 696 |
""" |
| 697 |
test_module = lazy_module() |
| 698 |
|
| 699 |
subapp_group = Group("Subapp Commands", sort_key=99) |
| 700 |
test_module.subapp = App(name="subapp", group_commands=subapp_group) |
| 701 |
|
| 702 |
|
| 703 |
parent_group = Group("Parent Commands", sort_key=1) |
| 704 |
parent = App(name="parent", group_commands=parent_group) |
| 705 |
|
| 706 |
|
| 707 |
parent.command("test_lazy_module:subapp", name="subapp") |
| 708 |
lazy_subapp = parent["subapp"] |
| 709 |
|
| 710 |
|
| 711 |
assert lazy_subapp._group_commands == subapp_group |
| 712 |
assert lazy_subapp._group_commands != parent_group |
| 713 |
|
| 714 |
|
| 715 |
def test_lazy_app_rejects_kwargs(lazy_module): |
| 716 |
"""Test that CommandSpec rejects app_kwargs for App imports. |
| 717 |
|
| 718 |
Note: help= and show= are extracted to CommandSpec fields and are |
| 719 |
NOT forwarded into app_kwargs, so they don't trigger the rejection. |
| 720 |
Other kwargs (e.g., group_commands) are still rejected for App imports. |
| 721 |
""" |
| 722 |
test_module = lazy_module() |
| 723 |
test_module.subapp = App(name="subapp") |
| 724 |
|
| 725 |
parent = App(name="parent") |
| 726 |
|
| 727 |
|
| 728 |
parent.command("test_lazy_module:subapp", name="subapp", help="Custom help") |
| 729 |
resolved = parent["subapp"] |
| 730 |
assert resolved is test_module.subapp |
| 731 |
|
| 732 |
|
| 733 |
test_module.subapp2 = App(name="subapp2") |
| 734 |
parent.command("test_lazy_module:subapp2", name="subapp2", group_commands="Foo") |
| 735 |
|
| 736 |
with pytest.raises(ValueError, match="Cannot apply configuration to imported App"): |
| 737 |
_ = parent["subapp2"] |
| 738 |
|
| 739 |
|
| 740 |
def test_lazy_app_name_must_match(lazy_module): |
| 741 |
"""Test that CommandSpec validates App name matches CLI command name.""" |
| 742 |
test_module = lazy_module() |
| 743 |
test_module.subapp = App(name="actual-name") |
| 744 |
|
| 745 |
parent = App(name="parent") |
| 746 |
|
| 747 |
|
| 748 |
parent.command("test_lazy_module:subapp", name="wrong-name") |
| 749 |
|
| 750 |
with pytest.raises(ValueError, match="Imported App name mismatch"): |
| 751 |
_ = parent["wrong-name"] |
| 752 |
|
| 753 |
|
| 754 |
def test_lazy_app_name_match_allows_resolution(lazy_module): |
| 755 |
"""Test that CommandSpec allows resolution when name matches.""" |
| 756 |
test_module = lazy_module() |
| 757 |
test_module.subapp = App(name="matching-name") |
| 758 |
|
| 759 |
parent = App(name="parent") |
| 760 |
|
| 761 |
|
| 762 |
parent.command("test_lazy_module:subapp", name="matching-name") |
| 763 |
resolved = parent["matching-name"] |
| 764 |
|
| 765 |
assert resolved.name[0] == "matching-name" |
| 766 |
assert resolved is test_module.subapp |
| 767 |
|
| 768 |
|
| 769 |
def test_lazy_command_not_resolved_when_executing_different_command(lazy_module): |
| 770 |
"""Test that lazy commands are not resolved when executing a different command. |
| 771 |
|
| 772 |
Regression test for issue #709. |
| 773 |
When executing a specific command (e.g., "b run"), other lazy-loaded commands |
| 774 |
(e.g., "c") should NOT be imported/resolved. |
| 775 |
""" |
| 776 |
|
| 777 |
module_b = lazy_module("test_lazy_module_b") |
| 778 |
module_c = lazy_module("test_lazy_module_c") |
| 779 |
|
| 780 |
def run_b(): |
| 781 |
return "running b" |
| 782 |
|
| 783 |
def run_c(): |
| 784 |
return "running c" |
| 785 |
|
| 786 |
|
| 787 |
b_app = App(name="b", result_action="return_value") |
| 788 |
b_app.command(run_b, name="run") |
| 789 |
|
| 790 |
c_app = App(name="c", result_action="return_value") |
| 791 |
c_app.command(run_c, name="run") |
| 792 |
|
| 793 |
module_b.b_app = b_app |
| 794 |
module_c.c_app = c_app |
| 795 |
|
| 796 |
|
| 797 |
app = App(name="a", result_action="return_value") |
| 798 |
app.command("test_lazy_module_b:b_app", name="b") |
| 799 |
app.command("test_lazy_module_c:c_app", name="c") |
| 800 |
|
| 801 |
|
| 802 |
assert isinstance(app._commands["b"], CommandSpec) |
| 803 |
assert isinstance(app._commands["c"], CommandSpec) |
| 804 |
assert not app._commands["b"].is_resolved |
| 805 |
assert not app._commands["c"].is_resolved |
| 806 |
|
| 807 |
|
| 808 |
result = app(["b", "run"]) |
| 809 |
assert result == "running b" |
| 810 |
|
| 811 |
|
| 812 |
assert app._commands["b"].is_resolved |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
assert not app._commands["c"].is_resolved, ( |
| 817 |
"Lazy command 'c' was resolved even though it was not executed. " |
| 818 |
"This indicates that lazy loading is not working correctly." |
| 819 |
) |
| 820 |
|
| 821 |
|
| 822 |
def test_format_usage_does_not_resolve_lazy_commands(lazy_module): |
| 823 |
"""Test that format_usage() does not resolve lazy commands. |
| 824 |
|
| 825 |
Regression test: format_usage() accessed _registered_commands which |
| 826 |
called __getitem__ on every command, triggering resolution of all |
| 827 |
lazy CommandSpecs. The fix in #710 addressed groups_from_app but |
| 828 |
missed this code path. |
| 829 |
""" |
| 830 |
module_a = lazy_module("test_lazy_fmt_a") |
| 831 |
module_b = lazy_module("test_lazy_fmt_b") |
| 832 |
|
| 833 |
a_app = App(name="cmd-a") |
| 834 |
module_a.a_app = a_app |
| 835 |
|
| 836 |
b_app = App(name="cmd-b") |
| 837 |
module_b.b_app = b_app |
| 838 |
|
| 839 |
app = App(name="myapp", help_flags=["--help"], version_flags=[], result_action="return_value") |
| 840 |
app.command("test_lazy_fmt_a:a_app", name="cmd-a") |
| 841 |
app.command("test_lazy_fmt_b:b_app", name="cmd-b") |
| 842 |
|
| 843 |
|
| 844 |
assert isinstance(app._commands["cmd-a"], CommandSpec) |
| 845 |
assert isinstance(app._commands["cmd-b"], CommandSpec) |
| 846 |
assert not app._commands["cmd-a"].is_resolved |
| 847 |
assert not app._commands["cmd-b"].is_resolved |
| 848 |
|
| 849 |
|
| 850 |
from io import StringIO |
| 851 |
|
| 852 |
from rich.console import Console |
| 853 |
|
| 854 |
buf = StringIO() |
| 855 |
console = Console(file=buf, width=70, force_terminal=True) |
| 856 |
app(["--help"], console=console) |
| 857 |
|
| 858 |
|
| 859 |
output = buf.getvalue() |
| 860 |
assert "COMMAND" in output |
| 861 |
|
| 862 |
|
| 863 |
assert isinstance(app._commands["cmd-a"], CommandSpec) |
| 864 |
assert isinstance(app._commands["cmd-b"], CommandSpec) |
| 865 |
assert not app._commands["cmd-a"].is_resolved, ( |
| 866 |
"Lazy command 'cmd-a' was resolved during --help rendering. format_usage() should not trigger lazy resolution." |
| 867 |
) |
| 868 |
assert not app._commands["cmd-b"].is_resolved, ( |
| 869 |
"Lazy command 'cmd-b' was resolved during --help rendering. format_usage() should not trigger lazy resolution." |
| 870 |
) |
| 871 |
|
| 872 |
|
| 873 |
def test_lazy_command_custom_group_in_help(console, lazy_module): |
| 874 |
"""Test that lazy commands with group= appear in the correct group in help output.""" |
| 875 |
from cyclopts import Group |
| 876 |
|
| 877 |
mod_a = lazy_module("test_lazy_grp_a") |
| 878 |
mod_b = lazy_module("test_lazy_grp_b") |
| 879 |
|
| 880 |
mod_a.cmd_a = lambda: None |
| 881 |
mod_b.cmd_b = lambda: None |
| 882 |
|
| 883 |
admin_group = Group("Admin", sort_key=1) |
| 884 |
|
| 885 |
app = App(name="myapp", result_action="return_value", help_flags=["--help"], version_flags=[]) |
| 886 |
app.command("test_lazy_grp_a:cmd_a", name="admin-cmd", group=admin_group, help="An admin command.") |
| 887 |
app.command("test_lazy_grp_b:cmd_b", name="user-cmd", help="A user command.") |
| 888 |
|
| 889 |
with console.capture() as capture: |
| 890 |
app(["--help"], console=console) |
| 891 |
|
| 892 |
actual = capture.get() |
| 893 |
|
| 894 |
|
| 895 |
assert "Admin" in actual |
| 896 |
assert "Commands" in actual |
| 897 |
|
| 898 |
|
| 899 |
admin_idx = actual.index("Admin") |
| 900 |
commands_idx = actual.index("Commands") |
| 901 |
admin_cmd_idx = actual.index("admin-cmd") |
| 902 |
user_cmd_idx = actual.index("user-cmd") |
| 903 |
|
| 904 |
|
| 905 |
assert admin_cmd_idx > admin_idx |
| 906 |
|
| 907 |
assert user_cmd_idx > commands_idx |
| 908 |
|
| 909 |
|
| 910 |
assert isinstance(app._commands["admin-cmd"], CommandSpec) |
| 911 |
assert not app._commands["admin-cmd"].is_resolved |
| 912 |
assert isinstance(app._commands["user-cmd"], CommandSpec) |
| 913 |
assert not app._commands["user-cmd"].is_resolved |
| 914 |
|
| 915 |
|
| 916 |
def test_lazy_subapp_help_excludes_help_version_flags(console, lazy_module): |
| 917 |
"""Test that lazy-loaded subapps don't show --help/--version in their help. |
| 918 |
|
| 919 |
Regression test for issue #697. |
| 920 |
When a subapp is registered via lazy loading, its help output should NOT |
| 921 |
include --help and --version flags (matching behavior of direct registration). |
| 922 |
""" |
| 923 |
test_module = lazy_module() |
| 924 |
|
| 925 |
|
| 926 |
subapp = App(name="greet") |
| 927 |
|
| 928 |
@subapp.default |
| 929 |
def greet(name: str): |
| 930 |
"""Greet a person by name.""" |
| 931 |
print(f"Hello, {name}!") |
| 932 |
|
| 933 |
test_module.subapp = subapp |
| 934 |
|
| 935 |
|
| 936 |
app_lazy = App(name="App", result_action="return_value") |
| 937 |
app_lazy.command("test_lazy_module:subapp", name="greet") |
| 938 |
|
| 939 |
with console.capture() as capture: |
| 940 |
app_lazy(["greet", "--help"], console=console) |
| 941 |
|
| 942 |
output_lazy = capture.get() |
| 943 |
|
| 944 |
|
| 945 |
assert "--help" not in output_lazy |
| 946 |
assert "--version" not in output_lazy |
| 947 |
assert "Commands" not in output_lazy |
| 948 |
assert "NAME" in output_lazy |
| 949 |
assert "Greet a person by name" in output_lazy |
| 950 |
|
| 951 |
|
| 952 |
def test_lazy_command_appears_in_completion_script(lazy_module): |
| 953 |
"""Test that lazy commands appear in shell completion scripts. |
| 954 |
|
| 955 |
Regression test for issue #742. |
| 956 |
Shell completion scripts are generated once and installed, so they need |
| 957 |
ALL commands including lazy-loaded ones. |
| 958 |
""" |
| 959 |
test_module = lazy_module() |
| 960 |
|
| 961 |
def deploy(env: str): |
| 962 |
"""Deploy to an environment.""" |
| 963 |
return f"deploying to {env}" |
| 964 |
|
| 965 |
test_module.deploy = deploy |
| 966 |
|
| 967 |
app = App(name="myapp", help_flags=[], version_flags=[]) |
| 968 |
|
| 969 |
|
| 970 |
app.command("test_lazy_module:deploy", name="deploy") |
| 971 |
|
| 972 |
|
| 973 |
@app.command |
| 974 |
def status(): |
| 975 |
"""Show status.""" |
| 976 |
pass |
| 977 |
|
| 978 |
|
| 979 |
assert isinstance(app._commands["deploy"], CommandSpec) |
| 980 |
assert not app._commands["deploy"].is_resolved |
| 981 |
|
| 982 |
|
| 983 |
script = generate_completion_script(app, "myapp") |
| 984 |
|
| 985 |
|
| 986 |
assert "deploy" in script, "Lazy command should appear in completion script" |
| 987 |
assert "status" in script, "Regular command should appear in completion script" |
| 988 |
|
| 989 |
|
| 990 |
assert app._commands["deploy"].is_resolved |
| 991 |
|
| 992 |
|
| 993 |
def test_lazy_nested_commands_appear_in_completion_script(lazy_module): |
| 994 |
"""Test that nested lazy commands appear in shell completion scripts. |
| 995 |
|
| 996 |
Regression test for issue #742. |
| 997 |
""" |
| 998 |
test_module = lazy_module() |
| 999 |
|
| 1000 |
|
| 1001 |
subapp = App(name="user") |
| 1002 |
|
| 1003 |
@subapp.command |
| 1004 |
def create(name: str): |
| 1005 |
"""Create a user.""" |
| 1006 |
return f"creating {name}" |
| 1007 |
|
| 1008 |
@subapp.command |
| 1009 |
def delete(name: str): |
| 1010 |
"""Delete a user.""" |
| 1011 |
return f"deleting {name}" |
| 1012 |
|
| 1013 |
test_module.user_app = subapp |
| 1014 |
|
| 1015 |
app = App(name="myapp", help_flags=[], version_flags=[]) |
| 1016 |
|
| 1017 |
|
| 1018 |
app.command("test_lazy_module:user_app", name="user") |
| 1019 |
|
| 1020 |
|
| 1021 |
assert isinstance(app._commands["user"], CommandSpec) |
| 1022 |
assert not app._commands["user"].is_resolved |
| 1023 |
|
| 1024 |
|
| 1025 |
script = generate_completion_script(app, "myapp") |
| 1026 |
|
| 1027 |
|
| 1028 |
assert "user" in script, "Lazy subapp should appear in completion script" |
| 1029 |
assert "create" in script, "Nested command 'create' should appear in completion script" |
| 1030 |
assert "delete" in script, "Nested command 'delete' should appear in completion script" |
| 1031 |
|
| 1032 |
|
| 1033 |
assert app._commands["user"].is_resolved |
| 1034 |
|
| 1035 |
|
| 1036 |
def test_iterate_commands_resolve_lazy(lazy_module): |
| 1037 |
"""Test that iterate_commands respects the resolve_lazy parameter.""" |
| 1038 |
from cyclopts.docs.base import iterate_commands |
| 1039 |
|
| 1040 |
test_module = lazy_module() |
| 1041 |
|
| 1042 |
def lazy_cmd(): |
| 1043 |
"""A lazy command.""" |
| 1044 |
pass |
| 1045 |
|
| 1046 |
test_module.lazy_cmd = lazy_cmd |
| 1047 |
|
| 1048 |
app = App(name="myapp", help_flags=[], version_flags=[]) |
| 1049 |
|
| 1050 |
|
| 1051 |
app.command("test_lazy_module:lazy_cmd", name="lazy") |
| 1052 |
|
| 1053 |
|
| 1054 |
@app.command |
| 1055 |
def regular(): |
| 1056 |
"""A regular command.""" |
| 1057 |
pass |
| 1058 |
|
| 1059 |
|
| 1060 |
assert isinstance(app._commands["lazy"], CommandSpec) |
| 1061 |
assert not app._commands["lazy"].is_resolved |
| 1062 |
|
| 1063 |
|
| 1064 |
commands_without_lazy = list(iterate_commands(app, resolve_lazy=False)) |
| 1065 |
command_names = [name for name, _ in commands_without_lazy] |
| 1066 |
assert "regular" in command_names |
| 1067 |
assert "lazy" not in command_names |
| 1068 |
|
| 1069 |
|
| 1070 |
assert not app._commands["lazy"].is_resolved |
| 1071 |
|
| 1072 |
|
| 1073 |
commands_with_lazy = list(iterate_commands(app, resolve_lazy=True)) |
| 1074 |
command_names = [name for name, _ in commands_with_lazy] |
| 1075 |
assert "regular" in command_names |
| 1076 |
assert "lazy" in command_names |
| 1077 |
|
| 1078 |
|
| 1079 |
assert app._commands["lazy"].is_resolved |