diff --git a/changelogs/fragments/ansible-test-validate-modules-non-python.yml b/changelogs/fragments/ansible-test-validate-modules-non-python.yml new file mode 100644 index 00000000000..a29fc17232c --- /dev/null +++ b/changelogs/fragments/ansible-test-validate-modules-non-python.yml @@ -0,0 +1,3 @@ +bugfixes: + - ansible-test - The ``validate-modules`` sanity test no longer attempts to process files with unrecognized extensions as Python + (resolves https://github.com/ansible/ansible/issues/82604). diff --git a/test/integration/targets/ansible-test-sanity-validate-modules/ansible_collections/ns/col/plugins/modules/unsupported_extension.nope b/test/integration/targets/ansible-test-sanity-validate-modules/ansible_collections/ns/col/plugins/modules/unsupported_extension.nope new file mode 100644 index 00000000000..28ec361bf12 --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-validate-modules/ansible_collections/ns/col/plugins/modules/unsupported_extension.nope @@ -0,0 +1,2 @@ +This file has an extension which is not supported by validate-modules. +It should not be treated as a Python file or have Python-specific rules applied to it. diff --git a/test/integration/targets/ansible-test-sanity-validate-modules/expected.txt b/test/integration/targets/ansible-test-sanity-validate-modules/expected.txt index ad3e79fd3b0..3ae113c5ef2 100644 --- a/test/integration/targets/ansible-test-sanity-validate-modules/expected.txt +++ b/test/integration/targets/ansible-test-sanity-validate-modules/expected.txt @@ -27,3 +27,5 @@ plugins/modules/semantic_markup.py:0:0: invalid-documentation-markup: Directive plugins/modules/semantic_markup.py:0:0: invalid-documentation-markup: Directive "O(foo.bar=1)" contains a non-existing option "foo.bar" plugins/modules/semantic_markup.py:0:0: invalid-documentation-markup: Directive "RV(bam)" contains a non-existing return value "bam" plugins/modules/semantic_markup.py:0:0: invalid-documentation-markup: Directive "RV(does.not.exist=true)" contains a non-existing return value "does.not.exist" +plugins/modules/unsupported_extension.nope:0:0: invalid-extension: Official Ansible modules must have a .py extension for python modules or a .ps1 for powershell modules +plugins/modules/unsupported_extension.nope:0:0: missing-gplv3-license: GPLv3 license header not found in the first 20 lines of the module diff --git a/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py b/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py index d180d464b85..e1d8ec2cd11 100644 --- a/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py +++ b/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py @@ -75,7 +75,7 @@ from ansible.plugins.list import IGNORE as REJECTLIST from ansible.utils.plugin_docs import add_collection_to_versions_and_dates, add_fragments, get_docstring from ansible.utils.version import SemanticVersion -from .module_args import AnsibleModuleImportError, AnsibleModuleNotInitialized, get_argument_spec +from .module_args import AnsibleModuleImportError, AnsibleModuleNotInitialized, get_py_argument_spec, get_ps_argument_spec from .schema import ( ansible_module_kwargs_schema, @@ -330,8 +330,6 @@ class ModuleValidator(Validator): self.git_cache = git_cache self.base_module = self.git_cache.get_original_path(self.path) - self._python_module_override = False - with open(path) as f: self.text = f.read() self.length = len(self.text.splitlines()) @@ -378,7 +376,7 @@ class ModuleValidator(Validator): pass def _python_module(self): - if self.path.endswith('.py') or self._python_module_override: + if self.path.endswith('.py'): return True return False @@ -416,7 +414,7 @@ class ModuleValidator(Validator): return self.git_cache.is_new(self.path) def _check_interpreter(self, powershell=False): - if powershell: + if self._powershell_module(): if not self.text.startswith('#!powershell\n'): self.reporter.error( path=self.object_path, @@ -425,20 +423,21 @@ class ModuleValidator(Validator): ) return - missing_python_interpreter = False + if self._python_module(): + missing_python_interpreter = False - if not self.text.startswith('#!/usr/bin/python'): - if NEW_STYLE_PYTHON_MODULE_RE.search(to_bytes(self.text)): - missing_python_interpreter = self.text.startswith('#!') # shebang optional, but if present must match - else: - missing_python_interpreter = True # shebang required + if not self.text.startswith('#!/usr/bin/python'): + if NEW_STYLE_PYTHON_MODULE_RE.search(to_bytes(self.text)): + missing_python_interpreter = self.text.startswith('#!') # shebang optional, but if present must match + else: + missing_python_interpreter = True # shebang required - if missing_python_interpreter: - self.reporter.error( - path=self.object_path, - code='missing-python-interpreter', - msg='Interpreter line is not "#!/usr/bin/python"', - ) + if missing_python_interpreter: + self.reporter.error( + path=self.object_path, + code='missing-python-interpreter', + msg='Interpreter line is not "#!/usr/bin/python"', + ) def _check_for_sys_exit(self): # Optimize out the happy path @@ -1292,7 +1291,12 @@ class ModuleValidator(Validator): def _validate_ansible_module_call(self, docs): try: - spec, kwargs = get_argument_spec(self.path, self.collection) + if self._python_module(): + spec, kwargs = get_py_argument_spec(self.path, self.collection) + elif self._powershell_module(): + spec, kwargs = get_ps_argument_spec(self.path, self.collection) + else: + raise NotImplementedError() except AnsibleModuleNotInitialized: self.reporter.error( path=self.object_path, @@ -2248,7 +2252,6 @@ class ModuleValidator(Validator): 'extension for python modules or a .ps1 ' 'for powershell modules') ) - self._python_module_override = True if self._python_module() and self.ast is None: self.reporter.error( @@ -2360,7 +2363,7 @@ class ModuleValidator(Validator): self._check_gpl3_header() if not self._just_docs() and not self._sidecar_doc() and not end_of_deprecation_should_be_removed_only: if self.plugin_type == 'module': - self._check_interpreter(powershell=self._powershell_module()) + self._check_interpreter() class PythonPackageValidator(Validator): diff --git a/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/module_args.py b/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/module_args.py index 1b7121713b6..bff93067478 100644 --- a/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/module_args.py +++ b/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/module_args.py @@ -167,10 +167,3 @@ def get_py_argument_spec(filename, collection): return argument_spec, fake.kwargs except (TypeError, IndexError): return {}, {} - - -def get_argument_spec(filename, collection): - if filename.endswith('.py'): - return get_py_argument_spec(filename, collection) - else: - return get_ps_argument_spec(filename, collection)