-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
test_errors.py
341 lines (275 loc) · 9.83 KB
/
test_errors.py
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""Negative tests for the parametrization."""
import logging
import re
import pytest
from dvc.parsing import ResolveError
from dvc.parsing.context import Context
from dvc.parsing.interpolate import embrace
from dvc.utils.humanize import join
from . import make_entry_definition, make_foreach_def
def escape_ansi(line):
ansi_escape = re.compile(r"(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]")
return ansi_escape.sub("", line)
# Tests for the interpolated entries
@pytest.mark.parametrize("vars_", ["${file}_params.yaml", {"foo": "${foo}"}])
def test_vars_interpolation_errors(tmp_dir, dvc, vars_):
definition = make_entry_definition(tmp_dir, "build", {"vars": [vars_]})
with pytest.raises(ResolveError) as exc_info:
definition.resolve()
assert (
str(exc_info.value)
== "failed to parse 'stages.build.vars' in 'dvc.yaml': "
"interpolating is not allowed"
)
def test_failed_to_interpolate(tmp_dir, dvc):
context = Context(models={"foo": "bar"})
definition = make_entry_definition(
tmp_dir, "build", {"cmd": "echo ${models.foo.}"}, context
)
with pytest.raises(ResolveError) as exc_info:
definition.resolve()
assert escape_ansi(str(exc_info.value)) == (
"failed to parse 'stages.build.cmd' in 'dvc.yaml':\n"
"${models.foo.}\n"
" ^\n"
"ParseException: Expected end of text, found '.'"
" (at char 12), (line:1, col:13)"
)
assert definition.context == {"models": {"foo": "bar"}}
def test_local_vars_params_file_not_exist(tmp_dir, dvc):
definition = make_entry_definition(
tmp_dir,
"build",
{"vars": ["not_existing_params.yaml"], "cmd": "echo ${models.foo}"},
)
with pytest.raises(ResolveError) as exc_info:
definition.resolve()
assert str(exc_info.value) == (
"failed to parse stage 'build' in 'dvc.yaml': "
"'not_existing_params.yaml' does not exist"
)
assert not definition.context
def test_specified_key_does_not_exist(tmp_dir, dvc):
definition = make_entry_definition(
tmp_dir,
"build",
{"cmd": "echo ${models.foobar}"},
Context(models={"foo": "foo"}),
)
with pytest.raises(ResolveError) as exc_info:
definition.resolve()
assert str(exc_info.value) == (
"failed to parse 'stages.build.cmd' in 'dvc.yaml': "
"Could not find 'models.foobar'"
)
assert definition.context == {"models": {"foo": "foo"}}
@pytest.mark.parametrize(
"wdir, expected_msg",
[
("${models[foobar]}", " Could not find 'models.foobar'"),
(
"${models.foo]}",
"\n${models.foo]}\n"
" ^\n"
"ParseException: Expected end of text, found ']'"
" (at char 12), (line:1, col:13)",
),
],
)
def test_wdir_failed_to_interpolate(tmp_dir, dvc, wdir, expected_msg):
definition = make_entry_definition(
tmp_dir,
"build",
{"wdir": wdir, "cmd": "echo ${models.bar}"},
Context(models={"bar": "bar"}),
)
with pytest.raises(ResolveError) as exc_info:
definition.resolve()
assert escape_ansi(str(exc_info.value)) == (
"failed to parse 'stages.build.wdir' in 'dvc.yaml':" + expected_msg
)
assert definition.context == {"models": {"bar": "bar"}}
def test_interpolate_non_string(tmp_dir, dvc):
definition = make_entry_definition(
tmp_dir, "build", {"outs": "${models}"}, Context(models={})
)
with pytest.raises(ResolveError) as exc_info:
definition.resolve()
assert str(exc_info.value) == (
"failed to parse 'stages.build.outs' in 'dvc.yaml':\n"
"Cannot interpolate data of type 'dict'"
)
assert definition.context == {"models": {}}
def test_interpolate_nested_iterable(tmp_dir, dvc):
definition = make_entry_definition(
tmp_dir,
"build",
{"cmd": "echo ${models}"},
Context(models={"list": [1, [2, 3]]}),
)
with pytest.raises(ResolveError) as exc_info:
definition.resolve()
assert str(exc_info.value) == (
"failed to parse 'stages.build.cmd' in 'dvc.yaml':\n"
"Cannot interpolate nested iterable in 'list'"
)
def test_partial_vars_doesnot_exist(tmp_dir, dvc):
(tmp_dir / "test_params.yaml").dump({"sub1": "sub1", "sub2": "sub2"})
definition = make_entry_definition(
tmp_dir,
"build",
{"vars": ["test_params.yaml:sub3"], "cmd": "echo ${sub1} ${sub2}"},
)
with pytest.raises(ResolveError) as exc_info:
definition.resolve()
assert str(exc_info.value) == (
"failed to parse stage 'build' in 'dvc.yaml': "
"could not find 'sub3' in 'test_params.yaml'"
)
assert not definition.context
# Tests foreach generated stages and their error messages
def test_foreach_data_syntax_error(tmp_dir, dvc):
definition = make_foreach_def(tmp_dir, "build", "${syntax.[error}", {})
with pytest.raises(ResolveError) as exc_info:
definition.resolve_all()
assert escape_ansi(str(exc_info.value)) == (
"failed to parse 'stages.build.foreach' in 'dvc.yaml':\n"
"${syntax.[error}\n"
" ^\n"
"ParseException: Expected end of text, found '.'"
" (at char 8), (line:1, col:9)"
)
@pytest.mark.parametrize("key", ["modelss", "modelss.123"])
def test_foreach_data_key_does_not_exists(tmp_dir, dvc, key):
definition = make_foreach_def(tmp_dir, "build", embrace(key), {})
with pytest.raises(ResolveError) as exc_info:
definition.resolve_all()
assert str(exc_info.value) == (
"failed to parse 'stages.build.foreach' in 'dvc.yaml': "
f"Could not find '{key}'"
)
@pytest.mark.parametrize(
"foreach_data", ["${foo}", "${dct.model1}", "${lst.0}", "foobar"]
)
def test_foreach_data_expects_list_or_dict(tmp_dir, dvc, foreach_data):
context = Context(
{"foo": "bar", "dct": {"model1": "a-out"}, "lst": ["foo", "bar"]}
)
definition = make_foreach_def(tmp_dir, "build", foreach_data, {}, context)
with pytest.raises(ResolveError) as exc_info:
definition.resolve_all()
assert str(exc_info.value) == (
"failed to resolve 'stages.build.foreach' in 'dvc.yaml': "
"expected list/dictionary, got str"
)
@pytest.mark.parametrize(
"global_data, where",
[
({"item": 10, "key": 10}, "item and key are"),
({"item": 10}, "item is"),
({"key": 5}, "key is"),
],
)
def test_foreach_overwriting_item_in_list(
tmp_dir, dvc, caplog, global_data, where
):
context = Context(global_data)
definition = make_foreach_def(
tmp_dir, "build", {"model1": 10, "model2": 5}, {}, context
)
with caplog.at_level(logging.WARNING, logger="dvc.parsing"):
definition.resolve_all()
assert caplog.messages == [
f"{where} already specified, "
"will be overwritten for stages generated from 'build'"
]
def test_foreach_do_syntax_errors(tmp_dir, dvc):
definition = make_foreach_def(
tmp_dir, "build", ["foo", "bar"], {"cmd": "echo ${syntax.[error}"}
)
with pytest.raises(ResolveError) as exc_info:
definition.resolve_all()
assert escape_ansi(str(exc_info.value)) == (
"failed to parse 'stages.build.cmd' in 'dvc.yaml':\n"
"${syntax.[error}\n"
" ^\n"
"ParseException: Expected end of text, found '.'"
" (at char 8), (line:1, col:9)"
)
@pytest.mark.parametrize(
"key, loc",
[
(
"item.thresh", # the `thresh` in not available on model2`
"stages.build@1.cmd",
),
("foo.bar", "stages.build@0.cmd"), # not available on any stages
],
)
def test_foreach_do_definition_item_does_not_exist(tmp_dir, dvc, key, loc):
context = Context(foo="bar")
definition = make_foreach_def(
tmp_dir,
"build",
[{"thresh": "10"}, {}],
{"cmd": embrace(key)},
context,
)
with pytest.raises(ResolveError) as exc_info:
definition.resolve_all()
assert str(exc_info.value) == (
f"failed to parse '{loc}' in 'dvc.yaml': Could not find '{key}'"
)
# should have no `item` and `key` even though it failed to resolve.
assert context == {"foo": "bar"}
@pytest.mark.parametrize(
"redefine",
[
{"item": 5},
{"key": 5},
{"item": 5, "key": 10},
{"item": {"epochs": 10}},
],
)
@pytest.mark.parametrize("from_file", [True, False])
def test_item_key_in_generated_stage_vars(tmp_dir, dvc, redefine, from_file):
context = Context(foo="bar")
vars_ = [redefine]
if from_file:
(tmp_dir / "test_params.yaml").dump(redefine)
vars_ = ["test_params.yaml"]
definition = make_foreach_def(
tmp_dir,
"build",
{"model1": {"thresh": "10"}, "model2": {"thresh": 5}},
{"vars": vars_, "cmd": "${item}"},
context,
)
with pytest.raises(ResolveError) as exc_info:
definition.resolve_all()
message = str(exc_info.value)
assert (
"failed to parse stage 'build@model1' in 'dvc.yaml': "
"attempted to modify reserved"
) in message
key_or_keys = "keys" if len(redefine) > 1 else "key"
assert f"{key_or_keys} {join(redefine)}" in message
if from_file:
assert "in 'test_params.yaml'" in message
assert context == {"foo": "bar"}
def test_foreach_wdir_key_does_not_exist(tmp_dir, dvc):
definition = make_foreach_def(
tmp_dir,
"build",
"${models}",
{"wdir": "${ite}", "cmd": "echo ${item}"},
Context(models=["foo", "bar"]),
)
with pytest.raises(ResolveError) as exc_info:
definition.resolve_all()
assert (
str(exc_info.value)
== "failed to parse 'stages.build@foo.wdir' in 'dvc.yaml': "
"Could not find 'ite'"
)
assert definition.context == {"models": ["foo", "bar"]}