From 76158db10c50eb9182eb1ad4ee5deb5bd4a705b0 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Fri, 14 Jul 2017 08:04:43 -0700 Subject: [PATCH] Improve sanity import test messaging. (#26782) --- test/runner/importer.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/test/runner/importer.py b/test/runner/importer.py index 4a5861bd84..f678052c81 100755 --- a/test/runner/importer.py +++ b/test/runner/importer.py @@ -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)