mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 01:18:58 +00:00
Merge pull request #8139 from s0undt3ch/features/mocked-which-test
Provide a default `PATHEXT` environment variable.
This commit is contained in:
commit
9c40e55c54
@ -274,7 +274,7 @@ def which(exe=None):
|
||||
# default path based on busybox's default
|
||||
default_path = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin'
|
||||
search_path = os.environ.get('PATH', default_path)
|
||||
path_ext = os.environ.get('PATHEXT', '')
|
||||
path_ext = os.environ.get('PATHEXT', '.EXE')
|
||||
ext_list = path_ext.split(';')
|
||||
|
||||
@real_memoize
|
||||
|
@ -59,7 +59,7 @@ class TestWhich(integration.TestCase):
|
||||
self.assertEqual(
|
||||
salt.utils.which('this-binary-exists-under-windows'),
|
||||
# The returned path should return the .exe suffix
|
||||
'/bin/this-binary-exists-under-windows.exe'
|
||||
'/bin/this-binary-exists-under-windows.EXE'
|
||||
)
|
||||
|
||||
@patch('os.access')
|
||||
@ -84,6 +84,38 @@ class TestWhich(integration.TestCase):
|
||||
None
|
||||
)
|
||||
|
||||
# The mock patch bellow, since we're not providing the return value, we
|
||||
# will be able to tweak it within the test case. The testcase MUST accept
|
||||
# an arguemnt which is the MagicMock'ed object
|
||||
@patch('os.access')
|
||||
def test_existing_binary_in_windows_pathext(self, osaccess):
|
||||
# We define the side_effect attribute on the mocked object in order to
|
||||
# specify which calls return which values. First call to os.access
|
||||
# returns X, the second Y, the third Z, etc...
|
||||
osaccess.side_effect = [
|
||||
# The first os.access should return False(the abspath one)
|
||||
False,
|
||||
# The second, iterating through $PATH, should also return False,
|
||||
# still checking for Linux
|
||||
False,
|
||||
# We will now also return False 3 times so we get a .CMD back from
|
||||
# the function, see PATHEXT below.
|
||||
# Lastly return True, this is the windows check.
|
||||
False, False, False,
|
||||
True
|
||||
]
|
||||
# Let's patch os.environ to provide a custom PATH variable
|
||||
with patch.dict(os.environ, {'PATH': '/bin',
|
||||
'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;'
|
||||
'.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY'}):
|
||||
# Let's also patch is_windows to return True
|
||||
with patch('salt.utils.is_windows', lambda: True):
|
||||
self.assertEqual(
|
||||
salt.utils.which('this-binary-exists-under-windows'),
|
||||
# The returned path should return the .exe suffix
|
||||
'/bin/this-binary-exists-under-windows.CMD'
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from integration import run_tests
|
||||
|
Loading…
Reference in New Issue
Block a user