From 53a3d1ffdb14a7f367945606d6ca240d47fe5e04 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Fri, 27 Mar 2020 13:58:04 -0700 Subject: [PATCH] Fix ansible-test change detection traceback. --- ...ble-test-change-detection-empty-python.yml | 2 ++ .../ansible_test/_internal/classification.py | 6 ----- .../ansible_test/_internal/import_analysis.py | 26 ++++++++++--------- 3 files changed, 16 insertions(+), 18 deletions(-) create mode 100644 changelogs/fragments/ansible-test-change-detection-empty-python.yml diff --git a/changelogs/fragments/ansible-test-change-detection-empty-python.yml b/changelogs/fragments/ansible-test-change-detection-empty-python.yml new file mode 100644 index 00000000000..12e4a910e4d --- /dev/null +++ b/changelogs/fragments/ansible-test-change-detection-empty-python.yml @@ -0,0 +1,2 @@ +bugfixes: + - ansible-test no longer tracebacks during change analysis due to processing an empty python file diff --git a/test/lib/ansible_test/_internal/classification.py b/test/lib/ansible_test/_internal/classification.py index 6ae87045074..c0357d59139 100644 --- a/test/lib/ansible_test/_internal/classification.py +++ b/test/lib/ansible_test/_internal/classification.py @@ -288,12 +288,6 @@ class PathMapper: :type path: str :rtype: list[str] """ - if path == 'lib/ansible/module_utils/__init__.py': - return [] - - if path == 'plugins/module_utils/__init__.py': - return [] - if not self.python_module_utils_imports: display.info('Analyzing python module_utils imports...') before = time.time() diff --git a/test/lib/ansible_test/_internal/import_analysis.py b/test/lib/ansible_test/_internal/import_analysis.py index 64b2e993c06..5d2b4ea7ca4 100644 --- a/test/lib/ansible_test/_internal/import_analysis.py +++ b/test/lib/ansible_test/_internal/import_analysis.py @@ -117,6 +117,11 @@ def get_python_module_utils_imports(compile_targets): for module_util in sorted(imports): if not imports[module_util]: + package_path = get_import_path(module_util, package=True) + + if os.path.exists(package_path) and not os.path.getsize(package_path): + continue # ignore empty __init__.py files + display.warning('No imports found which use the "%s" module_util.' % module_util) return imports @@ -127,14 +132,17 @@ def get_python_module_utils_name(path): # type: (str) -> str base_path = data_context().content.module_utils_path if data_context().content.collection: - prefix = 'ansible_collections.' + data_context().content.collection.prefix + 'plugins.module_utils.' + prefix = 'ansible_collections.' + data_context().content.collection.prefix + 'plugins.module_utils' else: - prefix = 'ansible.module_utils.' + prefix = 'ansible.module_utils' if path.endswith('/__init__.py'): path = os.path.dirname(path) - name = prefix + os.path.splitext(os.path.relpath(path, base_path))[0].replace(os.path.sep, '.') + if path == base_path: + name = prefix + else: + name = prefix + '.' + os.path.splitext(os.path.relpath(path, base_path))[0].replace(os.path.sep, '.') return name @@ -151,9 +159,6 @@ def enumerate_module_utils(): if ext != '.py': continue - if os.path.getsize(path) == 0: - continue - module_utils.append(get_python_module_utils_name(path)) return set(module_utils) @@ -192,7 +197,9 @@ def get_import_path(name, package=False): # type: (str, bool) -> str if name.startswith('ansible.module_utils.') or name == 'ansible.module_utils': path = os.path.join('lib', filename) - elif data_context().content.collection and name.startswith('ansible_collections.%s.plugins.module_utils.' % data_context().content.collection.full_name): + elif data_context().content.collection and ( + name.startswith('ansible_collections.%s.plugins.module_utils.' % data_context().content.collection.full_name) or + name == 'ansible_collections.%s.plugins.module_utils' % data_context().content.collection.full_name): path = '/'.join(filename.split('/')[3:]) else: raise Exception('Unexpected import name: %s' % name) @@ -274,11 +281,6 @@ class ModuleUtilFinder(ast.NodeVisitor): if is_subdir(self.path, data_context().content.test_path): return # invalid imports in tests are ignored - path = get_import_path(name, True) - - if os.path.exists(path) and os.path.getsize(path) == 0: - return # zero length __init__.py files are ignored during earlier processing, do not warn about them now - # 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))