| 1 |
import textwrap |
| 2 |
from io import StringIO |
| 3 |
from typing import Annotated |
| 4 |
|
| 5 |
import pytest |
| 6 |
from rich.console import Console |
| 7 |
|
| 8 |
from cyclopts import CycloptsPanel, Parameter |
| 9 |
from cyclopts.exceptions import ConsumeMultipleError, MissingArgumentError |
| 10 |
|
| 11 |
|
| 12 |
@pytest.mark.parametrize( |
| 13 |
"cmd_str,expected", |
| 14 |
[ |
| 15 |
("", None), |
| 16 |
("--empty-my-list", []), |
| 17 |
("--empty-my-list=True", []), |
| 18 |
("--empty-my-list=False", None), |
| 19 |
], |
| 20 |
) |
| 21 |
def test_optional_list_empty_flag_default(app, cmd_str, expected, assert_parse_args): |
| 22 |
@app.default |
| 23 |
def foo(my_list: list[int] | None = None): |
| 24 |
pass |
| 25 |
|
| 26 |
if expected is None: |
| 27 |
assert_parse_args(foo, cmd_str) |
| 28 |
else: |
| 29 |
assert_parse_args(foo, cmd_str, expected) |
| 30 |
|
| 31 |
|
| 32 |
@pytest.mark.parametrize( |
| 33 |
"cmd_str,expected", |
| 34 |
[ |
| 35 |
("", None), |
| 36 |
("--empty-my-set", set()), |
| 37 |
("--empty-my-set=True", set()), |
| 38 |
("--empty-my-set=False", None), |
| 39 |
], |
| 40 |
) |
| 41 |
def test_optional_set_empty_flag_default(app, cmd_str, expected, assert_parse_args): |
| 42 |
@app.default |
| 43 |
def foo(my_set: set[int] | None = None): |
| 44 |
pass |
| 45 |
|
| 46 |
if expected is None: |
| 47 |
assert_parse_args(foo, cmd_str) |
| 48 |
else: |
| 49 |
assert_parse_args(foo, cmd_str, expected) |
| 50 |
|
| 51 |
|
| 52 |
@pytest.mark.parametrize( |
| 53 |
"cmd_str,expected", |
| 54 |
[ |
| 55 |
("", None), |
| 56 |
("--empty-my-list", []), |
| 57 |
("--my-list", []), |
| 58 |
("--my-list http://example.com", ["http://example.com"]), |
| 59 |
("--my-list http://example.com http://example2.com", ["http://example.com", "http://example2.com"]), |
| 60 |
], |
| 61 |
) |
| 62 |
def test_optional_list_consume_multiple(app, cmd_str, expected, assert_parse_args): |
| 63 |
"""Test that --my-list with no values behaves like --empty-my-list when consume_multiple=True.""" |
| 64 |
|
| 65 |
@app.default |
| 66 |
def foo(my_list: Annotated[list[str] | None, Parameter(consume_multiple=True)] = None): |
| 67 |
pass |
| 68 |
|
| 69 |
if expected is None: |
| 70 |
assert_parse_args(foo, cmd_str) |
| 71 |
else: |
| 72 |
assert_parse_args(foo, cmd_str, expected) |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
def test_consume_multiple_int_rejects_empty(app): |
| 79 |
"""consume_multiple=1 should reject --option with no values.""" |
| 80 |
|
| 81 |
@app.default |
| 82 |
def foo(*, my_list: Annotated[list[str] | None, Parameter(consume_multiple=1)] = None): |
| 83 |
pass |
| 84 |
|
| 85 |
with pytest.raises(ConsumeMultipleError, match="requires at least 1 elements. Got 0"): |
| 86 |
app.parse_args("--my-list", print_error=False, exit_on_error=False) |
| 87 |
|
| 88 |
|
| 89 |
def test_consume_multiple_int_accepts_enough(app, assert_parse_args): |
| 90 |
"""consume_multiple=1 should accept --option with 1+ values.""" |
| 91 |
|
| 92 |
@app.default |
| 93 |
def foo(*, my_list: Annotated[list[str] | None, Parameter(consume_multiple=1)] = None): |
| 94 |
pass |
| 95 |
|
| 96 |
assert_parse_args(foo, "--my-list a", my_list=["a"]) |
| 97 |
assert_parse_args(foo, "--my-list a b c", my_list=["a", "b", "c"]) |
| 98 |
|
| 99 |
|
| 100 |
def test_consume_multiple_int_min_2(app): |
| 101 |
"""consume_multiple=2 should reject --option with fewer than 2 values.""" |
| 102 |
|
| 103 |
@app.default |
| 104 |
def foo(*, my_list: Annotated[list[str] | None, Parameter(consume_multiple=2)] = None): |
| 105 |
pass |
| 106 |
|
| 107 |
with pytest.raises(ConsumeMultipleError, match="requires at least 2 elements. Got 1"): |
| 108 |
app.parse_args("--my-list a", print_error=False, exit_on_error=False) |
| 109 |
|
| 110 |
|
| 111 |
def test_consume_multiple_int_min_2_accepts(app, assert_parse_args): |
| 112 |
"""consume_multiple=2 should accept --option with 2+ values.""" |
| 113 |
|
| 114 |
@app.default |
| 115 |
def foo(*, my_list: Annotated[list[str] | None, Parameter(consume_multiple=2)] = None): |
| 116 |
pass |
| 117 |
|
| 118 |
assert_parse_args(foo, "--my-list a b", my_list=["a", "b"]) |
| 119 |
assert_parse_args(foo, "--my-list a b c", my_list=["a", "b", "c"]) |
| 120 |
|
| 121 |
|
| 122 |
def test_consume_multiple_0_allows_empty(app, assert_parse_args): |
| 123 |
"""consume_multiple=0 should behave like True (allow empty).""" |
| 124 |
|
| 125 |
@app.default |
| 126 |
def foo(*, my_list: Annotated[list[str] | None, Parameter(consume_multiple=0)] = None): |
| 127 |
pass |
| 128 |
|
| 129 |
assert_parse_args(foo, "--my-list", my_list=[]) |
| 130 |
assert_parse_args(foo, "--my-list a b", my_list=["a", "b"]) |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
@pytest.mark.parametrize( |
| 137 |
"cmd_str,expected", |
| 138 |
[ |
| 139 |
("--my-list", []), |
| 140 |
("--my-list a", ["a"]), |
| 141 |
("--my-list a b c", ["a", "b", "c"]), |
| 142 |
], |
| 143 |
) |
| 144 |
def test_consume_multiple_tuple_0_3(app, cmd_str, expected, assert_parse_args): |
| 145 |
"""consume_multiple=(0, 3) allows empty but stops after 3.""" |
| 146 |
|
| 147 |
@app.default |
| 148 |
def foo(*, my_list: Annotated[list[str] | None, Parameter(consume_multiple=(0, 3))] = None): |
| 149 |
pass |
| 150 |
|
| 151 |
assert_parse_args(foo, cmd_str, my_list=expected) |
| 152 |
|
| 153 |
|
| 154 |
def test_consume_multiple_tuple_0_3_max_enforced(app): |
| 155 |
"""consume_multiple=(0, 3) should reject more than 3 values.""" |
| 156 |
|
| 157 |
@app.default |
| 158 |
def foo(other: str, *, my_list: Annotated[list[str] | None, Parameter(consume_multiple=(0, 3))] = None): |
| 159 |
pass |
| 160 |
|
| 161 |
|
| 162 |
with pytest.raises(ConsumeMultipleError, match="accepts at most 3 elements. Got 4"): |
| 163 |
app.parse_args("--my-list a b c d", print_error=False, exit_on_error=False) |
| 164 |
|
| 165 |
|
| 166 |
def test_consume_multiple_tuple_min_max(app, assert_parse_args): |
| 167 |
"""consume_multiple=(2, 5) requires 2-5 values.""" |
| 168 |
|
| 169 |
@app.default |
| 170 |
def foo(*, my_list: Annotated[list[str] | None, Parameter(consume_multiple=(2, 5))] = None): |
| 171 |
pass |
| 172 |
|
| 173 |
assert_parse_args(foo, "--my-list a b", my_list=["a", "b"]) |
| 174 |
assert_parse_args(foo, "--my-list a b c d e", my_list=["a", "b", "c", "d", "e"]) |
| 175 |
|
| 176 |
|
| 177 |
def test_consume_multiple_tuple_min_not_met(app): |
| 178 |
"""consume_multiple=(2, 5) should reject fewer than 2 values.""" |
| 179 |
|
| 180 |
@app.default |
| 181 |
def foo(*, my_list: Annotated[list[str] | None, Parameter(consume_multiple=(2, 5))] = None): |
| 182 |
pass |
| 183 |
|
| 184 |
with pytest.raises(ConsumeMultipleError, match="requires at least 2 elements. Got 1"): |
| 185 |
app.parse_args("--my-list a", print_error=False, exit_on_error=False) |
| 186 |
|
| 187 |
with pytest.raises(ConsumeMultipleError, match="requires at least 2 elements. Got 0"): |
| 188 |
app.parse_args("--my-list", print_error=False, exit_on_error=False) |
| 189 |
|
| 190 |
|
| 191 |
def test_consume_multiple_error_is_missing_argument_error(): |
| 192 |
"""ConsumeMultipleError should be a subclass of MissingArgumentError for backward compat.""" |
| 193 |
assert issubclass(ConsumeMultipleError, MissingArgumentError) |
| 194 |
|
| 195 |
|
| 196 |
def test_consume_multiple_error_message_max(app): |
| 197 |
"""Exceeding max should produce an 'accepts at most' message.""" |
| 198 |
|
| 199 |
@app.default |
| 200 |
def foo(*, urls: Annotated[list[str] | None, Parameter(consume_multiple=(2, 5))] = None): |
| 201 |
pass |
| 202 |
|
| 203 |
with pytest.raises(ConsumeMultipleError, match="accepts at most 5 elements. Got 6"): |
| 204 |
app.parse_args("--urls a b c d e f", print_error=False, exit_on_error=False) |
| 205 |
|
| 206 |
|
| 207 |
def test_consume_multiple_error_panel_min(app): |
| 208 |
"""Full rich panel output for min constraint violation.""" |
| 209 |
|
| 210 |
@app.default |
| 211 |
def foo(*, urls: Annotated[list[str] | None, Parameter(consume_multiple=(2, 5))] = None): |
| 212 |
pass |
| 213 |
|
| 214 |
with pytest.raises(ConsumeMultipleError) as exc_info: |
| 215 |
app.parse_args("--urls a", print_error=False, exit_on_error=False) |
| 216 |
|
| 217 |
buf = StringIO() |
| 218 |
console = Console(file=buf, width=70, force_terminal=True, highlight=False, color_system=None, legacy_windows=False) |
| 219 |
console.print(CycloptsPanel(exc_info.value)) |
| 220 |
actual = buf.getvalue() |
| 221 |
|
| 222 |
expected = textwrap.dedent( |
| 223 |
"""\ |
| 224 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 225 |
│ Parameter --urls requires at least 2 elements. Got 1. │ |
| 226 |
╰────────────────────────────────────────────────────────────────────╯ |
| 227 |
""" |
| 228 |
) |
| 229 |
|
| 230 |
assert actual == expected |
| 231 |
|
| 232 |
|
| 233 |
def test_consume_multiple_error_panel_empty(app): |
| 234 |
"""Full rich panel output when zero values provided.""" |
| 235 |
|
| 236 |
@app.default |
| 237 |
def foo(*, urls: Annotated[list[str] | None, Parameter(consume_multiple=3)] = None): |
| 238 |
pass |
| 239 |
|
| 240 |
with pytest.raises(ConsumeMultipleError) as exc_info: |
| 241 |
app.parse_args("--urls", print_error=False, exit_on_error=False) |
| 242 |
|
| 243 |
buf = StringIO() |
| 244 |
console = Console(file=buf, width=70, force_terminal=True, highlight=False, color_system=None, legacy_windows=False) |
| 245 |
console.print(CycloptsPanel(exc_info.value)) |
| 246 |
actual = buf.getvalue() |
| 247 |
|
| 248 |
expected = textwrap.dedent( |
| 249 |
"""\ |
| 250 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 251 |
│ Parameter --urls requires at least 3 elements. Got 0. │ |
| 252 |
╰────────────────────────────────────────────────────────────────────╯ |
| 253 |
""" |
| 254 |
) |
| 255 |
|
| 256 |
assert actual == expected |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
def test_consume_multiple_negative_int_raises(): |
| 263 |
"""Negative int should raise ValueError.""" |
| 264 |
with pytest.raises(ValueError, match="non-negative"): |
| 265 |
Parameter(consume_multiple=-1) |
| 266 |
|
| 267 |
|
| 268 |
def test_consume_multiple_tuple_min_gt_max_raises(): |
| 269 |
"""Tuple with min > max should raise ValueError.""" |
| 270 |
with pytest.raises(ValueError, match="min must be <= max"): |
| 271 |
Parameter(consume_multiple=(5, 3)) |
| 272 |
|
| 273 |
|
| 274 |
def test_consume_multiple_tuple_negative_raises(): |
| 275 |
"""Tuple with negative values should raise ValueError.""" |
| 276 |
with pytest.raises(ValueError, match="non-negative"): |
| 277 |
Parameter(consume_multiple=(-1, 3)) |