From 57aa3b850a0877790170efed328e4a2ff0a51219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20T=C3=B3th?= Date: Tue, 9 Mar 2021 13:45:17 +0100 Subject: [PATCH] Add regression tests for 'import_tasks' and 'include_tasks' (#1445) Regression tests for issue number 1446 --- test/TestFilePathEvaluation.py | 120 +++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 test/TestFilePathEvaluation.py diff --git a/test/TestFilePathEvaluation.py b/test/TestFilePathEvaluation.py new file mode 100644 index 0000000000..68096fef65 --- /dev/null +++ b/test/TestFilePathEvaluation.py @@ -0,0 +1,120 @@ +"""Testing file path evaluation when using import_tasks / include_tasks.""" +import textwrap + +import pytest + +from ansiblelint.runner import Runner + +LAYOUT_IMPORTS = { + 'main.yml': textwrap.dedent( + """\ + --- + - hosts: target + gather_facts: false + tasks: + - name: from main import task 1 + import_tasks: tasks/task_1.yml + """ + ), + 'tasks/task_1.yml': textwrap.dedent( + """\ + --- + - name: from task 1 import task 2 + import_tasks: tasks/task_2.yml + """ + ), + 'tasks/task_2.yml': textwrap.dedent( + """\ + --- + - name: from task 2 import subtask 1 + import_tasks: tasks/subtasks/subtask_1.yml + """ + ), + 'tasks/subtasks/subtask_1.yml': textwrap.dedent( + """\ + --- + - name: from subtask 1 import subtask 2 + import_tasks: tasks/subtasks/subtask_2.yml + """ + ), + 'tasks/subtasks/subtask_2.yml': textwrap.dedent( + """\ + --- + - name: from subtask 2 do something + debug: + msg: | + Something... + """ + ), +} + +LAYOUT_INCLUDES = { + 'main.yml': textwrap.dedent( + """\ + --- + - hosts: target + gather_facts: false + tasks: + - name: from main import task 1 + include_tasks: tasks/task_1.yml + """ + ), + 'tasks/task_1.yml': textwrap.dedent( + """\ + --- + - name: from task 1 import task 2 + include_tasks: tasks/task_2.yml + """ + ), + 'tasks/task_2.yml': textwrap.dedent( + """\ + --- + - name: from task 2 import subtask 1 + include_tasks: tasks/subtasks/subtask_1.yml + """ + ), + 'tasks/subtasks/subtask_1.yml': textwrap.dedent( + """\ + --- + - name: from subtask 1 import subtask 2 + include_tasks: tasks/subtasks/subtask_2.yml + """ + ), + 'tasks/subtasks/subtask_2.yml': textwrap.dedent( + """\ + --- + - name: from subtask 2 do something + debug: + 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'), + ), +) +@pytest.mark.xfail( + reason='https://github.com/ansible-community/ansible-lint/issues/1446' +) +def test_file_path_evaluation( + tmp_path, default_rules_collection, ansible_project_layout +): + """Test file path evaluation when using import_tasks / include_tasks in the project. + + Usage of import_tasks / include_tasks may introduce false positive load-failure due + to incorrect file path evaluation. + """ + 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 not result