From b19aff7bb8a5a14ff397daa8d7a266d5374dfaed Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Tue, 16 Nov 2021 09:34:59 -0800 Subject: [PATCH] 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 --- tests/unit/test_v3.py | 19 +++++++++++++++++++ zuul/configloader.py | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/tests/unit/test_v3.py b/tests/unit/test_v3.py index 24cf6d96e3..ebe9aeb3f4 100644 --- a/tests/unit/test_v3.py +++ b/tests/unit/test_v3.py @@ -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( """ diff --git a/zuul/configloader.py b/zuul/configloader.py index 50c5447ce3..81e50fa653 100644 --- a/zuul/configloader.py +++ b/zuul/configloader.py @@ -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)