|
| 1 | +"""Testing file path evaluation when using import_tasks / include_tasks.""" |
| 2 | +import textwrap |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from ansiblelint.runner import Runner |
| 7 | + |
| 8 | +LAYOUT_IMPORTS = { |
| 9 | + 'main.yml': textwrap.dedent( |
| 10 | + """\ |
| 11 | + --- |
| 12 | + - hosts: target |
| 13 | + gather_facts: false |
| 14 | + tasks: |
| 15 | + - name: from main import task 1 |
| 16 | + import_tasks: tasks/task_1.yml |
| 17 | + """ |
| 18 | + ), |
| 19 | + 'tasks/task_1.yml': textwrap.dedent( |
| 20 | + """\ |
| 21 | + --- |
| 22 | + - name: from task 1 import task 2 |
| 23 | + import_tasks: tasks/task_2.yml |
| 24 | + """ |
| 25 | + ), |
| 26 | + 'tasks/task_2.yml': textwrap.dedent( |
| 27 | + """\ |
| 28 | + --- |
| 29 | + - name: from task 2 import subtask 1 |
| 30 | + import_tasks: tasks/subtasks/subtask_1.yml |
| 31 | + """ |
| 32 | + ), |
| 33 | + 'tasks/subtasks/subtask_1.yml': textwrap.dedent( |
| 34 | + """\ |
| 35 | + --- |
| 36 | + - name: from subtask 1 import subtask 2 |
| 37 | + import_tasks: tasks/subtasks/subtask_2.yml |
| 38 | + """ |
| 39 | + ), |
| 40 | + 'tasks/subtasks/subtask_2.yml': textwrap.dedent( |
| 41 | + """\ |
| 42 | + --- |
| 43 | + - name: from subtask 2 do something |
| 44 | + debug: |
| 45 | + msg: | |
| 46 | + Something... |
| 47 | + """ |
| 48 | + ), |
| 49 | +} |
| 50 | + |
| 51 | +LAYOUT_INCLUDES = { |
| 52 | + 'main.yml': textwrap.dedent( |
| 53 | + """\ |
| 54 | + --- |
| 55 | + - hosts: target |
| 56 | + gather_facts: false |
| 57 | + tasks: |
| 58 | + - name: from main import task 1 |
| 59 | + include_tasks: tasks/task_1.yml |
| 60 | + """ |
| 61 | + ), |
| 62 | + 'tasks/task_1.yml': textwrap.dedent( |
| 63 | + """\ |
| 64 | + --- |
| 65 | + - name: from task 1 import task 2 |
| 66 | + include_tasks: tasks/task_2.yml |
| 67 | + """ |
| 68 | + ), |
| 69 | + 'tasks/task_2.yml': textwrap.dedent( |
| 70 | + """\ |
| 71 | + --- |
| 72 | + - name: from task 2 import subtask 1 |
| 73 | + include_tasks: tasks/subtasks/subtask_1.yml |
| 74 | + """ |
| 75 | + ), |
| 76 | + 'tasks/subtasks/subtask_1.yml': textwrap.dedent( |
| 77 | + """\ |
| 78 | + --- |
| 79 | + - name: from subtask 1 import subtask 2 |
| 80 | + include_tasks: tasks/subtasks/subtask_2.yml |
| 81 | + """ |
| 82 | + ), |
| 83 | + 'tasks/subtasks/subtask_2.yml': textwrap.dedent( |
| 84 | + """\ |
| 85 | + --- |
| 86 | + - name: from subtask 2 do something |
| 87 | + debug: |
| 88 | + msg: | |
| 89 | + Something... |
| 90 | + """ |
| 91 | + ), |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.parametrize( |
| 96 | + 'ansible_project_layout', |
| 97 | + ( |
| 98 | + pytest.param(LAYOUT_IMPORTS, id='using only import_tasks'), |
| 99 | + pytest.param(LAYOUT_INCLUDES, id='using only include_tasks'), |
| 100 | + ), |
| 101 | +) |
| 102 | +@pytest.mark.xfail( |
| 103 | + reason='https://github.com/ansible-community/ansible-lint/issues/1446' |
| 104 | +) |
| 105 | +def test_file_path_evaluation( |
| 106 | + tmp_path, default_rules_collection, ansible_project_layout |
| 107 | +): |
| 108 | + """Test file path evaluation when using import_tasks / include_tasks in the project. |
| 109 | +
|
| 110 | + Usage of import_tasks / include_tasks may introduce false positive load-failure due |
| 111 | + to incorrect file path evaluation. |
| 112 | + """ |
| 113 | + for file_path, file_content in ansible_project_layout.items(): |
| 114 | + full_path = tmp_path / file_path |
| 115 | + full_path.parent.mkdir(parents=True, exist_ok=True) |
| 116 | + full_path.write_text(file_content) |
| 117 | + |
| 118 | + result = Runner(str(tmp_path), rules=default_rules_collection).run() |
| 119 | + |
| 120 | + assert not result |
0 commit comments