summaryrefslogtreecommitdiffstats
path: root/src/ansiblelint/rules/empty_string_compare.py
blob: 5c7cafc88d1bb7f9489ff11b9c6e0eb04d744c7a (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
"""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

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

if TYPE_CHECKING:
    from ansiblelint.file_utils import Lintable
    from ansiblelint.utils import Task


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: Task,
        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:
    from ansiblelint.rules import RulesCollection  # pylint: disable=ungrouped-imports
    from ansiblelint.runner import Runner  # pylint: disable=ungrouped-imports

    def test_rule_empty_string_compare_fail() -> None:
        """Test rule matches."""
        rules = RulesCollection()
        rules.register(ComparisonToEmptyStringRule())
        results = Runner(
            "examples/playbooks/rule-empty-string-compare-fail.yml",
            rules=rules,
        ).run()
        assert len(results) == 3
        for result in results:
            assert result.message == ComparisonToEmptyStringRule().shortdesc

    def test_rule_empty_string_compare_pass() -> None:
        """Test rule matches."""
        rules = RulesCollection()
        rules.register(ComparisonToEmptyStringRule())
        results = Runner(
            "examples/playbooks/rule-empty-string-compare-pass.yml",
            rules=rules,
        ).run()
        assert len(results) == 0, results