From 0cf1e8e06105663ad7925563e947c64ce52b527f Mon Sep 17 00:00:00 2001
From: Matt Clay <matt@mystile.com>
Date: Fri, 7 Apr 2017 10:01:15 -0700
Subject: [PATCH] Fix ansible-test handling of import/syntax errors.

---
 test/runner/lib/import_analysis.py | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/test/runner/lib/import_analysis.py b/test/runner/lib/import_analysis.py
index c77d9e499ef..94f31f4d26b 100644
--- a/test/runner/lib/import_analysis.py
+++ b/test/runner/lib/import_analysis.py
@@ -156,11 +156,10 @@ def extract_python_module_utils_imports(path, module_utils):
         try:
             tree = ast.parse(code)
         except SyntaxError as ex:
-            # Setting the full path to the filename results in only the filename being given for str(ex).
-            # As a work-around, set the filename to a UUID and replace it in the final string output with the actual path.
-            ex.filename = str(uuid.uuid4())
-            error = str(ex).replace(ex.filename, path)
-            raise ApplicationError('AST parse error: %s' % error)
+            # Treat this error as a warning so tests can be executed as best as possible.
+            # The compile test will detect and report this syntax error.
+            display.warning('%s:%s Syntax error extracting module_utils imports: %s' % (path, ex.lineno, ex.msg))
+            return set()
 
         finder = ModuleUtilFinder(path, module_utils)
         finder.visit(tree)
@@ -239,4 +238,6 @@ class ModuleUtilFinder(ast.NodeVisitor):
         if self.path.startswith('test/'):
             return  # invalid imports in tests are ignored
 
-        raise ApplicationError('%s:%d Invalid module_utils import: %s' % (self.path, line_number, import_name))
+        # Treat this error as a warning so tests can be executed as best as possible.
+        # This error should be detected by unit or integration tests.
+        display.warning('%s:%d Invalid module_utils import: %s' % (self.path, line_number, import_name))