Add Loader.source_dir attribute

Currently, Loader.source_path is used for both error locations and
as base for include directory.
But, it is set to "<expanded j2-yaml>" by !j2-yaml: tag, which makes it
unusable as base for include directory.
Make separate source_dir attribute for this usage.

Change-Id: I67669eb42f761e2d76e89992e6ad89480ddd3df0
This commit is contained in:
Vsevolod Fedorov 2023-12-06 11:13:04 +03:00
parent c1ac0e03d6
commit af9e394c8e
5 changed files with 17 additions and 12 deletions

View File

@ -28,10 +28,13 @@ class Loader(LocLoader):
def empty(cls, jjb_config):
return cls(io.StringIO(), jjb_config)
def __init__(self, stream, jjb_config, source_path=None, anchors=None):
def __init__(
self, stream, jjb_config, source_path=None, source_dir=None, anchors=None
):
super().__init__(stream, source_path)
self.jjb_config = jjb_config
self.source_path = source_path
self.source_dir = source_dir
self._retain_anchors = jjb_config.yamlparser["retain_anchors"]
if anchors:
# Override default set by super class.
@ -48,17 +51,17 @@ class Loader(LocLoader):
self.get_event()
return node
def _with_stream(self, stream, source_path):
return Loader(stream, self.jjb_config, source_path, self.anchors)
def _with_stream(self, stream, source_path, source_dir):
return Loader(stream, self.jjb_config, source_path, source_dir, self.anchors)
def load_fp(self, fp):
return self.load(fp)
def load_path(self, path):
return self.load(path.read_text(), source_path=path)
return self.load(path.read_text(), source_path=path, source_dir=path.parent)
def load(self, stream, source_path=None):
loader = self._with_stream(stream, source_path)
def load(self, stream, source_path=None, source_dir=None):
loader = self._with_stream(stream, source_path, source_dir)
try:
return loader.get_single_data()
finally:

View File

@ -201,7 +201,6 @@ Examples:
"""
import abc
import os.path
import logging
import traceback
import sys
@ -250,9 +249,9 @@ class BaseYamlObject(metaclass=abc.ABCMeta):
def __init__(self, jjb_config, loader, pos):
self._search_path = jjb_config.yamlparser["include_path"]
if loader.source_path:
if loader.source_dir:
# Loaded from a file, find includes beside it too.
self._search_path.append(os.path.dirname(loader.source_path))
self._search_path.append(loader.source_dir)
self._loader = loader
self._pos = pos
allow_empty = jjb_config.yamlparser["allow_empty_variables"]
@ -357,7 +356,9 @@ class J2Yaml(J2Template):
def expand(self, expander, params):
text = self._render(params)
data = self._loader.load(text, source_path="<expanded j2-yaml>")
data = self._loader.load(
text, source_path="<expanded j2-yaml>", source_dir=self._loader.source_dir
)
try:
return expander.expand(data, params)
except JenkinsJobsException as x:

View File

@ -16,6 +16,6 @@ cases = [
@pytest.mark.parametrize("format,expected_used_params", cases)
def test_jinja2_required_params(format, expected_used_params):
config = JJBConfig()
loader = Mock(source_path=None)
loader = Mock(source_dir=None)
template = J2String(config, loader, pos=None, template_text=format)
assert template.required_params == set(expected_used_params)

View File

@ -40,6 +40,7 @@ def read_input(scenario, jjb_config):
scenario.in_path.read_text(),
jjb_config=jjb_config,
source_path=scenario.in_path,
source_dir=scenario.in_path.parent,
)
return loader.get_single_data()

View File

@ -7,6 +7,6 @@ include_missing_path_in_j2_yaml.yaml:3:3: In job template 'sample-job'
include_missing_path_in_j2_yaml.yaml:5:15: In expanded !j2-yaml:
builders: !j2-yaml: |
^
<expanded j2-yaml>:2:5: File missing-file.sh does not exist in any of include directories: .,fixtures-dir,.
<expanded j2-yaml>:2:5: File missing-file.sh does not exist in any of include directories: .,fixtures-dir,fixtures-dir
!include: missing-file.sh
^