More unit test code coverage improvements (#81136)

pull/81138/head
Matt Clay 1 year ago committed by GitHub
parent a5a4903b03
commit 5f58775a1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -28,22 +28,17 @@ cfg_in_homedir = os.path.expanduser('~/.ansible.cfg')
@pytest.fixture
def setup_env(request):
def setup_env(request, monkeypatch):
cur_config = os.environ.get('ANSIBLE_CONFIG', None)
cfg_path = request.param[0]
if cfg_path is None and cur_config:
del os.environ['ANSIBLE_CONFIG']
monkeypatch.delenv('ANSIBLE_CONFIG')
else:
os.environ['ANSIBLE_CONFIG'] = request.param[0]
monkeypatch.setenv('ANSIBLE_CONFIG', request.param[0])
yield
if cur_config is None and cfg_path:
del os.environ['ANSIBLE_CONFIG']
else:
os.environ['ANSIBLE_CONFIG'] = cur_config
@pytest.fixture
def setup_existing_files(request, monkeypatch):
@ -54,10 +49,8 @@ def setup_existing_files(request, monkeypatch):
return False
def _os_access(path, access):
if to_text(path) in (request.param[0]):
return True
else:
return False
assert to_text(path) in (request.param[0])
return True
# Enable user and system dirs so that we know cwd takes precedence
monkeypatch.setattr("os.path.exists", _os_path_exists)
@ -162,13 +155,11 @@ class TestFindIniFile:
real_stat = os.stat
def _os_stat(path):
if path == working_dir:
from posix import stat_result
stat_info = list(real_stat(path))
stat_info[stat.ST_MODE] |= stat.S_IWOTH
return stat_result(stat_info)
else:
return real_stat(path)
assert path == working_dir
from posix import stat_result
stat_info = list(real_stat(path))
stat_info[stat.ST_MODE] |= stat.S_IWOTH
return stat_result(stat_info)
monkeypatch.setattr('os.stat', _os_stat)
@ -187,13 +178,11 @@ class TestFindIniFile:
real_stat = os.stat
def _os_stat(path):
if path == working_dir:
from posix import stat_result
stat_info = list(real_stat(path))
stat_info[stat.ST_MODE] |= stat.S_IWOTH
return stat_result(stat_info)
else:
return real_stat(path)
assert path == working_dir
from posix import stat_result
stat_info = list(real_stat(path))
stat_info[stat.ST_MODE] |= stat.S_IWOTH
return stat_result(stat_info)
monkeypatch.setattr('os.stat', _os_stat)
@ -215,14 +204,14 @@ class TestFindIniFile:
real_stat = os.stat
def _os_stat(path):
if path == working_dir:
from posix import stat_result
stat_info = list(real_stat(path))
stat_info[stat.ST_MODE] |= stat.S_IWOTH
return stat_result(stat_info)
else:
if path != working_dir:
return real_stat(path)
from posix import stat_result
stat_info = list(real_stat(path))
stat_info[stat.ST_MODE] |= stat.S_IWOTH
return stat_result(stat_info)
monkeypatch.setattr('os.stat', _os_stat)
warnings = set()
@ -240,13 +229,11 @@ class TestFindIniFile:
real_stat = os.stat
def _os_stat(path):
if path == working_dir:
from posix import stat_result
stat_info = list(real_stat(path))
stat_info[stat.ST_MODE] |= stat.S_IWOTH
return stat_result(stat_info)
else:
return real_stat(path)
assert path == working_dir
from posix import stat_result
stat_info = list(real_stat(path))
stat_info[stat.ST_MODE] |= stat.S_IWOTH
return stat_result(stat_info)
monkeypatch.setattr('os.stat', _os_stat)

@ -21,7 +21,6 @@ __metaclass__ = type
import os
from ansible.errors import AnsibleParserError
from ansible.parsing.dataloader import DataLoader
from ansible.module_utils.common.text.converters import to_bytes, to_text
@ -48,11 +47,7 @@ class DictDataLoader(DataLoader):
# TODO: the real _get_file_contents returns a bytestring, so we actually convert the
# unicode/text it's created with to utf-8
def _get_file_contents(self, file_name):
path = to_text(file_name)
if path in self._file_mapping:
return to_bytes(self._file_mapping[file_name]), False
else:
raise AnsibleParserError("file not found: %s" % file_name)
return to_bytes(self._file_mapping[file_name]), False
def path_exists(self, path):
path = to_text(path)
@ -91,25 +86,6 @@ class DictDataLoader(DataLoader):
self._add_known_directory(dirname)
dirname = os.path.dirname(dirname)
def push(self, path, content):
rebuild_dirs = False
if path not in self._file_mapping:
rebuild_dirs = True
self._file_mapping[path] = content
if rebuild_dirs:
self._build_known_directories()
def pop(self, path):
if path in self._file_mapping:
del self._file_mapping[path]
self._build_known_directories()
def clear(self):
self._file_mapping = dict()
self._known_directories = []
def get_basedir(self):
return os.getcwd()

@ -55,9 +55,8 @@ def swap_stdin_and_argv(stdin_data='', argv_data=tuple()):
class ModuleTestCase(unittest.TestCase):
def setUp(self, module_args=None):
if module_args is None:
module_args = {'_ansible_remote_tmp': '/tmp', '_ansible_keep_remote_files': False}
def setUp(self):
module_args = {'_ansible_remote_tmp': '/tmp', '_ansible_keep_remote_files': False}
args = json.dumps(dict(ANSIBLE_MODULE_ARGS=module_args))

@ -7,8 +7,6 @@ __metaclass__ = type
from ansible.vars.clean import module_response_deepcopy
import pytest
def test_module_response_deepcopy_basic():
x = 42
@ -37,15 +35,6 @@ def test_module_response_deepcopy_empty_tuple():
assert x is y
@pytest.mark.skip(reason='No current support for this situation')
def test_module_response_deepcopy_tuple():
x = ([1, 2], 3)
y = module_response_deepcopy(x)
assert y == x
assert x is not y
assert x[0] is not y[0]
def test_module_response_deepcopy_tuple_of_immutables():
x = ((1, 2), 3)
y = module_response_deepcopy(x)

Loading…
Cancel
Save