Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix relative imports in args rule #4216

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ taskincludes
taskshandlers
templatevars
templating
testcollection
testinfra
testmon
testns
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
env:
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 883
PYTEST_REQPASS: 884
steps:
- uses: actions/checkout@v4
with:
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ src/ansiblelint/_version.py
test/eco/CODENOTIFY.html
test/eco
test/schemas/node_modules
test/local-content
.envrc
collections
# collections
# !/collections
site
_readthedocs
*.tmp.*
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ repos:
- wcmatch
exclude: >
(?x)^(
collections/.*|
test/local-content/.*|
plugins/.*
)$
Expand Down
2 changes: 1 addition & 1 deletion ansible.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[defaults]
collections_path = examples/playbooks/collections
collections_path = collections:examples/playbooks/collections
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ansible Collection - local.testcollection

Documentation for the collection.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
namespace: local
name: testcollection
version: 1.0.0
readme: README.md
authors:
- your name <[email protected]>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""module_utils package."""

# Some value that can be imported from a module
MY_STRING: str = "foo"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""modules package."""
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""module_with_relative_import module."""

from ansible.module_utils.basic import AnsibleModule

# pylint: disable=E0402
from ..module_utils import MY_STRING # noqa: TID252 # type: ignore[import-untyped]

DOCUMENTATION = r"""
options:
name:
required: True
"""


def main() -> AnsibleModule:
"""The main function."""
return AnsibleModule(
argument_spec={
"name": {"required": True, "aliases": [MY_STRING]},
},
)


if __name__ == "__main__":
main()
6 changes: 6 additions & 0 deletions examples/playbooks/module_relative_import.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- name: Module relative import
hosts: localhost
tasks:
- name: Module with relative import
local.testcollection.module_with_relative_import: {}
2 changes: 1 addition & 1 deletion src/ansiblelint/rules/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def matchtask(
CustomAnsibleModule,
):
spec = importlib.util.spec_from_file_location(
name=loaded_module.resolved_fqcn,
name=loaded_module.plugin_resolved_name,
location=loaded_module.plugin_resolved_path,
)
if not spec:
Expand Down
19 changes: 19 additions & 0 deletions test/rules/test_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Tests for args rule."""

from ansiblelint.file_utils import Lintable
from ansiblelint.rules import RulesCollection
from ansiblelint.runner import Runner


def test_args_module_relative_import(default_rules_collection: RulesCollection) -> None:
"""Validate args check of a module with a relative import."""
lintable = Lintable(
"examples/playbooks/module_relative_import.yml",
kind="playbook",
)
result = Runner(lintable, rules=default_rules_collection).run()
assert len(result) == 1, result
assert result[0].lineno == 5
assert result[0].filename == "examples/playbooks/module_relative_import.yml"
assert result[0].tag == "args[module]"
assert result[0].message == "missing required arguments: name"
Loading