mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-27 04:41:26 -07:00
Fix ${var.$other_var} and add test cases for it
This commit is contained in:
parent
fb27cb45e1
commit
efe83daf19
2 changed files with 25 additions and 3 deletions
|
@ -40,7 +40,7 @@ import pwd
|
||||||
_LISTRE = re.compile(r"(\w+)\[(\d+)\]")
|
_LISTRE = re.compile(r"(\w+)\[(\d+)\]")
|
||||||
|
|
||||||
|
|
||||||
def _varFindLimitSpace(space, part, depth):
|
def _varFindLimitSpace(vars, space, part, depth):
|
||||||
|
|
||||||
# TODO: comments
|
# TODO: comments
|
||||||
|
|
||||||
|
@ -101,7 +101,7 @@ def _varFind(text, vars, depth=0):
|
||||||
pass
|
pass
|
||||||
elif is_complex and text[end] == '.':
|
elif is_complex and text[end] == '.':
|
||||||
if brace_level == part_start[1]:
|
if brace_level == part_start[1]:
|
||||||
space = _varFindLimitSpace(space, text[part_start[0]:end], depth)
|
space = _varFindLimitSpace(vars, space, text[part_start[0]:end], depth)
|
||||||
part_start = (end + 1, brace_level)
|
part_start = (end + 1, brace_level)
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
@ -113,7 +113,7 @@ def _varFind(text, vars, depth=0):
|
||||||
return None
|
return None
|
||||||
if var_end == part_start[0]:
|
if var_end == part_start[0]:
|
||||||
return None
|
return None
|
||||||
space = _varFindLimitSpace(space, text[part_start[0]:var_end], depth)
|
space = _varFindLimitSpace(vars, space, text[part_start[0]:var_end], depth)
|
||||||
return {'replacement': space, 'start': start, 'end': end}
|
return {'replacement': space, 'start': start, 'end': end}
|
||||||
|
|
||||||
def varReplace(raw, vars, depth=0, expand_lists=False):
|
def varReplace(raw, vars, depth=0, expand_lists=False):
|
||||||
|
|
|
@ -238,6 +238,28 @@ class TestUtils(unittest.TestCase):
|
||||||
res = ansible.utils.varReplace(template, vars)
|
res = ansible.utils.varReplace(template, vars)
|
||||||
assert res == 'action $foo'
|
assert res == 'action $foo'
|
||||||
|
|
||||||
|
def test_varReplace_var_part(self):
|
||||||
|
vars = {
|
||||||
|
'foo': {
|
||||||
|
'bar': 'result',
|
||||||
|
},
|
||||||
|
'key': 'bar',
|
||||||
|
}
|
||||||
|
template = 'test ${foo.$key}'
|
||||||
|
res = ansible.utils.varReplace(template, vars)
|
||||||
|
assert res == 'test result'
|
||||||
|
|
||||||
|
def test_varReplace_var_partial_part(self):
|
||||||
|
vars = {
|
||||||
|
'foo': {
|
||||||
|
'barbaz': 'result',
|
||||||
|
},
|
||||||
|
'key': 'bar',
|
||||||
|
}
|
||||||
|
template = 'test ${foo.${key}baz}'
|
||||||
|
res = ansible.utils.varReplace(template, vars)
|
||||||
|
assert res == 'test result'
|
||||||
|
|
||||||
def test_template_varReplace_iterated(self):
|
def test_template_varReplace_iterated(self):
|
||||||
template = 'hello $who'
|
template = 'hello $who'
|
||||||
vars = {
|
vars = {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue