Skip to content

Commit

Permalink
Merge pull request #54631 from garethgreenaway/2019_2_1_port_50970
Browse files Browse the repository at this point in the history
[master] Porting #50970 to master
  • Loading branch information
dwoz authored Jan 6, 2020
2 parents 4aba6d9 + f91dc57 commit 93fc341
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
5 changes: 2 additions & 3 deletions salt/modules/win_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# Import Python libs
import logging
import os
import re

# Import Salt libs
import salt.utils.args
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions salt/states/win_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__():
Expand Down Expand Up @@ -89,7 +90,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)

Expand Down Expand Up @@ -221,7 +222,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 ''
)
)

Expand Down
7 changes: 7 additions & 0 deletions tests/unit/modules/test_win_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,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.
Expand Down

0 comments on commit 93fc341

Please sign in to comment.