| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# (c) 2020 Matt Martz <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import datetime |
| 8 |
import locale |
| 9 |
import sys |
| 10 |
import typing as t |
| 11 |
import unicodedata |
| 12 |
|
| 13 |
from unittest.mock import MagicMock |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
from ansible.module_utils.datatag import deprecator_from_collection_name |
| 18 |
from ansible.module_utils._internal import _deprecator, _errors, _messages |
| 19 |
from ansible.utils.display import _LIBC, _MAX_INT, Display, get_text_width |
| 20 |
from ansible.utils.multiprocessing import context as multiprocessing_context |
| 21 |
from ansible._internal import _display_utils |
| 22 |
|
| 23 |
|
| 24 |
@pytest.fixture |
| 25 |
def problematic_wcswidth_chars(): |
| 26 |
locale.setlocale(locale.LC_ALL, 'C.UTF-8') |
| 27 |
|
| 28 |
candidates = set(chr(c) for c in range(sys.maxunicode) if unicodedata.category(chr(c)) == 'Cf') |
| 29 |
problematic = [candidate for candidate in candidates if _LIBC.wcswidth(candidate, _MAX_INT) == -1] |
| 30 |
|
| 31 |
if not problematic: |
| 32 |
# Newer distributions (Ubuntu 22.04, Fedora 38) include a libc which does not report problematic characters. |
| 33 |
pytest.skip("no problematic wcswidth chars found") # pragma: nocover |
| 34 |
|
| 35 |
return problematic |
| 36 |
|
| 37 |
|
| 38 |
def test_get_text_width(): |
| 39 |
locale.setlocale(locale.LC_ALL, '') |
| 40 |
assert get_text_width(u'コンニチハ') == 10 |
| 41 |
assert get_text_width(u'abコcd') == 6 |
| 42 |
assert get_text_width(u'café') == 4 |
| 43 |
assert get_text_width(u'four') == 4 |
| 44 |
assert get_text_width(u'\u001B') == 0 |
| 45 |
assert get_text_width(u'ab\u0000') == 2 |
| 46 |
assert get_text_width(u'abコ\u0000') == 4 |
| 47 |
assert get_text_width(u'🚀🐮') == 4 |
| 48 |
assert get_text_width(u'\x08') == 0 |
| 49 |
assert get_text_width(u'\x08\x08') == 0 |
| 50 |
assert get_text_width(u'ab\x08cd') == 3 |
| 51 |
assert get_text_width(u'ab\x1bcd') == 3 |
| 52 |
assert get_text_width(u'ab\x7fcd') == 3 |
| 53 |
assert get_text_width(u'ab\x94cd') == 3 |
| 54 |
|
| 55 |
pytest.raises(TypeError, get_text_width, 1) |
| 56 |
pytest.raises(TypeError, get_text_width, b'four') |
| 57 |
|
| 58 |
|
| 59 |
def test_get_text_width_no_locale(problematic_wcswidth_chars): |
| 60 |
pytest.raises(EnvironmentError, get_text_width, problematic_wcswidth_chars[0]) |
| 61 |
|
| 62 |
|
| 63 |
def test_Display_banner_get_text_width(monkeypatch, display_resource): |
| 64 |
locale.setlocale(locale.LC_ALL, '') |
| 65 |
display = Display() |
| 66 |
display_mock = MagicMock() |
| 67 |
monkeypatch.setattr(display, 'display', display_mock) |
| 68 |
|
| 69 |
display.banner(u'🚀🐮', color=False, cows=False) |
| 70 |
args, kwargs = display_mock.call_args |
| 71 |
msg = args[0] |
| 72 |
stars = u' %s' % (75 * u'*') |
| 73 |
assert msg.endswith(stars) |
| 74 |
|
| 75 |
|
| 76 |
def test_Display_banner_get_text_width_fallback(monkeypatch, display_resource): |
| 77 |
locale.setlocale(locale.LC_ALL, 'C.UTF-8') |
| 78 |
display = Display() |
| 79 |
display_mock = MagicMock() |
| 80 |
monkeypatch.setattr(display, 'display', display_mock) |
| 81 |
|
| 82 |
display.banner(u'\U000110cd', color=False, cows=False) |
| 83 |
args, kwargs = display_mock.call_args |
| 84 |
msg = args[0] |
| 85 |
stars = u' %s' % (78 * u'*') |
| 86 |
assert msg.endswith(stars) |
| 87 |
|
| 88 |
|
| 89 |
def test_Display_set_queue_parent(display_resource): |
| 90 |
display = Display() |
| 91 |
pytest.raises(RuntimeError, display.set_queue, 'foo') |
| 92 |
|
| 93 |
|
| 94 |
def test_Display_set_queue_fork(display_resource): |
| 95 |
def test(): |
| 96 |
display = Display() |
| 97 |
display.set_queue('foo') |
| 98 |
assert display._final_q == 'foo' |
| 99 |
p = multiprocessing_context.Process(target=test) |
| 100 |
p.start() |
| 101 |
p.join() |
| 102 |
assert p.exitcode == 0 |
| 103 |
|
| 104 |
|
| 105 |
def test_Display_display_fork(display_resource): |
| 106 |
def test(): |
| 107 |
queue = MagicMock() |
| 108 |
display = Display() |
| 109 |
display.set_queue(queue) |
| 110 |
display.display('foo') |
| 111 |
queue.send_display.assert_called_once_with('display', 'foo') |
| 112 |
|
| 113 |
p = multiprocessing_context.Process(target=test) |
| 114 |
p.start() |
| 115 |
p.join() |
| 116 |
assert p.exitcode == 0 |
| 117 |
|
| 118 |
|
| 119 |
def test_Display_display_warn_fork(display_resource): |
| 120 |
def test(): |
| 121 |
queue = MagicMock() |
| 122 |
display = Display() |
| 123 |
display.set_queue(queue) |
| 124 |
display.warning('foo') |
| 125 |
queue.send_display.assert_called_once_with('_warning', _messages.WarningSummary(event=_messages.Event(msg='foo'))) |
| 126 |
|
| 127 |
p = multiprocessing_context.Process(target=test) |
| 128 |
p.start() |
| 129 |
p.join() |
| 130 |
assert p.exitcode == 0 |
| 131 |
|
| 132 |
|
| 133 |
def test_Display_display_lock(monkeypatch, display_resource): |
| 134 |
lock = MagicMock() |
| 135 |
display = Display() |
| 136 |
monkeypatch.setattr(display, '_lock', lock) |
| 137 |
display.display('foo') |
| 138 |
lock.__enter__.assert_called_once_with() |
| 139 |
|
| 140 |
|
| 141 |
def test_Display_display_lock_fork(monkeypatch, display_resource): |
| 142 |
lock = MagicMock() |
| 143 |
display = Display() |
| 144 |
monkeypatch.setattr(display, '_lock', lock) |
| 145 |
monkeypatch.setattr(display, '_final_q', MagicMock()) |
| 146 |
display.display('foo') |
| 147 |
lock.__enter__.assert_not_called() |
| 148 |
|
| 149 |
|
| 150 |
def test_format_message_deprecation_with_multiple_details() -> None: |
| 151 |
""" |
| 152 |
Verify that a DeprecationSummary with multiple Detail entries can be formatted. |
| 153 |
No existing code generates deprecations with multiple details, but a future deprecation exception type would need to make use of this. |
| 154 |
""" |
| 155 |
event = _messages.Event( |
| 156 |
msg='Ignoring ExceptionX.', |
| 157 |
help_text='Plugins must handle it internally.', |
| 158 |
chain=_messages.EventChain( |
| 159 |
msg_reason=_errors.MSG_REASON_DIRECT_CAUSE, |
| 160 |
traceback_reason='', # not used in this test |
| 161 |
event=_messages.Event( |
| 162 |
msg='Something went wrong.', |
| 163 |
formatted_source_context='Origin: /some/path\n\n...', |
| 164 |
), |
| 165 |
), |
| 166 |
) |
| 167 |
|
| 168 |
result = _display_utils.format_message(_messages.DeprecationSummary(event=event), False) |
| 169 |
|
| 170 |
assert result == '''Ignoring ExceptionX. This feature will be removed in the future: Something went wrong. |
| 171 |
|
| 172 |
Ignoring ExceptionX. This feature will be removed in the future. Plugins must handle it internally. |
| 173 |
|
| 174 |
<<< caused by >>> |
| 175 |
|
| 176 |
Something went wrong. |
| 177 |
Origin: /some/path |
| 178 |
|
| 179 |
... |
| 180 |
|
| 181 |
''' |
| 182 |
|
| 183 |
|
| 184 |
A_DATE = datetime.date(2025, 1, 1) |
| 185 |
CORE = deprecator_from_collection_name('ansible.builtin') |
| 186 |
CORE_MODULE = _messages.PluginInfo(resolved_name='ansible.builtin.ping', type=_messages.PluginType.MODULE) |
| 187 |
CORE_PLUGIN = _messages.PluginInfo(resolved_name='ansible.builtin.debug', type=_messages.PluginType.ACTION) |
| 188 |
COLL = deprecator_from_collection_name('ns.col') |
| 189 |
COLL_MODULE = _messages.PluginInfo(resolved_name='ns.col.ping', type=_messages.PluginType.MODULE) |
| 190 |
COLL_PLUGIN = _messages.PluginInfo(resolved_name='ns.col.debug', type=_messages.PluginType.ACTION) |
| 191 |
INDETERMINATE = _deprecator.INDETERMINATE_DEPRECATOR |
| 192 |
LEGACY_MODULE = _messages.PluginInfo(resolved_name='ping', type=_messages.PluginType.MODULE) |
| 193 |
LEGACY_PLUGIN = _messages.PluginInfo(resolved_name='debug', type=_messages.PluginType.ACTION) |
| 194 |
|
| 195 |
|
| 196 |
@pytest.mark.parametrize('kwargs, expected', ( |
| 197 |
# removed |
| 198 |
(dict(msg="Hi", removed=True), "Hi. This feature was removed."), |
| 199 |
(dict(msg="Hi", version="2.99", deprecator=CORE, removed=True), "Hi. This feature was removed from ansible-core version 2.99."), |
| 200 |
(dict(msg="Hi", date=A_DATE, deprecator=COLL_MODULE, removed=True), |
| 201 |
"Hi. This feature was removed from module 'ping' in collection 'ns.col' in a release after 2025-01-01."), |
| 202 |
# no deprecator or indeterminate |
| 203 |
(dict(msg="Hi"), "Hi. This feature will be removed in the future."), |
| 204 |
(dict(msg="Hi", version="2.99"), "Hi. This feature will be removed in the future."), |
| 205 |
(dict(msg="Hi", date=A_DATE), "Hi. This feature will be removed in the future."), |
| 206 |
(dict(msg="Hi", version="2.99", deprecator=INDETERMINATE), "Hi. This feature will be removed in the future."), |
| 207 |
(dict(msg="Hi", date=A_DATE, deprecator=INDETERMINATE), "Hi. This feature will be removed in the future."), |
| 208 |
# deprecator without plugin |
| 209 |
(dict(msg="Hi", deprecator=CORE), "Hi. This feature will be removed from ansible-core in a future release."), |
| 210 |
(dict(msg="Hi", deprecator=COLL), "Hi. This feature will be removed from collection 'ns.col' in a future release."), |
| 211 |
(dict(msg="Hi", version="2.99", deprecator=CORE), "Hi. This feature will be removed from ansible-core version 2.99."), |
| 212 |
(dict(msg="Hi", version="2.99", deprecator=COLL), "Hi. This feature will be removed from collection 'ns.col' version 2.99."), |
| 213 |
(dict(msg="Hi", date=A_DATE, deprecator=COLL), "Hi. This feature will be removed from collection 'ns.col' in a release after 2025-01-01."), |
| 214 |
# deprecator with module |
| 215 |
(dict(msg="Hi", deprecator=CORE_MODULE), "Hi. This feature will be removed from module 'ping' in ansible-core in a future release."), |
| 216 |
(dict(msg="Hi", deprecator=COLL_MODULE), "Hi. This feature will be removed from module 'ping' in collection 'ns.col' in a future release."), |
| 217 |
(dict(msg="Hi", deprecator=LEGACY_MODULE), "Hi. This feature will be removed from module 'ping' in the future."), |
| 218 |
(dict(msg="Hi", version="2.99", deprecator=CORE_MODULE), "Hi. This feature will be removed from module 'ping' in ansible-core version 2.99."), |
| 219 |
(dict(msg="Hi", version="2.99", deprecator=COLL_MODULE), "Hi. This feature will be removed from module 'ping' in collection 'ns.col' version 2.99."), |
| 220 |
(dict(msg="Hi", version="2.99", deprecator=LEGACY_MODULE), "Hi. This feature will be removed from module 'ping' in the future."), |
| 221 |
(dict(msg="Hi", date=A_DATE, deprecator=COLL_MODULE), |
| 222 |
"Hi. This feature will be removed from module 'ping' in collection 'ns.col' in a release after 2025-01-01."), |
| 223 |
(dict(msg="Hi", date=A_DATE, deprecator=LEGACY_MODULE), "Hi. This feature will be removed from module 'ping' in the future."), |
| 224 |
# deprecator with plugin |
| 225 |
(dict(msg="Hi", deprecator=CORE_PLUGIN), "Hi. This feature will be removed from action plugin 'debug' in ansible-core in a future release."), |
| 226 |
(dict(msg="Hi", deprecator=COLL_PLUGIN), "Hi. This feature will be removed from action plugin 'debug' in collection 'ns.col' in a future release."), |
| 227 |
(dict(msg="Hi", deprecator=LEGACY_PLUGIN), "Hi. This feature will be removed from action plugin 'debug' in the future."), |
| 228 |
(dict(msg="Hi", version="2.99", deprecator=CORE_PLUGIN), "Hi. This feature will be removed from action plugin 'debug' in ansible-core version 2.99."), |
| 229 |
(dict(msg="Hi", version="2.99", deprecator=COLL_PLUGIN), |
| 230 |
"Hi. This feature will be removed from action plugin 'debug' in collection 'ns.col' version 2.99."), |
| 231 |
(dict(msg="Hi", version="2.99", deprecator=LEGACY_PLUGIN), "Hi. This feature will be removed from action plugin 'debug' in the future."), |
| 232 |
(dict(msg="Hi", date=A_DATE, deprecator=COLL_PLUGIN), |
| 233 |
"Hi. This feature will be removed from action plugin 'debug' in collection 'ns.col' in a release after 2025-01-01."), |
| 234 |
(dict(msg="Hi", date=A_DATE, deprecator=LEGACY_PLUGIN), "Hi. This feature will be removed from action plugin 'debug' in the future."), |
| 235 |
)) |
| 236 |
def test_get_deprecation_message_with_plugin_info(kwargs: dict[str, t.Any], expected: str) -> None: |
| 237 |
for kwarg in ('version', 'date', 'deprecator'): |
| 238 |
kwargs.setdefault(kwarg, None) |
| 239 |
|
| 240 |
msg = _display_utils.get_deprecation_message_with_plugin_info(**kwargs) |
| 241 |
|
| 242 |
assert msg == expected |
| 243 |
|
| 244 |
|
| 245 |
@pytest.mark.parametrize("kw,expected", ( |
| 246 |
(dict(msg="hi"), "[DEPRECATION WARNING]: hi. This feature will be removed in the future."), |
| 247 |
(dict(msg="hi", removed=True), "[DEPRECATED]: hi. This feature was removed."), |
| 248 |
(dict(msg="hi", version="1.23"), "[DEPRECATION WARNING]: hi. This feature will be removed in the future."), |
| 249 |
(dict(msg="hi", date="2025-01-01"), "[DEPRECATION WARNING]: hi. This feature will be removed in the future."), |
| 250 |
(dict(msg="hi", collection_name="foo.bar"), "[DEPRECATION WARNING]: hi. This feature will be removed from collection 'foo.bar' in a future release."), |
| 251 |
(dict(msg="hi", version="1.23", collection_name="foo.bar"), |
| 252 |
"[DEPRECATION WARNING]: hi. This feature will be removed from collection 'foo.bar' version 1.23."), |
| 253 |
(dict(msg="hi", date="2025-01-01", collection_name="foo.bar"), |
| 254 |
"[DEPRECATION WARNING]: hi. This feature will be removed from collection 'foo.bar' in a release after 2025-01-01."), |
| 255 |
)) |
| 256 |
def test_get_deprecation_message(kw: dict[str, t.Any], expected: str) -> None: |
| 257 |
"""Validate the deprecated public version of this function.""" |
| 258 |
|
| 259 |
assert Display().get_deprecation_message(**kw) == expected |