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:
parent
3a4ee965f5
commit
421d67f1ee
2 changed files with 19 additions and 4 deletions
|
@ -92,10 +92,6 @@ class Conditional:
|
||||||
ds = getattr(self, '_ds')
|
ds = getattr(self, '_ds')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# this allows for direct boolean assignments to conditionals "when: False"
|
|
||||||
if isinstance(self.when, bool):
|
|
||||||
return self.when
|
|
||||||
|
|
||||||
for conditional in self.when:
|
for conditional in self.when:
|
||||||
if not self._check_conditional(conditional, templar, all_vars):
|
if not self._check_conditional(conditional, templar, all_vars):
|
||||||
return False
|
return False
|
||||||
|
@ -117,6 +113,10 @@ class Conditional:
|
||||||
if conditional is None or conditional == '':
|
if conditional is None or conditional == '':
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
# this allows for direct boolean assignments to conditionals "when: False"
|
||||||
|
if isinstance(conditional, bool):
|
||||||
|
return conditional
|
||||||
|
|
||||||
if templar.is_template(conditional):
|
if templar.is_template(conditional):
|
||||||
display.warning('when statements should not include jinja2 '
|
display.warning('when statements should not include jinja2 '
|
||||||
'templating delimiters such as {{ }} or {%% %%}. '
|
'templating delimiters such as {{ }} or {%% %%}. '
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
|
|
||||||
from units.compat import unittest
|
from units.compat import unittest
|
||||||
from units.mock.loader import DictDataLoader
|
from units.mock.loader import DictDataLoader
|
||||||
|
from units.compat.mock import MagicMock
|
||||||
|
|
||||||
from ansible.plugins.strategy import SharedPluginLoaderObj
|
from ansible.plugins.strategy import SharedPluginLoaderObj
|
||||||
from ansible.template import Templar
|
from ansible.template import Templar
|
||||||
|
@ -33,6 +34,20 @@ class TestConditional(unittest.TestCase):
|
||||||
ret = self._eval_con(when, {})
|
ret = self._eval_con(when, {})
|
||||||
self.assertTrue(ret)
|
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):
|
def test_undefined(self):
|
||||||
when = [u"{{ some_undefined_thing }}"]
|
when = [u"{{ some_undefined_thing }}"]
|
||||||
self.assertRaisesRegexp(errors.AnsibleError, "The conditional check '{{ some_undefined_thing }}' failed",
|
self.assertRaisesRegexp(errors.AnsibleError, "The conditional check '{{ some_undefined_thing }}' failed",
|
||||||
|
|
Loading…
Reference in a new issue