test: Handle Singleton Display class (#83673)

Fixes: #83538

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/83723/head
Abhijeet Kasurde 4 months ago committed by GitHub
parent 91f680a749
commit 207a5fbebb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,18 @@
# Copyright: Contributors to the Ansible project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import annotations
import pytest
from ansible.utils.display import Display
@pytest.fixture()
def display_resource(request):
Display._Singleton__instance = None
def teardown():
Display._Singleton__instance = None
request.addfinalizer(teardown)

@ -9,7 +9,7 @@ from ansible.utils.display import Display
from unittest.mock import MagicMock from unittest.mock import MagicMock
def test_display_with_fake_cowsay_binary(capsys, mocker): def test_display_with_fake_cowsay_binary(capsys, mocker, display_resource):
mocker.patch("ansible.constants.ANSIBLE_COW_PATH", "./cowsay.sh") mocker.patch("ansible.constants.ANSIBLE_COW_PATH", "./cowsay.sh")
mock_popen = MagicMock() mock_popen = MagicMock()

@ -8,7 +8,7 @@ from __future__ import annotations
from ansible.utils.display import Display from ansible.utils.display import Display
def test_display_basic_message(capsys, mocker): def test_display_basic_message(capsys, mocker, display_resource):
# Disable logging # Disable logging
mocker.patch('ansible.utils.display.logger', return_value=None) mocker.patch('ansible.utils.display.logger', return_value=None)

@ -54,7 +54,7 @@ def test_get_text_width_no_locale(problematic_wcswidth_chars):
pytest.raises(EnvironmentError, get_text_width, problematic_wcswidth_chars[0]) pytest.raises(EnvironmentError, get_text_width, problematic_wcswidth_chars[0])
def test_Display_banner_get_text_width(monkeypatch): def test_Display_banner_get_text_width(monkeypatch, display_resource):
locale.setlocale(locale.LC_ALL, '') locale.setlocale(locale.LC_ALL, '')
display = Display() display = Display()
display_mock = MagicMock() display_mock = MagicMock()
@ -67,7 +67,7 @@ def test_Display_banner_get_text_width(monkeypatch):
assert msg.endswith(stars) assert msg.endswith(stars)
def test_Display_banner_get_text_width_fallback(monkeypatch): def test_Display_banner_get_text_width_fallback(monkeypatch, display_resource):
locale.setlocale(locale.LC_ALL, 'C.UTF-8') locale.setlocale(locale.LC_ALL, 'C.UTF-8')
display = Display() display = Display()
display_mock = MagicMock() display_mock = MagicMock()
@ -80,12 +80,12 @@ def test_Display_banner_get_text_width_fallback(monkeypatch):
assert msg.endswith(stars) assert msg.endswith(stars)
def test_Display_set_queue_parent(): def test_Display_set_queue_parent(display_resource):
display = Display() display = Display()
pytest.raises(RuntimeError, display.set_queue, 'foo') pytest.raises(RuntimeError, display.set_queue, 'foo')
def test_Display_set_queue_fork(): def test_Display_set_queue_fork(display_resource):
def test(): def test():
display = Display() display = Display()
display.set_queue('foo') display.set_queue('foo')
@ -96,7 +96,7 @@ def test_Display_set_queue_fork():
assert p.exitcode == 0 assert p.exitcode == 0
def test_Display_display_fork(): def test_Display_display_fork(display_resource):
def test(): def test():
queue = MagicMock() queue = MagicMock()
display = Display() display = Display()
@ -110,7 +110,7 @@ def test_Display_display_fork():
assert p.exitcode == 0 assert p.exitcode == 0
def test_Display_display_warn_fork(): def test_Display_display_warn_fork(display_resource):
def test(): def test():
queue = MagicMock() queue = MagicMock()
display = Display() display = Display()
@ -124,7 +124,7 @@ def test_Display_display_warn_fork():
assert p.exitcode == 0 assert p.exitcode == 0
def test_Display_display_lock(monkeypatch): def test_Display_display_lock(monkeypatch, display_resource):
lock = MagicMock() lock = MagicMock()
display = Display() display = Display()
monkeypatch.setattr(display, '_lock', lock) monkeypatch.setattr(display, '_lock', lock)
@ -132,7 +132,7 @@ def test_Display_display_lock(monkeypatch):
lock.__enter__.assert_called_once_with() lock.__enter__.assert_called_once_with()
def test_Display_display_lock_fork(monkeypatch): def test_Display_display_lock_fork(monkeypatch, display_resource):
lock = MagicMock() lock = MagicMock()
display = Display() display = Display()
monkeypatch.setattr(display, '_lock', lock) monkeypatch.setattr(display, '_lock', lock)

Loading…
Cancel
Save