Report more YAML syntax errors

In our override of construct_mapping, we don't handle one of the
error cases that the base class does.  If the user supplies
"foo: {{ bar }}" we fail before the superclass method runs and
therefore we don't benefit from its error.  This change corrects
that by testing for that case and allowing the superclass method
to run.

Change-Id: I0b92053b08cb4991309163c94846bd34c4cb1393
This commit is contained in:
James E. Blair 2021-11-16 09:34:59 -08:00
parent 36526a3448
commit b19aff7bb8
2 changed files with 25 additions and 0 deletions

View File

@ -1708,6 +1708,25 @@ class TestInRepoConfig(ZuulTestCase):
self.assertIn('job_not_a_dict', A.messages[0],
"A should list the bad key")
def test_yaml_dict_error2(self):
in_repo_conf = textwrap.dedent(
"""
- foo: {{ not_a_dict }}
""")
file_dict = {'.zuul.yaml': in_repo_conf}
A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
files=file_dict)
A.addApproval('Code-Review', 2)
self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
self.waitUntilSettled()
self.assertEqual(A.data['status'], 'NEW')
self.assertEqual(A.reported, 1,
"A should report failure")
self.assertIn('while constructing a mapping', A.messages[0],
"A should have a syntax error reported")
def test_yaml_duplicate_key_error(self):
in_repo_conf = textwrap.dedent(
"""

View File

@ -347,6 +347,12 @@ class ZuulSafeLoader(yaml.EncryptedLoader):
if k.value == '<<':
continue
if not isinstance(k.value, collections.abc.Hashable):
# This happens with "foo: {{ bar }}"
# This will raise an error in the superclass
# construct_mapping below; ignore it for now.
continue
if k.value in keys:
mark = model.ZuulMark(node.start_mark, node.end_mark,
self.zuul_stream)