summaryrefslogtreecommitdiff
path: root/test/lib/ansible_test/_internal/import_analysis.py
diff options
context:
space:
mode:
authorMatt Clay <mclay@redhat.com>2020-02-04 11:21:53 -0800
committerGitHub <noreply@github.com>2020-02-04 11:21:53 -0800
commitf4a80bb600510669801c5d5c0a250952748e99fd (patch)
tree83ce363b557a66453a9f2c9d0b84c589e09fc57a /test/lib/ansible_test/_internal/import_analysis.py
parent994a6b0c5a7929051e5e2101004ef536ec47c0b3 (diff)
downloadansible-f4a80bb600510669801c5d5c0a250952748e99fd.tar.gz
Code cleanup and refactoring in ansible-test. (#67063)
* Code cleanup in ansible-test. * Split out encoding functions. * Consoldate loading of JSON files. * Split out disk IO functions. * Simplify file access. * Add functions for opening files. * Replace open calls with appropriate functions. * Expose more types from typing module. * Support writing compact JSON. * Add verbosity argument to display.warning. * Add changelog entry. * Update files overlooked during rebase. * Use `io.open` instead of `open`. * Fix file opening for imp.load_module. * Remove use of `r+` mode to access files. * Add missing import. * Fix httptester on Python 2.x. * Clarify changelog fragment. * Consolidate imports. Remove extra newlines. * Fix indirect imports.
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):