Skip to content

Commit dcd680a

Browse files
authored
Make no-jinja-when also detect list conditions (#2975)
1 parent 2df8d95 commit dcd680a

File tree

6 files changed

+71
-43
lines changed

6 files changed

+71
-43
lines changed

examples/playbooks/jinja2-when-failure.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

examples/playbooks/jinja2-when-success.yml

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
- name: One
3+
hosts: all
4+
tasks:
5+
- name: Test when with jinja2 # noqa: jinja[spacing]
6+
ansible.builtin.debug:
7+
msg: text
8+
when: "{{ false }}"
9+
10+
- name: Two
11+
hosts: all
12+
roles:
13+
- role: test
14+
when: "{{ '1' = '1' }}"
15+
16+
- name: Three
17+
hosts: all
18+
roles:
19+
- role: test
20+
when:
21+
- "{{ '1' = '1' }}"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
- name: Test fixture for no-jinja-when
3+
hosts: all
4+
tasks:
5+
- name: Test when
6+
ansible.builtin.debug:
7+
msg: text
8+
when: true
9+
- name: Test when 2
10+
ansible.builtin.debug:
11+
msg: text2
12+
when: 1 = 1
13+
- name: Three
14+
ansible.builtin.debug:
15+
msg: text2
16+
when:
17+
- "false"

src/ansiblelint/rules/no_jinja_when.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Implementation of no-jinja-when rule."""
22
from __future__ import annotations
33

4+
import sys
45
from typing import TYPE_CHECKING, Any
56

67
from ansiblelint.constants import LINE_NUMBER_KEY
@@ -24,6 +25,15 @@ class NoFormattingInWhenRule(AnsibleLintRule):
2425

2526
@staticmethod
2627
def _is_valid(when: str) -> bool:
28+
if isinstance(when, list):
29+
for item in when:
30+
if (
31+
isinstance(item, str)
32+
and item.find("{{") != -1
33+
and item.find("}}") != -1
34+
):
35+
return False
36+
return True
2737
if not isinstance(when, str):
2838
return True
2939
return when.find("{{") == -1 and when.find("}}") == -1
@@ -53,3 +63,26 @@ def matchtask(
5363
self, task: dict[str, Any], file: Lintable | None = None
5464
) -> bool | str:
5565
return "when" in task and not self._is_valid(task["when"])
66+
67+
68+
if "pytest" in sys.modules:
69+
# Tests for no-jinja-when rule.
70+
from ansiblelint.rules import RulesCollection
71+
from ansiblelint.runner import Runner
72+
73+
def test_file_positive() -> None:
74+
"""Positive test for no-jinja-when."""
75+
collection = RulesCollection()
76+
collection.register(NoFormattingInWhenRule())
77+
success = "examples/playbooks/rule-no-jinja-when-pass.yml"
78+
good_runner = Runner(success, rules=collection)
79+
assert [] == good_runner.run()
80+
81+
def test_file_negative() -> None:
82+
"""Negative test for no-jinja-when."""
83+
collection = RulesCollection()
84+
collection.register(NoFormattingInWhenRule())
85+
failure = "examples/playbooks/rule-no-jinja-when-fail.yml"
86+
bad_runner = Runner(failure, rules=collection)
87+
errs = bad_runner.run()
88+
assert len(errs) == 3

test/rules/test_no_jinja_when.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)