From 207a5fbebba79f348185d6335c82028d175db0df Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Thu, 1 Aug 2024 23:21:08 -0700 Subject: [PATCH] test: Handle Singleton Display class (#83673) Fixes: #83538 Signed-off-by: Abhijeet Kasurde --- test/units/utils/conftest.py | 18 ++++++++++++++++++ test/units/utils/display/test_broken_cowsay.py | 2 +- test/units/utils/display/test_display.py | 2 +- test/units/utils/test_display.py | 16 ++++++++-------- 4 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 test/units/utils/conftest.py diff --git a/test/units/utils/conftest.py b/test/units/utils/conftest.py new file mode 100644 index 00000000000..2528ae34424 --- /dev/null +++ b/test/units/utils/conftest.py @@ -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) diff --git a/test/units/utils/display/test_broken_cowsay.py b/test/units/utils/display/test_broken_cowsay.py index 50691c229b3..2dd697c0a78 100644 --- a/test/units/utils/display/test_broken_cowsay.py +++ b/test/units/utils/display/test_broken_cowsay.py @@ -9,7 +9,7 @@ from ansible.utils.display import Display 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") mock_popen = MagicMock() diff --git a/test/units/utils/display/test_display.py b/test/units/utils/display/test_display.py index af5f6597d00..660db0cf66e 100644 --- a/test/units/utils/display/test_display.py +++ b/test/units/utils/display/test_display.py @@ -8,7 +8,7 @@ from __future__ import annotations from ansible.utils.display import Display -def test_display_basic_message(capsys, mocker): +def test_display_basic_message(capsys, mocker, display_resource): # Disable logging mocker.patch('ansible.utils.display.logger', return_value=None) diff --git a/test/units/utils/test_display.py b/test/units/utils/test_display.py index ae7b16bb12b..9c2d9c26f63 100644 --- a/test/units/utils/test_display.py +++ b/test/units/utils/test_display.py @@ -54,7 +54,7 @@ def test_get_text_width_no_locale(problematic_wcswidth_chars): 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, '') display = Display() display_mock = MagicMock() @@ -67,7 +67,7 @@ def test_Display_banner_get_text_width(monkeypatch): 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') display = Display() display_mock = MagicMock() @@ -80,12 +80,12 @@ def test_Display_banner_get_text_width_fallback(monkeypatch): assert msg.endswith(stars) -def test_Display_set_queue_parent(): +def test_Display_set_queue_parent(display_resource): display = Display() pytest.raises(RuntimeError, display.set_queue, 'foo') -def test_Display_set_queue_fork(): +def test_Display_set_queue_fork(display_resource): def test(): display = Display() display.set_queue('foo') @@ -96,7 +96,7 @@ def test_Display_set_queue_fork(): assert p.exitcode == 0 -def test_Display_display_fork(): +def test_Display_display_fork(display_resource): def test(): queue = MagicMock() display = Display() @@ -110,7 +110,7 @@ def test_Display_display_fork(): assert p.exitcode == 0 -def test_Display_display_warn_fork(): +def test_Display_display_warn_fork(display_resource): def test(): queue = MagicMock() display = Display() @@ -124,7 +124,7 @@ def test_Display_display_warn_fork(): assert p.exitcode == 0 -def test_Display_display_lock(monkeypatch): +def test_Display_display_lock(monkeypatch, display_resource): lock = MagicMock() display = Display() monkeypatch.setattr(display, '_lock', lock) @@ -132,7 +132,7 @@ def test_Display_display_lock(monkeypatch): lock.__enter__.assert_called_once_with() -def test_Display_display_lock_fork(monkeypatch): +def test_Display_display_lock_fork(monkeypatch, display_resource): lock = MagicMock() display = Display() monkeypatch.setattr(display, '_lock', lock)