From 6627ba9acecb090ca1dd391c46630b3e1aad4e73 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Mon, 21 Aug 2023 15:54:03 -0700 Subject: [PATCH] Unit test cleanup (#81556) * Fix cleanup_tmp_file unit tests * Simplify the problematic wcwidth chars unit test --- test/units/utils/test_cleanup_tmp_file.py | 25 ++++++++++++----------- test/units/utils/test_display.py | 17 ++++++--------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/test/units/utils/test_cleanup_tmp_file.py b/test/units/utils/test_cleanup_tmp_file.py index 2a44a55b156..35374f4da70 100644 --- a/test/units/utils/test_cleanup_tmp_file.py +++ b/test/units/utils/test_cleanup_tmp_file.py @@ -6,16 +6,11 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type import os -import pytest import tempfile from ansible.utils.path import cleanup_tmp_file -def raise_error(): - raise OSError - - def test_cleanup_tmp_file_file(): tmp_fd, tmp = tempfile.mkstemp() cleanup_tmp_file(tmp) @@ -34,15 +29,21 @@ def test_cleanup_tmp_file_nonexistant(): assert None is cleanup_tmp_file('nope') -def test_cleanup_tmp_file_failure(mocker): +def test_cleanup_tmp_file_failure(mocker, capsys): tmp = tempfile.mkdtemp() - with pytest.raises(Exception): - mocker.patch('shutil.rmtree', side_effect=raise_error()) - cleanup_tmp_file(tmp) + rmtree = mocker.patch('shutil.rmtree', side_effect=OSError('test induced failure')) + cleanup_tmp_file(tmp) + out, err = capsys.readouterr() + assert out == '' + assert err == '' + rmtree.assert_called_once() def test_cleanup_tmp_file_failure_warning(mocker, capsys): tmp = tempfile.mkdtemp() - with pytest.raises(Exception): - mocker.patch('shutil.rmtree', side_effect=raise_error()) - cleanup_tmp_file(tmp, warn=True) + rmtree = mocker.patch('shutil.rmtree', side_effect=OSError('test induced failure')) + cleanup_tmp_file(tmp, warn=True) + out, err = capsys.readouterr() + assert out == 'Unable to remove temporary file test induced failure\n' + assert err == '' + rmtree.assert_called_once() diff --git a/test/units/utils/test_display.py b/test/units/utils/test_display.py index 94dfc9407ca..80b7a099c86 100644 --- a/test/units/utils/test_display.py +++ b/test/units/utils/test_display.py @@ -18,16 +18,14 @@ from ansible.utils.multiprocessing import context as multiprocessing_context @pytest.fixture def problematic_wcswidth_chars(): - problematic = [] - try: - locale.setlocale(locale.LC_ALL, 'C.UTF-8') - except Exception: - return problematic + locale.setlocale(locale.LC_ALL, 'C.UTF-8') candidates = set(chr(c) for c in range(sys.maxunicode) if unicodedata.category(chr(c)) == 'Cf') - for c in candidates: - if _LIBC.wcswidth(c, _MAX_INT) == -1: - problematic.append(c) + problematic = [candidate for candidate in candidates if _LIBC.wcswidth(candidate, _MAX_INT) == -1] + + if not problematic: + # Newer distributions (Ubuntu 22.04, Fedora 38) include a libc which does not report problematic characters. + pytest.skip("no problematic wcswidth chars found") # pragma: nocover return problematic @@ -54,9 +52,6 @@ def test_get_text_width(): def test_get_text_width_no_locale(problematic_wcswidth_chars): - if not problematic_wcswidth_chars: - pytest.skip("No problmatic wcswidth chars") - locale.setlocale(locale.LC_ALL, 'C.UTF-8') pytest.raises(EnvironmentError, get_text_width, problematic_wcswidth_chars[0])