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

Revert "Don't remove one directory level from slspath" #56341

Merged
merged 5 commits into from
Mar 11, 2020
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
9 changes: 5 additions & 4 deletions doc/ref/states/vars.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ include option.
slspath
=======

The `slspath` variable contains the path to the current sls file. The value
of `slspath` in files referenced in the current sls depends on the reference
method. For jinja includes `slspath` is the path to the current file. For
salt includes `slspath` is the path to the included file.
The `slspath` variable contains the path to the directory of the current sls
file. The value of `slspath` in files referenced in the current sls depends on
the reference method. For jinja includes `slspath` is the path to the current
directory of the file. For salt includes `slspath` is the path to the directory
of the included file.

.. code-block:: jinja

Expand Down
2 changes: 2 additions & 0 deletions salt/utils/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ def render_tmpl(tmplsrc,
slspath = context['sls'].replace('.', '/')
if tmplpath is not None:
context['tplpath'] = tmplpath
if not tmplpath.lower().replace('\\', '/').endswith('/init.sls'):
slspath = os.path.dirname(slspath)
template = tmplpath.replace('\\', '/')
i = template.rfind(slspath.replace('.', '/'))
if i != -1:
Expand Down
46 changes: 46 additions & 0 deletions tests/unit/utils/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

# Import python libs
from __future__ import absolute_import, print_function, unicode_literals
import os
import sys
import logging

# Import Salt libs
import salt.utils.templates
import salt.utils.files

# Import Salt Testing Libs
from tests.support.helpers import with_tempdir
from tests.support.unit import TestCase, skipIf

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -181,3 +184,46 @@ def test_render_cheetah_variable(self):
ctx['var'] = 'OK'
res = salt.utils.templates.render_cheetah_tmpl(tmpl, ctx)
self.assertEqual(res.strip(), 'OK')


class MockRender(object):
def __call__(self, tplstr, context, tmplpath=None):
self.tplstr = tplstr
self.context = context
self.tmplpath = tmplpath
return tplstr


class WrapRenderTestCase(TestCase):

@with_tempdir()
def test_wrap_issue_56119_a(self, tempdir):
slsfile = os.path.join(tempdir, 'foo')
with salt.utils.files.fopen(slsfile, 'w') as fp:
fp.write('{{ slspath }}')
context = {'opts': {}, 'saltenv': 'base', 'sls': 'foo.bar'}
render = MockRender()
wrapped = salt.utils.templates.wrap_tmpl_func(render)
res = wrapped(
slsfile,
context=context,
tmplpath='/tmp/foo/bar/init.sls'
)
assert render.context['slspath'] == 'foo/bar', render.context['slspath']
assert render.context['tpldir'] == 'foo/bar', render.context['tpldir']

@with_tempdir()
def test_wrap_issue_56119_b(self, tempdir):
slsfile = os.path.join(tempdir, 'foo')
with salt.utils.files.fopen(slsfile, 'w') as fp:
fp.write('{{ slspath }}')
context = {'opts': {}, 'saltenv': 'base', 'sls': 'foo.bar.bang'}
render = MockRender()
wrapped = salt.utils.templates.wrap_tmpl_func(render)
res = wrapped(
slsfile,
context=context,
tmplpath='/tmp/foo/bar/bang.sls'
)
assert render.context['slspath'] == 'foo/bar', render.context['slspath']
assert render.context['tpldir'] == 'foo/bar', render.context['tpldir']