Improve coverage of role unit tests (#80695)

pull/80703/head
Matt Clay 2 years ago committed by GitHub
parent be6f2f43b6
commit 23e2ca91c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -108,8 +108,6 @@ class TestIncludeRole(unittest.TestCase):
# skip meta: role_complete
continue
role = task._role
if not role:
continue
yield (role.get_name(),
self.var_manager.get_vars(play=play, task=task))
@ -201,7 +199,7 @@ class TestIncludeRole(unittest.TestCase):
self.assertEqual(task_vars.get('l3_variable'), 'l3-main')
self.assertEqual(task_vars.get('test_variable'), 'l3-main')
else:
self.fail()
self.fail() # pragma: nocover
self.assertFalse(expected_roles)
@patch('ansible.playbook.role.definition.unfrackpath',
@ -247,5 +245,5 @@ class TestIncludeRole(unittest.TestCase):
self.assertEqual(task_vars.get('l3_variable'), 'l3-alt')
self.assertEqual(task_vars.get('test_variable'), 'l3-alt')
else:
self.fail()
self.fail() # pragma: nocover
self.assertFalse(expected_roles)

@ -21,6 +21,8 @@ __metaclass__ = type
from collections.abc import Container
import pytest
from units.compat import unittest
from unittest.mock import patch, MagicMock
@ -42,12 +44,9 @@ class TestHashParams(unittest.TestCase):
self._assert_set(res)
self._assert_hashable(res)
def _assert_hashable(self, res):
a_dict = {}
try:
a_dict[res] = res
except TypeError as e:
self.fail('%s is not hashable: %s' % (res, e))
@staticmethod
def _assert_hashable(res):
hash(res)
def _assert_set(self, res):
self.assertIsInstance(res, frozenset)
@ -87,36 +86,28 @@ class TestHashParams(unittest.TestCase):
def test_generator(self):
def my_generator():
for i in ['a', 1, None, {}]:
yield i
yield
params = my_generator()
res = hash_params(params)
self._assert_hashable(res)
assert list(params)
def test_container_but_not_iterable(self):
# This is a Container that is not iterable, which is unlikely but...
class MyContainer(Container):
def __init__(self, some_thing):
self.data = []
self.data.append(some_thing)
def __init__(self, _some_thing):
pass
def __contains__(self, item):
return item in self.data
def __hash__(self):
return hash(self.data)
def __len__(self):
return len(self.data)
"""Implementation omitted, since it will never be called."""
def __call__(self):
return False
params = MyContainer('foo bar')
foo = MyContainer('foo bar')
params = foo
with pytest.raises(TypeError) as ex:
hash_params(params)
self.assertRaises(TypeError, hash_params, params)
assert ex.value.args == ("'MyContainer' object is not iterable",)
def test_param_dict_dupe_values(self):
params1 = {'foo': False}

Loading…
Cancel
Save