summaryrefslogtreecommitdiff
path: root/test/lib/ansible_test/_internal/import_analysis.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/lib/ansible_test/_internal/import_analysis.py')
-rw-r--r--test/lib/ansible_test/_internal/import_analysis.py33
1 files changed, 18 insertions, 15 deletions
diff --git a/test/lib/ansible_test/_internal/import_analysis.py b/test/lib/ansible_test/_internal/import_analysis.py
index fd52173861..f1cb66353e 100644
--- a/test/lib/ansible_test/_internal/import_analysis.py
+++ b/test/lib/ansible_test/_internal/import_analysis.py
@@ -7,6 +7,10 @@ import os
from . import types as t
+from .io import (
+ read_text_file,
+)
+
from .util import (
display,
ApplicationError,
@@ -130,7 +134,7 @@ def get_python_module_utils_name(path): # type: (str) -> str
if path.endswith('/__init__.py'):
path = os.path.dirname(path)
- name = prefix + os.path.splitext(os.path.relpath(path, base_path))[0].replace(os.sep, '.')
+ name = prefix + os.path.splitext(os.path.relpath(path, base_path))[0].replace(os.path.sep, '.')
return name
@@ -161,20 +165,19 @@ def extract_python_module_utils_imports(path, module_utils):
:type module_utils: set[str]
:rtype: set[str]
"""
- with open(path, 'r') as module_fd:
- code = module_fd.read()
-
- try:
- tree = ast.parse(code)
- except SyntaxError as ex:
- # 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)
- return finder.imports
+ code = read_text_file(path)
+
+ try:
+ tree = ast.parse(code)
+ except SyntaxError as ex:
+ # 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)
+ return finder.imports
class ModuleUtilFinder(ast.NodeVisitor):