from ansible.plugins.shell.powershell import _parse_clixml, ShellModule
def test_parse_clixml_empty():
empty = b'#< CLIXML\r\n'
expected = b''
actual = _parse_clixml(empty)
assert actual == expected
def test_parse_clixml_with_progress():
progress = b'#< CLIXML\r\n' \
b'System.Management.Automation.PSCustomObjectSystem.Object' \
b'1Preparing modules for first use.0' \
b'-1-1Completed-1 '
expected = b''
actual = _parse_clixml(progress)
assert actual == expected
def test_parse_clixml_single_stream():
single_stream = b'#< CLIXML\r\n' \
b'fake : The term \'fake\' is not recognized as the name of a cmdlet. Check _x000D__x000A_' \
b'the spelling of the name, or if a path was included._x000D__x000A_' \
b'At line:1 char:1_x000D__x000A_' \
b'+ fake cmdlet_x000D__x000A_+ ~~~~_x000D__x000A_' \
b' + CategoryInfo : ObjectNotFound: (fake:String) [], CommandNotFoundException_x000D__x000A_' \
b' + FullyQualifiedErrorId : CommandNotFoundException_x000D__x000A_ _x000D__x000A_' \
b''
expected = b"fake : The term 'fake' is not recognized as the name of a cmdlet. Check \r\n" \
b"the spelling of the name, or if a path was included.\r\n" \
b"At line:1 char:1\r\n" \
b"+ fake cmdlet\r\n" \
b"+ ~~~~\r\n" \
b" + CategoryInfo : ObjectNotFound: (fake:String) [], CommandNotFoundException\r\n" \
b" + FullyQualifiedErrorId : CommandNotFoundException\r\n "
actual = _parse_clixml(single_stream)
assert actual == expected
def test_parse_clixml_multiple_streams():
multiple_stream = b'#< CLIXML\r\n' \
b'fake : The term \'fake\' is not recognized as the name of a cmdlet. Check _x000D__x000A_' \
b'the spelling of the name, or if a path was included._x000D__x000A_' \
b'At line:1 char:1_x000D__x000A_' \
b'+ fake cmdlet_x000D__x000A_+ ~~~~_x000D__x000A_' \
b' + CategoryInfo : ObjectNotFound: (fake:String) [], CommandNotFoundException_x000D__x000A_' \
b' + FullyQualifiedErrorId : CommandNotFoundException_x000D__x000A_ _x000D__x000A_' \
b'hi info' \
b''
expected = b"hi info"
actual = _parse_clixml(multiple_stream, stream="Info")
assert actual == expected
def test_join_path_unc():
pwsh = ShellModule()
unc_path_parts = ['\\\\host\\share\\dir1\\\\dir2\\', '\\dir3/dir4', 'dir5', 'dir6\\']
expected = '\\\\host\\share\\dir1\\dir2\\dir3\\dir4\\dir5\\dir6'
actual = pwsh.join_path(*unc_path_parts)
assert actual == expected