Improve sanity import test messaging. (#26782)

This commit is contained in:
Matt Clay 2017-07-14 08:04:43 -07:00 committed by GitHub
parent 088b930808
commit 76158db10c

View file

@ -23,7 +23,8 @@ def main():
message = str(ex)
results = list(reversed(traceback.extract_tb(exc_tb)))
source = None
line = None
line = 0
offset = 0
for result in results:
if result[0].startswith(base_dir):
@ -32,11 +33,24 @@ def main():
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
line = 0
message += ' (in %s:%d)' % (results[-1][0], results[-1][1])
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 # pylint: disable=locally-disabled, no-member
offset = ex.offset # pylint: disable=locally-disabled, no-member
message = str(ex)
error = '%s:%d:0: %s: %s' % (source, line, exc_type.__name__, message)
# Hack to remove the filename and line number from the message, if present.
message = message.replace(' (%s, line %d)' % (os.path.basename(path), line), '')
error = '%s:%d:%d: %s: %s' % (source, line, offset, exc_type.__name__, message)
if error not in messages:
messages.add(error)