ansible-test - Fix validate-modules Python handling

The ``validate-modules`` sanity test no longer attempts to process files with unrecognized extensions as Python.

Integration tests have been added to verify Python-specific checks do not apply to these files.

The `invalid-extension` and `missing-gplv3-license` checks still apply to these files. This may change in the future.
pull/82677/head
Matt Clay 4 months ago
parent e78be30baa
commit c8d6f7b95e

@ -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).

@ -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.

@ -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

@ -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):

@ -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)

Loading…
Cancel
Save