summaryrefslogtreecommitdiffstats
path: root/test/test_file_path_evaluation.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 16:04:56 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 16:04:56 +0000
commitd964cec5e6aa807b75c7a4e7cdc5f11e54b2eda2 (patch)
tree794bc3738a00b5e599f06d1f2f6d79048d87ff8e /test/test_file_path_evaluation.py
parentInitial commit. (diff)
downloadansible-lint-d964cec5e6aa807b75c7a4e7cdc5f11e54b2eda2.tar.xz
ansible-lint-d964cec5e6aa807b75c7a4e7cdc5f11e54b2eda2.zip
Adding upstream version 6.13.1.upstream/6.13.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/test_file_path_evaluation.py')
-rw-r--r--test/test_file_path_evaluation.py126
1 files changed, 126 insertions, 0 deletions
diff --git a/test/test_file_path_evaluation.py b/test/test_file_path_evaluation.py
new file mode 100644
index 0000000..15692e4
--- /dev/null
+++ b/test/test_file_path_evaluation.py
@@ -0,0 +1,126 @@
+"""Testing file path evaluation when using import_tasks / include_tasks."""
+from __future__ import annotations
+
+import textwrap
+from pathlib import Path
+
+import pytest
+
+from ansiblelint.rules import RulesCollection
+from ansiblelint.runner import Runner
+
+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: 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: 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: 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: 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"