Improve sanity import test messaging. (#26782)

pull/26412/merge
Matt Clay 7 years ago committed by GitHub
parent 088b930808
commit 76158db10c

@ -23,7 +23,8 @@ def main():
message = str(ex) message = str(ex)
results = list(reversed(traceback.extract_tb(exc_tb))) results = list(reversed(traceback.extract_tb(exc_tb)))
source = None source = None
line = None line = 0
offset = 0
for result in results: for result in results:
if result[0].startswith(base_dir): if result[0].startswith(base_dir):
@ -32,11 +33,24 @@ def main():
break break
if not source: 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
line = 0
message += ' (in %s:%d)' % (results[-1][0], results[-1][1]) message += ' (in %s:%d)' % (results[-1][0], results[-1][1])
elif isinstance(ex, SyntaxError):
error = '%s:%d:0: %s: %s' % (source, line, exc_type.__name__, message) 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)
# 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: if error not in messages:
messages.add(error) messages.add(error)

Loading…
Cancel
Save