1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

conditional: fix direct boolean "shortcut" (#47941)

* conditional: fix direct boolean "shortcut"

* Add unit tests
This commit is contained in:
Martin Krizek 2018-11-07 21:33:54 +01:00 committed by GitHub
parent 3a4ee965f5
commit 421d67f1ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 4 deletions

View file

@ -92,10 +92,6 @@ class Conditional:
ds = getattr(self, '_ds')
try:
# this allows for direct boolean assignments to conditionals "when: False"
if isinstance(self.when, bool):
return self.when
for conditional in self.when:
if not self._check_conditional(conditional, templar, all_vars):
return False
@ -117,6 +113,10 @@ class Conditional:
if conditional is None or conditional == '':
return True
# this allows for direct boolean assignments to conditionals "when: False"
if isinstance(conditional, bool):
return conditional
if templar.is_template(conditional):
display.warning('when statements should not include jinja2 '
'templating delimiters such as {{ }} or {%% %%}. '

View file

@ -1,6 +1,7 @@
from units.compat import unittest
from units.mock.loader import DictDataLoader
from units.compat.mock import MagicMock
from ansible.plugins.strategy import SharedPluginLoaderObj
from ansible.template import Templar
@ -33,6 +34,20 @@ class TestConditional(unittest.TestCase):
ret = self._eval_con(when, {})
self.assertTrue(ret)
def test_true_boolean(self):
self.cond.when = [True]
m = MagicMock()
ret = self.cond.evaluate_conditional(m, {})
self.assertTrue(ret)
self.assertFalse(m.is_template.called)
def test_false_boolean(self):
self.cond.when = [False]
m = MagicMock()
ret = self.cond.evaluate_conditional(m, {})
self.assertFalse(ret)
self.assertFalse(m.is_template.called)
def test_undefined(self):
when = [u"{{ some_undefined_thing }}"]
self.assertRaisesRegexp(errors.AnsibleError, "The conditional check '{{ some_undefined_thing }}' failed",