diff --git a/salt/modules/win_path.py b/salt/modules/win_path.py index ce54f97ebc32..1da944b222db 100644 --- a/salt/modules/win_path.py +++ b/salt/modules/win_path.py @@ -11,7 +11,6 @@ # Import Python libs import logging import os -import re # Import Salt libs import salt.utils.args @@ -50,7 +49,7 @@ def _normalize_dir(string_): ''' Normalize the directory to make comparison possible ''' - return re.sub(r'\\$', '', salt.utils.stringutils.to_unicode(string_)) + return os.path.normpath(salt.utils.stringutils.to_unicode(string_)) def rehash(): @@ -200,7 +199,7 @@ def _check_path(dirs, path, index): elif index <= -num_dirs: # Negative index is too large, shift index to beginning of list index = pos = 0 - elif index <= 0: + elif index < 0: # Negative indexes (other than -1 which is handled above) must # be inserted at index + 1 for the item to end up in the # position you want, since list.insert() inserts before the diff --git a/salt/states/win_path.py b/salt/states/win_path.py index af956d59b89c..5b91de303def 100644 --- a/salt/states/win_path.py +++ b/salt/states/win_path.py @@ -4,11 +4,12 @@ ''' from __future__ import absolute_import, print_function, unicode_literals -# Import Salt libs -import salt.utils.stringutils +# Import Python libs +import os -# Import 3rd-party libs +# Import Salt libs from salt.ext import six +import salt.utils.stringutils def __virtual__(): @@ -91,7 +92,7 @@ def exists(name, index=None): - index: -1 ''' try: - name = salt.utils.stringutils.to_unicode(name) + name = os.path.normpath(salt.utils.stringutils.to_unicode(name)) except TypeError: name = six.text_type(name) @@ -223,7 +224,7 @@ def _changes(old, new): '{0} {1} to the PATH{2}.'.format( 'Added' if ret['result'] else 'Failed to add', name, - ' at index {0}'.format(index) if index else '' + ' at index {0}'.format(index) if index is not None else '' ) ) diff --git a/tests/unit/modules/test_win_path.py b/tests/unit/modules/test_win_path.py index 891a13149797..a21875ab1789 100644 --- a/tests/unit/modules/test_win_path.py +++ b/tests/unit/modules/test_win_path.py @@ -117,6 +117,13 @@ def _run(name, index=None, retval=True, path=None): self.assert_call_matches(mock_set, new_path) self.assert_path_matches(env, new_path) + # Test adding with a custom index of 0 + ret, env, mock_set = _run('c:\\salt', index=0, retval=True) + new_path = ('c:\\salt', 'C:\\Foo', 'C:\\Bar') + self.assertTrue(ret) + self.assert_call_matches(mock_set, new_path) + self.assert_path_matches(env, new_path) + # Test adding path with a case-insensitive match already present, and # no index provided. The path should remain unchanged and we should not # update the registry.