Files
mandy/tests/test_boot_ps1_python.py
budanga 298313282a test_boot_ps1_python: test the 3.12 floor and that a hanging candidate leaves no process (BOOT-PS1-PYTHON-ALIAS)
The older-Python stand-in only exited 1, so changing (3, 12) in Test-Python left every test green. A stand-in now
runs Test-Python's own check with sys.version_info set: 3.11.9 is passed over for py -3 and 3.12.0 is taken. The
hanging stand-in sleeps in a Python child with a unique tag on its command line, and the test asserts that no
process holding the tag is left once Resolve-Python returns.

Test-Python's comment now gives the requirement it follows: the repository needs 3.12+ (README.md).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 15:46:47 -03:00

99 lines
4.7 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
import uuid
BOOT = Path(__file__).resolve().parents[1] / "scripts" / "windows-boot" / "boot.ps1"
# Stand-ins: the Store alias, the real interpreter running these tests (3.12+), and one that reports another version.
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"
REAL = f'@"{sys.executable}" %*\r\n'
REAL_PY = f'@if "%1"=="-3" shift\r\n@"{sys.executable}" %1 %2 %3\r\n'
def reports(version):
"""A python.cmd that runs Test-Python's check (its third argument) with sys.version_info set to version."""
return f'@"{sys.executable}" -I -c "import sys; sys.version_info = {version!r}; exec(sys.argv[1])" %3\r\n'
def hangs(tag):
"""A python.cmd that sleeps for 2 minutes in a child process whose command line holds tag."""
return f'@"{sys.executable}" -I -c "import time; time.sleep(120)" {tag}\r\n'
def processes_with(tag):
# The tag is split so that this PowerShell's own command line does not hold it.
half = len(tag) // 2
script = (f"$t = '{tag[:half]}' + '{tag[half:]}'; "
"@(Get-CimInstance Win32_Process | Where-Object { $_.CommandLine -like \"*$t*\" }).Count")
result = subprocess.run(["powershell.exe", "-NoProfile", "-NonInteractive", "-Command", script],
capture_output=True, text=True, timeout=60)
return int(result.stdout.strip())
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=""):
# A stand-in left running holds the folder; the hang test then says so instead of failing on the cleanup.
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) 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": reports((3, 11, 9)), "py.cmd": REAL_PY}), "py -3")
def test_python_3_12_is_taken(self):
self.assertEqual(self.resolve({"python.cmd": reports((3, 12, 0)), "py.cmd": REAL_PY}), "python")
def test_a_python_that_hangs_is_given_up_on(self):
tag = "boot-ps1-hang-" + uuid.uuid4().hex
start = time.monotonic()
self.assertEqual(self.resolve({"python.cmd": hangs(tag), "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
self.assertEqual(processes_with(tag), 0, "the hanging stand-in's child outlived Test-Python")
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()