Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix literal-compare test with when sequences #1332

Merged
merged 2 commits into from Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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