From 1a65a1d44b4661e92051615d27dbedb7f523c5fd Mon Sep 17 00:00:00 2001 From: Vsevolod Fedorov Date: Wed, 15 Nov 2023 11:25:59 +0300 Subject: [PATCH] Handle nested brace inside variable Change-Id: I1004ea869bfe56b380109c7bcae866931b4d4725 --- jenkins_jobs/formatter.py | 2 +- .../error_fixtures/nested_brace_in_variable.error | 9 +++++++++ .../error_fixtures/nested_brace_in_variable.yaml | 13 +++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/yamlparser/error_fixtures/nested_brace_in_variable.error create mode 100644 tests/yamlparser/error_fixtures/nested_brace_in_variable.yaml diff --git a/jenkins_jobs/formatter.py b/jenkins_jobs/formatter.py index c7741c358..725592d89 100644 --- a/jenkins_jobs/formatter.py +++ b/jenkins_jobs/formatter.py @@ -77,7 +77,7 @@ class CustomFormatter(Formatter): try: return super().vformat(format_string, args, kwargs) - except (JenkinsJobsException, UndefinedError) as x: + except (JenkinsJobsException, UndefinedError, ValueError) as x: if len(format_string) > 40: short_fmt = format_string[:80] + "..." else: diff --git a/tests/yamlparser/error_fixtures/nested_brace_in_variable.error b/tests/yamlparser/error_fixtures/nested_brace_in_variable.error new file mode 100644 index 000000000..5a58c4465 --- /dev/null +++ b/tests/yamlparser/error_fixtures/nested_brace_in_variable.error @@ -0,0 +1,9 @@ +nested_brace_in_variable.yaml:10:3: In project 'sample-project' + - project: + ^ +nested_brace_in_variable.yaml:5:3: In job template 'sample-job' + - job-template: + ^ +nested_brace_in_variable.yaml:8:15: While formatting string '{ {x} }': unexpected '{' in field name + - shell: '{ {x} }' + ^ diff --git a/tests/yamlparser/error_fixtures/nested_brace_in_variable.yaml b/tests/yamlparser/error_fixtures/nested_brace_in_variable.yaml new file mode 100644 index 000000000..d9c6a5d37 --- /dev/null +++ b/tests/yamlparser/error_fixtures/nested_brace_in_variable.yaml @@ -0,0 +1,13 @@ +# Check braces inside variable does not cause parser to fail. +# This may happen when tag !include-raw: is used instead of !include-raw-escape: +# and there are nested blocks of groovy code inside included file. + +- job-template: + name: sample-job + builders: + - shell: '{ {x} }' + +- project: + name: sample-project + jobs: + - sample-job