Skip to content

Commit

Permalink
Merge pull request #56341 from dwoz/issue-56119
Browse files Browse the repository at this point in the history
Revert "Don't remove one directory level from slspath"
  • Loading branch information
dwoz authored Mar 11, 2020
2 parents 4f9813a + afe6e84 commit 84c6070
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
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']

0 comments on commit 84c6070

Please sign in to comment.