Unit test cleanup (#81556)

* Fix cleanup_tmp_file unit tests

* Simplify the problematic wcwidth chars unit test
pull/81559/head
Matt Clay 10 months ago committed by GitHub
parent f28e32c063
commit 6627ba9ace
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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()

@ -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])

Loading…
Cancel
Save