Skip to content

Commit 47b10b2

Browse files
corubbassbarnea
andauthored
Fix relative imports in args rule (#4216)
Co-authored-by: Sorin Sbarnea <[email protected]>
1 parent e97867b commit 47b10b2

File tree

14 files changed

+73
-4
lines changed

14 files changed

+73
-4
lines changed

.config/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ taskincludes
366366
taskshandlers
367367
templatevars
368368
templating
369+
testcollection
369370
testinfra
370371
testmon
371372
testns

.github/workflows/tox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ jobs:
7272
env:
7373
# Number of expected test passes, safety measure for accidental skip of
7474
# tests. Update value if you add/remove tests.
75-
PYTEST_REQPASS: 883
75+
PYTEST_REQPASS: 884
7676
steps:
7777
- uses: actions/checkout@v4
7878
with:

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ src/ansiblelint/_version.py
7272
test/eco/CODENOTIFY.html
7373
test/eco
7474
test/schemas/node_modules
75+
test/local-content
7576
.envrc
76-
collections
77+
# collections
78+
# !/collections
7779
site
7880
_readthedocs
7981
*.tmp.*

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ repos:
173173
- wcmatch
174174
exclude: >
175175
(?x)^(
176+
collections/.*|
176177
test/local-content/.*|
177178
plugins/.*
178179
)$

ansible.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[defaults]
2-
collections_path = examples/playbooks/collections
2+
collections_path = collections:examples/playbooks/collections
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Ansible Collection - local.testcollection
2+
3+
Documentation for the collection.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
namespace: local
3+
name: testcollection
4+
version: 1.0.0
5+
readme: README.md
6+
authors:
7+
- your name <[email protected]>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""module_utils package."""
2+
3+
# Some value that can be imported from a module
4+
MY_STRING: str = "foo"

collections/ansible_collections/local/testcollection/plugins/module_utils/py.typed

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""modules package."""
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""module_with_relative_import module."""
2+
3+
from ansible.module_utils.basic import AnsibleModule
4+
5+
# pylint: disable=E0402
6+
from ..module_utils import MY_STRING # noqa: TID252 # type: ignore[import-untyped]
7+
8+
DOCUMENTATION = r"""
9+
options:
10+
name:
11+
required: True
12+
"""
13+
14+
15+
def main() -> AnsibleModule:
16+
"""The main function."""
17+
return AnsibleModule(
18+
argument_spec={
19+
"name": {"required": True, "aliases": [MY_STRING]},
20+
},
21+
)
22+
23+
24+
if __name__ == "__main__":
25+
main()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- name: Module relative import
3+
hosts: localhost
4+
tasks:
5+
- name: Module with relative import
6+
local.testcollection.module_with_relative_import: {}

src/ansiblelint/rules/args.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def matchtask(
144144
CustomAnsibleModule,
145145
):
146146
spec = importlib.util.spec_from_file_location(
147-
name=loaded_module.resolved_fqcn,
147+
name=loaded_module.plugin_resolved_name,
148148
location=loaded_module.plugin_resolved_path,
149149
)
150150
if not spec:

test/rules/test_args.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Tests for args rule."""
2+
3+
from ansiblelint.file_utils import Lintable
4+
from ansiblelint.rules import RulesCollection
5+
from ansiblelint.runner import Runner
6+
7+
8+
def test_args_module_relative_import(default_rules_collection: RulesCollection) -> None:
9+
"""Validate args check of a module with a relative import."""
10+
lintable = Lintable(
11+
"examples/playbooks/module_relative_import.yml",
12+
kind="playbook",
13+
)
14+
result = Runner(lintable, rules=default_rules_collection).run()
15+
assert len(result) == 1, result
16+
assert result[0].lineno == 5
17+
assert result[0].filename == "examples/playbooks/module_relative_import.yml"
18+
assert result[0].tag == "args[module]"
19+
assert result[0].message == "missing required arguments: name"

0 commit comments

Comments
 (0)