diff --git a/jenkins_jobs/local_yaml.py b/jenkins_jobs/local_yaml.py index e1b26bd1c..896cc24a9 100644 --- a/jenkins_jobs/local_yaml.py +++ b/jenkins_jobs/local_yaml.py @@ -613,7 +613,10 @@ class LateYamlLoader(CustomLoader): return LateYamlLoader(self._yaml_str, copy.deepcopy(self._loader, memo)) def get_object_to_format(self): - return load(self._yaml_str, search_path=self._loader._search_path) + return yaml.load( + self._yaml_str, + functools.partial(LocalLoader, search_path=self._loader._search_path), + ) class Jinja2YamlLoader(Jinja2Loader): diff --git a/tests/localyaml/fixtures/custom_retain_anchors_j2_yaml.yaml b/tests/localyaml/fixtures/custom_retain_anchors_j2_yaml.yaml new file mode 100644 index 000000000..f5471853c --- /dev/null +++ b/tests/localyaml/fixtures/custom_retain_anchors_j2_yaml.yaml @@ -0,0 +1,12 @@ +- job-template: + name: 'test-job-template' + builders: + - shell: + docker run {docker-image} + +- project: + name: test-project + jobs: + - 'test-job-template': + docker-image: !j2-yaml: | + *docker-image diff --git a/tests/localyaml/fixtures/custom_retain_anchors_j2_yaml_include001.yaml b/tests/localyaml/fixtures/custom_retain_anchors_j2_yaml_include001.yaml new file mode 100644 index 000000000..18a72a5bb --- /dev/null +++ b/tests/localyaml/fixtures/custom_retain_anchors_j2_yaml_include001.yaml @@ -0,0 +1,3 @@ +- globals: + name: globals + docker-image: &docker-image "ubuntu:latest" diff --git a/tests/localyaml/test_localyaml.py b/tests/localyaml/test_localyaml.py index c41c9ed8f..3814fb56c 100644 --- a/tests/localyaml/test_localyaml.py +++ b/tests/localyaml/test_localyaml.py @@ -22,6 +22,7 @@ from yaml.composer import ComposerError from jenkins_jobs.config import JJBConfig from jenkins_jobs.parser import YamlParser +from jenkins_jobs.registry import ModuleRegistry from tests import base @@ -118,3 +119,23 @@ class TestCaseLocalYamlRetainAnchors(base.BaseTestCase): jjb_config.validate() j = YamlParser(jjb_config) j.load_files([os.path.join(self.fixtures_path, f) for f in files]) + + def test_retain_anchors_enabled_j2_yaml(self): + """ + Verify that anchors are retained across files and are properly retained when using !j2-yaml. + """ + + files = [ + "custom_retain_anchors_j2_yaml_include001.yaml", + "custom_retain_anchors_j2_yaml.yaml", + ] + + jjb_config = JJBConfig() + jjb_config.yamlparser["retain_anchors"] = True + jjb_config.validate() + j = YamlParser(jjb_config) + j.load_files([os.path.join(self.fixtures_path, f) for f in files]) + + registry = ModuleRegistry(jjb_config, None) + jobs, _ = j.expandYaml(registry) + self.assertEqual(jobs[0]["builders"][0]["shell"], "docker run ubuntu:latest")