From dae4e2f07ff3132797252262206e0427218e79c3 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Tue, 7 Nov 2023 12:41:42 -0800 Subject: [PATCH] Remove unused/unreachable unit test code (#82160) --- .../module_utils/basic/test_filesystem.py | 2 -- test/units/module_utils/basic/test_no_log.py | 2 +- .../module_utils/basic/test_run_command.py | 11 -------- test/units/module_utils/basic/test_tmpdir.py | 2 -- test/units/module_utils/test_distro.py | 6 ----- .../test_collection_loader.py | 26 +++++++++---------- 6 files changed, 14 insertions(+), 35 deletions(-) diff --git a/test/units/module_utils/basic/test_filesystem.py b/test/units/module_utils/basic/test_filesystem.py index 45d3dbcc9b7..9991b769da3 100644 --- a/test/units/module_utils/basic/test_filesystem.py +++ b/test/units/module_utils/basic/test_filesystem.py @@ -49,8 +49,6 @@ class TestOtherFilesystem(ModuleTestCase): def _mock_ismount(path): if path == b'/subdir/mount': return True - if path == b'/': - return True return False with patch('os.path.ismount', side_effect=_mock_ismount): diff --git a/test/units/module_utils/basic/test_no_log.py b/test/units/module_utils/basic/test_no_log.py index 0b04a74e38c..c8c3ecea688 100644 --- a/test/units/module_utils/basic/test_no_log.py +++ b/test/units/module_utils/basic/test_no_log.py @@ -146,7 +146,7 @@ class TestRemoveValues(unittest.TestCase): levels = 0 inner_list = actual_data_list - while inner_list: + while True: if isinstance(inner_list, list): self.assertEqual(len(inner_list), 1) else: diff --git a/test/units/module_utils/basic/test_run_command.py b/test/units/module_utils/basic/test_run_command.py index 56c681aac25..2f77d5f3747 100644 --- a/test/units/module_utils/basic/test_run_command.py +++ b/test/units/module_utils/basic/test_run_command.py @@ -4,7 +4,6 @@ from __future__ import annotations -import errno import selectors from itertools import product from io import BytesIO @@ -26,10 +25,6 @@ class OpenBytesIO(BytesIO): @pytest.fixture def mock_os(mocker): - def mock_os_chdir(path): - if path == '/inaccessible': - raise OSError(errno.EPERM, "Permission denied: '/inaccessible'") - def mock_os_abspath(path): if path.startswith('/'): return path @@ -43,17 +38,11 @@ def mock_os(mocker): os.environ = {'PATH': '/bin'} os.getcwd.return_value = '/home/foo' os.path.isdir.return_value = True - os.chdir.side_effect = mock_os_chdir os.path.abspath.side_effect = mock_os_abspath yield os -class DummyFileObj(): - def __init__(self, fileobj): - self.fileobj = fileobj - - class SpecialBytesIO(BytesIO): def __init__(self, *args, **kwargs): fh = kwargs.pop('fh', None) diff --git a/test/units/module_utils/basic/test_tmpdir.py b/test/units/module_utils/basic/test_tmpdir.py index 0e4f53d7115..65d68011276 100644 --- a/test/units/module_utils/basic/test_tmpdir.py +++ b/test/units/module_utils/basic/test_tmpdir.py @@ -6,7 +6,6 @@ from __future__ import annotations import json import os -import shutil import tempfile import pytest @@ -76,7 +75,6 @@ class TestAnsibleModuleTmpDir: monkeypatch.setattr(tempfile, 'mkdtemp', mock_mkdtemp) monkeypatch.setattr(os.path, 'exists', lambda x: stat_exists) monkeypatch.setattr(os, 'makedirs', mock_makedirs) - monkeypatch.setattr(shutil, 'rmtree', lambda x: None) monkeypatch.setattr(basic, '_ANSIBLE_ARGS', to_bytes(json.dumps({'ANSIBLE_MODULE_ARGS': args}))) with patch('time.time', return_value=42): diff --git a/test/units/module_utils/test_distro.py b/test/units/module_utils/test_distro.py index 55a40f5a5c1..462651c4cc3 100644 --- a/test/units/module_utils/test_distro.py +++ b/test/units/module_utils/test_distro.py @@ -29,9 +29,3 @@ class TestDistro(): def test_id(self): id = distro.id() assert isinstance(id, str), 'distro.id() returned %s (%s) which is not a string' % (id, type(id)) - - def test_opensuse_leap_id(self): - name = distro.name() - if name == 'openSUSE Leap': - id = distro.id() - assert id == 'opensuse', "OpenSUSE Leap did not return 'opensuse' as id" diff --git a/test/units/utils/collection_loader/test_collection_loader.py b/test/units/utils/collection_loader/test_collection_loader.py index 172b4f70aa8..51aab2c5cea 100644 --- a/test/units/utils/collection_loader/test_collection_loader.py +++ b/test/units/utils/collection_loader/test_collection_loader.py @@ -185,8 +185,7 @@ def test_root_loader(): name = 'ansible_collections' # ensure this works even when ansible_collections doesn't exist on disk for paths in [], default_test_collection_paths: - if name in sys.modules: - del sys.modules[name] + sys.modules.pop(name, None) loader = _AnsibleCollectionRootPkgLoader(name, paths) assert repr(loader).startswith('_AnsibleCollectionRootPkgLoader(path=') module = loader.load_module(name) @@ -213,8 +212,7 @@ def test_nspkg_loader_load_module(): module_to_load = name.rpartition('.')[2] paths = extend_paths(default_test_collection_paths, parent_pkg) existing_child_paths = [p for p in extend_paths(paths, module_to_load) if os.path.exists(p)] - if name in sys.modules: - del sys.modules[name] + sys.modules.pop(name, None) loader = _AnsibleCollectionNSPkgLoader(name, path_list=paths) assert repr(loader).startswith('_AnsibleCollectionNSPkgLoader(path=') module = loader.load_module(name) @@ -243,8 +241,7 @@ def test_collpkg_loader_load_module(): paths = extend_paths(default_test_collection_paths, parent_pkg) existing_child_paths = [p for p in extend_paths(paths, module_to_load) if os.path.exists(p)] is_builtin = 'ansible.builtin' in name - if name in sys.modules: - del sys.modules[name] + sys.modules.pop(name, None) loader = _AnsibleCollectionPkgLoader(name, path_list=paths) assert repr(loader).startswith('_AnsibleCollectionPkgLoader(path=') module = loader.load_module(name) @@ -266,13 +263,16 @@ def test_collpkg_loader_load_module(): # FIXME: validate _collection_meta contents match what's on disk (or not) - # if the module has metadata, try loading it with busted metadata - if module._collection_meta: - _collection_finder = import_module('ansible.utils.collection_loader._collection_finder') - with patch.object(_collection_finder, '_meta_yml_to_dict', side_effect=Exception('bang')): - with pytest.raises(Exception) as ex: - _AnsibleCollectionPkgLoader(name, path_list=paths).load_module(name) - assert 'error parsing collection metadata' in str(ex.value) + # verify the module has metadata, then try loading it with busted metadata + assert module._collection_meta + + _collection_finder = import_module('ansible.utils.collection_loader._collection_finder') + + with patch.object(_collection_finder, '_meta_yml_to_dict', side_effect=Exception('bang')): + with pytest.raises(Exception) as ex: + _AnsibleCollectionPkgLoader(name, path_list=paths).load_module(name) + + assert 'error parsing collection metadata' in str(ex.value) def test_coll_loader():