diff options
Diffstat (limited to 'test/lib/ansible_test/_internal/powershell_import_analysis.py')
-rw-r--r-- | test/lib/ansible_test/_internal/powershell_import_analysis.py | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/test/lib/ansible_test/_internal/powershell_import_analysis.py b/test/lib/ansible_test/_internal/powershell_import_analysis.py index 9c4019f533..4c9d7b8e1a 100644 --- a/test/lib/ansible_test/_internal/powershell_import_analysis.py +++ b/test/lib/ansible_test/_internal/powershell_import_analysis.py @@ -5,6 +5,10 @@ __metaclass__ = type import os import re +from .io import ( + read_text_file, +) + from .util import ( display, ) @@ -49,7 +53,7 @@ def get_powershell_module_utils_name(path): # type: (str) -> str else: prefix = '' - 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 @@ -71,27 +75,26 @@ def extract_powershell_module_utils_imports(path, module_utils): """ imports = set() - with open(path, 'r') as module_fd: - code = module_fd.read() + code = read_text_file(path) - if '# POWERSHELL_COMMON' in code: - imports.add('Ansible.ModuleUtils.Legacy') + if '# POWERSHELL_COMMON' in code: + imports.add('Ansible.ModuleUtils.Legacy') - lines = code.splitlines() - line_number = 0 + lines = code.splitlines() + line_number = 0 - for line in lines: - line_number += 1 - match = re.search(r'(?i)^#\s*(?:requires\s+-module(?:s?)|ansiblerequires\s+-powershell)\s*((?:Ansible|ansible_collections)\..+)', line) + for line in lines: + line_number += 1 + match = re.search(r'(?i)^#\s*(?:requires\s+-module(?:s?)|ansiblerequires\s+-powershell)\s*((?:Ansible|ansible_collections)\..+)', line) - if not match: - continue + if not match: + continue - import_name = match.group(1) + import_name = match.group(1) - if import_name in module_utils: - imports.add(import_name) - else: - display.warning('%s:%d Invalid module_utils import: %s' % (path, line_number, import_name)) + if import_name in module_utils: + imports.add(import_name) + else: + display.warning('%s:%d Invalid module_utils import: %s' % (path, line_number, import_name)) return imports |