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