| 1 |
from dataclasses import dataclass |
| 2 |
from pathlib import Path |
| 3 |
from typing import Annotated, Any |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from cyclopts.argument import Token |
| 8 |
from cyclopts.config._common import ConfigFromFile |
| 9 |
from cyclopts.exceptions import CycloptsError |
| 10 |
from cyclopts.parameter import Parameter |
| 11 |
|
| 12 |
|
| 13 |
class DummyErrorConfigNoMsg(ConfigFromFile): |
| 14 |
def _load_config(self, path: Path) -> dict[str, Any]: |
| 15 |
raise ValueError |
| 16 |
|
| 17 |
|
| 18 |
class DummyErrorConfigMsg(ConfigFromFile): |
| 19 |
def _load_config(self, path: Path) -> dict[str, Any]: |
| 20 |
raise ValueError("My exception's message.") |
| 21 |
|
| 22 |
|
| 23 |
class Dummy(ConfigFromFile): |
| 24 |
def _load_config(self, path: Path) -> dict[str, Any]: |
| 25 |
return { |
| 26 |
"key1": "foo1", |
| 27 |
"key2": "foo2", |
| 28 |
"function1": { |
| 29 |
"key1": "bar1", |
| 30 |
"key2": "bar2", |
| 31 |
}, |
| 32 |
"meta_param": 123, |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
class DummyRootKeys(ConfigFromFile): |
| 37 |
def _load_config(self, path: Path) -> dict[str, Any]: |
| 38 |
return { |
| 39 |
"tool": { |
| 40 |
"cyclopts": { |
| 41 |
"key1": "foo1", |
| 42 |
"key2": "foo2", |
| 43 |
"function1": { |
| 44 |
"key1": "bar1", |
| 45 |
"key2": "bar2", |
| 46 |
}, |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
class DummySubKeys(ConfigFromFile): |
| 53 |
def _load_config(self, path: Path) -> dict[str, Any]: |
| 54 |
return { |
| 55 |
"key1": { |
| 56 |
"subkey1": ["subkey1val1", "subkey1val2"], |
| 57 |
"subkey2": ["subkey2val1", "subkey2val2"], |
| 58 |
}, |
| 59 |
"key2": "foo2", |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
def function1(key1, key2): |
| 64 |
pass |
| 65 |
|
| 66 |
|
| 67 |
@pytest.fixture |
| 68 |
def config(tmp_path): |
| 69 |
return Dummy(tmp_path / "cyclopts-config-test-file.dummy") |
| 70 |
|
| 71 |
|
| 72 |
@pytest.fixture |
| 73 |
def config_root_keys(tmp_path): |
| 74 |
return DummyRootKeys(tmp_path / "cyclopts-config-test-file.dummy") |
| 75 |
|
| 76 |
|
| 77 |
@pytest.fixture |
| 78 |
def config_sub_keys(tmp_path): |
| 79 |
return DummySubKeys(tmp_path / "cyclopts-config-test-file.dummy") |
| 80 |
|
| 81 |
|
| 82 |
@pytest.fixture |
| 83 |
def configured_app(app): |
| 84 |
@app.command |
| 85 |
def function1(): |
| 86 |
pass |
| 87 |
|
| 88 |
@app.default |
| 89 |
def foo(key1, key2): |
| 90 |
pass |
| 91 |
|
| 92 |
@app.meta.default |
| 93 |
def meta( |
| 94 |
*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)], |
| 95 |
meta_param: Annotated[int, Parameter(negative=())] = 42, |
| 96 |
): |
| 97 |
pass |
| 98 |
|
| 99 |
return app |
| 100 |
|
| 101 |
|
| 102 |
def test_config_common_root_keys_empty(configured_app, config): |
| 103 |
config.path.touch() |
| 104 |
configured_app.config = config |
| 105 |
_, _, _, _, argument_collection = configured_app._parse_known_args("--key1 cli1") |
| 106 |
assert len(argument_collection) == 2 |
| 107 |
assert argument_collection[0].tokens == [Token(keyword="--key1", value="cli1", source="cli")] |
| 108 |
assert argument_collection[1].tokens == [Token(keyword="[key2]", value="foo2", source=str(config.path))] |
| 109 |
|
| 110 |
|
| 111 |
def test_config_common_root_keys_populated(configured_app, config_root_keys): |
| 112 |
configured_app.config = config_root_keys |
| 113 |
config_root_keys.path.touch() |
| 114 |
config_root_keys.root_keys = ["tool", "cyclopts"] |
| 115 |
_, _, _, _, argument_collection = configured_app._parse_known_args("--key1 cli1") |
| 116 |
|
| 117 |
assert len(argument_collection) == 2 |
| 118 |
assert argument_collection[0].tokens == [Token(keyword="--key1", value="cli1", source="cli")] |
| 119 |
assert argument_collection[1].tokens == [ |
| 120 |
Token(keyword="[tool][cyclopts][key2]", value="foo2", source=str(config_root_keys.path)) |
| 121 |
] |
| 122 |
|
| 123 |
|
| 124 |
def test_config_common_must_exist_false(config, mocker): |
| 125 |
"""If ``must_exist==False``, then the specified file is allowed to not exist. |
| 126 |
|
| 127 |
If the file does not exist, then have an empty config. |
| 128 |
""" |
| 129 |
spy_load_config = mocker.spy(config, "_load_config") |
| 130 |
config.must_exist = False |
| 131 |
_ = config.config |
| 132 |
assert config.config == {} |
| 133 |
spy_load_config.assert_not_called() |
| 134 |
|
| 135 |
|
| 136 |
def test_config_common_must_exist_true(config): |
| 137 |
"""If ``must_exist==True``, then the specified file must exist.""" |
| 138 |
config.must_exist = True |
| 139 |
with pytest.raises(FileNotFoundError): |
| 140 |
_ = config.config |
| 141 |
|
| 142 |
|
| 143 |
@pytest.mark.parametrize("must_exist", [True, False]) |
| 144 |
def test_config_common_search_parents_absolute_true_exists(tmp_path, must_exist, config, mocker): |
| 145 |
"""Tests finding an existing parent if path is absolute.""" |
| 146 |
spy_load_config = mocker.spy(config, "_load_config") |
| 147 |
|
| 148 |
original_path = config.path |
| 149 |
original_path.touch() |
| 150 |
config.path = tmp_path / "folder1" / "folder2" / "folder3" / "folder4" / config.path.name |
| 151 |
config.must_exist = must_exist |
| 152 |
config.search_parents = True |
| 153 |
|
| 154 |
_ = config.config |
| 155 |
|
| 156 |
spy_load_config.assert_called_once_with(original_path) |
| 157 |
|
| 158 |
|
| 159 |
def test_config_common_search_parents_relative_true_exists(tmp_path, mocker, monkeypatch): |
| 160 |
"""Tests finding an existing parent if path is relative.""" |
| 161 |
config_path = tmp_path / "cyclopts-config-test-file.dummy" |
| 162 |
config_path.touch() |
| 163 |
config = Dummy("cyclopts-config-test-file.dummy", search_parents=True) |
| 164 |
spy_load_config = mocker.spy(config, "_load_config") |
| 165 |
|
| 166 |
deep_dir = tmp_path / "foo" / "bar" / "baz" |
| 167 |
deep_dir.mkdir(parents=True) |
| 168 |
monkeypatch.chdir(deep_dir) |
| 169 |
|
| 170 |
_ = config.config |
| 171 |
|
| 172 |
spy_load_config.assert_called_once_with(config_path.resolve()) |
| 173 |
|
| 174 |
|
| 175 |
def test_config_common_must_exist_true_search_parents_true_missing(tmp_path, config, mocker): |
| 176 |
"""Tests finding a missing parent.""" |
| 177 |
spy_load_config = mocker.spy(config, "_load_config") |
| 178 |
|
| 179 |
config.path = tmp_path / "folder1" / "folder2" / "folder3" / "folder4" / config.path.name |
| 180 |
config.must_exist = True |
| 181 |
config.search_parents = True |
| 182 |
|
| 183 |
with pytest.raises(FileNotFoundError): |
| 184 |
_ = config.config |
| 185 |
|
| 186 |
spy_load_config.assert_not_called() |
| 187 |
|
| 188 |
|
| 189 |
def test_config_common_must_exist_false_search_parents_true_missing(tmp_path, config, mocker): |
| 190 |
"""Tests finding a missing parent.""" |
| 191 |
spy_load_config = mocker.spy(config, "_load_config") |
| 192 |
|
| 193 |
config.path = tmp_path / "folder1" / "folder2" / "folder3" / "folder4" / config.path.name |
| 194 |
config.must_exist = False |
| 195 |
config.search_parents = True |
| 196 |
|
| 197 |
assert config.config == {} |
| 198 |
|
| 199 |
spy_load_config.assert_not_called() |
| 200 |
|
| 201 |
|
| 202 |
def test_config_common_kwargs(app, config): |
| 203 |
"""Make sure that we don't look for the string "kwargs" as a key.""" |
| 204 |
app.config = config |
| 205 |
config.path.touch() |
| 206 |
|
| 207 |
@app.default |
| 208 |
def foo(key1, **kwargs): |
| 209 |
pass |
| 210 |
|
| 211 |
|
| 212 |
@app.command |
| 213 |
def function1(): |
| 214 |
pass |
| 215 |
|
| 216 |
@app.meta.default |
| 217 |
def meta( |
| 218 |
*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)], |
| 219 |
meta_param: Annotated[int, Parameter(negative=())] = 42, |
| 220 |
): |
| 221 |
pass |
| 222 |
|
| 223 |
_, _, _, _, argument_collection = app._parse_known_args("--key1 foo1") |
| 224 |
|
| 225 |
|
| 226 |
assert len(argument_collection) == 2 |
| 227 |
assert argument_collection[-1].tokens == [ |
| 228 |
Token(keyword="[key2]", value="foo2", source=str(config.path.absolute()), index=0, keys=("key2",)), |
| 229 |
] |
| 230 |
|
| 231 |
|
| 232 |
def test_config_common_subkeys(app, config_sub_keys): |
| 233 |
config_sub_keys.path.touch() |
| 234 |
app.config = config_sub_keys |
| 235 |
|
| 236 |
@dataclass |
| 237 |
class Example: |
| 238 |
subkey1: list[str] |
| 239 |
subkey2: list[str] |
| 240 |
|
| 241 |
@app.default |
| 242 |
def foo(key1: Example, key2): |
| 243 |
pass |
| 244 |
|
| 245 |
_, _, _, _, argument_collection = app._parse_known_args("") |
| 246 |
|
| 247 |
assert len(argument_collection) == 4 |
| 248 |
|
| 249 |
assert len(argument_collection[0].tokens) == 0 |
| 250 |
|
| 251 |
assert len(argument_collection[1].tokens) == 2 |
| 252 |
assert argument_collection[1].tokens[0].keyword == "[key1][subkey1]" |
| 253 |
assert argument_collection[1].tokens[0].value == "subkey1val1" |
| 254 |
assert argument_collection[1].tokens[0].index == 0 |
| 255 |
assert argument_collection[1].tokens[0].keys == () |
| 256 |
assert argument_collection[1].tokens[0].source.endswith("cyclopts-config-test-file.dummy") |
| 257 |
assert argument_collection[1].tokens[1].keyword == "[key1][subkey1]" |
| 258 |
assert argument_collection[1].tokens[1].value == "subkey1val2" |
| 259 |
assert argument_collection[1].tokens[1].index == 1 |
| 260 |
assert argument_collection[1].tokens[1].keys == () |
| 261 |
assert argument_collection[1].tokens[1].source.endswith("cyclopts-config-test-file.dummy") |
| 262 |
|
| 263 |
assert len(argument_collection[2].tokens) == 2 |
| 264 |
assert argument_collection[2].tokens[0].keyword == "[key1][subkey2]" |
| 265 |
assert argument_collection[2].tokens[0].value == "subkey2val1" |
| 266 |
assert argument_collection[2].tokens[0].index == 0 |
| 267 |
assert argument_collection[2].tokens[0].keys == () |
| 268 |
assert argument_collection[2].tokens[0].source.endswith("cyclopts-config-test-file.dummy") |
| 269 |
assert argument_collection[2].tokens[1].keyword == "[key1][subkey2]" |
| 270 |
assert argument_collection[2].tokens[1].value == "subkey2val2" |
| 271 |
assert argument_collection[2].tokens[1].index == 1 |
| 272 |
assert argument_collection[2].tokens[1].keys == () |
| 273 |
assert argument_collection[2].tokens[1].source.endswith("cyclopts-config-test-file.dummy") |
| 274 |
|
| 275 |
assert len(argument_collection[3].tokens) == 1 |
| 276 |
assert argument_collection[3].tokens[0].keyword == "[key2]" |
| 277 |
assert argument_collection[3].tokens[0].value == "foo2" |
| 278 |
assert argument_collection[3].tokens[0].index == 0 |
| 279 |
assert argument_collection[3].tokens[0].keys == () |
| 280 |
assert argument_collection[3].tokens[0].source.endswith("cyclopts-config-test-file.dummy") |
| 281 |
|
| 282 |
|
| 283 |
def test_config_exception_during_load_config_no_msg(tmp_path): |
| 284 |
path = tmp_path / "config" |
| 285 |
path.touch() |
| 286 |
dummy_error_config = DummyErrorConfigNoMsg(path) |
| 287 |
with pytest.raises(CycloptsError) as e: |
| 288 |
_ = dummy_error_config.config |
| 289 |
assert str(e.value) == "ValueError" |
| 290 |
|
| 291 |
|
| 292 |
def test_config_exception_during_load_config_msg(tmp_path): |
| 293 |
path = tmp_path / "config" |
| 294 |
path.touch() |
| 295 |
dummy_error_config = DummyErrorConfigMsg(path) |
| 296 |
with pytest.raises(CycloptsError) as e: |
| 297 |
_ = dummy_error_config.config |
| 298 |
assert str(e.value) == "ValueError: My exception's message." |