Debian Codegraph @

test/units/_internal/_powershell/test_clixml.py

raw
1 from __future__ import annotations
2
3 import pytest
4
5 from ansible._internal._powershell import _clixml
6
7
8 CLIXML_WITH_ERROR = b'#< CLIXML\r\n<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">' \
9 b'<S S="Error">My error</S></Objs>'
10
11
12 def test_replace_stderr_clixml_by_itself():
13 data = CLIXML_WITH_ERROR
14 expected = b"My error"
15 actual = _clixml.replace_stderr_clixml(data)
16
17 assert actual == expected
18
19
20 def test_replace_stderr_clixml_with_pre_and_post_lines():
21 data = b"pre\r\n" + CLIXML_WITH_ERROR + b"\r\npost"
22 expected = b"pre\r\nMy error\r\npost"
23 actual = _clixml.replace_stderr_clixml(data)
24
25 assert actual == expected
26
27
28 def test_replace_stderr_clixml_with_remaining_data_on_line():
29 data = b"pre\r\n" + CLIXML_WITH_ERROR + b"inline\r\npost"
30 expected = b"pre\r\nMy errorinline\r\npost"
31 actual = _clixml.replace_stderr_clixml(data)
32
33 assert actual == expected
34
35
36 def test_replace_stderr_clixml_with_non_utf8_data():
37 # \x82 in cp437 is é but is an invalid UTF-8 sequence
38 data = CLIXML_WITH_ERROR.replace(b"error", b"\x82rror")
39 expected = "My érror".encode("utf-8")
40 actual = _clixml.replace_stderr_clixml(data)
41
42 assert actual == expected
43
44
45 def test_replace_stderr_clixml_across_liens():
46 data = b"#< CLIXML\r\n<Objs Version=\"foo\">\r\n</Objs>"
47 expected = data
48 actual = _clixml.replace_stderr_clixml(data)
49
50 assert actual == expected
51
52
53 @pytest.mark.parametrize("newline", ["\n", "\r\n"])
54 def test_replace_stderr_clixml_with_invalid_clixml_data(newline):
55 data = f"#< CLIXML{newline}<Objs Version=\"foo\"><</Objs>".encode()
56 expected = data
57 actual = _clixml.replace_stderr_clixml(data)
58
59 assert actual == expected
60
61
62 def test_replace_stderr_clixml_with_no_clixml():
63 data = b"foo"
64 expected = data
65 actual = _clixml.replace_stderr_clixml(data)
66
67 assert actual == expected
68
69
70 def test_replace_stderr_clixml_with_header_but_no_data():
71 data = b"foo\r\n#< CLIXML\r\n"
72 expected = data
73 actual = _clixml.replace_stderr_clixml(data)
74
75 assert actual == expected
76
77
78 def test_replace_stderr_clixml_with_vt_colors():
79 data = b'#< CLIXML\r\n<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">' \
80 b'<S S="Error">_x001B_[31;1mException: _x001B_[31;1mabc_x001B_[0m_x000D__x000A_</S></Objs>'
81 expected = b"Exception: abc\r\n"
82 actual = _clixml.replace_stderr_clixml(data)
83 assert actual == expected
84
85
86 def test_extract_clixml_empty():
87 empty = '#< CLIXML\r\n<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"></Objs>'
88 expected = []
89 actual = _clixml.extract_clixml_strings(empty)
90 assert actual == expected
91
92
93 def test_extract_clixml_with_progress():
94 progress = '#< CLIXML\r\n<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">' \
95 '<Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS>' \
96 '<I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil />' \
97 '<PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj></Objs>'
98 expected = []
99 actual = _clixml.extract_clixml_strings(progress)
100 assert actual == expected
101
102
103 def test_extract_clixml_error_single_stream():
104 single_stream = '#< CLIXML\r\n<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">' \
105 '<S S="Error">fake : The term \'fake\' is not recognized as the name of a cmdlet. Check _x000D__x000A_</S>' \
106 '<S S="Error">the spelling of the name, or if a path was included._x000D__x000A_</S>' \
107 '<S S="Error">At line:1 char:1_x000D__x000A_</S>' \
108 '<S S="Error">+ fake cmdlet_x000D__x000A_</S><S S="Error">+ ~~~~_x000D__x000A_</S>' \
109 '<S S="Error"> + CategoryInfo : ObjectNotFound: (fake:String) [], CommandNotFoundException_x000D__x000A_</S>' \
110 '<S S="Error"> + FullyQualifiedErrorId : CommandNotFoundException_x000D__x000A_</S>' \
111 '<S S="Error"> _x000D__x000A_</S>' \
112 '</Objs>'
113 expected = [
114 "fake : The term 'fake' is not recognized as the name of a cmdlet. Check \r\n",
115 "the spelling of the name, or if a path was included.\r\n",
116 "At line:1 char:1\r\n",
117 "+ fake cmdlet\r\n",
118 "+ ~~~~\r\n",
119 " + CategoryInfo : ObjectNotFound: (fake:String) [], CommandNotFoundException\r\n",
120 " + FullyQualifiedErrorId : CommandNotFoundException\r\n",
121 " \r\n",
122 ]
123 actual = _clixml.extract_clixml_strings(single_stream, stream="Error")
124 assert actual == expected
125
126
127 def test_extract_clixml_error_multiple_streams():
128 multiple_stream = '#< CLIXML\r\n<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">' \
129 '<S S="Error">fake : The term \'fake\' is not recognized as the name of a cmdlet. Check _x000D__x000A_</S>' \
130 '<S S="Error">the spelling of the name, or if a path was included._x000D__x000A_</S>' \
131 '<S S="Error">At line:1 char:1_x000D__x000A_</S>' \
132 '<S S="Error">+ fake cmdlet_x000D__x000A_</S><S S="Error">+ ~~~~_x000D__x000A_</S>' \
133 '<S S="Error"> + CategoryInfo : ObjectNotFound: (fake:String) [], CommandNotFoundException_x000D__x000A_</S>' \
134 '<S S="Error"> + FullyQualifiedErrorId : CommandNotFoundException_x000D__x000A_</S><S S="Error"> _x000D__x000A_</S>' \
135 '<S S="Info">hi info</S>' \
136 '<S S="Info">other</S>' \
137 '</Objs>'
138 expected = ["hi info", "other"]
139 actual = _clixml.extract_clixml_strings(multiple_stream, stream="Info")
140 assert actual == expected
141
142
143 def test_extract_clixml_error_multiple_elements():
144 multiple_elements = '#< CLIXML\r\n#< CLIXML\r\n<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">' \
145 '<Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS>' \
146 '<I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil />' \
147 '<PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj>' \
148 '<S S="Error">Error 1</S></Objs>' \
149 '<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0">' \
150 '<TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS>' \
151 '<I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil />' \
152 '<PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj>' \
153 '<Obj S="progress" RefId="1"><TNRef RefId="0" /><MS><I64 N="SourceId">2</I64>' \
154 '<PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil />' \
155 '<PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj>' \
156 '<S S="Error">Error 2</S></Objs>'
157 expected = ["Error 1", "\r\n", "Error 2"]
158 actual = _clixml.extract_clixml_strings(multiple_elements, stream="Error")
159 assert actual == expected
160
161
162 @pytest.mark.parametrize('clixml, expected', [
163 ('', ''),
164 ('just newline _x000A_', 'just newline \n'),
165 ('surrogate pair _xD83C__xDFB5_', 'surrogate pair 🎵'),
166 ('null char _x0000_', 'null char \0'),
167 ('normal char _x0061_', 'normal char a'),
168 ('escaped literal _x005F_x005F_', 'escaped literal _x005F_'),
169 ('underscope before escape _x005F__x000A_', 'underscope before escape _\n'),
170 ('surrogate high _xD83C_', 'surrogate high \uD83C'),
171 ('surrogate low _xDFB5_', 'surrogate low \uDFB5'),
172 ('lower case hex _x005f_', 'lower case hex _'),
173 ('invalid hex _x005G_', 'invalid hex _x005G_'),
174 # Tests regex actually matches UTF-16-BE hex chars (b"\x00" then hex char).
175 ("_x\u6100\u6200\u6300\u6400_", "_x\u6100\u6200\u6300\u6400_"),
176 ])
177 def test_extract_clixml_error_with_comlex_escaped_chars(clixml, expected):
178 clixml_data = (
179 '<# CLIXML\r\n'
180 '<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">'
181 f'<S S="Error">{clixml}</S>'
182 '</Objs>'
183 )
184 # b_expected = expected.encode(errors="surrogatepass")
185
186 actual = _clixml.extract_clixml_strings(clixml_data, stream="Error")
187 assert actual == [expected]
188
189
190 def test_extract_clixml_string_encoded_arguments():
191 # Generated from - change pwsh to an exe that prints back argv
192 # pwsh {} -args 'simple', '_x005F_', ([char]::ConvertFromUtf32(0x1F3B5))
193 encoded_clixml = (
194 '<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">'
195 '<Obj RefId="0">'
196 '<TN RefId="0"><T>System.Collections.ArrayList</T><T>System.Object</T></TN>'
197 '<LST>'
198 '<S>simple</S>'
199 '<S>_x005F_x005F_</S>'
200 '<S>_xD83C__xDFB5_</S>'
201 '</LST>'
202 '</Obj>'
203 '</Objs>'
204 )
205 expected = ['simple', '_x005F_', "\U0001F3B5"]
206
207 actual = _clixml.extract_clixml_strings(encoded_clixml)
208 assert actual == expected