Fix test_script for Windows

This commit is contained in:
Twangboy 2025-01-17 14:03:07 -07:00 committed by Daniel Wozniak
parent 08853b63a3
commit 445749cbda

View file

@ -11,6 +11,7 @@ import functools
import glob
import logging
import os
import pathlib
import re
import shutil
import subprocess
@ -2937,28 +2938,37 @@ def script(
os.chmod(path, 320)
os.chown(path, __salt__["file.user_to_uid"](runas), -1)
if salt.utils.platform.is_windows() and shell.lower() != "powershell":
cmd_path = _cmd_quote(path, escape=False)
if salt.utils.platform.is_windows():
if shell.lower() != "powershell":
cmd_path = _cmd_quote(path, escape=False)
else:
cmd_path = path
if not env:
env = {}
mod_paths = [
pathlib.Path(
rf'{os.getenv("SystemRoot")}\System32\WindowsPowerShell\v1.0\Modules'
),
pathlib.Path(rf'{os.getenv("ProgramFiles")}\WindowsPowerShell\Modules'),
pathlib.Path(
rf'{os.getenv("ProgramFiles(x86)", "")}\WindowsPowerShell\Modules'
),
]
ps_module_path = [
pathlib.Path(x) for x in os.getenv("PSModulePath", "").split(";")
]
for mod_path in mod_paths:
if mod_path.exists():
if mod_path not in ps_module_path:
ps_module_path.append(mod_path)
mod_paths = ""
for mod_path in ps_module_path:
if mod_path:
mod_paths += f"{str(path)};"
env.update({"PSModulePath": mod_paths})
else:
cmd_path = _cmd_quote(path)
if not env:
env = {}
paths = [
rf'{os.getenv("SystemRoot")}\System32\WindowsPowerShell\v1.0\Modules',
rf'{os.getenv("ProgramFiles")}\WindowsPowerShell\Modules',
rf'{os.getenv("ProgramFiles(x86)", "")}\WindowsPowerShell\Modules',
]
ps_module_path = os.getenv("PSModulePath", "").split(";")
for path in paths:
if os.path.exists(path):
ps_module_path.append(path)
env.update({"PSModulePath": ";".join(ps_module_path)})
ret = _run(
cmd_path + " " + str(args) if args else cmd_path,
cwd=cwd,