| 1 |
"""Tests for subapp flattening feature (name="*").""" |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from cyclopts import App |
| 6 |
|
| 7 |
|
| 8 |
def test_flatten_basic(app): |
| 9 |
"""Test basic flattening of subapp commands.""" |
| 10 |
subapp = App(name="sub") |
| 11 |
|
| 12 |
@subapp.command |
| 13 |
def foo(): |
| 14 |
return "foo" |
| 15 |
|
| 16 |
@subapp.command |
| 17 |
def bar(): |
| 18 |
return "bar" |
| 19 |
|
| 20 |
app.command(subapp, name="*") |
| 21 |
|
| 22 |
assert app("foo") == "foo" |
| 23 |
assert app("bar") == "bar" |
| 24 |
|
| 25 |
|
| 26 |
def test_flatten_dynamic_registration(app): |
| 27 |
"""Test that commands added to subapp after flattening are still accessible.""" |
| 28 |
subapp = App(name="sub") |
| 29 |
|
| 30 |
@subapp.command |
| 31 |
def foo(): |
| 32 |
return "foo" |
| 33 |
|
| 34 |
app.command(subapp, name="*") |
| 35 |
|
| 36 |
@subapp.command |
| 37 |
def bar(): |
| 38 |
return "bar" |
| 39 |
|
| 40 |
assert app("foo") == "foo" |
| 41 |
assert app("bar") == "bar" |
| 42 |
|
| 43 |
|
| 44 |
def test_flatten_parent_precedence(app): |
| 45 |
"""Test that parent commands take precedence over flattened commands.""" |
| 46 |
|
| 47 |
@app.command |
| 48 |
def foo(): |
| 49 |
return "parent_foo" |
| 50 |
|
| 51 |
subapp = App(name="sub") |
| 52 |
|
| 53 |
@subapp.command |
| 54 |
def foo(): |
| 55 |
return "subapp_foo" |
| 56 |
|
| 57 |
@subapp.command |
| 58 |
def bar(): |
| 59 |
return "bar" |
| 60 |
|
| 61 |
app.command(subapp, name="*") |
| 62 |
|
| 63 |
assert app("foo") == "parent_foo" |
| 64 |
assert app("bar") == "bar" |
| 65 |
|
| 66 |
|
| 67 |
def test_flatten_multiple_subapps(app): |
| 68 |
"""Test flattening multiple subapps with collision handling.""" |
| 69 |
subapp1 = App(name="sub1") |
| 70 |
|
| 71 |
@subapp1.command |
| 72 |
def foo(): |
| 73 |
return "foo1" |
| 74 |
|
| 75 |
@subapp1.command |
| 76 |
def bar(): |
| 77 |
return "bar1" |
| 78 |
|
| 79 |
subapp2 = App(name="sub2") |
| 80 |
|
| 81 |
@subapp2.command |
| 82 |
def foo(): |
| 83 |
return "foo2" |
| 84 |
|
| 85 |
@subapp2.command |
| 86 |
def baz(): |
| 87 |
return "baz2" |
| 88 |
|
| 89 |
app.command(subapp1, name="*") |
| 90 |
app.command(subapp2, name="*") |
| 91 |
|
| 92 |
|
| 93 |
assert app("foo") == "foo1" |
| 94 |
assert app("bar") == "bar1" |
| 95 |
assert app("baz") == "baz2" |
| 96 |
|
| 97 |
|
| 98 |
@pytest.mark.parametrize( |
| 99 |
"method,expected", |
| 100 |
[ |
| 101 |
("contains_foo", True), |
| 102 |
("contains_bar", False), |
| 103 |
("iter_has_foo", True), |
| 104 |
("iter_no_duplicates", True), |
| 105 |
("getitem_works", True), |
| 106 |
("resolved_commands", True), |
| 107 |
("registered_commands", True), |
| 108 |
], |
| 109 |
) |
| 110 |
def test_flatten_dict_interface(app, method, expected): |
| 111 |
"""Test that flattened commands work with dict-like interfaces.""" |
| 112 |
subapp = App(name="sub") |
| 113 |
|
| 114 |
@subapp.command |
| 115 |
def foo(): |
| 116 |
return "foo" |
| 117 |
|
| 118 |
app.command(subapp, name="*") |
| 119 |
|
| 120 |
if method == "contains_foo": |
| 121 |
assert ("foo" in app) == expected |
| 122 |
elif method == "contains_bar": |
| 123 |
assert ("bar" in app) == expected |
| 124 |
elif method == "iter_has_foo": |
| 125 |
assert ("foo" in list(app)) == expected |
| 126 |
elif method == "iter_no_duplicates": |
| 127 |
commands = list(app) |
| 128 |
assert commands.count("foo") == 1 |
| 129 |
elif method == "getitem_works": |
| 130 |
foo_app = app["foo"] |
| 131 |
assert foo_app.default_command() == "foo" |
| 132 |
elif method == "resolved_commands": |
| 133 |
assert ("foo" in app.resolved_commands()) == expected |
| 134 |
elif method == "registered_commands": |
| 135 |
registered = app._registered_commands |
| 136 |
assert ("foo" in registered) == expected |
| 137 |
assert "--help" not in registered |
| 138 |
|
| 139 |
|
| 140 |
@pytest.mark.parametrize( |
| 141 |
"obj,error_match", |
| 142 |
[ |
| 143 |
(lambda: "foo", "only supported for App instances"), |
| 144 |
("some.module:function", "only supported for App instances"), |
| 145 |
], |
| 146 |
) |
| 147 |
def test_flatten_error_non_app(app, obj, error_match): |
| 148 |
"""Test that flattening only works with App instances.""" |
| 149 |
with pytest.raises(TypeError, match=error_match): |
| 150 |
app.command(obj, name="*") |
| 151 |
|
| 152 |
|
| 153 |
def test_flatten_error_with_kwargs(app): |
| 154 |
"""Test that flattening with kwargs raises error.""" |
| 155 |
subapp = App(name="sub") |
| 156 |
|
| 157 |
with pytest.raises(ValueError, match="Cannot supply additional configuration"): |
| 158 |
app.command(subapp, name="*", help_flags=[]) |
| 159 |
|
| 160 |
|
| 161 |
def test_flatten_nested_subapp(app): |
| 162 |
"""Test flattening only affects direct commands, not nested subapps.""" |
| 163 |
subapp2 = App(name="sub2") |
| 164 |
|
| 165 |
@subapp2.command |
| 166 |
def deeply_nested(): |
| 167 |
return "deep" |
| 168 |
|
| 169 |
subapp1 = App(name="sub1") |
| 170 |
subapp1.command(subapp2, name="nested") |
| 171 |
|
| 172 |
@subapp1.command |
| 173 |
def shallow(): |
| 174 |
return "shallow" |
| 175 |
|
| 176 |
app.command(subapp1, name="*") |
| 177 |
|
| 178 |
assert app("shallow") == "shallow" |
| 179 |
assert app("nested deeply-nested") == "deep" |
| 180 |
|
| 181 |
|
| 182 |
def test_flatten_recursive(app): |
| 183 |
"""Test that flatten works recursively through multiple levels.""" |
| 184 |
subapp1 = App(name="sub1") |
| 185 |
|
| 186 |
@subapp1.command |
| 187 |
def foo(): |
| 188 |
return "foo" |
| 189 |
|
| 190 |
subapp2 = App(name="sub2") |
| 191 |
|
| 192 |
@subapp2.command |
| 193 |
def bar(): |
| 194 |
return "bar" |
| 195 |
|
| 196 |
subapp1.command(subapp2, name="*") |
| 197 |
app.command(subapp1, name="*") |
| 198 |
|
| 199 |
assert app("foo") == "foo" |
| 200 |
assert app("bar") == "bar" |
| 201 |
|
| 202 |
|
| 203 |
def test_flatten_with_arguments(app): |
| 204 |
"""Test flattening works with commands that have arguments.""" |
| 205 |
subapp = App(name="sub") |
| 206 |
|
| 207 |
@subapp.command |
| 208 |
def foo(x: int, y: str): |
| 209 |
return f"{x}-{y}" |
| 210 |
|
| 211 |
app.command(subapp, name="*") |
| 212 |
|
| 213 |
assert app("foo 42 hello") == "42-hello" |