Debian Codegraph @

test/units/utils/display/test_warning.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2020 Ansible Project
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 pytest
8
9 from ansible.utils.display import Display
10 from ansible.module_utils import basic
11 from units.test_utils.controller.display import emits_warnings
12
13
14 def test_module_utils_warn() -> None:
15 """Verify that `module_utils.basic.warn` on the controller is routed to `Display.warning`."""
16 with emits_warnings(warning_pattern="hello"):
17 basic.warn("hello")
18
19
20 def test_module_utils_error_as_warning() -> None:
21 """Verify that `module_utils.basic.error_as_warning` on the controller is routed to `Display.error_as_warning`."""
22 with emits_warnings(warning_pattern="hello.*world"):
23 try:
24 raise Exception("world")
25 except Exception as ex:
26 basic.error_as_warning("hello", ex)
27
28
29 def test_module_utils_deprecate() -> None:
30 """Verify that `module_utils.basic.deprecate` on the controller is routed to `Display.deprecated`."""
31 with emits_warnings(deprecation_pattern="hello"):
32 basic.deprecate("hello", version='9999.9')
33
34
35 @pytest.fixture
36 def warning_message():
37 warning_message = 'bad things will happen'
38 expected_warning_message = '[WARNING]: {0}\n'.format(warning_message)
39 return warning_message, expected_warning_message
40
41
42 def test_warning(capsys, mocker, warning_message):
43 warning_message, expected_warning_message = warning_message
44
45 mocker.patch('ansible.utils.color.ANSIBLE_COLOR', True)
46 mocker.patch('ansible.utils.color.parsecolor', return_value=u'1;35') # value for 'bright purple'
47
48 d = Display()
49 d._warns.clear()
50 d.warning(warning_message)
51 out, err = capsys.readouterr()
52 assert d._warns == {expected_warning_message}
53 assert err == '\x1b[1;35m{0}\x1b[0m\n'.format(expected_warning_message.rstrip('\n'))
54
55
56 def test_warning_no_color(capsys, mocker, warning_message):
57 warning_message, expected_warning_message = warning_message
58
59 mocker.patch('ansible.utils.color.ANSIBLE_COLOR', False)
60
61 d = Display()
62 d._warns.clear()
63 d.warning(warning_message)
64 out, err = capsys.readouterr()
65 assert d._warns == {expected_warning_message}
66 assert err == expected_warning_message