ansible-test - Sanity test code cleanup. (#78497)

pull/78504/head
Matt Clay 2 years ago committed by GitHub
parent 3f1838bf91
commit 89862fda3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,6 +6,7 @@ from __future__ import annotations
import datetime import datetime
import re import re
import typing as t
import astroid import astroid
@ -144,18 +145,6 @@ class AnsibleDeprecatedChecker(BaseChecker):
}), }),
) )
def __init__(self, *args, **kwargs):
self.collection_version = None
self.collection_name = None
super().__init__(*args, **kwargs)
def set_option(self, optname, value, action=None, optdict=None):
super().set_option(optname, value, action, optdict)
if optname == 'collection-version' and value is not None:
self.collection_version = SemanticVersion(self.config.collection_version)
if optname == 'collection-name' and value is not None:
self.collection_name = self.config.collection_name
def _check_date(self, node, date): def _check_date(self, node, date):
if not isinstance(date, str): if not isinstance(date, str):
self.add_message('ansible-invalid-deprecated-date', node=node, args=(date,)) self.add_message('ansible-invalid-deprecated-date', node=node, args=(date,))
@ -205,6 +194,16 @@ class AnsibleDeprecatedChecker(BaseChecker):
except ValueError: except ValueError:
self.add_message('collection-invalid-deprecated-version', node=node, args=(version,)) self.add_message('collection-invalid-deprecated-version', node=node, args=(version,))
@property
def collection_name(self) -> t.Optional[str]:
"""Return the collection name, or None if ansible-core is being tested."""
return self.config.collection_name
@property
def collection_version(self) -> t.Optional[SemanticVersion]:
"""Return the collection version, or None if ansible-core is being tested."""
return SemanticVersion(self.config.collection_version) if self.config.collection_version is not None else None
@check_messages(*(MSGS.keys())) @check_messages(*(MSGS.keys()))
def visit_call(self, node): def visit_call(self, node):
"""Visit a call node.""" """Visit a call node."""

@ -679,14 +679,14 @@ class ModuleValidator(Validator):
def _ensure_imports_below_docs(self, doc_info, first_callable): def _ensure_imports_below_docs(self, doc_info, first_callable):
try: try:
min_doc_line = min( min_doc_line = min(
[doc_info[key]['lineno'] for key in doc_info if doc_info[key]['lineno']] doc_info[key]['lineno'] for key in doc_info if doc_info[key]['lineno']
) )
except ValueError: except ValueError:
# We can't perform this validation, as there are no DOCs provided at all # We can't perform this validation, as there are no DOCs provided at all
return return
max_doc_line = max( max_doc_line = max(
[doc_info[key]['end_lineno'] for key in doc_info if doc_info[key]['end_lineno']] doc_info[key]['end_lineno'] for key in doc_info if doc_info[key]['end_lineno']
) )
import_lines = [] import_lines = []

@ -12,28 +12,33 @@ Text = type(u'')
def main(): def main():
"""Main program entry point.""" """Main program entry point."""
for path in sys.argv[1:] or sys.stdin.read().splitlines(): for path in sys.argv[1:] or sys.stdin.read().splitlines():
with open(path, 'rb') as source_fd: compile_source(path)
source = source_fd.read()
try:
compile(source, path, 'exec', dont_inherit=True)
except SyntaxError as ex:
extype, message, lineno, offset = type(ex), ex.text, ex.lineno, ex.offset
except BaseException as ex: # pylint: disable=broad-except
extype, message, lineno, offset = type(ex), str(ex), 0, 0
else:
continue
# In some situations offset can be None. This can happen for syntax errors on Python 2.6 def compile_source(path):
# (__future__ import following after a regular import). """Compile the specified source file, printing an error if one occurs."""
offset = offset or 0 with open(path, 'rb') as source_fd:
source = source_fd.read()
result = "%s:%d:%d: %s: %s" % (path, lineno, offset, extype.__name__, safe_message(message)) try:
compile(source, path, 'exec', dont_inherit=True)
except SyntaxError as ex:
extype, message, lineno, offset = type(ex), ex.text, ex.lineno, ex.offset
except BaseException as ex: # pylint: disable=broad-except
extype, message, lineno, offset = type(ex), str(ex), 0, 0
else:
return
if sys.version_info <= (3,): # In some situations offset can be None. This can happen for syntax errors on Python 2.6
result = result.encode(ENCODING, ERRORS) # (__future__ import following after a regular import).
offset = offset or 0
print(result) result = "%s:%d:%d: %s: %s" % (path, lineno, offset, extype.__name__, safe_message(message))
if sys.version_info <= (3,):
result = result.encode(ENCODING, ERRORS)
print(result)
def safe_message(value): def safe_message(value):

Loading…
Cancel
Save