diff --git a/jenkins_jobs/loader.py b/jenkins_jobs/loader.py index 113266ba2..c4ba948a3 100644 --- a/jenkins_jobs/loader.py +++ b/jenkins_jobs/loader.py @@ -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: diff --git a/jenkins_jobs/yaml_objects.py b/jenkins_jobs/yaml_objects.py index 88c2447f6..5ff2eb968 100644 --- a/jenkins_jobs/yaml_objects.py +++ b/jenkins_jobs/yaml_objects.py @@ -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="") + data = self._loader.load( + text, source_path="", source_dir=self._loader.source_dir + ) try: return expander.expand(data, params) except JenkinsJobsException as x: diff --git a/tests/formatter/test_jinja2.py b/tests/formatter/test_jinja2.py index 35eee0045..408ac9b58 100644 --- a/tests/formatter/test_jinja2.py +++ b/tests/formatter/test_jinja2.py @@ -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) diff --git a/tests/loader/test_loader.py b/tests/loader/test_loader.py index cb35fc202..b48160d50 100644 --- a/tests/loader/test_loader.py +++ b/tests/loader/test_loader.py @@ -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() diff --git a/tests/yamlparser/error_fixtures/include_missing_path_in_j2_yaml.error b/tests/yamlparser/error_fixtures/include_missing_path_in_j2_yaml.error index 4367d4c8a..9b3c67bb8 100644 --- a/tests/yamlparser/error_fixtures/include_missing_path_in_j2_yaml.error +++ b/tests/yamlparser/error_fixtures/include_missing_path_in_j2_yaml.error @@ -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: | ^ -:2:5: File missing-file.sh does not exist in any of include directories: .,fixtures-dir,. +:2:5: File missing-file.sh does not exist in any of include directories: .,fixtures-dir,fixtures-dir !include: missing-file.sh ^