Files
mandy/tests/test_windows_boot_journal_seed.py
MoonEyes 79f89c4906 Merge pull request #1609 from lixonik/windows-boot-scratch-cwd
windows-boot: keep driver artifacts outside the checkout (RT-WINDOWS-BOOT-SCRATCH-CWD)
2026-11-29 20:01:20 +01:00

220 lines
12 KiB
Python

"""scripts/windows-boot/boot.ps1 hands the journal of `gta6rt --precompile-pipelines` to the boot as its read-only seed.
The pre-compile writes pipeline_cache.d beside gta6rt.exe; boot.ps1 points the runtime at its own cache folder, so without
LEONIDA_PIPELINE_JOURNAL_SEED_DIR a boot compiled again every pipeline the pre-compile had built. These cases run
`boot.ps1 -DryRun` (nothing is launched, no game files are needed) against a temporary runtime folder and read the
`[boot] env` lines it prints. Windows only: the script is Windows PowerShell.
They also pin its LEONIDA_DEVICE_FAULT default, 2. With 1 the runtime also turns on NVIDIA's diagnostics config, which is
part of every compute journal file name, so a boot read none of a journal pre-compiled without it; 2 keeps the
device-fault report and checkpoints without that config. LEONIDA_AFTERMATH=1 gets 1 unless the user chose a value: the
runtime reads LEONIDA_DEVICE_FAULT before LEONIDA_AFTERMATH, and only 1 gives the dump its diagnostics config. The
README's table is read as text, on any system.
"""
import os
import re
import shutil
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
REPO = Path(__file__).resolve().parents[1]
BOOT = REPO / "scripts" / "windows-boot" / "boot.ps1"
POWERSHELL = shutil.which("powershell") or shutil.which("pwsh")
SEED = "LEONIDA_PIPELINE_JOURNAL_SEED_DIR"
DEVICE_FAULT = "LEONIDA_DEVICE_FAULT"
AFTERMATH = "LEONIDA_AFTERMATH"
README = REPO / "scripts" / "windows-boot" / "README.md"
@unittest.skipUnless(sys.platform == "win32" and POWERSHELL, "boot.ps1 is a Windows PowerShell script")
class BootJournalSeedTest(unittest.TestCase):
def setUp(self):
self.tmp = Path(os.path.realpath(tempfile.mkdtemp(prefix="leonida_boot_seed_"))) # not the RUNNER~1 short name
self.addCleanup(shutil.rmtree, self.tmp, ignore_errors=True)
self.runtime = self.tmp / "port"
self.runtime.mkdir()
(self.runtime / "gta6rt.exe").write_bytes(b"")
self.game = self.tmp / "game"
self.game.mkdir()
self.out = self.tmp / "runs"
def env_lines(self, *pairs, shell=None, set_env=None, runtime=None, command_in=None):
env = {k: v for k, v in os.environ.items() if not k.startswith("LEONIDA_") and k not in ("GTA6RT",)}
env.update(shell or {})
runtime = runtime or str(self.runtime / "gta6rt.exe")
args = ["-DryRun", "-NoInput", "-Runtime", runtime, "-GameDir", str(self.game), "-OutDir", str(self.out), *pairs]
if command_in or set_env:
# -SetEnv takes a hashtable, which only -Command can pass. Set-Location inside the session moves $PWD but not
# the process's current directory, as in an interactive shell. Parameter names stay bare (quoted, '-DryRun'
# would be a KEY=VALUE pair); every value is quoted.
def q(text):
return "'" + text.replace("'", "''") + "'"
quoted = " ".join(a if a.startswith("-") else q(a) for a in args)
if set_env:
quoted += " -SetEnv @{" + "; ".join(f"{q(k)} = {q(v)}" for k, v in set_env.items()) + "}"
location = f"Set-Location -LiteralPath {q(command_in)}; " if command_in else ""
cmd = [POWERSHELL, "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command",
f"{location}& {q(str(BOOT))} {quoted}"]
else:
cmd = [POWERSHELL, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(BOOT), *args]
# Windows PowerShell writes the console's OEM code page when its output is redirected.
result = subprocess.run(cmd, capture_output=True, encoding="oem", errors="replace", env=env, timeout=180,
cwd=str(REPO))
lines = {}
for line in result.stdout.splitlines():
if line.startswith("[boot] env "):
key, _, value = line[len("[boot] env "):].strip().partition("=")
lines[key] = value
self.assertIn("LEONIDA_D3D12", lines, result.stdout + result.stderr) # the script got as far as its env list
return lines
def test_precompiled_journal_beside_the_runtime_is_the_seed(self):
(self.runtime / "pipeline_cache.d").mkdir()
lines = self.env_lines()
self.assertEqual(os.path.normcase(lines.get(SEED, "")), os.path.normcase(str(self.runtime / "pipeline_cache.d")))
def test_no_precompiled_journal_no_seed(self):
self.assertNotIn(SEED, self.env_lines())
def test_a_seed_given_on_the_command_line_wins(self):
(self.runtime / "pipeline_cache.d").mkdir()
other = str(self.tmp / "elsewhere")
self.assertEqual(self.env_lines(f"{SEED}={other}").get(SEED), other)
def test_key_with_no_value_removes_the_seed(self):
(self.runtime / "pipeline_cache.d").mkdir()
self.assertNotIn(SEED, self.env_lines(f"{SEED}="))
def test_a_seed_set_in_the_shell_is_kept(self):
(self.runtime / "pipeline_cache.d").mkdir()
other = str(self.tmp / "from-shell")
self.assertEqual(self.env_lines(shell={SEED: other}).get(SEED), other)
def test_no_seed_when_the_boot_cache_is_the_runtime_folder(self):
# The runtime's writable journal would then be the seed itself, and it turns the journal off for that.
(self.runtime / "pipeline_cache.d").mkdir()
self.assertNotIn(SEED, self.env_lines(f"LEONIDA_PIPELINE_CACHE_DIR={self.runtime}"))
def test_no_seed_when_the_boot_cache_sits_inside_it(self):
(self.runtime / "pipeline_cache.d").mkdir()
inside = self.runtime / "pipeline_cache.d" / "boot"
self.assertNotIn(SEED, self.env_lines(f"LEONIDA_PIPELINE_CACHE_DIR={inside}"))
def test_no_seed_when_the_cache_folder_is_empty(self):
# The runtime then keeps its writable journal beside gta6rt.exe: that is the precompiled folder itself.
(self.runtime / "pipeline_cache.d").mkdir()
self.assertNotIn(SEED, self.env_lines("LEONIDA_PIPELINE_CACHE_DIR="))
def test_no_seed_when_the_runtime_sits_under_the_boot_journal(self):
cache = self.tmp / "cache"
runtime = cache / "pipeline_cache.d" / "port"
(runtime / "pipeline_cache.d").mkdir(parents=True)
(runtime / "gta6rt.exe").write_bytes(b"")
self.assertNotIn(SEED, self.env_lines(f"LEONIDA_PIPELINE_CACHE_DIR={cache}", runtime=str(runtime / "gta6rt.exe")))
def test_paths_compare_without_case(self):
(self.runtime / "pipeline_cache.d").mkdir()
self.assertNotIn(SEED, self.env_lines(f"LEONIDA_PIPELINE_CACHE_DIR={str(self.runtime).upper()}"))
def test_a_sibling_folder_is_not_an_overlap(self):
(self.runtime / "pipeline_cache.d").mkdir()
sibling = self.runtime / "pipeline_cache.d-x"
lines = self.env_lines(f"LEONIDA_PIPELINE_CACHE_DIR={sibling}")
self.assertEqual(os.path.normcase(lines.get(SEED, "")), os.path.normcase(str(self.runtime / "pipeline_cache.d")))
def test_no_seed_for_a_relative_cache_folder(self):
(self.runtime / "pipeline_cache.d").mkdir()
lines = self.env_lines("LEONIDA_PIPELINE_CACHE_DIR=relative\\cache", command_in=str(self.tmp))
self.assertNotIn(SEED, lines)
self.assertEqual(os.path.normcase(lines["LEONIDA_PIPELINE_CACHE_DIR"]),
os.path.normcase(str(self.tmp / "relative" / "cache")))
def test_a_long_path_prefix_names_the_same_folder(self):
(self.runtime / "pipeline_cache.d").mkdir()
self.assertNotIn(SEED, self.env_lines(f"LEONIDA_PIPELINE_CACHE_DIR=\\\\?\\{self.runtime}"))
def test_a_device_path_prefix_names_the_same_folder(self):
(self.runtime / "pipeline_cache.d").mkdir()
self.assertNotIn(SEED, self.env_lines(f"LEONIDA_PIPELINE_CACHE_DIR=\\\\.\\{self.runtime}"))
def test_a_junction_to_the_runtime_folder_is_recognised(self):
(self.runtime / "pipeline_cache.d").mkdir()
alias = self.tmp / "port-alias"
made = subprocess.run(["cmd", "/c", "mklink", "/J", str(alias), str(self.runtime)], capture_output=True)
if made.returncode != 0 or not alias.is_dir():
self.skipTest("could not make a junction here")
self.addCleanup(lambda: os.path.isdir(alias) and os.rmdir(alias)) # removes the junction, not its target
self.assertNotIn(SEED, self.env_lines(f"LEONIDA_PIPELINE_CACHE_DIR={alias}"))
def test_an_unusable_cache_folder_name_does_not_stop_the_script(self):
(self.runtime / "pipeline_cache.d").mkdir()
self.assertNotIn(SEED, self.env_lines("LEONIDA_PIPELINE_CACHE_DIR=C:\\a|b"))
def test_a_relative_runtime_is_resolved_from_the_shell_location(self):
(self.runtime / "pipeline_cache.d").mkdir()
lines = self.env_lines(runtime="port\\gta6rt.exe", command_in=str(self.tmp))
self.assertEqual(os.path.normcase(lines.get(SEED, "")), os.path.normcase(str(self.runtime / "pipeline_cache.d")))
def test_device_fault_default_leaves_the_journal_names_alone(self):
self.assertEqual(self.env_lines().get(DEVICE_FAULT), "2")
def test_device_fault_given_on_the_command_line_wins(self):
self.assertEqual(self.env_lines(f"{DEVICE_FAULT}=1").get(DEVICE_FAULT), "1")
def test_key_with_no_value_removes_device_fault(self):
self.assertNotIn(DEVICE_FAULT, self.env_lines(f"{DEVICE_FAULT}="))
def test_device_fault_set_in_the_shell_is_kept(self):
self.assertEqual(self.env_lines(shell={DEVICE_FAULT: "1"}).get(DEVICE_FAULT), "1")
def test_device_fault_given_with_set_env_wins(self):
self.assertEqual(self.env_lines(set_env={DEVICE_FAULT: "1"}).get(DEVICE_FAULT), "1")
def test_aftermath_gets_the_diagnostics_config(self):
# The runtime reads LEONIDA_DEVICE_FAULT first; with 2 a crash dump would have no shader debug info.
for how in ("argument", "shell", "set_env"):
with self.subTest(how=how):
pairs, kwargs = self.given(how, {AFTERMATH: "1"})
lines = self.env_lines(*pairs, **kwargs)
self.assertEqual(lines.get(AFTERMATH), "1")
self.assertEqual(lines.get(DEVICE_FAULT), "1")
def test_aftermath_keeps_a_device_fault_the_user_gave(self):
for how in ("argument", "shell", "set_env"):
with self.subTest(how=how):
pairs, kwargs = self.given(how, {DEVICE_FAULT: "2"})
self.assertEqual(self.env_lines(f"{AFTERMATH}=1", *pairs, **kwargs).get(DEVICE_FAULT), "2")
def test_aftermath_off_keeps_the_default(self):
# No LEONIDA_AFTERMATH at all is test_device_fault_default_leaves_the_journal_names_alone.
self.assertEqual(self.env_lines(f"{AFTERMATH}=0").get(DEVICE_FAULT), "2")
@staticmethod
def given(how, values):
"""The values as KEY=VALUE arguments, or as env_lines' shell= or set_env=, the three ways a user sets them."""
if how == "argument":
return [f"{k}={v}" for k, v in values.items()], {}
return [], {how: values}
class DeviceFaultReadmeTest(unittest.TestCase):
def test_readme_gives_the_default_2(self):
# Markdown marks (backticks, bold) are dropped, so a change of formatting alone does not fail this.
text = re.sub(r"[`*]", "", README.read_text(encoding="utf-8"))
rows = [cells for cells in (line.split("|") for line in text.splitlines() if line.startswith("|"))
if len(cells) > 3 and cells[1].strip() == DEVICE_FAULT]
self.assertEqual(len(rows), 1, rows)
self.assertEqual(rows[0][2].strip(), "2", rows[0])
# No prose says the script sets 1 (the row of LEONIDA_PIPELINE_JOURNAL_SEED_DIR said so before).
said_1 = re.findall(rf"script\s+sets\s+(?:{DEVICE_FAULT}\s*=\s*)?1\b[^|]*", text)
self.assertEqual(said_1, [])
if __name__ == "__main__":
unittest.main()