Debian Codegraph @

test/units/errors/test_utils.py

raw
1 from __future__ import annotations
2
3 import pytest
4
5 from ansible._internal._errors import _error_factory
6
7 from ansible.errors import AnsibleError
8 from ansible._internal import _display_utils
9 from ansible._internal._datatag._tags import Origin
10 from ansible._internal._errors._error_utils import format_exception_message
11 from ansible.module_utils._internal import _messages
12 from units.mock.error_helper import raise_exceptions
13
14
15 _shared_cause = Exception('shared cause')
16 _origin_x = Origin(path='/x')
17 _origin_y = Origin(path='/y')
18
19
20 @pytest.mark.parametrize("exceptions, expected_message_chain, expected_formatted_message", (
21 ([AnsibleError('a')], 'a', None),
22 ([AnsibleError('a'), AnsibleError('b')], 'a: b', None),
23 ([AnsibleError('a: b'), AnsibleError('b')], 'a: b', None),
24 ([Exception('a')], 'a', None),
25 ([Exception('a'), Exception('b')], 'a: b', None),
26 ([Exception('a: b'), Exception('b')], 'a: b', None),
27 ([AnsibleError('a'), Exception('b')], 'a: b', None),
28 ([AnsibleError('a: b'), Exception('b')], 'a: b', None),
29 ([Exception('a'), AnsibleError('b')], 'a: b', None),
30 ([Exception('a: b'), AnsibleError('b')], 'a: b', None),
31 ([AnsibleError('a'), AnsibleError('b'), Exception('c'), Exception('d')], 'a: b: c: d', None),
32 ([AnsibleError('a: b: c: d'), AnsibleError('b'), Exception('c: d'), Exception('d')], 'a: b: c: d', None),
33
34 # collapsing help_text
35
36 (
37 [
38 AnsibleError('a', help_text='one'),
39 Exception('b'),
40 ],
41 'a: b',
42 (
43 'a: b one'
44 ),
45 ),
46 (
47 [
48 AnsibleError('a', help_text='one'),
49 AnsibleError('b', help_text='one'),
50 ],
51 'a: b',
52 (
53 'a: b one'
54 ),
55 ),
56 (
57 [
58 AnsibleError('a', help_text='one'),
59 Exception('b'),
60 AnsibleError('c', help_text='one'),
61 ],
62 'a: b: c',
63 (
64 'a: b: c one'
65 ),
66 ),
67 (
68 [
69 AnsibleError('a', help_text='one'),
70 AnsibleError('b', help_text='two'),
71 ],
72 'a: b',
73 (
74 'a: b\n\n'
75 'a one\n\n'
76 '<<< caused by >>>\n\n'
77 'b two'
78 ),
79 ),
80 (
81 [
82 AnsibleError('a'),
83 AnsibleError('b', help_text='one'),
84 ],
85 'a: b',
86 (
87 'a: b\n\n'
88 'a\n\n'
89 '<<< caused by >>>\n\n'
90 'b one'
91 ),
92 ),
93
94 # collapsing origin
95
96 (
97 [
98 AnsibleError('a', obj=_origin_x.tag('x'), orig_exc=Exception('ignored')),
99 Exception('b'),
100 ],
101 'a: b',
102 (
103 'a: b\n'
104 'Origin: /x'
105 ),
106 ),
107 (
108 [
109 AnsibleError('a', obj=_origin_x.tag('x'), orig_exc=_shared_cause),
110 _shared_cause,
111 ],
112 'a: shared cause',
113 (
114 'a: shared cause\n'
115 'Origin: /x'
116 ),
117 ),
118 (
119 [
120 # same as above, but exercises the old `orig_exc` path that displays a warning
121 AnsibleError('a', obj=_origin_x.tag('x'), orig_exc=Exception('b')),
122 ],
123 'a: b',
124 (
125 'a: b\n'
126 'Origin: /x'
127 ),
128 ),
129 (
130 [
131 AnsibleError('a', obj=_origin_x.tag('x')),
132 AnsibleError('b', obj=_origin_x.tag('x')),
133 ],
134 'a: b',
135 (
136 'a: b\n'
137 'Origin: /x'
138 ),
139 ),
140 (
141 [
142 AnsibleError('a', obj=_origin_x.tag('x')),
143 Exception('b'),
144 AnsibleError('c', obj=_origin_x.tag('x')),
145 ],
146 'a: b: c',
147 (
148 'a: b: c\n'
149 'Origin: /x'
150 ),
151 ),
152 (
153 [
154 AnsibleError('a', obj=_origin_x.tag('x')),
155 AnsibleError('b', obj=_origin_y.tag('x')),
156 ],
157 'a: b',
158 (
159 'a: b\n\n'
160 'a\n'
161 'Origin: /x\n\n'
162 '<<< caused by >>>\n\n'
163 'b\n'
164 'Origin: /y'
165 ),
166 ),
167 (
168 [
169 AnsibleError('a'),
170 AnsibleError('b', obj=_origin_y.tag('x')),
171 ],
172 'a: b',
173 (
174 'a: b\n\n'
175 'a\n\n'
176 '<<< caused by >>>\n\n'
177 'b\n'
178 'Origin: /y'
179 ),
180 ),
181 ), ids=str)
182 def test_error_messages(exceptions: list[BaseException], expected_message_chain: str, expected_formatted_message: str | None) -> None:
183 with pytest.raises(Exception) as error:
184 raise_exceptions(exceptions)
185
186 event = _error_factory.ControllerEventFactory.from_exception(error.value, False)
187
188 message_chain = format_exception_message(error.value)
189 formatted_message = _display_utils.format_message(_messages.ErrorSummary(event=event), False)
190
191 assert message_chain == expected_message_chain
192 assert formatted_message.strip() == (expected_formatted_message or expected_message_chain)