Skip to content

Commit

Permalink
Fix literal-bool test with when sequences (#1332)
Browse files Browse the repository at this point in the history
Fixes exception with when conditions using sequences.
  • Loading branch information
ssbarnea committed Feb 10, 2021
1 parent bbccd25 commit 712a18a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/ansiblelint/rules/ComparisonToLiteralBoolRule.py
Expand Up @@ -24,6 +24,16 @@ class ComparisonToLiteralBoolRule(AnsibleLintRule):
def matchtask(self, task: Dict[str, Any]) -> Union[bool, str]:
for k, v in nested_items(task):
if k == 'when':
if self.literal_bool_compare.search(v):
return True
if isinstance(v, str):
if self.literal_bool_compare.search(v):
return True
elif isinstance(v, bool):
pass
else:
for item in v:
if isinstance(item, str) and self.literal_bool_compare.search(
item
):
return True

return False
15 changes: 14 additions & 1 deletion test/TestComparisonToLiteralBool.py
Expand Up @@ -10,6 +10,13 @@
debug:
msg: test
when: my_var
- name: another example task
debug:
msg: test
when:
- 1 + 1 == 2
- true
'''

PASS_WHEN_NOT_FALSE = '''
Expand Down Expand Up @@ -38,6 +45,12 @@
debug:
msg: test
when: my_var == false
- name: another example task
debug:
msg: test
when:
- my_var == false
'''


Expand Down Expand Up @@ -66,4 +79,4 @@ def test_literal_true(self):

def test_literal_false(self):
results = self.runner.run_role_tasks_main(FAIL_LITERAL_FALSE)
assert len(results) == 1, results
assert len(results) == 2, results

0 comments on commit 712a18a

Please sign in to comment.