Fixes for ansible-test sanity import test. (#45249)

* Fix import test on Python 3.7.
* Fix path processing in import sanity test.

(cherry picked from commit 6fb333faff)
pull/45354/head
Matt Clay 6 years ago
parent 0c3faa2e1c
commit 8e048c0ab7

@ -4,12 +4,18 @@
from __future__ import absolute_import, print_function from __future__ import absolute_import, print_function
import contextlib import contextlib
import imp
import os import os
import re import re
import sys import sys
import traceback import traceback
try:
import importlib.util
imp = None
except ImportError:
importlib = None
import imp
try: try:
from StringIO import StringIO from StringIO import StringIO
except ImportError: except ImportError:
@ -76,10 +82,18 @@ def test_python_module(path, base_dir, messages, ansible_module):
filter_dir = base_dir filter_dir = base_dir
capture = Capture() capture = Capture()
try: try:
if imp:
with open(path, 'r') as module_fd: with open(path, 'r') as module_fd:
with capture_output(capture): with capture_output(capture):
imp.load_module(name, module_fd, os.path.abspath(path), ('.py', 'r', imp.PY_SOURCE)) 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) capture_report(path, capture, messages)
except ImporterAnsibleModuleException: except ImporterAnsibleModuleException:
@ -95,6 +109,18 @@ def test_python_module(path, base_dir, messages, ansible_module):
line = 0 line = 0
offset = 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: for result in results:
if result[0].startswith(filter_dir): if result[0].startswith(filter_dir):
source = result[0][len(base_dir) + 1:].replace('test/sanity/import/', '') 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. # I haven't been able to come up with a test case that encounters this issue yet.
source = path source = path
message += ' (in %s:%d)' % (results[-1][0], results[-1][1] or 0) 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) message = re.sub(r'\n *', ': ', message)
error = '%s:%d:%d: %s: %s' % (source, line, offset, exc_type.__name__, message) error = '%s:%d:%d: %s: %s' % (source, line, offset, exc_type.__name__, message)

Loading…
Cancel
Save