mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-16 09:55:26 -07:00
template: fix KeyError: 'undefined variable: 0 (#27972)
* template: fix KeyError: 'undefined variable: 0
For compatibility with the Context.get_all() implementation
in jinja 2.9, make AnsibleJ2Vars implement collections.Mapping.
Also, make AnsibleJ2Template.newcontext() handle dict type
for the 'vars' parameter.
See: d67f0fd4cc
Fixes: https://github.com/ansible/ansible/issues/20494
* add units/template/test_vars
* intg tests for jinja-2.9 issues like 20494
test cases here are based on
https://github.com/ansible/ansible/issues/20494#issue-202108318
This commit is contained in:
parent
49aa64a5b8
commit
501fc7a248
13 changed files with 160 additions and 2 deletions
|
@ -18,3 +18,64 @@
|
|||
# Make coding more python3-ish
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import MagicMock
|
||||
|
||||
from ansible.template.vars import AnsibleJ2Vars
|
||||
|
||||
|
||||
class TestVars(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.mock_templar = MagicMock(name='mock_templar')
|
||||
|
||||
def test(self):
|
||||
ajvars = AnsibleJ2Vars(None, None)
|
||||
print(ajvars)
|
||||
|
||||
def test_globals_empty_2_8(self):
|
||||
ajvars = AnsibleJ2Vars(self.mock_templar, {})
|
||||
res28 = self._dict_jinja28(ajvars)
|
||||
self.assertIsInstance(res28, dict)
|
||||
|
||||
def test_globals_empty_2_9(self):
|
||||
ajvars = AnsibleJ2Vars(self.mock_templar, {})
|
||||
res29 = self._dict_jinja29(ajvars)
|
||||
self.assertIsInstance(res29, dict)
|
||||
|
||||
def _assert_globals(self, res):
|
||||
self.assertIsInstance(res, dict)
|
||||
self.assertIn('foo', res)
|
||||
self.assertEqual(res['foo'], 'bar')
|
||||
|
||||
def test_globals_2_8(self):
|
||||
ajvars = AnsibleJ2Vars(self.mock_templar, {'foo': 'bar', 'blip': [1, 2, 3]})
|
||||
res28 = self._dict_jinja28(ajvars)
|
||||
self._assert_globals(res28)
|
||||
|
||||
def test_globals_2_9(self):
|
||||
ajvars = AnsibleJ2Vars(self.mock_templar, {'foo': 'bar', 'blip': [1, 2, 3]})
|
||||
res29 = self._dict_jinja29(ajvars)
|
||||
self._assert_globals(res29)
|
||||
|
||||
def _dicts(self, ajvars):
|
||||
print(ajvars)
|
||||
res28 = self._dict_jinja28(ajvars)
|
||||
res29 = self._dict_jinja29(ajvars)
|
||||
# res28_other = self._dict_jinja28(ajvars, {'other_key': 'other_value'})
|
||||
# other = {'other_key': 'other_value'}
|
||||
# res29_other = self._dict_jinja29(ajvars, *other)
|
||||
print('res28: %s' % res28)
|
||||
print('res29: %s' % res29)
|
||||
# print('res28_other: %s' % res28_other)
|
||||
# print('res29_other: %s' % res29_other)
|
||||
# return (res28, res29, res28_other, res29_other)
|
||||
# assert ajvars == res28
|
||||
# assert ajvars == res29
|
||||
return (res28, res29)
|
||||
|
||||
def _dict_jinja28(self, *args, **kwargs):
|
||||
return dict(*args, **kwargs)
|
||||
|
||||
def _dict_jinja29(self, the_vars):
|
||||
return dict(the_vars)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue