Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

templates: move the globals up to the Environment (Jinja2 3.0.0) #418

Merged
merged 2 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/55159.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Jinja renderer resolves wrong relative paths when importing subdirectories
2 changes: 1 addition & 1 deletion salt/utils/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def get_source(self, environment, template):
'Relative path "%s" cannot be resolved without an environment',
template,
)
raise TemplateNotFound
raise TemplateNotFound(template)
base_path = environment.globals["tpldir"]
_template = os.path.normpath("/".join((base_path, _template)))
if _template.split("/", 1)[0] == "..":
Expand Down
2 changes: 1 addition & 1 deletion salt/utils/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,9 @@ def opt_jinja_env_helper(opts, optname):
)
decoded_context[key] = salt.utils.data.decode(value)

jinja_env.globals.update(decoded_context)
try:
template = jinja_env.from_string(tmplstr)
template.globals.update(decoded_context)
output = template.render(**decoded_context)
except jinja2.exceptions.UndefinedError as exc:
trace = traceback.extract_tb(sys.exc_info()[2])
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/utils/test_jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,22 @@ def test_render_with_undefined_variable_unicode(self):
dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
)

def test_relative_include(self):
aplanas marked this conversation as resolved.
Show resolved Hide resolved
template = "{% include './hello_import' %}"
expected = "Hey world !a b !"
filename = os.path.join(self.template_dir, "hello_import")
with salt.utils.files.fopen(filename) as fp_:
out = render_jinja_tmpl(
template,
dict(
opts=self.local_opts,
saltenv="test",
salt=self.local_salt,
tpldir=self.template_dir,
),
)
self.assertEqual(out, expected)


class TestJinjaDefaultOptions(TestCase):
@classmethod
Expand Down