From 3fd61582883093e0e8b1c141fcc8c81647727be6 Mon Sep 17 00:00:00 2001 From: corubba Date: Sat, 8 Jun 2024 01:43:32 +0200 Subject: [PATCH 1/2] Fix relative imports in args rule --- .config/dictionary.txt | 1 + .gitignore | 4 ++- .pre-commit-config.yaml | 1 + ansible.cfg | 2 +- .../local/testcollection/README.md | 3 +++ .../local/testcollection/galaxy.yml | 7 ++++++ .../plugins/module_utils/__init__.py | 4 +++ .../plugins/module_utils/py.typed | 0 .../plugins/modules/__init__.py | 1 + .../modules/module_with_relative_import.py | 25 +++++++++++++++++++ examples/playbooks/module_relative_import.yml | 6 +++++ src/ansiblelint/rules/args.py | 2 +- test/rules/test_args.py | 19 ++++++++++++++ 13 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 collections/ansible_collections/local/testcollection/README.md create mode 100644 collections/ansible_collections/local/testcollection/galaxy.yml create mode 100644 collections/ansible_collections/local/testcollection/plugins/module_utils/__init__.py create mode 100644 collections/ansible_collections/local/testcollection/plugins/module_utils/py.typed create mode 100644 collections/ansible_collections/local/testcollection/plugins/modules/__init__.py create mode 100644 collections/ansible_collections/local/testcollection/plugins/modules/module_with_relative_import.py create mode 100644 examples/playbooks/module_relative_import.yml create mode 100644 test/rules/test_args.py diff --git a/.config/dictionary.txt b/.config/dictionary.txt index 27c781904c..6065fc0875 100644 --- a/.config/dictionary.txt +++ b/.config/dictionary.txt @@ -366,6 +366,7 @@ taskincludes taskshandlers templatevars templating +testcollection testinfra testmon testns diff --git a/.gitignore b/.gitignore index ccfa853eae..4e8710757a 100644 --- a/.gitignore +++ b/.gitignore @@ -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.* diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 230ae43001..fa6297f7ab 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -173,6 +173,7 @@ repos: - wcmatch exclude: > (?x)^( + collections/.*| test/local-content/.*| plugins/.* )$ diff --git a/ansible.cfg b/ansible.cfg index b341954825..3b5eeca7ef 100644 --- a/ansible.cfg +++ b/ansible.cfg @@ -1,2 +1,2 @@ [defaults] -collections_path = examples/playbooks/collections +collections_path = collections:examples/playbooks/collections diff --git a/collections/ansible_collections/local/testcollection/README.md b/collections/ansible_collections/local/testcollection/README.md new file mode 100644 index 0000000000..6c38018b32 --- /dev/null +++ b/collections/ansible_collections/local/testcollection/README.md @@ -0,0 +1,3 @@ +# Ansible Collection - local.testcollection + +Documentation for the collection. diff --git a/collections/ansible_collections/local/testcollection/galaxy.yml b/collections/ansible_collections/local/testcollection/galaxy.yml new file mode 100644 index 0000000000..ac9bb7e42a --- /dev/null +++ b/collections/ansible_collections/local/testcollection/galaxy.yml @@ -0,0 +1,7 @@ +--- +namespace: local +name: testcollection +version: 1.0.0 +readme: README.md +authors: + - your name diff --git a/collections/ansible_collections/local/testcollection/plugins/module_utils/__init__.py b/collections/ansible_collections/local/testcollection/plugins/module_utils/__init__.py new file mode 100644 index 0000000000..9854911518 --- /dev/null +++ b/collections/ansible_collections/local/testcollection/plugins/module_utils/__init__.py @@ -0,0 +1,4 @@ +"""module_utils package.""" + +# Some value that can be imported from a module +MY_STRING: str = "foo" diff --git a/collections/ansible_collections/local/testcollection/plugins/module_utils/py.typed b/collections/ansible_collections/local/testcollection/plugins/module_utils/py.typed new file mode 100644 index 0000000000..e69de29bb2 diff --git a/collections/ansible_collections/local/testcollection/plugins/modules/__init__.py b/collections/ansible_collections/local/testcollection/plugins/modules/__init__.py new file mode 100644 index 0000000000..0d3a56a8c0 --- /dev/null +++ b/collections/ansible_collections/local/testcollection/plugins/modules/__init__.py @@ -0,0 +1 @@ +"""modules package.""" diff --git a/collections/ansible_collections/local/testcollection/plugins/modules/module_with_relative_import.py b/collections/ansible_collections/local/testcollection/plugins/modules/module_with_relative_import.py new file mode 100644 index 0000000000..147a8d3109 --- /dev/null +++ b/collections/ansible_collections/local/testcollection/plugins/modules/module_with_relative_import.py @@ -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() diff --git a/examples/playbooks/module_relative_import.yml b/examples/playbooks/module_relative_import.yml new file mode 100644 index 0000000000..88579667e0 --- /dev/null +++ b/examples/playbooks/module_relative_import.yml @@ -0,0 +1,6 @@ +--- +- name: Module relative import + hosts: localhost + tasks: + - name: Module with relative import + local.testcollection.module_with_relative_import: {} diff --git a/src/ansiblelint/rules/args.py b/src/ansiblelint/rules/args.py index 715c600af8..fb9f9918c4 100644 --- a/src/ansiblelint/rules/args.py +++ b/src/ansiblelint/rules/args.py @@ -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: diff --git a/test/rules/test_args.py b/test/rules/test_args.py new file mode 100644 index 0000000000..30d83f1b4a --- /dev/null +++ b/test/rules/test_args.py @@ -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" From 0813fa61cd8f0f139eb6bf16514b092a46232dcf Mon Sep 17 00:00:00 2001 From: corubba Date: Wed, 19 Jun 2024 08:48:57 +0200 Subject: [PATCH 2/2] Increase expected test case count --- .github/workflows/tox.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 87cceaaa2d..2f77549bf8 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -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: