Skip to content

Commit

Permalink
Fix test_script for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
twangboy authored and dwoz committed Jan 17, 2025
1 parent 08853b6 commit 445749c
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions salt/modules/cmdmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import glob
import logging
import os
import pathlib
import re
import shutil
import subprocess
Expand Down Expand Up @@ -2937,28 +2938,37 @@ def _cleanup_tempfile(path):
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,
Expand Down

0 comments on commit 445749c

Please sign in to comment.