Merge "Fix retaining anchors when using with !j2-yaml"

This commit is contained in:
Zuul 2021-04-10 14:00:34 +00:00 committed by Gerrit Code Review
commit 0d0dc94a08
4 changed files with 40 additions and 1 deletions

View File

@ -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):

View File

@ -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

View File

@ -0,0 +1,3 @@
- globals:
name: globals
docker-image: &docker-image "ubuntu:latest"

View File

@ -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")