Fixed broken tests (#84088)

* Add `match=` in pytest.raises
* Remove redundant assert statements

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
Co-authored-by: Matt Clay <matt@mystile.com>
pull/84158/head
Abhijeet Kasurde 1 month ago committed by GitHub
parent 1727bbecce
commit 1e6ffc1d02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -158,7 +158,7 @@ class TestAnsibleModuleExitValuesRemoved:
monkeypatch.setattr(warnings, '_global_deprecations', []) monkeypatch.setattr(warnings, '_global_deprecations', [])
expected['failed'] = True expected['failed'] = True
with pytest.raises(SystemExit): with pytest.raises(SystemExit):
am.fail_json(**return_val) == expected am.fail_json(**return_val)
out, err = capfd.readouterr() out, err = capfd.readouterr()
assert json.loads(out) == expected assert json.loads(out) == expected

@ -8,7 +8,6 @@ from __future__ import annotations
import pytest import pytest
from ansible.module_utils.common.parameters import _handle_aliases from ansible.module_utils.common.parameters import _handle_aliases
from ansible.module_utils.common.text.converters import to_native
def test_handle_aliases_no_aliases(): def test_handle_aliases_no_aliases():
@ -21,10 +20,7 @@ def test_handle_aliases_no_aliases():
'path': 'bar' 'path': 'bar'
} }
expected = {} assert _handle_aliases(argument_spec, params) == {}
result = _handle_aliases(argument_spec, params)
assert expected == result
def test_handle_aliases_basic(): def test_handle_aliases_basic():
@ -54,9 +50,8 @@ def test_handle_aliases_value_error():
'name': 'foo', 'name': 'foo',
} }
with pytest.raises(ValueError) as ve: with pytest.raises(ValueError, match='internal error: required and default are mutually exclusive'):
_handle_aliases(argument_spec, params) _handle_aliases(argument_spec, params)
assert 'internal error: aliases must be a list or tuple' == to_native(ve.error)
def test_handle_aliases_type_error(): def test_handle_aliases_type_error():
@ -68,6 +63,5 @@ def test_handle_aliases_type_error():
'name': 'foo', 'name': 'foo',
} }
with pytest.raises(TypeError) as te: with pytest.raises(TypeError, match='internal error: aliases must be a list or tuple'):
_handle_aliases(argument_spec, params) _handle_aliases(argument_spec, params)
assert 'internal error: required and default are mutually exclusive' in to_native(te.error)

@ -1,20 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# (c) 2017, Toshio Kuratomi <tkuratomi@ansible.com> # Copyright: (c) 2017, Toshio Kuratomi <tkuratomi@ansible.com>
# # Copyright: Contributors to the Ansible project
# This file is part of Ansible # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#
# 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/>.
from __future__ import annotations from __future__ import annotations
@ -83,8 +70,8 @@ def test_implicit_file_default_timesout(monkeypatch):
monkeypatch.setattr(timeout, 'DEFAULT_GATHER_TIMEOUT', 1) monkeypatch.setattr(timeout, 'DEFAULT_GATHER_TIMEOUT', 1)
# sleep_time is greater than the default # sleep_time is greater than the default
sleep_time = timeout.DEFAULT_GATHER_TIMEOUT + 1 sleep_time = timeout.DEFAULT_GATHER_TIMEOUT + 1
with pytest.raises(timeout.TimeoutError): with pytest.raises(timeout.TimeoutError, match=r"^Timer expired after"):
assert sleep_amount_implicit(sleep_time) == '(Not expected to succeed)' sleep_amount_implicit(sleep_time)
def test_implicit_file_overridden_succeeds(set_gather_timeout_higher): def test_implicit_file_overridden_succeeds(set_gather_timeout_higher):
@ -96,8 +83,8 @@ def test_implicit_file_overridden_succeeds(set_gather_timeout_higher):
def test_implicit_file_overridden_timesout(set_gather_timeout_lower): def test_implicit_file_overridden_timesout(set_gather_timeout_lower):
# Set sleep_time greater than our new timeout but less than the default # Set sleep_time greater than our new timeout but less than the default
sleep_time = 3 sleep_time = 3
with pytest.raises(timeout.TimeoutError): with pytest.raises(timeout.TimeoutError, match=r"^Timer expired after"):
assert sleep_amount_implicit(sleep_time) == '(Not expected to Succeed)' sleep_amount_implicit(sleep_time)
def test_explicit_succeeds(monkeypatch): def test_explicit_succeeds(monkeypatch):
@ -110,8 +97,8 @@ def test_explicit_succeeds(monkeypatch):
def test_explicit_timeout(): def test_explicit_timeout():
# Set sleep_time greater than our new timeout but less than the default # Set sleep_time greater than our new timeout but less than the default
sleep_time = 3 sleep_time = 3
with pytest.raises(timeout.TimeoutError): with pytest.raises(timeout.TimeoutError, match=r"^Timer expired after"):
assert sleep_amount_explicit_lower(sleep_time) == '(Not expected to succeed)' sleep_amount_explicit_lower(sleep_time)
# #
@ -149,21 +136,21 @@ def function_catches_all_exceptions():
def test_timeout_raises_timeout(): def test_timeout_raises_timeout():
with pytest.raises(timeout.TimeoutError): with pytest.raises(timeout.TimeoutError, match=r"^Timer expired after"):
assert function_times_out() == '(Not expected to succeed)' function_times_out()
@pytest.mark.parametrize('stdin', ({},), indirect=['stdin']) @pytest.mark.parametrize('stdin', ({},), indirect=['stdin'])
def test_timeout_raises_timeout_integration_test(am): def test_timeout_raises_timeout_integration_test(am):
with pytest.raises(timeout.TimeoutError): with pytest.raises(timeout.TimeoutError, match=r"^Timer expired after"):
assert function_times_out_in_run_command(am) == '(Not expected to succeed)' function_times_out_in_run_command(am)
def test_timeout_raises_other_exception(): def test_timeout_raises_other_exception():
with pytest.raises(ZeroDivisionError): with pytest.raises(ZeroDivisionError, match=r"^division by"):
assert function_raises() == '(Not expected to succeed)' function_raises()
def test_exception_not_caught_by_called_code(): def test_exception_not_caught_by_called_code():
with pytest.raises(timeout.TimeoutError): with pytest.raises(timeout.TimeoutError, match=r"^Timer expired after"):
assert function_catches_all_exceptions() == '(Not expected to succeed)' function_catches_all_exceptions()

@ -9,50 +9,32 @@ import pytest
from ansible.module_utils.parsing.convert_bool import boolean from ansible.module_utils.parsing.convert_bool import boolean
class TestBoolean: junk_values = ("flibbity", 42, 42.0, object(), None, 2, -1, 0.1)
def test_bools(self):
assert boolean(True) is True
assert boolean(False) is False @pytest.mark.parametrize(("value", "expected"), [
(True, True),
def test_none(self): (False, False),
with pytest.raises(TypeError): (1, True),
assert boolean(None, strict=True) is False (0, False),
assert boolean(None, strict=False) is False (0.0, False),
("true", True),
def test_numbers(self): ("TRUE", True),
assert boolean(1) is True ("t", True),
assert boolean(0) is False ("yes", True),
assert boolean(0.0) is False ("y", True),
("on", True),
# Current boolean() doesn't consider these to be true values ])
# def test_other_numbers(self): def test_boolean(value: object, expected: bool) -> None:
# assert boolean(2) is True assert boolean(value) is expected
# assert boolean(-1) is True
# assert boolean(0.1) is True
@pytest.mark.parametrize("value", junk_values)
def test_strings(self): def test_junk_values_non_strict(value: object) -> None:
assert boolean("true") is True assert boolean(value, strict=False) is False
assert boolean("TRUE") is True
assert boolean("t") is True
assert boolean("yes") is True @pytest.mark.parametrize("value", junk_values)
assert boolean("y") is True def test_junk_values_strict(value: object) -> None:
assert boolean("on") is True with pytest.raises(TypeError, match=f"^The value '{value}' is not"):
boolean(value, strict=True)
def test_junk_values_nonstrict(self):
assert boolean("flibbity", strict=False) is False
assert boolean(42, strict=False) is False
assert boolean(42.0, strict=False) is False
assert boolean(object(), strict=False) is False
def test_junk_values_strict(self):
with pytest.raises(TypeError):
assert boolean("flibbity", strict=True) is False
with pytest.raises(TypeError):
assert boolean(42, strict=True) is False
with pytest.raises(TypeError):
assert boolean(42.0, strict=True) is False
with pytest.raises(TypeError):
assert boolean(object(), strict=True) is False

@ -71,7 +71,7 @@ class TestCachePluginAdjudicator(unittest.TestCase):
def test_pop_without_default(self): def test_pop_without_default(self):
with pytest.raises(KeyError): with pytest.raises(KeyError):
assert self.cache.pop('foo') self.cache.pop('foo')
def test_pop(self): def test_pop(self):
v = self.cache.pop('cache_key_2') v = self.cache.pop('cache_key_2')

@ -60,8 +60,8 @@ def test_pause_missing_curses(mocker, monkeypatch):
assert mod.HAS_CURSES is False assert mod.HAS_CURSES is False
with pytest.raises(AttributeError): with pytest.raises(AttributeError, match=r"^module 'ansible\.utils\.display' has no attribute"):
assert mod.curses mod.curses # pylint: disable=pointless-statement
assert mod.HAS_CURSES is False assert mod.HAS_CURSES is False
assert mod.MOVE_TO_BOL == b'\r' assert mod.MOVE_TO_BOL == b'\r'

Loading…
Cancel
Save