diff options
author | Matt Clay <matt@mystile.com> | 2023-02-01 16:00:57 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-01 16:00:57 -0800 |
commit | 91807695c363c765197a982a0266ed3d59e3fac5 (patch) | |
tree | f7224bbed3c65afa82e53a5fe3218123d2a3fb49 /test/units | |
parent | 0a53309f47a7fe914f6b2ac65fc2af149b9cd2ad (diff) | |
download | ansible-91807695c363c765197a982a0266ed3d59e3fac5.tar.gz |
Remove unused unit test code (#79879)
* Remove unused test fixtures
* Removed unused _old_dump_load_cycle method
* Remove Python 2.x compat
* Remove unused code
* Remove unused context manager
* Fix cowsay test
- The test no longer depends on another test to initialize config.
- Also remove unreachable code.
* Remove Python 2.x compat
Diffstat (limited to 'test/units')
-rw-r--r-- | test/units/mock/procenv.py | 20 | ||||
-rw-r--r-- | test/units/mock/yaml_helper.py | 73 | ||||
-rw-r--r-- | test/units/test_constants.py | 94 | ||||
-rw-r--r-- | test/units/utils/display/test_broken_cowsay.py | 7 | ||||
-rw-r--r-- | test/units/utils/test_unsafe_proxy.py | 6 |
5 files changed, 5 insertions, 195 deletions
diff --git a/test/units/mock/procenv.py b/test/units/mock/procenv.py index 271a207e2e..7a61bfc37c 100644 --- a/test/units/mock/procenv.py +++ b/test/units/mock/procenv.py @@ -54,26 +54,6 @@ def swap_stdin_and_argv(stdin_data='', argv_data=tuple()): sys.argv = real_argv -@contextmanager -def swap_stdout(): - """ - context manager that temporarily replaces stdout for tests that need to verify output - """ - old_stdout = sys.stdout - - if PY3: - fake_stream = StringIO() - else: - fake_stream = BytesIO() - - try: - sys.stdout = fake_stream - - yield fake_stream - finally: - sys.stdout = old_stdout - - class ModuleTestCase(unittest.TestCase): def setUp(self, module_args=None): if module_args is None: diff --git a/test/units/mock/yaml_helper.py b/test/units/mock/yaml_helper.py index 1ef172159d..9f8b063b6b 100644 --- a/test/units/mock/yaml_helper.py +++ b/test/units/mock/yaml_helper.py @@ -4,8 +4,6 @@ __metaclass__ = type import io import yaml -from ansible.module_utils.six import PY3 -from ansible.parsing.yaml.loader import AnsibleLoader from ansible.parsing.yaml.dumper import AnsibleDumper @@ -15,21 +13,14 @@ class YamlTestUtils(object): """Vault related tests will want to override this. Vault cases should setup a AnsibleLoader that has the vault password.""" - return AnsibleLoader(stream) def _dump_stream(self, obj, stream, dumper=None): """Dump to a py2-unicode or py3-string stream.""" - if PY3: - return yaml.dump(obj, stream, Dumper=dumper) - else: - return yaml.dump(obj, stream, Dumper=dumper, encoding=None) + return yaml.dump(obj, stream, Dumper=dumper) def _dump_string(self, obj, dumper=None): """Dump to a py2-unicode or py3-string""" - if PY3: - return yaml.dump(obj, Dumper=dumper) - else: - return yaml.dump(obj, Dumper=dumper, encoding=None) + return yaml.dump(obj, Dumper=dumper) def _dump_load_cycle(self, obj): # Each pass though a dump or load revs the 'generation' @@ -62,63 +53,3 @@ class YamlTestUtils(object): # should be transitive, but... self.assertEqual(obj_2, obj_3) self.assertEqual(string_from_object_dump, string_from_object_dump_3) - - def _old_dump_load_cycle(self, obj): - '''Dump the passed in object to yaml, load it back up, dump again, compare.''' - stream = io.StringIO() - - yaml_string = self._dump_string(obj, dumper=AnsibleDumper) - self._dump_stream(obj, stream, dumper=AnsibleDumper) - - yaml_string_from_stream = stream.getvalue() - - # reset stream - stream.seek(0) - - loader = self._loader(stream) - # loader = AnsibleLoader(stream, vault_password=self.vault_password) - obj_from_stream = loader.get_data() - - stream_from_string = io.StringIO(yaml_string) - loader2 = self._loader(stream_from_string) - # loader2 = AnsibleLoader(stream_from_string, vault_password=self.vault_password) - obj_from_string = loader2.get_data() - - stream_obj_from_stream = io.StringIO() - stream_obj_from_string = io.StringIO() - - if PY3: - yaml.dump(obj_from_stream, stream_obj_from_stream, Dumper=AnsibleDumper) - yaml.dump(obj_from_stream, stream_obj_from_string, Dumper=AnsibleDumper) - else: - yaml.dump(obj_from_stream, stream_obj_from_stream, Dumper=AnsibleDumper, encoding=None) - yaml.dump(obj_from_stream, stream_obj_from_string, Dumper=AnsibleDumper, encoding=None) - - yaml_string_stream_obj_from_stream = stream_obj_from_stream.getvalue() - yaml_string_stream_obj_from_string = stream_obj_from_string.getvalue() - - stream_obj_from_stream.seek(0) - stream_obj_from_string.seek(0) - - if PY3: - yaml_string_obj_from_stream = yaml.dump(obj_from_stream, Dumper=AnsibleDumper) - yaml_string_obj_from_string = yaml.dump(obj_from_string, Dumper=AnsibleDumper) - else: - yaml_string_obj_from_stream = yaml.dump(obj_from_stream, Dumper=AnsibleDumper, encoding=None) - yaml_string_obj_from_string = yaml.dump(obj_from_string, Dumper=AnsibleDumper, encoding=None) - - assert yaml_string == yaml_string_obj_from_stream - assert yaml_string == yaml_string_obj_from_stream == yaml_string_obj_from_string - assert (yaml_string == yaml_string_obj_from_stream == yaml_string_obj_from_string == yaml_string_stream_obj_from_stream == - yaml_string_stream_obj_from_string) - assert obj == obj_from_stream - assert obj == obj_from_string - assert obj == yaml_string_obj_from_stream - assert obj == yaml_string_obj_from_string - assert obj == obj_from_stream == obj_from_string == yaml_string_obj_from_stream == yaml_string_obj_from_string - return {'obj': obj, - 'yaml_string': yaml_string, - 'yaml_string_from_stream': yaml_string_from_stream, - 'obj_from_stream': obj_from_stream, - 'obj_from_string': obj_from_string, - 'yaml_string_obj_from_string': yaml_string_obj_from_string} diff --git a/test/units/test_constants.py b/test/units/test_constants.py deleted file mode 100644 index a206d2310f..0000000000 --- a/test/units/test_constants.py +++ /dev/null @@ -1,94 +0,0 @@ -# -*- coding: utf-8 -*- -# (c) 2017 Toshio Kuratomi <tkuratomi@ansible.com> -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see <http://www.gnu.org/licenses/>. - -# Make coding more python3-ish -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - -import pwd -import os - -import pytest - -from ansible import constants -from ansible.module_utils.six import StringIO -from ansible.module_utils.six.moves import configparser -from ansible.module_utils._text import to_text - - -@pytest.fixture -def cfgparser(): - CFGDATA = StringIO(""" -[defaults] -defaults_one = 'data_defaults_one' - -[level1] -level1_one = 'data_level1_one' - """) - p = configparser.ConfigParser() - p.readfp(CFGDATA) - return p - - -@pytest.fixture -def user(): - user = {} - user['uid'] = os.geteuid() - - pwd_entry = pwd.getpwuid(user['uid']) - user['username'] = pwd_entry.pw_name - user['home'] = pwd_entry.pw_dir - - return user - - -@pytest.fixture -def cfg_file(): - data = '/ansible/test/cfg/path' - old_cfg_file = constants.CONFIG_FILE - constants.CONFIG_FILE = os.path.join(data, 'ansible.cfg') - yield data - - constants.CONFIG_FILE = old_cfg_file - - -@pytest.fixture -def null_cfg_file(): - old_cfg_file = constants.CONFIG_FILE - del constants.CONFIG_FILE - yield - - constants.CONFIG_FILE = old_cfg_file - - -@pytest.fixture -def cwd(): - data = '/ansible/test/cwd/' - old_cwd = os.getcwd - os.getcwd = lambda: data - - old_cwdu = None - if hasattr(os, 'getcwdu'): - old_cwdu = os.getcwdu - os.getcwdu = lambda: to_text(data) - - yield data - - os.getcwd = old_cwd - if hasattr(os, 'getcwdu'): - os.getcwdu = old_cwdu diff --git a/test/units/utils/display/test_broken_cowsay.py b/test/units/utils/display/test_broken_cowsay.py index d888010ae5..96157e1a8b 100644 --- a/test/units/utils/display/test_broken_cowsay.py +++ b/test/units/utils/display/test_broken_cowsay.py @@ -12,16 +12,13 @@ from unittest.mock import MagicMock def test_display_with_fake_cowsay_binary(capsys, mocker): - mocker.patch("ansible.constants.ANSIBLE_COW_PATH", "./cowsay.sh") + display = Display() - def mock_communicate(input=None, timeout=None): - return b"", b"" + mocker.patch("ansible.constants.ANSIBLE_COW_PATH", "./cowsay.sh") mock_popen = MagicMock() - mock_popen.return_value.communicate = mock_communicate mock_popen.return_value.returncode = 1 mocker.patch("subprocess.Popen", mock_popen) - display = Display() assert not hasattr(display, "cows_available") assert display.b_cowsay is None diff --git a/test/units/utils/test_unsafe_proxy.py b/test/units/utils/test_unsafe_proxy.py index ea653cfeab..3c40a7a321 100644 --- a/test/units/utils/test_unsafe_proxy.py +++ b/test/units/utils/test_unsafe_proxy.py @@ -5,7 +5,6 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type -from ansible.module_utils.six import PY3 from ansible.utils.unsafe_proxy import AnsibleUnsafe, AnsibleUnsafeBytes, AnsibleUnsafeText, wrap_var from ansible.module_utils.common.text.converters import to_text, to_bytes @@ -19,10 +18,7 @@ def test_wrap_var_bytes(): def test_wrap_var_string(): - if PY3: - assert isinstance(wrap_var('foo'), AnsibleUnsafeText) - else: - assert isinstance(wrap_var('foo'), AnsibleUnsafeBytes) + assert isinstance(wrap_var('foo'), AnsibleUnsafeText) def test_wrap_var_dict(): |