toml-combine 0.6.0__tar.gz → 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- toml_combine-1.0.0/CONTRIBUTING.md +34 -0
- toml_combine-1.0.0/LICENSE +7 -0
- {toml_combine-0.6.0 → toml_combine-1.0.0}/PKG-INFO +9 -2
- {toml_combine-0.6.0 → toml_combine-1.0.0}/README.md +7 -1
- {toml_combine-0.6.0 → toml_combine-1.0.0}/tests/test_combiner.py +196 -194
- {toml_combine-0.6.0 → toml_combine-1.0.0}/toml_combine/combiner.py +3 -3
- {toml_combine-0.6.0 → toml_combine-1.0.0}/toml_combine/exceptions.py +1 -1
- {toml_combine-0.6.0 → toml_combine-1.0.0}/.github/PULL_REQUEST_TEMPLATE.md +0 -0
- {toml_combine-0.6.0 → toml_combine-1.0.0}/.github/renovate.json5 +0 -0
- {toml_combine-0.6.0 → toml_combine-1.0.0}/.github/workflows/ci.yml +0 -0
- {toml_combine-0.6.0 → toml_combine-1.0.0}/.pre-commit-config.yaml +0 -0
- {toml_combine-0.6.0 → toml_combine-1.0.0}/pyproject.toml +0 -0
- {toml_combine-0.6.0 → toml_combine-1.0.0}/tests/result.json +0 -0
- {toml_combine-0.6.0 → toml_combine-1.0.0}/tests/test.toml +0 -0
- {toml_combine-0.6.0 → toml_combine-1.0.0}/tests/test_cli.py +0 -0
- {toml_combine-0.6.0 → toml_combine-1.0.0}/tests/test_lib.py +0 -0
- {toml_combine-0.6.0 → toml_combine-1.0.0}/toml_combine/__init__.py +0 -0
- {toml_combine-0.6.0 → toml_combine-1.0.0}/toml_combine/__main__.py +0 -0
- {toml_combine-0.6.0 → toml_combine-1.0.0}/toml_combine/cli.py +0 -0
- {toml_combine-0.6.0 → toml_combine-1.0.0}/toml_combine/lib.py +0 -0
- {toml_combine-0.6.0 → toml_combine-1.0.0}/toml_combine/toml.py +0 -0
- {toml_combine-0.6.0 → toml_combine-1.0.0}/uv.lock +0 -0
@@ -0,0 +1,34 @@
|
|
1
|
+
## Development
|
2
|
+
|
3
|
+
This project uses [uv](https://docs.astral.sh/uv/).
|
4
|
+
|
5
|
+
If you don't have uv installed, install it with:
|
6
|
+
|
7
|
+
```console
|
8
|
+
$ curl -LsSf https://astral.sh/uv/install.sh | sh
|
9
|
+
```
|
10
|
+
|
11
|
+
or using a package manager, such as [brew](https://brew.sh/):
|
12
|
+
|
13
|
+
```console
|
14
|
+
$ brew install uv
|
15
|
+
```
|
16
|
+
|
17
|
+
Then you can directly launch the tests with:
|
18
|
+
|
19
|
+
```console
|
20
|
+
$ uv run pytest
|
21
|
+
```
|
22
|
+
|
23
|
+
or create a virtual environment to work on the package and activate it:
|
24
|
+
|
25
|
+
```console
|
26
|
+
$ uv sync
|
27
|
+
$ source .venv/bin/activate
|
28
|
+
```
|
29
|
+
|
30
|
+
or launch the command itself with uv (no need to activate the venv):
|
31
|
+
|
32
|
+
```console
|
33
|
+
$ uv run toml-combine --help
|
34
|
+
```
|
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2025- Joachim Jablon
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -1,9 +1,10 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: toml-combine
|
3
|
-
Version: 0.
|
3
|
+
Version: 1.0.0
|
4
4
|
Summary: A tool for combining complex configurations in TOML format.
|
5
5
|
Author-email: Joachim Jablon <ewjoachim@gmail.com>
|
6
6
|
License-Expression: MIT
|
7
|
+
License-File: LICENSE
|
7
8
|
Classifier: Development Status :: 4 - Beta
|
8
9
|
Classifier: Intended Audience :: Developers
|
9
10
|
Classifier: License :: OSI Approved :: MIT License
|
@@ -19,6 +20,12 @@ Description-Content-Type: text/markdown
|
|
19
20
|
|
20
21
|
# Toml-combine
|
21
22
|
|
23
|
+
[](https://pypi.org/pypi/toml-combine)
|
24
|
+
[](https://pypi.org/pypi/toml-combine)
|
25
|
+
[](https://github.com/ewjoachim/toml-combine/)
|
26
|
+
[](https://github.com/ewjoachim/toml-combine/actions?workflow=CI)
|
27
|
+
[](https://github.com/ewjoachim/toml-combine/blob/main/LICENSE)
|
28
|
+
|
22
29
|
`toml-combine` is a Python lib and CLI-tool that reads a TOML configuration file
|
23
30
|
defining a default configuration alongside with overrides, and merges everything
|
24
31
|
following rules you define to get final configurations. Let's say: you have multiple
|
@@ -62,7 +69,7 @@ The common configuration to start from, before we start overlaying overrides on
|
|
62
69
|
### Overrides
|
63
70
|
|
64
71
|
Overrides define a set of condition where they apply (`when.<dimension> =
|
65
|
-
"<value>"`) and the values that are
|
72
|
+
"<value>"`) and the values that are overridgden when they're applicable.
|
66
73
|
|
67
74
|
- In case 2 overrides are applicable and define a value for the same key, if one is more
|
68
75
|
specific than the other (e.g. env=prod,region=us is more specific than env=prod) then
|
@@ -1,5 +1,11 @@
|
|
1
1
|
# Toml-combine
|
2
2
|
|
3
|
+
[](https://pypi.org/pypi/toml-combine)
|
4
|
+
[](https://pypi.org/pypi/toml-combine)
|
5
|
+
[](https://github.com/ewjoachim/toml-combine/)
|
6
|
+
[](https://github.com/ewjoachim/toml-combine/actions?workflow=CI)
|
7
|
+
[](https://github.com/ewjoachim/toml-combine/blob/main/LICENSE)
|
8
|
+
|
3
9
|
`toml-combine` is a Python lib and CLI-tool that reads a TOML configuration file
|
4
10
|
defining a default configuration alongside with overrides, and merges everything
|
5
11
|
following rules you define to get final configurations. Let's say: you have multiple
|
@@ -43,7 +49,7 @@ The common configuration to start from, before we start overlaying overrides on
|
|
43
49
|
### Overrides
|
44
50
|
|
45
51
|
Overrides define a set of condition where they apply (`when.<dimension> =
|
46
|
-
"<value>"`) and the values that are
|
52
|
+
"<value>"`) and the values that are overridgden when they're applicable.
|
47
53
|
|
48
54
|
- In case 2 overrides are applicable and define a value for the same key, if one is more
|
49
55
|
specific than the other (e.g. env=prod,region=us is more specific than env=prod) then
|
@@ -31,90 +31,6 @@ def test_merge_configs__dicts_error():
|
|
31
31
|
combiner.merge_configs({"a": 1}, {"a": {"b": 2}})
|
32
32
|
|
33
33
|
|
34
|
-
@pytest.mark.parametrize(
|
35
|
-
"mapping, expected",
|
36
|
-
[
|
37
|
-
pytest.param(
|
38
|
-
{"env": "dev"},
|
39
|
-
{
|
40
|
-
"a": 1,
|
41
|
-
"b": 2,
|
42
|
-
"c": 3,
|
43
|
-
"d": {"e": {"h": {"i": {"j": 4}}}},
|
44
|
-
"g": 6,
|
45
|
-
},
|
46
|
-
id="no_matches",
|
47
|
-
),
|
48
|
-
pytest.param(
|
49
|
-
{"env": "prod"},
|
50
|
-
{
|
51
|
-
"a": 10,
|
52
|
-
"b": 2,
|
53
|
-
"c": 30,
|
54
|
-
"d": {"e": {"h": {"i": {"j": 40}}}},
|
55
|
-
"g": 60,
|
56
|
-
},
|
57
|
-
id="single_match",
|
58
|
-
),
|
59
|
-
pytest.param(
|
60
|
-
{"env": "staging"},
|
61
|
-
{
|
62
|
-
"a": 1,
|
63
|
-
"b": 200,
|
64
|
-
"c": 300,
|
65
|
-
"d": {"e": {"h": {"i": {"j": 400}}}},
|
66
|
-
"f": 500,
|
67
|
-
"g": 6,
|
68
|
-
},
|
69
|
-
id="dont_override_if_match_is_more_specific",
|
70
|
-
),
|
71
|
-
],
|
72
|
-
)
|
73
|
-
def __full_chain(mapping: dict, expected: dict[str, int]):
|
74
|
-
default = {
|
75
|
-
"a": 1,
|
76
|
-
"b": 2,
|
77
|
-
"c": 3,
|
78
|
-
"d": {"e": {"h": {"i": {"j": 4}}}},
|
79
|
-
"g": 6,
|
80
|
-
}
|
81
|
-
|
82
|
-
overrides = [
|
83
|
-
combiner.Override(
|
84
|
-
when={"env": ["prod"]},
|
85
|
-
config={
|
86
|
-
"a": 10,
|
87
|
-
"c": 30,
|
88
|
-
"d": {"e": {"h": {"i": {"j": 40}}}},
|
89
|
-
"g": 60,
|
90
|
-
},
|
91
|
-
),
|
92
|
-
combiner.Override(
|
93
|
-
when={"env": ["staging"]},
|
94
|
-
config={
|
95
|
-
"b": 200,
|
96
|
-
"c": 300,
|
97
|
-
"d": {"e": {"h": {"i": {"j": 400}}}},
|
98
|
-
"f": 500,
|
99
|
-
},
|
100
|
-
),
|
101
|
-
combiner.Override(
|
102
|
-
when={"env": ["staging"], "region": ["us"]},
|
103
|
-
config={"f": 5000, "g": 6000},
|
104
|
-
),
|
105
|
-
]
|
106
|
-
|
107
|
-
result = combiner.generate_for_mapping(
|
108
|
-
config=combiner.Config(
|
109
|
-
dimensions={"env": ["prod", "staging"], "region": ["us"]},
|
110
|
-
default=default,
|
111
|
-
overrides=overrides,
|
112
|
-
),
|
113
|
-
mapping=mapping,
|
114
|
-
)
|
115
|
-
assert result == expected
|
116
|
-
|
117
|
-
|
118
34
|
@pytest.mark.parametrize(
|
119
35
|
"mapping, override, expected",
|
120
36
|
[
|
@@ -159,16 +75,18 @@ def test_build_config():
|
|
159
75
|
raw_config = """
|
160
76
|
[dimensions]
|
161
77
|
env = ["dev", "staging", "prod"]
|
78
|
+
region = ["eu"]
|
162
79
|
|
163
80
|
[default]
|
164
81
|
foo = "bar"
|
165
82
|
|
166
83
|
[[override]]
|
167
84
|
when.env = ["dev", "staging"]
|
85
|
+
when.region = ["eu"]
|
168
86
|
foo = "baz"
|
169
87
|
|
170
88
|
[[override]]
|
171
|
-
when.env = "
|
89
|
+
when.env = "dev"
|
172
90
|
foo = "qux"
|
173
91
|
"""
|
174
92
|
|
@@ -176,89 +94,23 @@ def test_build_config():
|
|
176
94
|
config = combiner.build_config(config_dict)
|
177
95
|
|
178
96
|
assert config == combiner.Config(
|
179
|
-
dimensions={"env": ["dev", "staging", "prod"]},
|
97
|
+
dimensions={"env": ["dev", "staging", "prod"], "region": ["eu"]},
|
180
98
|
default={"foo": "bar"},
|
181
99
|
overrides=[
|
100
|
+
# Note: The order of the overrides is important: more specific overrides
|
101
|
+
# must be listed last.
|
182
102
|
combiner.Override(
|
183
|
-
when={"env": ["dev"
|
184
|
-
config={"foo": "
|
103
|
+
when={"env": ["dev"]},
|
104
|
+
config={"foo": "qux"},
|
185
105
|
),
|
186
106
|
combiner.Override(
|
187
|
-
when={"env": ["
|
188
|
-
config={"foo": "
|
107
|
+
when={"env": ["dev", "staging"], "region": ["eu"]},
|
108
|
+
config={"foo": "baz"},
|
189
109
|
),
|
190
110
|
],
|
191
111
|
)
|
192
112
|
|
193
113
|
|
194
|
-
def test_generate_for_mapping__duplicate_overrides():
|
195
|
-
raw_config = """
|
196
|
-
[dimensions]
|
197
|
-
env = ["prod"]
|
198
|
-
|
199
|
-
[[override]]
|
200
|
-
when.env = "prod"
|
201
|
-
foo = "baz"
|
202
|
-
|
203
|
-
[[override]]
|
204
|
-
when.env = "prod"
|
205
|
-
foo = "qux"
|
206
|
-
"""
|
207
|
-
|
208
|
-
config = combiner.build_config(toml.loads(raw_config))
|
209
|
-
with pytest.raises(exceptions.DuplicateError):
|
210
|
-
combiner.generate_for_mapping(config=config, mapping={"env": "prod"})
|
211
|
-
|
212
|
-
|
213
|
-
def test_build_config__duplicate_overrides_different_vars():
|
214
|
-
raw_config = """
|
215
|
-
[dimensions]
|
216
|
-
env = ["prod"]
|
217
|
-
|
218
|
-
[[override]]
|
219
|
-
when.env = "prod"
|
220
|
-
foo = "baz"
|
221
|
-
|
222
|
-
[[override]]
|
223
|
-
when.env = "prod"
|
224
|
-
baz = "qux"
|
225
|
-
"""
|
226
|
-
|
227
|
-
config = combiner.build_config(toml.loads(raw_config))
|
228
|
-
assert combiner.generate_for_mapping(config=config, mapping={"env": "prod"}) == {
|
229
|
-
"foo": "baz",
|
230
|
-
"baz": "qux",
|
231
|
-
}
|
232
|
-
|
233
|
-
|
234
|
-
def test_build_config__duplicate_overrides_list():
|
235
|
-
raw_config = """
|
236
|
-
[dimensions]
|
237
|
-
env = ["prod", "dev"]
|
238
|
-
|
239
|
-
[[override]]
|
240
|
-
when.env = ["prod"]
|
241
|
-
hello.world = 1
|
242
|
-
|
243
|
-
[[override]]
|
244
|
-
when.env = ["prod", "dev"]
|
245
|
-
hello.world = 2
|
246
|
-
"""
|
247
|
-
|
248
|
-
config = combiner.build_config(toml.loads(raw_config))
|
249
|
-
with pytest.raises(exceptions.DuplicateError) as excinfo:
|
250
|
-
combiner.generate_for_mapping(config=config, mapping={"env": "prod"})
|
251
|
-
|
252
|
-
# Message is a bit complex so we test it too.
|
253
|
-
assert (
|
254
|
-
str(excinfo.value)
|
255
|
-
== "In override {'env': ['prod', 'dev']}: Overrides defining the same "
|
256
|
-
"configuration keys must be included in one another or mutually exclusive.\n"
|
257
|
-
"Key defined multiple times: hello.world\n"
|
258
|
-
"Other override: {'env': ['prod']}"
|
259
|
-
)
|
260
|
-
|
261
|
-
|
262
114
|
def test_build_config__dimension_not_found_in_override():
|
263
115
|
raw_config = """
|
264
116
|
[dimensions]
|
@@ -287,42 +139,6 @@ def test_build_config__dimension_value_not_found_in_override():
|
|
287
139
|
combiner.build_config(config)
|
288
140
|
|
289
141
|
|
290
|
-
@pytest.mark.parametrize(
|
291
|
-
"mapping, expected",
|
292
|
-
[
|
293
|
-
(
|
294
|
-
{"env": "prod"},
|
295
|
-
{"foo": "bar"},
|
296
|
-
),
|
297
|
-
(
|
298
|
-
{"env": "dev"},
|
299
|
-
{"foo": "baz"},
|
300
|
-
),
|
301
|
-
],
|
302
|
-
)
|
303
|
-
def test_generate_for_mapping__full_chain(mapping, expected):
|
304
|
-
config = combiner.build_config(
|
305
|
-
toml.loads(
|
306
|
-
"""
|
307
|
-
[dimensions]
|
308
|
-
env = ["prod", "dev"]
|
309
|
-
|
310
|
-
[default]
|
311
|
-
foo = "bar"
|
312
|
-
|
313
|
-
[[override]]
|
314
|
-
when.env = "dev"
|
315
|
-
foo = "baz"
|
316
|
-
""",
|
317
|
-
)
|
318
|
-
)
|
319
|
-
result = combiner.generate_for_mapping(
|
320
|
-
config=config,
|
321
|
-
mapping=mapping,
|
322
|
-
)
|
323
|
-
assert result == expected
|
324
|
-
|
325
|
-
|
326
142
|
def test_extract_keys():
|
327
143
|
config = toml.loads(
|
328
144
|
"""
|
@@ -393,3 +209,189 @@ def test_extract_keys():
|
|
393
209
|
)
|
394
210
|
def test_are_conditions_compatible(a, b, expected):
|
395
211
|
assert combiner.are_conditions_compatible(a, b) == expected
|
212
|
+
|
213
|
+
|
214
|
+
@pytest.mark.parametrize(
|
215
|
+
"mapping, expected",
|
216
|
+
[
|
217
|
+
(
|
218
|
+
{"env": "prod"},
|
219
|
+
{"foo": "bar"},
|
220
|
+
),
|
221
|
+
(
|
222
|
+
{"env": "staging"},
|
223
|
+
{"foo": "baz"},
|
224
|
+
),
|
225
|
+
],
|
226
|
+
)
|
227
|
+
def test_generate_for_mapping__simple_case(mapping, expected):
|
228
|
+
config = combiner.build_config(
|
229
|
+
toml.loads(
|
230
|
+
"""
|
231
|
+
[dimensions]
|
232
|
+
env = ["prod", "staging"]
|
233
|
+
|
234
|
+
[default]
|
235
|
+
foo = "bar"
|
236
|
+
|
237
|
+
[[override]]
|
238
|
+
when.env = "staging"
|
239
|
+
foo = "baz"
|
240
|
+
""",
|
241
|
+
)
|
242
|
+
)
|
243
|
+
result = combiner.generate_for_mapping(
|
244
|
+
config=config,
|
245
|
+
mapping=mapping,
|
246
|
+
)
|
247
|
+
assert result == expected
|
248
|
+
|
249
|
+
|
250
|
+
@pytest.mark.parametrize(
|
251
|
+
"mapping, expected",
|
252
|
+
[
|
253
|
+
pytest.param(
|
254
|
+
{"env": "dev"},
|
255
|
+
{
|
256
|
+
"a": 1,
|
257
|
+
"b": 2,
|
258
|
+
"c": 3,
|
259
|
+
"d": {"e": {"h": {"i": {"j": 4}}}},
|
260
|
+
"g": 6,
|
261
|
+
},
|
262
|
+
id="no_matches",
|
263
|
+
),
|
264
|
+
pytest.param(
|
265
|
+
{"env": "prod"},
|
266
|
+
{
|
267
|
+
"a": 10,
|
268
|
+
"b": 2,
|
269
|
+
"c": 30,
|
270
|
+
"d": {"e": {"h": {"i": {"j": 40}}}},
|
271
|
+
"g": 60,
|
272
|
+
},
|
273
|
+
id="single_match",
|
274
|
+
),
|
275
|
+
pytest.param(
|
276
|
+
{"env": "staging"},
|
277
|
+
{
|
278
|
+
"a": 1,
|
279
|
+
"b": 200,
|
280
|
+
"c": 300,
|
281
|
+
"d": {"e": {"h": {"i": {"j": 400}}}},
|
282
|
+
"f": 500,
|
283
|
+
"g": 6,
|
284
|
+
},
|
285
|
+
id="dont_override_if_match_is_more_specific",
|
286
|
+
),
|
287
|
+
],
|
288
|
+
)
|
289
|
+
def test_generate_for_mapping__complex_case(mapping: dict, expected: dict[str, int]):
|
290
|
+
default = {
|
291
|
+
"a": 1,
|
292
|
+
"b": 2,
|
293
|
+
"c": 3,
|
294
|
+
"d": {"e": {"h": {"i": {"j": 4}}}},
|
295
|
+
"g": 6,
|
296
|
+
}
|
297
|
+
|
298
|
+
overrides = [
|
299
|
+
combiner.Override(
|
300
|
+
when={"env": ["prod"]},
|
301
|
+
config={
|
302
|
+
"a": 10,
|
303
|
+
"c": 30,
|
304
|
+
"d": {"e": {"h": {"i": {"j": 40}}}},
|
305
|
+
"g": 60,
|
306
|
+
},
|
307
|
+
),
|
308
|
+
combiner.Override(
|
309
|
+
when={"env": ["staging"]},
|
310
|
+
config={
|
311
|
+
"b": 200,
|
312
|
+
"c": 300,
|
313
|
+
"d": {"e": {"h": {"i": {"j": 400}}}},
|
314
|
+
"f": 500,
|
315
|
+
},
|
316
|
+
),
|
317
|
+
combiner.Override(
|
318
|
+
when={"env": ["staging"], "region": ["us"]},
|
319
|
+
config={"f": 5000, "g": 6000},
|
320
|
+
),
|
321
|
+
]
|
322
|
+
config = combiner.Config(
|
323
|
+
dimensions={"env": ["prod", "staging"], "region": ["us"]},
|
324
|
+
default=default,
|
325
|
+
overrides=overrides,
|
326
|
+
)
|
327
|
+
|
328
|
+
result = combiner.generate_for_mapping(config=config, mapping=mapping)
|
329
|
+
assert result == expected
|
330
|
+
|
331
|
+
|
332
|
+
def test_generate_for_mapping__duplicate_overrides():
|
333
|
+
raw_config = """
|
334
|
+
[dimensions]
|
335
|
+
env = ["prod"]
|
336
|
+
|
337
|
+
[[override]]
|
338
|
+
when.env = "prod"
|
339
|
+
foo = "baz"
|
340
|
+
|
341
|
+
[[override]]
|
342
|
+
when.env = "prod"
|
343
|
+
foo = "qux"
|
344
|
+
"""
|
345
|
+
|
346
|
+
config = combiner.build_config(toml.loads(raw_config))
|
347
|
+
with pytest.raises(exceptions.IncompatibleOverrides):
|
348
|
+
combiner.generate_for_mapping(config=config, mapping={"env": "prod"})
|
349
|
+
|
350
|
+
|
351
|
+
def test_generate_for_mapping__duplicate_overrides_different_vars():
|
352
|
+
raw_config = """
|
353
|
+
[dimensions]
|
354
|
+
env = ["prod"]
|
355
|
+
|
356
|
+
[[override]]
|
357
|
+
when.env = "prod"
|
358
|
+
foo = "baz"
|
359
|
+
|
360
|
+
[[override]]
|
361
|
+
when.env = "prod"
|
362
|
+
baz = "qux"
|
363
|
+
"""
|
364
|
+
|
365
|
+
config = combiner.build_config(toml.loads(raw_config))
|
366
|
+
assert combiner.generate_for_mapping(config=config, mapping={"env": "prod"}) == {
|
367
|
+
"foo": "baz",
|
368
|
+
"baz": "qux",
|
369
|
+
}
|
370
|
+
|
371
|
+
|
372
|
+
def test_generate_for_mapping__duplicate_overrides_list():
|
373
|
+
raw_config = """
|
374
|
+
[dimensions]
|
375
|
+
env = ["prod", "dev"]
|
376
|
+
|
377
|
+
[[override]]
|
378
|
+
when.env = ["prod"]
|
379
|
+
hello.world = 1
|
380
|
+
|
381
|
+
[[override]]
|
382
|
+
when.env = ["prod", "dev"]
|
383
|
+
hello.world = 2
|
384
|
+
"""
|
385
|
+
|
386
|
+
config = combiner.build_config(toml.loads(raw_config))
|
387
|
+
with pytest.raises(exceptions.IncompatibleOverrides) as excinfo:
|
388
|
+
combiner.generate_for_mapping(config=config, mapping={"env": "prod"})
|
389
|
+
|
390
|
+
# Message is a bit complex so we test it too.
|
391
|
+
assert (
|
392
|
+
str(excinfo.value)
|
393
|
+
== "In override {'env': ['prod', 'dev']}: Overrides defining the same "
|
394
|
+
"configuration keys must be included in one another or mutually exclusive.\n"
|
395
|
+
"Key defined multiple times: hello.world\n"
|
396
|
+
"Other override: {'env': ['prod']}"
|
397
|
+
)
|
@@ -97,7 +97,7 @@ def are_conditions_compatible(
|
|
97
97
|
) -> bool:
|
98
98
|
"""
|
99
99
|
`a` and `b` are dictionaries representing override conditions (`when`). Return
|
100
|
-
`True` if the conditions represented by `a` are compatible
|
100
|
+
`True` if the conditions represented by `a` are compatible with `b`. Conditions are
|
101
101
|
compatible if one is stricly more specific than the other or if they're mutually
|
102
102
|
exclusive.
|
103
103
|
"""
|
@@ -166,7 +166,7 @@ def generate_for_mapping(
|
|
166
166
|
mapping: Mapping[str, str],
|
167
167
|
) -> Mapping[str, Any]:
|
168
168
|
result = copy.deepcopy(config.default)
|
169
|
-
keys_to_conditions: dict[tuple[str, ...], list[
|
169
|
+
keys_to_conditions: dict[tuple[str, ...], list[Mapping[str, list[str]]]] = {}
|
170
170
|
# Apply each matching override
|
171
171
|
for override in config.overrides:
|
172
172
|
# Check if all dimension values in the override match
|
@@ -180,7 +180,7 @@ def generate_for_mapping(
|
|
180
180
|
|
181
181
|
for previous_condition in previous_conditions:
|
182
182
|
if not are_conditions_compatible(previous_condition, override.when):
|
183
|
-
raise exceptions.
|
183
|
+
raise exceptions.IncompatibleOverrides(
|
184
184
|
id=override.when,
|
185
185
|
key=".".join(key),
|
186
186
|
other_override=previous_condition,
|
@@ -19,7 +19,7 @@ class TomlEncodeError(TomlCombineError):
|
|
19
19
|
"""Error while encoding configuration file."""
|
20
20
|
|
21
21
|
|
22
|
-
class
|
22
|
+
class IncompatibleOverrides(TomlCombineError):
|
23
23
|
"""In override {id}: Overrides defining the same configuration keys must be included in one another or mutually exclusive.\nKey defined multiple times: {key}\nOther override: {other_override}"""
|
24
24
|
|
25
25
|
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|