2019.2 bacpkport: modules.cmdmod: handle windows environ better

python exposes an nt.environ for case insensitive environment behavior
that is native to windows; so it makes sense to use this instead of
os.environ to avoid enexpected behavior and failure.

further detail: https://bugs.python.org/issue28824
This commit is contained in:
Matt Phillips 2019-04-10 09:39:32 -04:00
parent 3173673dcc
commit 736a437520
2 changed files with 20 additions and 2 deletions

View file

@ -570,7 +570,11 @@ def _run(cmd,
run_env = env
else:
run_env = os.environ.copy()
if salt.utils.platform.is_windows():
import nt
run_env = nt.environ.copy()
else:
run_env = os.environ.copy()
run_env.update(env)
if prepend_path:
@ -3284,7 +3288,12 @@ def shell_info(shell, list_modules=False):
# salt-call will general have home set, the salt-minion service may not
# We need to assume ports of unix shells to windows will look after
# themselves in setting HOME as they do it in many different ways
newenv = os.environ
if salt.utils.platform.is_windows():
import nt
newenv = nt.environ
else:
newenv = os.environ
if ('HOME' not in newenv) and (not salt.utils.platform.is_windows()):
newenv['HOME'] = os.path.expanduser('~')
log.debug('HOME environment set to %s', newenv['HOME'])

View file

@ -403,3 +403,12 @@ class CMDModuleTest(ModuleCase):
self.assertIn('administrator', cmd)
else:
self.assertEqual('root', cmd)
@skipIf(not salt.utils.platform.is_windows(), 'minion is not windows')
def test_windows_env_handling(self):
'''
Ensure that nt.environ is used properly with cmd.run*
'''
out = self.run_function('cmd.run', ['set'], env={"abc": "123", "ABC": "456"}).splitlines()
self.assertIn('abc=123', out)
self.assertIn('ABC=456', out)