From 4897d5e047006c4a49a85c8709abe9711d949d53 Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Mon, 15 Apr 2024 16:34:30 +0200 Subject: [PATCH] Story 2011112: include-raw-verbatim fails on job-template don't expand IncludeRawVerbatim twice > FAILED tests/loader/test_loader.py::test_include_job[include002] - jenkins_jobs.errors.JenkinsJobsException: While formatting string "#!/bin/bash\n#\n# sample script to check that variables aren't expanded\n# when usi...": Missing parameter: 'VAR1' > FAILED tests/loader/test_loader.py::test_include[include002] - jenkins_jobs.errors.JenkinsJobsException: While formatting string "#!/bin/bash\n#\n# sample script to check that variables aren't expanded\n# when usi...": Missing parameter: 'VAR1' Change-Id: I88b22987eabe99f00d99755299256d4aba8a1c76 --- jenkins_jobs/expander.py | 5 +-- jenkins_jobs/loader.py | 4 +-- .../job_fixtures/include-raw-vars.sh | 14 ++++++++ tests/yamlparser/job_fixtures/include002.xml | 33 +++++++++++++++++++ tests/yamlparser/job_fixtures/include002.yaml | 6 ++++ .../job_fixtures/include002.yaml.inc | 4 +++ 6 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 tests/yamlparser/job_fixtures/include-raw-vars.sh create mode 100644 tests/yamlparser/job_fixtures/include002.xml create mode 100644 tests/yamlparser/job_fixtures/include002.yaml create mode 100644 tests/yamlparser/job_fixtures/include002.yaml.inc diff --git a/jenkins_jobs/expander.py b/jenkins_jobs/expander.py index 57ded06f8..fed2f51f3 100644 --- a/jenkins_jobs/expander.py +++ b/jenkins_jobs/expander.py @@ -143,12 +143,13 @@ class Expander: # Expand only yaml objects. class YamlObjectsExpander(Expander): - def __init__(self): - super().__init__() + def __init__(self, config=None): + super().__init__(config) self.expanders.update( { str: dont_expand, LocString: dont_expand, + IncludeRawVerbatim: dont_expand, } ) diff --git a/jenkins_jobs/loader.py b/jenkins_jobs/loader.py index 6ffded141..a79a26284 100644 --- a/jenkins_jobs/loader.py +++ b/jenkins_jobs/loader.py @@ -18,7 +18,7 @@ from functools import partial from .errors import JenkinsJobsException from .loc_loader import LocLoader from .yaml_objects import BaseYamlObject -from .expander import Expander, deprecated_yaml_tags, yaml_classes_list +from .expander import YamlObjectsExpander, deprecated_yaml_tags, yaml_classes_list from .roots import root_adders logger = logging.getLogger(__name__) @@ -118,7 +118,7 @@ def enum_expanded_paths(path_list): def load_files(config, roots, path_list): - expander = Expander(config) + expander = YamlObjectsExpander(config) loader = Loader.empty(config) for path in enum_expanded_paths(path_list): if is_stdin(path): diff --git a/tests/yamlparser/job_fixtures/include-raw-vars.sh b/tests/yamlparser/job_fixtures/include-raw-vars.sh new file mode 100644 index 000000000..7cedeaed4 --- /dev/null +++ b/tests/yamlparser/job_fixtures/include-raw-vars.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# +# sample script to check that variables aren't expanded +# when using the !include-raw-verbatim: application yaml tag + +VAR1="hello" +VAR2="world" +VAR3="${VAR1} ${VAR2}" + +[[ -n "${VAR3}" ]] && { + # this next section is executed as one + echo "${VAR3}" + exit 0 +} diff --git a/tests/yamlparser/job_fixtures/include002.xml b/tests/yamlparser/job_fixtures/include002.xml new file mode 100644 index 000000000..f341ef024 --- /dev/null +++ b/tests/yamlparser/job_fixtures/include002.xml @@ -0,0 +1,33 @@ + + + + <!-- Managed by Jenkins Job Builder --> + false + false + false + false + true + + + + + #!/bin/bash +# +# sample script to check that variables aren't expanded +# when using the !include-raw-verbatim: application yaml tag + +VAR1="hello" +VAR2="world" +VAR3="${VAR1} ${VAR2}" + +[[ -n "${VAR3}" ]] && { + # this next section is executed as one + echo "${VAR3}" + exit 0 +} + + + + + + diff --git a/tests/yamlparser/job_fixtures/include002.yaml b/tests/yamlparser/job_fixtures/include002.yaml new file mode 100644 index 000000000..6b1906a47 --- /dev/null +++ b/tests/yamlparser/job_fixtures/include002.yaml @@ -0,0 +1,6 @@ +# using !include in job-template +- job-template: !include: include002.yaml.inc +- project: + name: acc-cdi + jobs: + - test-gerrit diff --git a/tests/yamlparser/job_fixtures/include002.yaml.inc b/tests/yamlparser/job_fixtures/include002.yaml.inc new file mode 100644 index 000000000..e918fab21 --- /dev/null +++ b/tests/yamlparser/job_fixtures/include002.yaml.inc @@ -0,0 +1,4 @@ +name: test-gerrit +builders: + - shell: + !include-raw-verbatim: include-raw-vars.sh