| 1 |
from typing import Annotated |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from cyclopts import Argument, Group, Parameter, Token |
| 6 |
from cyclopts.argument import ArgumentCollection |
| 7 |
from cyclopts.exceptions import ValidationError |
| 8 |
from cyclopts.validators import LimitedChoice, all_or_none |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture |
| 12 |
def argument_collection_0(): |
| 13 |
return ArgumentCollection( |
| 14 |
[ |
| 15 |
Argument(parameter=Parameter(name="--foo")), |
| 16 |
Argument(parameter=Parameter(name="--bar")), |
| 17 |
Argument(parameter=Parameter(name="--baz")), |
| 18 |
] |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
@pytest.fixture |
| 23 |
def argument_collection_1(): |
| 24 |
return ArgumentCollection( |
| 25 |
[ |
| 26 |
Argument( |
| 27 |
tokens=[Token(keyword="--foo", value="100", source="test")], |
| 28 |
parameter=Parameter(name="--foo"), |
| 29 |
value=100, |
| 30 |
), |
| 31 |
Argument(parameter=Parameter(name="--bar")), |
| 32 |
Argument(parameter=Parameter(name="--baz")), |
| 33 |
] |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
@pytest.fixture |
| 38 |
def argument_collection_2(): |
| 39 |
return ArgumentCollection( |
| 40 |
[ |
| 41 |
Argument( |
| 42 |
tokens=[Token(keyword="--foo", value="100", source="test")], |
| 43 |
parameter=Parameter(name="--foo"), |
| 44 |
value=100, |
| 45 |
), |
| 46 |
Argument( |
| 47 |
tokens=[Token(keyword="--bar", value="200", source="test")], |
| 48 |
parameter=Parameter(name="--bar"), |
| 49 |
value=200, |
| 50 |
), |
| 51 |
Argument(parameter=Parameter(name="--baz")), |
| 52 |
] |
| 53 |
) |
| 54 |
|
| 55 |
|
| 56 |
@pytest.fixture |
| 57 |
def argument_collection_3(): |
| 58 |
return ArgumentCollection( |
| 59 |
[ |
| 60 |
Argument( |
| 61 |
tokens=[Token(keyword="--foo", value="100", source="test")], |
| 62 |
parameter=Parameter(name="--foo"), |
| 63 |
value=100, |
| 64 |
), |
| 65 |
Argument( |
| 66 |
tokens=[Token(keyword="--bar", value="200", source="test")], |
| 67 |
parameter=Parameter(name="--bar"), |
| 68 |
value=200, |
| 69 |
), |
| 70 |
Argument( |
| 71 |
tokens=[Token(keyword="--baz", value="300", source="test")], |
| 72 |
parameter=Parameter(name="--baz"), |
| 73 |
value=300, |
| 74 |
), |
| 75 |
] |
| 76 |
) |
| 77 |
|
| 78 |
|
| 79 |
def test_limited_choice_default_success(argument_collection_0, argument_collection_1, argument_collection_2): |
| 80 |
validator = LimitedChoice() |
| 81 |
validator(argument_collection_0) |
| 82 |
validator(argument_collection_1) |
| 83 |
|
| 84 |
validator = LimitedChoice(2) |
| 85 |
validator(argument_collection_2) |
| 86 |
with pytest.raises(ValueError): |
| 87 |
validator(argument_collection_0) |
| 88 |
|
| 89 |
validator = LimitedChoice(2, allow_none=True) |
| 90 |
validator(argument_collection_0) |
| 91 |
|
| 92 |
|
| 93 |
def test_limited_choice_failure_1(argument_collection_2): |
| 94 |
validator = LimitedChoice() |
| 95 |
with pytest.raises(ValueError): |
| 96 |
|
| 97 |
validator(argument_collection_2) |
| 98 |
|
| 99 |
|
| 100 |
def test_limited_choice_failure_2(argument_collection_2, argument_collection_3): |
| 101 |
validator = LimitedChoice(2) |
| 102 |
validator(argument_collection_2) |
| 103 |
with pytest.raises(ValueError): |
| 104 |
validator(argument_collection_3) |
| 105 |
|
| 106 |
|
| 107 |
def test_limited_choice_default_min_max( |
| 108 |
argument_collection_0, argument_collection_1, argument_collection_2, argument_collection_3 |
| 109 |
): |
| 110 |
validator = LimitedChoice(1, 2) |
| 111 |
with pytest.raises(ValueError): |
| 112 |
validator(argument_collection_0) |
| 113 |
validator(argument_collection_1) |
| 114 |
validator(argument_collection_2) |
| 115 |
with pytest.raises(ValueError): |
| 116 |
validator(argument_collection_3) |
| 117 |
|
| 118 |
|
| 119 |
def test_limited_choice_invalid_min_max(): |
| 120 |
"""Minimum value must be less than maximum value.""" |
| 121 |
with pytest.raises(ValueError): |
| 122 |
LimitedChoice(2, 1) |
| 123 |
|
| 124 |
|
| 125 |
def test_limited_choice_all_or_none(argument_collection_0, argument_collection_1, argument_collection_3): |
| 126 |
"""All arguments in the group must be supplied.""" |
| 127 |
all_or_none(argument_collection_0) |
| 128 |
all_or_none(argument_collection_3) |
| 129 |
|
| 130 |
with pytest.raises(ValueError): |
| 131 |
all_or_none(argument_collection_1) |
| 132 |
|
| 133 |
|
| 134 |
def test_bind_group_validator_limited_choice(app): |
| 135 |
@app.command |
| 136 |
def foo( |
| 137 |
*, |
| 138 |
car: Annotated[bool, Parameter(group=Group("Vehicle", validator=LimitedChoice()))] = False, |
| 139 |
motorcycle: Annotated[bool, Parameter(group="Vehicle")] = False, |
| 140 |
): |
| 141 |
pass |
| 142 |
|
| 143 |
with pytest.raises(ValidationError) as e: |
| 144 |
app("foo --car --motorcycle", exit_on_error=False) |
| 145 |
|
| 146 |
assert str(e.value) == "Invalid values for group Vehicle. Mutually exclusive arguments: {--car, --motorcycle}" |
| 147 |
|
| 148 |
app("foo") |
| 149 |
app("foo --car") |
| 150 |
app("foo --motorcycle") |
| 151 |
|
| 152 |
|
| 153 |
def test_bind_group_validator_limited_choice_name_override(app): |
| 154 |
@app.command |
| 155 |
def foo( |
| 156 |
*, |
| 157 |
car: Annotated[bool, Parameter(group=Group("Vehicle", validator=LimitedChoice()))] = False, |
| 158 |
motorcycle: Annotated[bool, Parameter(name="--bike", group="Vehicle")] = False, |
| 159 |
): |
| 160 |
pass |
| 161 |
|
| 162 |
with pytest.raises(ValidationError) as e: |
| 163 |
app("foo --car --bike", exit_on_error=False) |
| 164 |
|
| 165 |
assert str(e.value) == "Invalid values for group Vehicle. Mutually exclusive arguments: {--car, --bike}" |
| 166 |
|
| 167 |
|
| 168 |
def test_group_validator_complete_argument_collection(app, mocker): |
| 169 |
custom_validator = mocker.MagicMock() |
| 170 |
group = Group("Vehicle", validator=(custom_validator,)) |
| 171 |
|
| 172 |
@app.default |
| 173 |
def default( |
| 174 |
*, |
| 175 |
car: Annotated[bool, Parameter(group=group)] = False, |
| 176 |
motorcycle: Annotated[bool, Parameter(group=group)] = False, |
| 177 |
): |
| 178 |
pass |
| 179 |
|
| 180 |
app([]) |
| 181 |
|
| 182 |
custom_validator.assert_called_once() |
| 183 |
argument_collection_names = [x.name for x in custom_validator.call_args_list[0][0][0]] |
| 184 |
assert argument_collection_names == ["--car", "--motorcycle"] |
| 185 |
|
| 186 |
|
| 187 |
def test_limited_choice_negative_flag_error_message(app): |
| 188 |
"""Test for Issue #631: Error message should show the actual flag the user typed. |
| 189 |
|
| 190 |
When a mutually exclusive validation error occurs, the error message should display |
| 191 |
the exact flags the user provided, not alternative names for the same parameter. |
| 192 |
|
| 193 |
This tests that negative flags (--neg) appear in error messages when used, |
| 194 |
rather than showing the positive flag name (--affirm). |
| 195 |
""" |
| 196 |
group = Group(show=False, validator=LimitedChoice()) |
| 197 |
|
| 198 |
@app.default |
| 199 |
def cmd( |
| 200 |
*, |
| 201 |
param: Annotated[bool, Parameter(name=("--affirm", "-a"), negative=("--neg", "-n"), group=group)] = False, |
| 202 |
opt: Annotated[int, Parameter(group=group)] = 0, |
| 203 |
): |
| 204 |
pass |
| 205 |
|
| 206 |
|
| 207 |
with pytest.raises(ValidationError) as exc_info: |
| 208 |
app("--neg --opt 10", exit_on_error=False) |
| 209 |
|
| 210 |
error_message = str(exc_info.value) |
| 211 |
assert "--neg" in error_message |
| 212 |
assert "--opt" in error_message |
| 213 |
assert "--affirm" not in error_message |
| 214 |
|
| 215 |
|
| 216 |
with pytest.raises(ValidationError) as exc_info: |
| 217 |
app("--affirm --opt 10", exit_on_error=False) |
| 218 |
|
| 219 |
error_message = str(exc_info.value) |
| 220 |
assert "--affirm" in error_message |
| 221 |
assert "--opt" in error_message |
| 222 |
assert "--neg" not in error_message |
| 223 |
|
| 224 |
|
| 225 |
with pytest.raises(ValidationError) as exc_info: |
| 226 |
app("-n --opt 10", exit_on_error=False) |
| 227 |
|
| 228 |
error_message = str(exc_info.value) |
| 229 |
assert "-n" in error_message |
| 230 |
assert "--opt" in error_message |
| 231 |
|
| 232 |
|
| 233 |
with pytest.raises(ValidationError) as exc_info: |
| 234 |
app("-a --opt 10", exit_on_error=False) |
| 235 |
|
| 236 |
error_message = str(exc_info.value) |
| 237 |
assert "-a" in error_message |
| 238 |
assert "--opt" in error_message |