Isolate globals in import sanity test.

pull/59742/head
Matt Clay 5 years ago
parent 0b8354751b
commit ecddbdf0cb

@ -3,53 +3,55 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import contextlib
import os def main():
import re """
import sys Main program function used to isolate globals from imported code.
import traceback Changes to globals in imported modules on Python 2.7 will overwrite our own globals.
import warnings """
import contextlib
try: import os
import re
import sys
import traceback
import warnings
try:
import importlib.util import importlib.util
imp = None # pylint: disable=invalid-name imp = None # pylint: disable=invalid-name
except ImportError: except ImportError:
importlib = None # pylint: disable=invalid-name importlib = None # pylint: disable=invalid-name
import imp import imp
try: try:
from StringIO import StringIO from StringIO import StringIO
except ImportError: except ImportError:
from io import StringIO from io import StringIO
import ansible.module_utils.basic import ansible.module_utils.basic
import ansible.module_utils.common.removed import ansible.module_utils.common.removed
try: try:
from ansible.utils.collection_loader import AnsibleCollectionLoader from ansible.utils.collection_loader import AnsibleCollectionLoader
except ImportError: except ImportError:
AnsibleCollectionLoader = None AnsibleCollectionLoader = None
class ImporterAnsibleModuleException(Exception):
class ImporterAnsibleModuleException(Exception):
"""Exception thrown during initialization of ImporterAnsibleModule.""" """Exception thrown during initialization of ImporterAnsibleModule."""
class ImporterAnsibleModule:
class ImporterAnsibleModule:
"""Replacement for AnsibleModule to support import testing.""" """Replacement for AnsibleModule to support import testing."""
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
raise ImporterAnsibleModuleException() raise ImporterAnsibleModuleException()
# stop Ansible module execution during AnsibleModule instantiation
ansible.module_utils.basic.AnsibleModule = ImporterAnsibleModule
# no-op for _load_params since it may be called before instantiating AnsibleModule
ansible.module_utils.basic._load_params = lambda *args, **kwargs: {} # pylint: disable=protected-access
# no-op for removed_module since it is called in place of AnsibleModule instantiation
ansible.module_utils.common.removed.removed_module = lambda *args, **kwargs: None
# stop Ansible module execution during AnsibleModule instantiation def run():
ansible.module_utils.basic.AnsibleModule = ImporterAnsibleModule
# no-op for _load_params since it may be called before instantiating AnsibleModule
ansible.module_utils.basic._load_params = lambda *args, **kwargs: {} # pylint: disable=protected-access
# no-op for removed_module since it is called in place of AnsibleModule instantiation
ansible.module_utils.common.removed.removed_module = lambda *args, **kwargs: None
def main():
"""Main program function.""" """Main program function."""
base_dir = os.getcwd() base_dir = os.getcwd()
messages = set() messages = set()
@ -65,8 +67,7 @@ def main():
if messages: if messages:
exit(10) exit(10)
def test_python_module(path, base_dir, messages, ansible_module):
def test_python_module(path, base_dir, messages, ansible_module):
if ansible_module: if ansible_module:
# importing modules with __main__ under Python 2.6 exits with status code 1 # importing modules with __main__ under Python 2.6 exits with status code 1
if sys.version_info < (2, 7): if sys.version_info < (2, 7):
@ -149,16 +150,14 @@ def test_python_module(path, base_dir, messages, ansible_module):
report_message(error, messages) report_message(error, messages)
class Capture:
class Capture:
"""Captured output and/or exception.""" """Captured output and/or exception."""
def __init__(self): def __init__(self):
self.stdout = StringIO() self.stdout = StringIO()
self.stderr = StringIO() self.stderr = StringIO()
self.warnings = [] self.warnings = []
def capture_report(path, capture, messages):
def capture_report(path, capture, messages):
"""Report on captured output. """Report on captured output.
:type path: str :type path: str
:type capture: Capture :type capture: Capture
@ -198,8 +197,7 @@ def capture_report(path, capture, messages):
message = '%s:%d:%d: %s: %s' % (filepath, lineno, 0, warning.category.__name__, msg) message = '%s:%d:%d: %s: %s' % (filepath, lineno, 0, warning.category.__name__, msg)
report_message(message, messages) report_message(message, messages)
def report_message(message, messages):
def report_message(message, messages):
"""Report message if not already reported. """Report message if not already reported.
:type message: str :type message: str
:type messages: set[str] :type messages: set[str]
@ -208,9 +206,8 @@ def report_message(message, messages):
messages.add(message) messages.add(message)
print(message) print(message)
@contextlib.contextmanager
@contextlib.contextmanager def capture_output(capture):
def capture_output(capture):
"""Capture sys.stdout and sys.stderr. """Capture sys.stdout and sys.stderr.
:type capture: Capture :type capture: Capture
""" """
@ -229,6 +226,8 @@ def capture_output(capture):
sys.stdout = old_stdout sys.stdout = old_stdout
sys.stderr = old_stderr sys.stderr = old_stderr
run()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

Loading…
Cancel
Save