From 7c728118e63b740d47cd59bf3443b11ea264b9b8 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Tue, 23 Jul 2019 18:10:24 -0700 Subject: [PATCH] Fix import sanity test for collections. (#59484) --- test/runner/lib/sanity/import.py | 16 ++++++++++++++++ test/sanity/import/importer.py | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/test/runner/lib/sanity/import.py b/test/runner/lib/sanity/import.py index 29270883faf..7617e9f3773 100644 --- a/test/runner/lib/sanity/import.py +++ b/test/runner/lib/sanity/import.py @@ -110,6 +110,22 @@ class ImportTest(SanityMultipleVersion): os.symlink(os.path.join(ANSIBLE_ROOT, 'lib/ansible/module_utils'), ansible_link) + if data_context().content.collection: + # inject just enough Ansible code for the collections loader to work on all supported Python versions + # the __init__.py files are needed only for Python 2.x + # the empty modules directory is required for the collection loader to generate the synthetic packages list + + make_dirs(os.path.join(ansible_path, 'utils')) + with open(os.path.join(ansible_path, 'utils/__init__.py'), 'w'): + pass + + os.symlink(os.path.join(ANSIBLE_ROOT, 'lib/ansible/utils/collection_loader.py'), os.path.join(ansible_path, 'utils/collection_loader.py')) + os.symlink(os.path.join(ANSIBLE_ROOT, 'lib/ansible/utils/singleton.py'), os.path.join(ansible_path, 'utils/singleton.py')) + + make_dirs(os.path.join(ansible_path, 'modules')) + with open(os.path.join(ansible_path, 'modules/__init__.py'), 'w'): + pass + # activate the virtual environment env['PATH'] = '%s:%s' % (virtual_environment_bin, env['PATH']) env['PYTHONPATH'] = python_path diff --git a/test/sanity/import/importer.py b/test/sanity/import/importer.py index 43bb9e622a3..58aa2255687 100755 --- a/test/sanity/import/importer.py +++ b/test/sanity/import/importer.py @@ -25,6 +25,11 @@ except ImportError: import ansible.module_utils.basic import ansible.module_utils.common.removed +try: + from ansible.utils.collection_loader import AnsibleCollectionLoader +except ImportError: + AnsibleCollectionLoader = None + class ImporterAnsibleModuleException(Exception): """Exception thrown during initialization of ImporterAnsibleModule.""" @@ -49,6 +54,10 @@ def main(): base_dir = os.getcwd() messages = set() + if AnsibleCollectionLoader: + # allow importing code from collections + sys.meta_path.insert(0, AnsibleCollectionLoader()) + for path in sys.argv[1:] or sys.stdin.read().splitlines(): test_python_module(path, base_dir, messages, False) test_python_module(path, base_dir, messages, True)