From 23e2ca91c2b73280e84e805d04be3a768b584aee Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Tue, 2 May 2023 10:53:42 -0700 Subject: [PATCH] Improve coverage of role unit tests (#80695) --- test/units/playbook/role/test_include_role.py | 6 +-- test/units/playbook/role/test_role.py | 37 +++++++------------ 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/test/units/playbook/role/test_include_role.py b/test/units/playbook/role/test_include_role.py index 5e7625ba14c..aa97da15d46 100644 --- a/test/units/playbook/role/test_include_role.py +++ b/test/units/playbook/role/test_include_role.py @@ -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) diff --git a/test/units/playbook/role/test_role.py b/test/units/playbook/role/test_role.py index 0690b11ff2e..3604772008b 100644 --- a/test/units/playbook/role/test_role.py +++ b/test/units/playbook/role/test_role.py @@ -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}