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

* conditional: fix direct boolean "shortcut"

* Add unit tests
pull/48370/head
Martin Krizek 6 years ago committed by GitHub
parent 3a4ee965f5
commit 421d67f1ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 {%% %%}. '

@ -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",

Loading…
Cancel
Save