Skip to content

Commit 61ed3ea

Browse files
authored
Recognize import_role and import_role when using fqcn (#2034)
1 parent aa305f7 commit 61ed3ea

File tree

4 files changed

+69
-36
lines changed

4 files changed

+69
-36
lines changed

.github/workflows/tox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ jobs:
156156
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:TOX_PARALLEL_NO_SPINNER
157157
# Number of expected test passes, safety measure for accidental skip of
158158
# tests. Update value if you add/remove tests.
159-
PYTEST_REQPASS: 548
159+
PYTEST_REQPASS: 550
160160

161161
steps:
162162
- name: Activate WSL1

src/ansiblelint/utils.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,14 @@ def _taskshandlers_children(
334334
results.append(children)
335335
continue
336336

337-
if (
338-
"include_role" in task_handler or "import_role" in task_handler
339-
): # lgtm [py/unreachable-statement]
337+
including_commands = (
338+
"import_role",
339+
"ansible.builtin.import_role",
340+
"include_role",
341+
"ansible.builtin.include_role",
342+
)
343+
if any(x in task_handler for x in including_commands):
344+
# lgtm [py/unreachable-statement]
340345
task_handler = normalize_task_v2(task_handler)
341346
_validate_task_handler_action_for_role(task_handler["action"])
342347
results.extend(

test/include-import-role.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

test/test_import_include_role.py

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,76 @@
88
from ansiblelint.rules import RulesCollection
99
from ansiblelint.runner import Runner
1010

11-
ROLE_TASKS_MAIN = """
11+
ROLE_TASKS_MAIN = """\
12+
---
1213
- name: shell instead of command
1314
shell: echo hello world
15+
changed_when: false
1416
"""
1517

16-
ROLE_TASKS_WORLD = """
17-
- command: echo this is a task without a name
18+
ROLE_TASKS_WORLD = """\
19+
---
20+
- debug: msg="this is a task without a name"
1821
"""
1922

20-
PLAY_IMPORT_ROLE = """
23+
PLAY_IMPORT_ROLE = """\
24+
---
2125
- hosts: all
2226
2327
tasks:
24-
- import_role:
28+
- name: some import
29+
import_role:
2530
name: test-role
2631
"""
2732

28-
PLAY_IMPORT_ROLE_INLINE = """
33+
PLAY_IMPORT_ROLE_FQCN = """\
34+
---
2935
- hosts: all
3036
3137
tasks:
32-
- import_role: name=test-role
38+
- name: some import
39+
ansible.builtin.import_role:
40+
name: test-role
41+
"""
42+
43+
PLAY_IMPORT_ROLE_INLINE = """\
44+
---
45+
- hosts: all
46+
47+
tasks:
48+
- name: some import
49+
import_role: name=test-role
3350
"""
3451

35-
PLAY_INCLUDE_ROLE = """
52+
PLAY_INCLUDE_ROLE = """\
53+
---
3654
- hosts: all
3755
3856
tasks:
39-
- include_role:
57+
- name: some import
58+
include_role:
4059
name: test-role
4160
tasks_from: world
4261
"""
4362

44-
PLAY_INCLUDE_ROLE_INLINE = """
63+
PLAY_INCLUDE_ROLE_FQCN = """\
64+
---
4565
- hosts: all
4666
4767
tasks:
48-
- include_role: name=test-role tasks_from=world
68+
- name: some import
69+
ansible.builtin.include_role:
70+
name: test-role
71+
tasks_from: world
72+
"""
73+
74+
PLAY_INCLUDE_ROLE_INLINE = """\
75+
---
76+
- hosts: all
77+
78+
tasks:
79+
- name: some import
80+
include_role: name=test-role tasks_from=world
4981
"""
5082

5183

@@ -67,19 +99,29 @@ def fixture_playbook_path(request: SubRequest, tmp_path: Path) -> str:
6799
(
68100
pytest.param(
69101
PLAY_IMPORT_ROLE,
70-
["only when shell functionality is required"],
102+
["only when shell functionality is required", "All tasks should be named"],
71103
id="IMPORT_ROLE",
72104
),
105+
pytest.param(
106+
PLAY_IMPORT_ROLE_FQCN,
107+
["only when shell functionality is required", "All tasks should be named"],
108+
id="IMPORT_ROLE_FQCN",
109+
),
73110
pytest.param(
74111
PLAY_IMPORT_ROLE_INLINE,
75-
["only when shell functionality is require"],
112+
["only when shell functionality is require", "All tasks should be named"],
76113
id="IMPORT_ROLE_INLINE",
77114
),
78115
pytest.param(
79116
PLAY_INCLUDE_ROLE,
80117
["only when shell functionality is require", "All tasks should be named"],
81118
id="INCLUDE_ROLE",
82119
),
120+
pytest.param(
121+
PLAY_INCLUDE_ROLE_FQCN,
122+
["only when shell functionality is require", "All tasks should be named"],
123+
id="INCLUDE_ROLE_FQCN",
124+
),
83125
pytest.param(
84126
PLAY_INCLUDE_ROLE_INLINE,
85127
["only when shell functionality is require", "All tasks should be named"],
@@ -92,7 +134,11 @@ def test_import_role2(
92134
default_rules_collection: RulesCollection, playbook_path: str, messages: List[str]
93135
) -> None:
94136
"""Test that include_role digs deeper than import_role."""
95-
runner = Runner(playbook_path, rules=default_rules_collection)
137+
runner = Runner(
138+
playbook_path, rules=default_rules_collection, skip_list=["fqcn-builtins"]
139+
)
96140
results = runner.run()
97141
for message in messages:
98142
assert message in str(results)
143+
# Ensure no other unexpected messages are present
144+
assert len(messages) == len(results), results

0 commit comments

Comments
 (0)