Skip to content

Commit

Permalink
Make no-jinja-when also detect list conditions (#2975)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Feb 3, 2023
1 parent 2df8d95 commit dcd680a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 43 deletions.
11 changes: 0 additions & 11 deletions examples/playbooks/jinja2-when-failure.yml

This file was deleted.

9 changes: 0 additions & 9 deletions examples/playbooks/jinja2-when-success.yml

This file was deleted.

21 changes: 21 additions & 0 deletions examples/playbooks/rule-no-jinja-when-fail.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
- name: One
hosts: all
tasks:
- name: Test when with jinja2 # noqa: jinja[spacing]
ansible.builtin.debug:
msg: text
when: "{{ false }}"

- name: Two
hosts: all
roles:
- role: test
when: "{{ '1' = '1' }}"

- name: Three
hosts: all
roles:
- role: test
when:
- "{{ '1' = '1' }}"
17 changes: 17 additions & 0 deletions examples/playbooks/rule-no-jinja-when-pass.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
- name: Test fixture for no-jinja-when
hosts: all
tasks:
- name: Test when
ansible.builtin.debug:
msg: text
when: true
- name: Test when 2
ansible.builtin.debug:
msg: text2
when: 1 = 1
- name: Three
ansible.builtin.debug:
msg: text2
when:
- "false"
33 changes: 33 additions & 0 deletions src/ansiblelint/rules/no_jinja_when.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Implementation of no-jinja-when rule."""
from __future__ import annotations

import sys
from typing import TYPE_CHECKING, Any

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

@staticmethod
def _is_valid(when: str) -> bool:
if isinstance(when, list):
for item in when:
if (
isinstance(item, str)
and item.find("{{") != -1
and item.find("}}") != -1
):
return False
return True
if not isinstance(when, str):
return True
return when.find("{{") == -1 and when.find("}}") == -1
Expand Down Expand Up @@ -53,3 +63,26 @@ def matchtask(
self, task: dict[str, Any], file: Lintable | None = None
) -> bool | str:
return "when" in task and not self._is_valid(task["when"])


if "pytest" in sys.modules:
# Tests for no-jinja-when rule.
from ansiblelint.rules import RulesCollection
from ansiblelint.runner import Runner

def test_file_positive() -> None:
"""Positive test for no-jinja-when."""
collection = RulesCollection()
collection.register(NoFormattingInWhenRule())
success = "examples/playbooks/rule-no-jinja-when-pass.yml"
good_runner = Runner(success, rules=collection)
assert [] == good_runner.run()

def test_file_negative() -> None:
"""Negative test for no-jinja-when."""
collection = RulesCollection()
collection.register(NoFormattingInWhenRule())
failure = "examples/playbooks/rule-no-jinja-when-fail.yml"
bad_runner = Runner(failure, rules=collection)
errs = bad_runner.run()
assert len(errs) == 3
23 changes: 0 additions & 23 deletions test/rules/test_no_jinja_when.py

This file was deleted.

0 comments on commit dcd680a

Please sign in to comment.