Fix UNC path support in the powershell shell plugin (#66604)

* Fix UNC path joining in the powershell shell plugin, add test

* Remove testy bits and a redundant line

* Fix style nits

* Update to use os.ntpath

* Add changelog for #66604
pull/66673/head
Brian Scholer 5 years ago committed by GitHub
parent 81378b3e74
commit fc7980af9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
minor_changes:
- "powershell (shell plugin) - Fix `join_path` to support UNC paths (https://github.com/ansible/ansible/issues/66341)"

@ -22,6 +22,7 @@ import re
import shlex import shlex
import pkgutil import pkgutil
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
import ntpath
from ansible.errors import AnsibleError from ansible.errors import AnsibleError
from ansible.module_utils._text import to_bytes, to_text from ansible.module_utils._text import to_bytes, to_text
@ -93,14 +94,13 @@ class ShellModule(ShellBase):
return "" return ""
def join_path(self, *args): def join_path(self, *args):
parts = [] # use normpath() to remove doubled slashed and convert forward to backslashes
for arg in args: parts = [ntpath.normpath(self._unquote(arg)) for arg in args]
arg = self._unquote(arg).replace('/', '\\')
parts.extend([a for a in arg.split('\\') if a]) # Becuase ntpath.join treats any component that begins with a backslash as an absolute path,
path = '\\'.join(parts) # we have to strip slashes from at least the beginning, otherwise join will ignore all previous
if path.startswith('~'): # path components except for the drive.
return path return ntpath.join(parts[0], *[part.strip('\\') for part in parts[1:]])
return path
def get_remote_filename(self, pathname): def get_remote_filename(self, pathname):
# powershell requires that script files end with .ps1 # powershell requires that script files end with .ps1

@ -1,4 +1,4 @@
from ansible.plugins.shell.powershell import _parse_clixml from ansible.plugins.shell.powershell import _parse_clixml, ShellModule
def test_parse_clixml_empty(): def test_parse_clixml_empty():
@ -51,3 +51,11 @@ def test_parse_clixml_multiple_streams():
expected = b"hi info" expected = b"hi info"
actual = _parse_clixml(multiple_stream, stream="Info") actual = _parse_clixml(multiple_stream, stream="Info")
assert actual == expected 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

Loading…
Cancel
Save