| 1 |
from textwrap import dedent |
| 2 |
from typing import Annotated |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from cyclopts import App, Group, Parameter, UnknownOptionError |
| 7 |
|
| 8 |
|
| 9 |
@pytest.mark.parametrize( |
| 10 |
"cmd_str", |
| 11 |
[ |
| 12 |
"1 --b 2 --c=c-value-manual --meta-flag", |
| 13 |
"1 --b=2 --c=c-value-manual --meta-flag", |
| 14 |
"1 --b=2 --c c-value-manual --meta-flag", |
| 15 |
], |
| 16 |
) |
| 17 |
def test_meta_basic(app, cmd_str): |
| 18 |
@app.default |
| 19 |
def foo(a: int, b: int, c="c-value"): |
| 20 |
assert a == 1 |
| 21 |
assert b == 2 |
| 22 |
assert c == "c-value-manual" |
| 23 |
|
| 24 |
@app.meta.default |
| 25 |
def meta(*tokens: Annotated[str, Parameter(allow_leading_hyphen=True)], meta_flag: bool = False): |
| 26 |
assert meta_flag |
| 27 |
app(tokens) |
| 28 |
|
| 29 |
app.meta(cmd_str) |
| 30 |
|
| 31 |
|
| 32 |
def test_meta_app_config_inheritance(app): |
| 33 |
app.config = ("foo", "bar") |
| 34 |
assert app.meta.config == ("foo", "bar") |
| 35 |
|
| 36 |
|
| 37 |
@pytest.fixture |
| 38 |
def queue(): |
| 39 |
return [] |
| 40 |
|
| 41 |
|
| 42 |
@pytest.fixture |
| 43 |
def nested_meta_app(queue, console): |
| 44 |
subapp = App(console=console, result_action="return_value") |
| 45 |
|
| 46 |
@subapp.meta.default |
| 47 |
def subapp_meta(*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)]) -> None: |
| 48 |
"""This is subapp's help.""" |
| 49 |
queue.append("subapp meta") |
| 50 |
subapp(tokens) |
| 51 |
|
| 52 |
@subapp.command |
| 53 |
def foo(value: int) -> None: |
| 54 |
"""Subapp foo help string. |
| 55 |
|
| 56 |
Parameters |
| 57 |
---------- |
| 58 |
value: int |
| 59 |
The value a user inputted. |
| 60 |
""" |
| 61 |
queue.append(f"subapp foo body {value}") |
| 62 |
|
| 63 |
app = App(name="test_app", console=console, result_action="return_value") |
| 64 |
app.command(subapp.meta, name="subapp") |
| 65 |
|
| 66 |
@app.meta.default |
| 67 |
def meta(*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)]): |
| 68 |
queue.append("root meta") |
| 69 |
app(tokens) |
| 70 |
|
| 71 |
return app |
| 72 |
|
| 73 |
|
| 74 |
def test_meta_app_nested_root_help(nested_meta_app, console, queue): |
| 75 |
with console.capture() as capture: |
| 76 |
nested_meta_app.meta(["--help"]) |
| 77 |
|
| 78 |
actual = capture.get() |
| 79 |
|
| 80 |
expected = dedent( |
| 81 |
"""\ |
| 82 |
Usage: test_app COMMAND |
| 83 |
|
| 84 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 85 |
│ subapp This is subapp's help. │ |
| 86 |
│ --help (-h) Display this message and exit. │ |
| 87 |
│ --version Display application version. │ |
| 88 |
╰────────────────────────────────────────────────────────────────────╯ |
| 89 |
""" |
| 90 |
) |
| 91 |
|
| 92 |
assert actual == expected |
| 93 |
assert not queue |
| 94 |
|
| 95 |
|
| 96 |
def test_meta_app_nested_subapp_help(nested_meta_app, console, queue): |
| 97 |
""" |
| 98 |
Classical command chain |
| 99 |
app.meta -> app -> subapp.meta -> subapp -> help |
| 100 |
0 -> MISSING -> 1 -> MISSING -> help |
| 101 |
|
| 102 |
hierarchy-command-chain. |
| 103 |
app -> app.meta -> subapp -> subapp.meta -> help |
| 104 |
""" |
| 105 |
with console.capture() as capture: |
| 106 |
nested_meta_app.meta(["subapp", "--help"]) |
| 107 |
|
| 108 |
actual = capture.get() |
| 109 |
|
| 110 |
expected = dedent( |
| 111 |
"""\ |
| 112 |
Usage: test_app subapp COMMAND |
| 113 |
|
| 114 |
This is subapp's help. |
| 115 |
|
| 116 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 117 |
│ foo Subapp foo help string. │ |
| 118 |
╰────────────────────────────────────────────────────────────────────╯ |
| 119 |
""" |
| 120 |
) |
| 121 |
|
| 122 |
assert actual == expected |
| 123 |
assert not queue |
| 124 |
|
| 125 |
|
| 126 |
def test_meta_app_nested_subapp_foo_help(nested_meta_app, console, queue): |
| 127 |
with console.capture() as capture: |
| 128 |
nested_meta_app.meta(["subapp", "foo", "--help"]) |
| 129 |
|
| 130 |
actual = capture.get() |
| 131 |
|
| 132 |
expected = dedent( |
| 133 |
"""\ |
| 134 |
Usage: test_app subapp foo VALUE |
| 135 |
|
| 136 |
Subapp foo help string. |
| 137 |
|
| 138 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 139 |
│ * VALUE --value The value a user inputted. [required] │ |
| 140 |
╰────────────────────────────────────────────────────────────────────╯ |
| 141 |
""" |
| 142 |
) |
| 143 |
|
| 144 |
assert actual == expected |
| 145 |
assert not queue |
| 146 |
|
| 147 |
|
| 148 |
@pytest.mark.parametrize( |
| 149 |
"cmd_str,expected", |
| 150 |
[ |
| 151 |
("", ["root meta"]), |
| 152 |
("subapp", ["root meta", "subapp meta"]), |
| 153 |
("subapp foo 5", ["root meta", "subapp meta", "subapp foo body 5"]), |
| 154 |
], |
| 155 |
) |
| 156 |
def test_meta_app_nested_exec(nested_meta_app, queue, cmd_str, expected): |
| 157 |
nested_meta_app.meta(cmd_str) |
| 158 |
assert queue == expected |
| 159 |
|
| 160 |
|
| 161 |
def test_meta_app_inheriting_root_default_parameter(app, console): |
| 162 |
"""Confirms that default_parameter gets inherited root_app->meta_app (as well as to subapps when a meta app is used).""" |
| 163 |
app.default_parameter = Parameter(negative=[]) |
| 164 |
app.meta.group_parameters = Group("Global options", sort_key=0) |
| 165 |
|
| 166 |
@app.meta.default |
| 167 |
def meta( |
| 168 |
*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)], |
| 169 |
flag1: bool = False, |
| 170 |
): |
| 171 |
app(tokens) |
| 172 |
|
| 173 |
@app.command |
| 174 |
def foo(*, flag2: bool = False): |
| 175 |
pass |
| 176 |
|
| 177 |
with console.capture() as capture: |
| 178 |
app.meta(["--help"], console=console) |
| 179 |
|
| 180 |
actual = capture.get() |
| 181 |
expected = dedent( |
| 182 |
"""\ |
| 183 |
Usage: test_meta COMMAND [OPTIONS] |
| 184 |
|
| 185 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 186 |
│ foo │ |
| 187 |
│ --help (-h) Display this message and exit. │ |
| 188 |
│ --version Display application version. │ |
| 189 |
╰────────────────────────────────────────────────────────────────────╯ |
| 190 |
╭─ Global options ───────────────────────────────────────────────────╮ |
| 191 |
│ --flag1 [default: False] │ |
| 192 |
╰────────────────────────────────────────────────────────────────────╯ |
| 193 |
""" |
| 194 |
) |
| 195 |
assert actual == expected |
| 196 |
|
| 197 |
with console.capture() as capture: |
| 198 |
app.meta(["foo", "--help"], console=console) |
| 199 |
|
| 200 |
actual = capture.get() |
| 201 |
expected = dedent( |
| 202 |
"""\ |
| 203 |
Usage: test_meta foo [OPTIONS] |
| 204 |
|
| 205 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 206 |
│ --flag2 [default: False] │ |
| 207 |
╰────────────────────────────────────────────────────────────────────╯ |
| 208 |
╭─ Global options ───────────────────────────────────────────────────╮ |
| 209 |
│ --flag1 [default: False] │ |
| 210 |
╰────────────────────────────────────────────────────────────────────╯ |
| 211 |
""" |
| 212 |
) |
| 213 |
assert actual == expected |
| 214 |
|
| 215 |
|
| 216 |
def test_nested_meta_app_inheriting_root_default_parameter(app, console): |
| 217 |
app.default_parameter = Parameter(negative=[]) |
| 218 |
|
| 219 |
@app.meta.default |
| 220 |
def meta( |
| 221 |
*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)], |
| 222 |
flag1: bool = False, |
| 223 |
): |
| 224 |
return app(tokens) |
| 225 |
|
| 226 |
foo_app = App(name="foo") |
| 227 |
|
| 228 |
@foo_app.meta.default |
| 229 |
def foo_meta( |
| 230 |
*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)], |
| 231 |
flag2: bool = False, |
| 232 |
): |
| 233 |
return foo_app(tokens) |
| 234 |
|
| 235 |
@foo_app.default |
| 236 |
def foo_app_default( |
| 237 |
*, |
| 238 |
flag3: bool = False, |
| 239 |
): |
| 240 |
return flag3 |
| 241 |
|
| 242 |
app.command(foo_app.meta, name="foo") |
| 243 |
|
| 244 |
assert app.meta("foo --flag3") is True |
| 245 |
|
| 246 |
with pytest.raises(UnknownOptionError): |
| 247 |
app.meta("foo --no-flag3", exit_on_error=False) |
| 248 |
|
| 249 |
|
| 250 |
def test_meta_app_help_inconsistency_with_argument_order(app, console): |
| 251 |
"""Test for issue #551: Inconsistent help page display with different argument orders. |
| 252 |
|
| 253 |
The issue is that when meta app parameters come before the command name, |
| 254 |
the help display is different than when they come after the command name. |
| 255 |
Both should display the same help page for the foo command. |
| 256 |
""" |
| 257 |
app["--help"].group = "Global options" |
| 258 |
app["--version"].group = "Global options" |
| 259 |
app.meta.group_parameters = Group("Global options", sort_key=0) |
| 260 |
|
| 261 |
@app.command |
| 262 |
def foo(loops: int, *, user: Annotated[str, Parameter(parse=False)]): |
| 263 |
print(f"Hello {user}") |
| 264 |
for i in range(loops): |
| 265 |
print(f"Looping! {i}") |
| 266 |
|
| 267 |
@app.meta.default |
| 268 |
def my_app_launcher(*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)], user: str): |
| 269 |
command, bound, ignored = app.parse_args(tokens) |
| 270 |
return command(*bound.args, **bound.kwargs, user=user) |
| 271 |
|
| 272 |
expected = dedent( |
| 273 |
"""\ |
| 274 |
Usage: test_meta foo --user STR LOOPS |
| 275 |
|
| 276 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 277 |
│ * LOOPS --loops [required] │ |
| 278 |
╰────────────────────────────────────────────────────────────────────╯ |
| 279 |
╭─ Global options ───────────────────────────────────────────────────╮ |
| 280 |
│ * --user [required] │ |
| 281 |
╰────────────────────────────────────────────────────────────────────╯ |
| 282 |
""" |
| 283 |
) |
| 284 |
|
| 285 |
|
| 286 |
with console.capture() as capture: |
| 287 |
app.meta(["--user", "test", "foo", "--help"], console=console) |
| 288 |
|
| 289 |
help_user_before_foo = capture.get() |
| 290 |
assert help_user_before_foo == expected |
| 291 |
|
| 292 |
|
| 293 |
with console.capture() as capture: |
| 294 |
app.meta(["foo", "--user", "test", "--help"], console=console) |
| 295 |
|
| 296 |
help_user_after_foo = capture.get() |
| 297 |
|
| 298 |
assert help_user_after_foo == expected |
| 299 |
|
| 300 |
|
| 301 |
def test_issue_680_nested_meta_command_resolution(app, queue): |
| 302 |
"""Test for issue #680: Commands parsed at wrong level with multiple meta apps.""" |
| 303 |
|
| 304 |
@app.meta.meta.default |
| 305 |
def meta_meta_default( |
| 306 |
*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)], |
| 307 |
): |
| 308 |
queue.append("meta_meta_default") |
| 309 |
command, bound, _ignored = app.meta.parse_args(tokens) |
| 310 |
command(*bound.args, **bound.kwargs) |
| 311 |
|
| 312 |
@app.meta.default |
| 313 |
def meta_default( |
| 314 |
*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)], |
| 315 |
meta_param: str = "default", |
| 316 |
): |
| 317 |
queue.append(f"meta_default({meta_param})") |
| 318 |
app(tokens) |
| 319 |
|
| 320 |
@app.default |
| 321 |
def default(): |
| 322 |
queue.append("default") |
| 323 |
|
| 324 |
@app.command |
| 325 |
def command(): |
| 326 |
queue.append("command") |
| 327 |
|
| 328 |
|
| 329 |
queue.clear() |
| 330 |
app.meta.meta([]) |
| 331 |
assert queue == ["meta_meta_default", "meta_default(default)", "default"] |
| 332 |
|
| 333 |
|
| 334 |
queue.clear() |
| 335 |
app.meta.meta(["command"]) |
| 336 |
assert queue == ["meta_meta_default", "meta_default(default)", "command"] |
| 337 |
|
| 338 |
|
| 339 |
queue.clear() |
| 340 |
app.meta.meta(["--meta-param", "value", "command"]) |
| 341 |
assert queue == ["meta_meta_default", "meta_default(value)", "command"] |
| 342 |
|
| 343 |
|
| 344 |
def test_issue_783_meta_default_usage_shows_options(console): |
| 345 |
"""Test for issue #783: Usage string missing ``[OPTIONS]`` for custom meta.default commands. |
| 346 |
|
| 347 |
When ``meta.default`` defines parameters (besides the ``*tokens`` passthrough), |
| 348 |
the usage line should include ``[OPTIONS]`` to reflect them. |
| 349 |
""" |
| 350 |
from dataclasses import dataclass |
| 351 |
from typing import Optional |
| 352 |
|
| 353 |
app = App(name="test_app", console=console, result_action="return_value") |
| 354 |
|
| 355 |
@dataclass |
| 356 |
class Options: |
| 357 |
input: Annotated[Optional[str], Parameter(name=["--input", "-i"])] = None |
| 358 |
output: Annotated[Optional[str], Parameter(name=["--output", "-o"])] = None |
| 359 |
|
| 360 |
@app.meta.default |
| 361 |
def main( |
| 362 |
*tokens: Annotated[str, Parameter(show=False)], |
| 363 |
opts: Annotated[Optional[Options], Parameter()] = None, |
| 364 |
) -> None: |
| 365 |
command, bound, _ = app.parse_args(tokens) |
| 366 |
if opts is None: |
| 367 |
opts = Options() |
| 368 |
command(*bound.args, **bound.kwargs, opts=opts) |
| 369 |
|
| 370 |
@app.command |
| 371 |
def add(*args: str, opts: Annotated[Options, Parameter(parse=False)]) -> None: |
| 372 |
pass |
| 373 |
|
| 374 |
with console.capture() as capture: |
| 375 |
app.meta(["--help"]) |
| 376 |
|
| 377 |
actual = capture.get() |
| 378 |
assert actual.splitlines()[0] == "Usage: test_app COMMAND [OPTIONS]" |
| 379 |
|
| 380 |
|
| 381 |
def test_nested_meta_app_command_help(console): |
| 382 |
"""Test that --help works for commands in nested meta-apps (issue-680).""" |
| 383 |
app = App(console=console, result_action="return_value") |
| 384 |
|
| 385 |
@app.meta.meta.default |
| 386 |
def meta_meta_default( |
| 387 |
*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)], |
| 388 |
): |
| 389 |
command, bound, _ignored = app.meta.parse_args(tokens) |
| 390 |
command(*bound.args, **bound.kwargs) |
| 391 |
|
| 392 |
@app.meta.meta.command |
| 393 |
def foo(value: str): |
| 394 |
"""Test command in nested meta-app. |
| 395 |
|
| 396 |
Parameters |
| 397 |
---------- |
| 398 |
value : str |
| 399 |
A test value. |
| 400 |
""" |
| 401 |
pass |
| 402 |
|
| 403 |
@app.meta.default |
| 404 |
def meta_default( |
| 405 |
*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)], |
| 406 |
meta_param: str = "default", |
| 407 |
): |
| 408 |
app(tokens) |
| 409 |
|
| 410 |
@app.default |
| 411 |
def default(): |
| 412 |
"""Default command.""" |
| 413 |
pass |
| 414 |
|
| 415 |
@app.command |
| 416 |
def command(): |
| 417 |
"""Regular command.""" |
| 418 |
pass |
| 419 |
|
| 420 |
|
| 421 |
with console.capture() as capture: |
| 422 |
app.meta.meta(["foo", "--help"]) |
| 423 |
|
| 424 |
actual = capture.get() |
| 425 |
|
| 426 |
|
| 427 |
assert "foo" in actual.lower() |
| 428 |
assert "value" in actual.lower() |