summaryrefslogtreecommitdiffstats
path: root/test/test_file_path_evaluation.py
blob: 69f02bb15f387aa0ebb765779ef544f4a5a26f98 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""Testing file path evaluation when using import_tasks / include_tasks."""

from __future__ import annotations

import textwrap
from typing import TYPE_CHECKING

import pytest

from ansiblelint.runner import Runner

if TYPE_CHECKING:
    from pathlib import Path

    from ansiblelint.rules import RulesCollection

LAYOUT_IMPORTS: dict[str, str] = {
    "main.yml": textwrap.dedent(
        """\
        ---
        - name: Fixture
          hosts: target
          gather_facts: false
          tasks:
            - name: From main import task 1
              ansible.builtin.import_tasks: tasks/task_1.yml
        """,
    ),
    "tasks/task_1.yml": textwrap.dedent(
        """\
        ---
        - name: task_1 | From task 1 import task 2
          ansible.builtin.import_tasks: tasks/task_2.yml
        """,
    ),
    "tasks/task_2.yml": textwrap.dedent(
        """\
        ---
        - name: task_2 | From task 2 import subtask 1
          ansible.builtin.import_tasks: tasks/subtasks/subtask_1.yml
        """,
    ),
    "tasks/subtasks/subtask_1.yml": textwrap.dedent(
        """\
        ---
        - name: subtasks | subtask_1 | From subtask 1 import subtask 2
          ansible.builtin.import_tasks: tasks/subtasks/subtask_2.yml
        """,
    ),
    "tasks/subtasks/subtask_2.yml": textwrap.dedent(
        """\
        ---
        - name: subtasks | subtask_2 | From subtask 2 do something
          debug:  # <-- expected to raise fqcn[action-core]
            msg: |
              Something...
        """,
    ),
}

LAYOUT_INCLUDES: dict[str, str] = {
    "main.yml": textwrap.dedent(
        """\
        ---
        - name: Fixture
          hosts: target
          gather_facts: false
          tasks:
            - name: From main import task 1
              ansible.builtin.include_tasks: tasks/task_1.yml
        """,
    ),
    "tasks/task_1.yml": textwrap.dedent(
        """\
        ---
        - name: task_1 | From task 1 import task 2
          ansible.builtin.include_tasks: tasks/task_2.yml
        """,
    ),
    "tasks/task_2.yml": textwrap.dedent(
        """\
        ---
        - name: task_2 | From task 2 import subtask 1
          ansible.builtin.include_tasks: tasks/subtasks/subtask_1.yml
        """,
    ),
    "tasks/subtasks/subtask_1.yml": textwrap.dedent(
        """\
        ---
        - name: subtasks | subtask_1 | From subtask 1 import subtask 2
          ansible.builtin.include_tasks: tasks/subtasks/subtask_2.yml
        """,
    ),
    "tasks/subtasks/subtask_2.yml": textwrap.dedent(
        """\
        ---
        - name: subtasks | subtask_2 | From subtask 2 do something
          debug:  # <-- expected to raise fqcn[action-core]
            msg: |
              Something...
        """,
    ),
}


@pytest.mark.parametrize(
    "ansible_project_layout",
    (
        pytest.param(LAYOUT_IMPORTS, id="using-only-import_tasks"),
        pytest.param(LAYOUT_INCLUDES, id="using-only-include_tasks"),
    ),
)
def test_file_path_evaluation(
    tmp_path: Path,
    default_rules_collection: RulesCollection,
    ansible_project_layout: dict[str, str],
) -> None:
    """Test file path evaluation when using import_tasks / include_tasks in the project.

    The goal of this test is to verify our ability to find errors from within
    nested includes.
    """
    for file_path, file_content in ansible_project_layout.items():
        full_path = tmp_path / file_path
        full_path.parent.mkdir(parents=True, exist_ok=True)
        full_path.write_text(file_content)

    result = Runner(str(tmp_path), rules=default_rules_collection).run()

    assert len(result) == 1
    assert result[0].rule.id == "fqcn"