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:
parent
c1ac0e03d6
commit
af9e394c8e
@ -28,10 +28,13 @@ class Loader(LocLoader):
|
|||||||
def empty(cls, jjb_config):
|
def empty(cls, jjb_config):
|
||||||
return cls(io.StringIO(), 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)
|
super().__init__(stream, source_path)
|
||||||
self.jjb_config = jjb_config
|
self.jjb_config = jjb_config
|
||||||
self.source_path = source_path
|
self.source_path = source_path
|
||||||
|
self.source_dir = source_dir
|
||||||
self._retain_anchors = jjb_config.yamlparser["retain_anchors"]
|
self._retain_anchors = jjb_config.yamlparser["retain_anchors"]
|
||||||
if anchors:
|
if anchors:
|
||||||
# Override default set by super class.
|
# Override default set by super class.
|
||||||
@ -48,17 +51,17 @@ class Loader(LocLoader):
|
|||||||
self.get_event()
|
self.get_event()
|
||||||
return node
|
return node
|
||||||
|
|
||||||
def _with_stream(self, stream, source_path):
|
def _with_stream(self, stream, source_path, source_dir):
|
||||||
return Loader(stream, self.jjb_config, source_path, self.anchors)
|
return Loader(stream, self.jjb_config, source_path, source_dir, self.anchors)
|
||||||
|
|
||||||
def load_fp(self, fp):
|
def load_fp(self, fp):
|
||||||
return self.load(fp)
|
return self.load(fp)
|
||||||
|
|
||||||
def load_path(self, path):
|
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):
|
def load(self, stream, source_path=None, source_dir=None):
|
||||||
loader = self._with_stream(stream, source_path)
|
loader = self._with_stream(stream, source_path, source_dir)
|
||||||
try:
|
try:
|
||||||
return loader.get_single_data()
|
return loader.get_single_data()
|
||||||
finally:
|
finally:
|
||||||
|
@ -201,7 +201,6 @@ Examples:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import abc
|
import abc
|
||||||
import os.path
|
|
||||||
import logging
|
import logging
|
||||||
import traceback
|
import traceback
|
||||||
import sys
|
import sys
|
||||||
@ -250,9 +249,9 @@ class BaseYamlObject(metaclass=abc.ABCMeta):
|
|||||||
|
|
||||||
def __init__(self, jjb_config, loader, pos):
|
def __init__(self, jjb_config, loader, pos):
|
||||||
self._search_path = jjb_config.yamlparser["include_path"]
|
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.
|
# 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._loader = loader
|
||||||
self._pos = pos
|
self._pos = pos
|
||||||
allow_empty = jjb_config.yamlparser["allow_empty_variables"]
|
allow_empty = jjb_config.yamlparser["allow_empty_variables"]
|
||||||
@ -357,7 +356,9 @@ class J2Yaml(J2Template):
|
|||||||
|
|
||||||
def expand(self, expander, params):
|
def expand(self, expander, params):
|
||||||
text = self._render(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:
|
try:
|
||||||
return expander.expand(data, params)
|
return expander.expand(data, params)
|
||||||
except JenkinsJobsException as x:
|
except JenkinsJobsException as x:
|
||||||
|
@ -16,6 +16,6 @@ cases = [
|
|||||||
@pytest.mark.parametrize("format,expected_used_params", cases)
|
@pytest.mark.parametrize("format,expected_used_params", cases)
|
||||||
def test_jinja2_required_params(format, expected_used_params):
|
def test_jinja2_required_params(format, expected_used_params):
|
||||||
config = JJBConfig()
|
config = JJBConfig()
|
||||||
loader = Mock(source_path=None)
|
loader = Mock(source_dir=None)
|
||||||
template = J2String(config, loader, pos=None, template_text=format)
|
template = J2String(config, loader, pos=None, template_text=format)
|
||||||
assert template.required_params == set(expected_used_params)
|
assert template.required_params == set(expected_used_params)
|
||||||
|
@ -40,6 +40,7 @@ def read_input(scenario, jjb_config):
|
|||||||
scenario.in_path.read_text(),
|
scenario.in_path.read_text(),
|
||||||
jjb_config=jjb_config,
|
jjb_config=jjb_config,
|
||||||
source_path=scenario.in_path,
|
source_path=scenario.in_path,
|
||||||
|
source_dir=scenario.in_path.parent,
|
||||||
)
|
)
|
||||||
return loader.get_single_data()
|
return loader.get_single_data()
|
||||||
|
|
||||||
|
@ -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:
|
include_missing_path_in_j2_yaml.yaml:5:15: In expanded !j2-yaml:
|
||||||
builders: !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
|
!include: missing-file.sh
|
||||||
^
|
^
|
||||||
|
Loading…
x
Reference in New Issue
Block a user