| 1 |
import pytest |
| 2 |
|
| 3 |
from cyclopts import App, Parameter |
| 4 |
from cyclopts.exceptions import UnknownOptionError |
| 5 |
|
| 6 |
|
| 7 |
def test_subapp_basic(app): |
| 8 |
@app.command |
| 9 |
def foo(a: int, b: int, c: int): |
| 10 |
return a + b + c |
| 11 |
|
| 12 |
app.command(bar := App(name="bar")) |
| 13 |
|
| 14 |
@bar.command |
| 15 |
def fizz(a: int, b: int, c: int): |
| 16 |
return a - b - c |
| 17 |
|
| 18 |
@bar.command |
| 19 |
def buzz(): |
| 20 |
return 100 |
| 21 |
|
| 22 |
@bar.default |
| 23 |
def default(a: int): |
| 24 |
return 100 * a |
| 25 |
|
| 26 |
assert 6 == app("foo 1 2 3") |
| 27 |
assert -4 == app("bar fizz 1 2 3") |
| 28 |
assert 100 == app("bar buzz") |
| 29 |
assert 200 == app("bar 2") |
| 30 |
|
| 31 |
|
| 32 |
def test_subapp_must_have_name(app): |
| 33 |
with pytest.raises(ValueError): |
| 34 |
app.command(App()) |
| 35 |
|
| 36 |
app.command(App(), name="foo") |
| 37 |
|
| 38 |
|
| 39 |
def test_subapp_registering_cannot_have_other_kwargs(app): |
| 40 |
with pytest.raises(ValueError): |
| 41 |
app.command(App(name="foo"), help="this is invalid.") |
| 42 |
|
| 43 |
|
| 44 |
def test_subapp_cannot_be_default(app): |
| 45 |
with pytest.raises(TypeError): |
| 46 |
app.default(App(name="foo")) |
| 47 |
|
| 48 |
with pytest.raises(TypeError): |
| 49 |
App(default_command=App(name="foo")) |
| 50 |
|
| 51 |
|
| 52 |
def test_resolve_default_parameter_1(): |
| 53 |
"""Test that sub-app inherits default_parameter from parent when it doesn't have its own.""" |
| 54 |
|
| 55 |
parent_app = App(default_parameter=Parameter(negative_bool=()), result_action="return_value") |
| 56 |
|
| 57 |
parent_app.command(sub_app := App(name="bar")) |
| 58 |
|
| 59 |
@sub_app.default |
| 60 |
def sub_command(*, flag: bool = False): |
| 61 |
return flag |
| 62 |
|
| 63 |
result = parent_app("bar --flag") |
| 64 |
assert result is True |
| 65 |
|
| 66 |
|
| 67 |
with pytest.raises(UnknownOptionError): |
| 68 |
parent_app("bar --no-flag", exit_on_error=False) |
| 69 |
|
| 70 |
|
| 71 |
def test_resolve_default_parameter_2(): |
| 72 |
"""Test that sub-app's default_parameter overrides parent's default_parameter.""" |
| 73 |
|
| 74 |
parent_app = App(default_parameter=Parameter(negative_bool=()), result_action="return_value") |
| 75 |
|
| 76 |
parent_app.command(sub_app := App(name="bar", default_parameter=Parameter(negative_bool="disable-"))) |
| 77 |
|
| 78 |
@sub_app.default |
| 79 |
def sub_command(*, flag: bool = False): |
| 80 |
return flag |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
result = parent_app("bar --disable-flag") |
| 85 |
assert result is False |
| 86 |
|
| 87 |
|
| 88 |
with pytest.raises(UnknownOptionError): |
| 89 |
parent_app("bar --no-flag", exit_on_error=False) |
| 90 |
|
| 91 |
|
| 92 |
def test_subapp_name_alias(app, assert_parse_args): |
| 93 |
@app.command(alias="bar") |
| 94 |
def foo(a): |
| 95 |
pass |
| 96 |
|
| 97 |
assert_parse_args(foo, "foo 5", "5") |
| 98 |
assert_parse_args(foo, "bar 5", "5") |
| 99 |
|
| 100 |
|
| 101 |
def test_subapp_name_and_alias(app, assert_parse_args): |
| 102 |
"""https://github.com/BrianPugh/cyclopts/issues/508""" |
| 103 |
|
| 104 |
@app.command(name=["fizz", "buzz"], alias="bar") |
| 105 |
def foo(a): |
| 106 |
pass |
| 107 |
|
| 108 |
assert_parse_args(foo, "fizz 5", "5") |
| 109 |
assert_parse_args(foo, "buzz 5", "5") |
| 110 |
assert_parse_args(foo, "bar 5", "5") |