summaryrefslogtreecommitdiffstats
path: root/test/rules/test_literal_compare.py
blob: 6953cb1e054177fc7de7851c20cc60aafeac9b79 (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
"""Tests for literal-compare rule."""
import pytest

from ansiblelint.rules import RulesCollection
from ansiblelint.rules.literal_compare import ComparisonToLiteralBoolRule
from ansiblelint.testing import RunFromText

PASS_WHEN = """
- name: Example task
  debug:
    msg: test
  when: my_var

- name: Another example task
  debug:
    msg: test
  when:
    - 1 + 1 == 2
    - true
"""

PASS_WHEN_NOT_FALSE = """
- name: Example task
  debug:
    msg: test
  when: not my_var
"""

PASS_WHEN_NOT_NULL = """
- name: Example task
  debug:
    msg: test
  when: my_var not None
"""

FAIL_LITERAL_TRUE = """
- name: Example task
  debug:
    msg: test
  when: my_var == True
"""

FAIL_LITERAL_FALSE = """
- name: Example task
  debug:
    msg: test
  when: my_var == false

- name: Another example task
  debug:
    msg: test
  when:
    - my_var == false
"""


@pytest.mark.parametrize(
    ("input_str", "found_errors"),
    (
        pytest.param(
            PASS_WHEN,
            0,
            id="pass_when",
        ),
        pytest.param(
            PASS_WHEN_NOT_FALSE,
            0,
            id="when_not_false",
        ),
        pytest.param(
            PASS_WHEN_NOT_NULL,
            0,
            id="when_not_null",
        ),
        pytest.param(
            FAIL_LITERAL_TRUE,
            1,
            id="literal_true",
        ),
        pytest.param(
            FAIL_LITERAL_FALSE,
            2,
            id="literal_false",
        ),
    ),
)
def test_literal_compare(input_str: str, found_errors: int) -> None:
    """Test literal-compare."""
    collection = RulesCollection()
    collection.register(ComparisonToLiteralBoolRule())
    runner = RunFromText(collection)
    results = runner.run_role_tasks_main(input_str)
    assert len(results) == found_errors