summaryrefslogtreecommitdiffstats
path: root/src/ansiblelint/rules/empty_string_compare.py
blob: ae42ceb93b74100b553c561a098c7eed7a1e30ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""Implementation of empty-string-compare rule."""
# Copyright (c) 2016, Will Thames and contributors
# Copyright (c) 2018, Ansible Project

from __future__ import annotations

import re
import sys
from typing import TYPE_CHECKING, Any

from ansiblelint.rules import AnsibleLintRule
from ansiblelint.yaml_utils import nested_items_path

if TYPE_CHECKING:
    from ansiblelint.file_utils import Lintable


class ComparisonToEmptyStringRule(AnsibleLintRule):
    """Don't compare to empty string."""

    id = "empty-string-compare"
    description = (
        'Use ``when: var|length > 0`` rather than ``when: var != ""`` (or '
        'conversely ``when: var|length == 0`` rather than ``when: var == ""``)'
    )
    severity = "HIGH"
    tags = ["idiom", "opt-in"]
    version_added = "v4.0.0"

    empty_string_compare = re.compile("[=!]= ?(\"{2}|'{2})")

    def matchtask(
        self, task: dict[str, Any], file: Lintable | None = None
    ) -> bool | str:
        for k, v, _ in nested_items_path(task):
            if k == "when":
                if isinstance(v, str):
                    if self.empty_string_compare.search(v):
                        return True
                elif isinstance(v, bool):
                    pass
                else:
                    for item in v:
                        if isinstance(item, str) and self.empty_string_compare.search(
                            item
                        ):
                            return True

        return False


# testing code to be loaded only with pytest or when executed the rule file
if "pytest" in sys.modules:
    import pytest

    from ansiblelint.testing import RunFromText  # pylint: disable=ungrouped-imports

    SUCCESS_PLAY = """
- hosts: all
  tasks:
    - name: Shut down
      shell: |
        /sbin/shutdown -t now
        echo $var == ""
      when: ansible_os_family
    - name: Shut down
      shell: |
        /sbin/shutdown -t now
        echo $var == ""
      when: [ansible_os_family]
"""

    FAIL_PLAY = """
- hosts: all
  tasks:
  - name: Shut down
    command: /sbin/shutdown -t now
    when: ansible_os_family == ""
  - name: Shut down
    command: /sbin/shutdown -t now
    when: ansible_os_family !=""
  - name: Shut down
    command: /sbin/shutdown -t now
    when: False
"""

    @pytest.mark.parametrize(
        "rule_runner", (ComparisonToEmptyStringRule,), indirect=["rule_runner"]
    )
    def test_rule_empty_string_compare_fail(rule_runner: RunFromText) -> None:
        """Test rule matches."""
        results = rule_runner.run_playbook(FAIL_PLAY)
        assert len(results) == 2
        for result in results:
            assert result.message == ComparisonToEmptyStringRule().shortdesc

    @pytest.mark.parametrize(
        "rule_runner", (ComparisonToEmptyStringRule,), indirect=["rule_runner"]
    )
    def test_rule_empty_string_compare_pass(rule_runner: RunFromText) -> None:
        """Test rule matches."""
        results = rule_runner.run_playbook(SUCCESS_PLAY)
        assert len(results) == 0, results