mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Fixes for ansible-test sanity import test. (#45249)
* Fix import test on Python 3.7. * Fix path processing in import sanity test.
This commit is contained in:
parent
6a026cd2f0
commit
6fb333faff
1 changed files with 38 additions and 24 deletions
|
@ -4,12 +4,18 @@
|
|||
from __future__ import absolute_import, print_function
|
||||
|
||||
import contextlib
|
||||
import imp
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
try:
|
||||
import importlib.util
|
||||
imp = None
|
||||
except ImportError:
|
||||
importlib = None
|
||||
import imp
|
||||
|
||||
try:
|
||||
from StringIO import StringIO
|
||||
except ImportError:
|
||||
|
@ -76,10 +82,18 @@ def test_python_module(path, base_dir, messages, ansible_module):
|
|||
filter_dir = base_dir
|
||||
|
||||
capture = Capture()
|
||||
|
||||
try:
|
||||
if imp:
|
||||
with open(path, 'r') as module_fd:
|
||||
with capture_output(capture):
|
||||
imp.load_module(name, module_fd, os.path.abspath(path), ('.py', 'r', imp.PY_SOURCE))
|
||||
else:
|
||||
spec = importlib.util.spec_from_file_location(name, os.path.abspath(path))
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
|
||||
with capture_output(capture):
|
||||
spec.loader.exec_module(module)
|
||||
|
||||
capture_report(path, capture, messages)
|
||||
except ImporterAnsibleModuleException:
|
||||
|
@ -95,6 +109,18 @@ def test_python_module(path, base_dir, messages, ansible_module):
|
|||
line = 0
|
||||
offset = 0
|
||||
|
||||
if isinstance(ex, SyntaxError) and ex.filename.endswith(path): # pylint: disable=locally-disabled, no-member
|
||||
# A SyntaxError in the source we're importing will have the correct path, line and offset.
|
||||
# However, the traceback will report the path to this importer.py script instead.
|
||||
# We'll use the details from the SyntaxError in this case, as it's more accurate.
|
||||
source = path
|
||||
line = ex.lineno or 0 # pylint: disable=locally-disabled, no-member
|
||||
offset = ex.offset or 0 # pylint: disable=locally-disabled, no-member
|
||||
message = str(ex)
|
||||
|
||||
# Hack to remove the filename and line number from the message, if present.
|
||||
message = message.replace(' (%s, line %d)' % (os.path.basename(path), line), '')
|
||||
else:
|
||||
for result in results:
|
||||
if result[0].startswith(filter_dir):
|
||||
source = result[0][len(base_dir) + 1:].replace('test/sanity/import/', '')
|
||||
|
@ -106,18 +132,6 @@ def test_python_module(path, base_dir, messages, ansible_module):
|
|||
# I haven't been able to come up with a test case that encounters this issue yet.
|
||||
source = path
|
||||
message += ' (in %s:%d)' % (results[-1][0], results[-1][1] or 0)
|
||||
elif isinstance(ex, SyntaxError):
|
||||
if ex.filename.endswith(path): # pylint: disable=locally-disabled, no-member
|
||||
# A SyntaxError in the source we're importing will have the correct path, line and offset.
|
||||
# However, the traceback will report the path to this importer.py script instead.
|
||||
# We'll use the details from the SyntaxError in this case, as it's more accurate.
|
||||
source = path
|
||||
line = ex.lineno or 0 # pylint: disable=locally-disabled, no-member
|
||||
offset = ex.offset or 0 # pylint: disable=locally-disabled, no-member
|
||||
message = str(ex)
|
||||
|
||||
# Hack to remove the filename and line number from the message, if present.
|
||||
message = message.replace(' (%s, line %d)' % (os.path.basename(path), line), '')
|
||||
|
||||
message = re.sub(r'\n *', ': ', message)
|
||||
error = '%s:%d:%d: %s: %s' % (source, line, offset, exc_type.__name__, message)
|
||||
|
|
Loading…
Reference in a new issue