Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct win_path index checks to allow for 0 #50970

Merged
merged 8 commits into from
Feb 14, 2019
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 @@ -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)

Expand Down Expand Up @@ -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 ''
)
)

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 @@ -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.
Expand Down