Skip to content

Commit cd0e161

Browse files
authored
Fix Windows tests (#4086)
1 parent 0c812de commit cd0e161

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

micromamba/tests/test_activation.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,48 @@
3535
"nu": ".nu",
3636
}
3737

38+
powershell_cmd = None
39+
40+
if plat == "win":
41+
42+
def detect_powershell():
43+
try:
44+
result = subprocess.run(
45+
["pwsh", "-Command", "$PSVersionTable.PSVersion.Major"],
46+
capture_output=True,
47+
text=True,
48+
check=True,
49+
)
50+
version = int(result.stdout.strip())
51+
if version >= 7:
52+
return "pwsh"
53+
except (subprocess.CalledProcessError, FileNotFoundError):
54+
pass
55+
56+
try:
57+
result = subprocess.run(
58+
["powershell", "-Command", "$PSVersionTable.PSVersion.Major"],
59+
capture_output=True,
60+
text=True,
61+
check=True,
62+
)
63+
version = int(result.stdout.strip())
64+
if version >= 5:
65+
return "powershell"
66+
except (subprocess.CalledProcessError, FileNotFoundError):
67+
pass
68+
69+
raise OSError("No compatible PowerShell installation found.")
70+
71+
powershell_cmd = detect_powershell()
72+
3873

3974
class WindowsProfiles:
4075
def __getitem__(self, shell: str) -> str:
4176
if shell == "powershell":
4277
# find powershell profile path dynamically
4378
args = [
44-
"powershell",
79+
powershell_cmd,
4580
"-NoProfile",
4681
"-Command",
4782
"$PROFILE.CurrentUserAllHosts",
@@ -172,7 +207,7 @@ def call_interpreter(s, tmp_path, interpreter, interactive=False, env=None):
172207
if interpreter == "cmd.exe":
173208
args = ["cmd.exe", "/Q", "/C", f]
174209
elif interpreter == "powershell":
175-
args = ["powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", f]
210+
args = [powershell_cmd, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", f]
176211
elif interpreter == "bash" and plat == "win":
177212
args = [os.path.join(os.environ["PROGRAMFILES"], "Git", "bin", "bash.exe"), f]
178213
else:

0 commit comments

Comments
 (0)