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
|
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:
|
||||||
with open(path, 'r') as module_fd:
|
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):
|
with capture_output(capture):
|
||||||
imp.load_module(name, module_fd, os.path.abspath(path), ('.py', 'r', imp.PY_SOURCE))
|
spec.loader.exec_module(module)
|
||||||
|
|
||||||
capture_report(path, capture, messages)
|
capture_report(path, capture, messages)
|
||||||
except ImporterAnsibleModuleException:
|
except ImporterAnsibleModuleException:
|
||||||
|
@ -95,29 +109,29 @@ def test_python_module(path, base_dir, messages, ansible_module):
|
||||||
line = 0
|
line = 0
|
||||||
offset = 0
|
offset = 0
|
||||||
|
|
||||||
for result in results:
|
if isinstance(ex, SyntaxError) and ex.filename.endswith(path): # pylint: disable=locally-disabled, no-member
|
||||||
if result[0].startswith(filter_dir):
|
# A SyntaxError in the source we're importing will have the correct path, line and offset.
|
||||||
source = result[0][len(base_dir) + 1:].replace('test/sanity/import/', '')
|
# However, the traceback will report the path to this importer.py script instead.
|
||||||
line = result[1] or 0
|
# We'll use the details from the SyntaxError in this case, as it's more accurate.
|
||||||
break
|
|
||||||
|
|
||||||
if not source:
|
|
||||||
# If none of our source files are found in the traceback, report the file we were testing.
|
|
||||||
# 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)
|
line = ex.lineno or 0 # pylint: disable=locally-disabled, no-member
|
||||||
elif isinstance(ex, SyntaxError):
|
offset = ex.offset or 0 # pylint: disable=locally-disabled, no-member
|
||||||
if ex.filename.endswith(path): # pylint: disable=locally-disabled, no-member
|
message = str(ex)
|
||||||
# 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.
|
# 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 = 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/', '')
|
||||||
|
line = result[1] or 0
|
||||||
|
break
|
||||||
|
|
||||||
|
if not source:
|
||||||
|
# If none of our source files are found in the traceback, report the file we were testing.
|
||||||
|
# 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)
|
||||||
|
|
||||||
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…
Reference in a new issue