diff --git a/changelogs/fragments/ansible-test-code-cleanup.yml b/changelogs/fragments/ansible-test-code-cleanup.yml new file mode 100644 index 00000000000..ab437a97544 --- /dev/null +++ b/changelogs/fragments/ansible-test-code-cleanup.yml @@ -0,0 +1,3 @@ +bugfixes: + - ansible-test - Use ``sys.exit`` instead of ``exit``. + - ansible-test - Code cleanup. diff --git a/test/lib/ansible_test/_data/injector/python.py b/test/lib/ansible_test/_data/injector/python.py index 48198805110..290b995cc17 100755 --- a/test/lib/ansible_test/_data/injector/python.py +++ b/test/lib/ansible_test/_data/injector/python.py @@ -37,7 +37,8 @@ def main(): found = False if not found: - exit('ERROR: Could not find `coverage` module. Did you use a virtualenv created without --system-site-packages or with the wrong interpreter?') + sys.exit('ERROR: Could not find `coverage` module. ' + 'Did you use a virtualenv created without --system-site-packages or with the wrong interpreter?') if name == 'python.py': if sys.argv[1] == '-c': diff --git a/test/lib/ansible_test/_data/sanity/import/importer.py b/test/lib/ansible_test/_data/sanity/import/importer.py index f42ff787f0e..2b81190c5e7 100755 --- a/test/lib/ansible_test/_data/sanity/import/importer.py +++ b/test/lib/ansible_test/_data/sanity/import/importer.py @@ -191,7 +191,7 @@ def main(): test_python_module(path, name, base_dir, messages) if messages: - exit(10) + sys.exit(10) def test_python_module(path, name, base_dir, messages): """Test the given python module by importing it. diff --git a/test/lib/ansible_test/_data/sanity/pylint/plugins/blacklist.py b/test/lib/ansible_test/_data/sanity/pylint/plugins/blacklist.py index 611a30a20e5..ac53aedaa65 100644 --- a/test/lib/ansible_test/_data/sanity/pylint/plugins/blacklist.py +++ b/test/lib/ansible_test/_data/sanity/pylint/plugins/blacklist.py @@ -25,7 +25,7 @@ class BlacklistEntry: """ self.alternative = alternative self.modules_only = modules_only - self.names = set(names) if names else None + self.names = set(names) if names else set() self.ignore_paths = ignore_paths def applies_to(self, path, name=None): diff --git a/test/lib/ansible_test/_internal/cli.py b/test/lib/ansible_test/_internal/cli.py index d1624f78b04..341ae06e0a4 100644 --- a/test/lib/ansible_test/_internal/cli.py +++ b/test/lib/ansible_test/_internal/cli.py @@ -183,15 +183,15 @@ def main(): display.review_warnings() except ApplicationWarning as ex: display.warning(u'%s' % ex) - exit(0) + sys.exit(0) except ApplicationError as ex: display.error(u'%s' % ex) - exit(1) + sys.exit(1) except KeyboardInterrupt: - exit(2) + sys.exit(2) except IOError as ex: if ex.errno == errno.EPIPE: - exit(3) + sys.exit(3) raise @@ -675,7 +675,7 @@ def key_value(argparse, value): # type: (argparse_module, str) -> t.Tuple[str, if len(parts) != 2: raise argparse.ArgumentTypeError('"%s" must be in the format "key=value"' % value) - return tuple(parts) + return parts[0], parts[1] # noinspection PyProtectedMember diff --git a/test/lib/ansible_test/_internal/cloud/vcenter.py b/test/lib/ansible_test/_internal/cloud/vcenter.py index eb387375b96..37cfa6d5e6b 100644 --- a/test/lib/ansible_test/_internal/cloud/vcenter.py +++ b/test/lib/ansible_test/_internal/cloud/vcenter.py @@ -14,6 +14,7 @@ from ..util import ( find_executable, display, ConfigParser, + ApplicationError, ) from ..docker_util import ( @@ -85,8 +86,7 @@ class VcenterProvider(CloudProvider): self._use_static_config() self._setup_static() else: - display.error('Unknown vmware_test_platform: %s' % self.vmware_test_platform) - exit(1) + raise ApplicationError('Unknown vmware_test_platform: %s' % self.vmware_test_platform) def get_docker_run_options(self): """Get any additional options needed when delegating tests to a docker container. @@ -161,8 +161,8 @@ class VcenterProvider(CloudProvider): def _setup_static(self): if not os.path.exists(self.config_static_path): - display.error('Configuration file does not exist: %s' % self.config_static_path) - exit(1) + raise ApplicationError('Configuration file does not exist: %s' % self.config_static_path) + parser = ConfigParser({ 'vcenter_port': '443', 'vmware_proxy_host': '', diff --git a/test/lib/ansible_test/_internal/sanity/__init__.py b/test/lib/ansible_test/_internal/sanity/__init__.py index 70524c640f1..2fdf62e5265 100644 --- a/test/lib/ansible_test/_internal/sanity/__init__.py +++ b/test/lib/ansible_test/_internal/sanity/__init__.py @@ -665,7 +665,7 @@ class SanityTest(ABC): """A tuple of supported Python versions or None if the test does not depend on specific Python versions.""" return tuple(python_version for python_version in SUPPORTED_PYTHON_VERSIONS if python_version.startswith('3.')) - def filter_targets(self, targets): # type: (t.List[TestTarget]) -> t.List[TestTarget] + def filter_targets(self, targets): # type: (t.List[TestTarget]) -> t.List[TestTarget] # pylint: disable=unused-argument """Return the given list of test targets, filtered to include only those relevant for the test.""" if self.no_targets: return []