Files
mandy/tests/test_boot_ps1_python.py
budanga 48defb9c08 boot.ps1: take a Python that starts and is 3.12+, not the Microsoft Store alias (BOOT-PS1-PYTHON-ALIAS)
Resolve-Python took `python` as soon as Get-Command found it. On Windows that also finds the Store's
app execution alias in WindowsApps, which only says how to install Python and exits 9009, so with
Python installed through `py` the boot's summary held that message and `py -3` was never tried.

Test-Python now starts each candidate (python, then py -3) and takes the first that exits 0 on a
3.12+ check, within 15 s; one that hangs is ended with its children and passed over. -Python is
used as given. tests/test_boot_ps1_python.py runs both functions against stand-in commands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 17:24:49 -03:00

74 lines
3.4 KiB
Python

"""scripts/windows-boot/boot.ps1: Resolve-Python takes a Python that starts and is 3.12+, not just one on PATH.
python -I tests/test_boot_ps1_python.py -v
The Microsoft Store's python.exe alias in WindowsApps is found by Get-Command but only says how to install Python and
exits 9009; when it was taken, tools/boot_trace.py never ran and <name>.summary.txt held only that message. The test
runs the script's own Test-Python and Resolve-Python against stand-in python.cmd and py.cmd. No game data.
"""
import os
from pathlib import Path
import re
import subprocess
import sys
import tempfile
import time
import unittest
BOOT = Path(__file__).resolve().parents[1] / "scripts" / "windows-boot" / "boot.ps1"
# Stand-ins: the Store alias, a Python older than 3.12, and the real interpreter running these tests (3.12+).
STORE_ALIAS = "@echo off\r\necho Python was not found; run without arguments to install from the Microsoft Store\r\nexit /b 9009\r\n"
OLD = "@echo off\r\nexit /b 1\r\n"
REAL = f'@"{sys.executable}" %*\r\n'
REAL_PY = f'@if "%1"=="-3" shift\r\n@"{sys.executable}" %1 %2 %3\r\n'
HANGS = "@echo off\r\nping -n 120 127.0.0.1 >nul\r\n"
def functions():
text = BOOT.read_text("utf-8")
found = [re.search(r"^function %s \{.*?^\}" % name, text, re.S | re.M) for name in ("Test-Python", "Resolve-Python")]
assert all(found), "Test-Python and Resolve-Python are no longer in boot.ps1"
return "\n\n".join(m.group(0) for m in found)
@unittest.skipUnless(os.name == "nt", "Windows PowerShell")
class ResolvePythonTests(unittest.TestCase):
def resolve(self, files, requested=""):
with tempfile.TemporaryDirectory() as tmp:
for name, body in files.items():
Path(tmp, name).write_text(body, encoding="ascii", newline="")
system32 = str(Path(os.environ["SystemRoot"], "System32"))
env = {**os.environ, "PATH": tmp + os.pathsep + system32}
script = (f"$ErrorActionPreference = 'Stop'\n{functions()}\n"
f"(@(Resolve-Python '{requested}') -join ' ')")
result = subprocess.run(["powershell.exe", "-NoProfile", "-NonInteractive", "-Command", script],
cwd=tmp, env=env, capture_output=True, text=True, timeout=60)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
return result.stdout.strip()
def test_the_store_alias_is_passed_over_for_py(self):
self.assertEqual(self.resolve({"python.cmd": STORE_ALIAS, "py.cmd": REAL_PY}), "py -3")
def test_a_working_python_is_taken(self):
self.assertEqual(self.resolve({"python.cmd": REAL, "py.cmd": REAL_PY}), "python")
def test_a_python_older_than_3_12_is_passed_over(self):
self.assertEqual(self.resolve({"python.cmd": OLD, "py.cmd": REAL_PY}), "py -3")
def test_a_python_that_hangs_is_given_up_on(self):
start = time.monotonic()
self.assertEqual(self.resolve({"python.cmd": HANGS, "py.cmd": REAL_PY}), "py -3")
self.assertLess(time.monotonic() - start, 45) # 15 s for python, then py; the stand-in would hang 2 min
def test_none_that_works_gives_none(self):
self.assertEqual(self.resolve({"python.cmd": STORE_ALIAS}), "")
def test_a_named_python_is_used_as_given(self):
self.assertEqual(self.resolve({"python.cmd": STORE_ALIAS}, requested="C:\\Python312\\python.exe"),
"C:\\Python312\\python.exe")
if __name__ == "__main__":
unittest.main()