mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 13:38:36 +00:00
273 lines
12 KiB
Python
273 lines
12 KiB
Python
"""Tests for the Windows launchers.
|
|
|
|
python -I tests/test_win_launchers.py
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
BUILD_BAT = ROOT / "Build.bat"
|
|
MODS_BAT = ROOT / "Mods.bat"
|
|
NVIDIA_LAUNCHER = ROOT / "d3d12" / "scripts" / "play-nvidia.ps1"
|
|
POWERSHELL = shutil.which("pwsh") or shutil.which("powershell.exe")
|
|
|
|
FAKE_PY_CS = r"""
|
|
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
public class FakePython {
|
|
public static int Main(string[] args) {
|
|
if (args.Length >= 2 && args[0] == "-c") {
|
|
return Environment.GetEnvironmentVariable("FAKE_PY_OLD") == "1" ? 1 : 0;
|
|
}
|
|
string log = Environment.GetEnvironmentVariable("FAKE_PY_LOG");
|
|
if (!string.IsNullOrEmpty(log)) {
|
|
File.WriteAllLines(log, args, Encoding.UTF8);
|
|
}
|
|
string rc = Environment.GetEnvironmentVariable("FAKE_PY_RC");
|
|
return string.IsNullOrEmpty(rc) ? 0 : int.Parse(rc);
|
|
}
|
|
}
|
|
"""
|
|
|
|
|
|
class WinLaunchersTests(unittest.TestCase):
|
|
@unittest.skipUnless(POWERSHELL, "requires PowerShell")
|
|
def test_nvidia_menu_restart_preserves_motion_blur_switch(self) -> None:
|
|
with tempfile.TemporaryDirectory() as td:
|
|
root = Path(td)
|
|
scripts = root / "scripts"
|
|
scripts.mkdir()
|
|
launcher = scripts / NVIDIA_LAUNCHER.name
|
|
shutil.copyfile(NVIDIA_LAUNCHER, launcher)
|
|
executable = root / "synthetic title.exe"
|
|
executable.touch()
|
|
|
|
command = r"""
|
|
$ErrorActionPreference = 'Stop'
|
|
function Get-CimInstance {
|
|
param([string]$ClassName)
|
|
if ($ClassName -ne 'Win32_OperatingSystem') { throw "Unexpected CIM query: $ClassName" }
|
|
[pscustomobject]@{ BuildNumber = '26100' }
|
|
}
|
|
$options = @{
|
|
Exe = $env:TEST_NVIDIA_EXE
|
|
DryRun = $true
|
|
NoDlss = $true
|
|
NoPrecompile = $true
|
|
WindowsBarMB = 0
|
|
}
|
|
& $env:TEST_NVIDIA_LAUNCHER @options -MotionBlur
|
|
& $env:TEST_NVIDIA_LAUNCHER @options -MotionBlur:$false
|
|
& $env:TEST_NVIDIA_LAUNCHER @options
|
|
"""
|
|
env = {name: value for name, value in os.environ.items() if not name.upper().startswith("LEONIDA_")}
|
|
env.update(TEST_NVIDIA_LAUNCHER=str(launcher), TEST_NVIDIA_EXE=str(executable))
|
|
result = subprocess.run(
|
|
[POWERSHELL, "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", command],
|
|
env=env,
|
|
check=True,
|
|
capture_output=True,
|
|
encoding="utf-8",
|
|
errors="replace",
|
|
timeout=30,
|
|
)
|
|
restart_args = [line for line in result.stdout.splitlines() if line.startswith("LEONIDA_MENU_ARGS=")]
|
|
self.assertEqual(len(restart_args), 3, result.stdout)
|
|
self.assertRegex(restart_args[0], r"(?:^| )-MotionBlur(?: |$)")
|
|
for args in restart_args[1:]:
|
|
self.assertNotRegex(args, r"(?:^| )-MotionBlur(?: |$)")
|
|
|
|
def test_static_quoting_and_no_pause_guards(self) -> None:
|
|
build_txt = BUILD_BAT.read_text(encoding="utf-8")
|
|
mods_txt = MODS_BAT.read_text(encoding="utf-8")
|
|
for txt in (build_txt, mods_txt):
|
|
self.assertIn('set "PY_EXE=', txt)
|
|
self.assertIn('set "PY_EXE=%~1"', txt)
|
|
self.assertNotIn('set PY="%~1"', txt)
|
|
self.assertIn("LEONIDA_NO_PAUSE", txt)
|
|
self.assertIn(":find_user_python", mods_txt)
|
|
self.assertIn("if not defined PYTMP", build_txt)
|
|
|
|
@unittest.skipUnless(os.name == "nt" and shutil.which("powershell.exe"), "requires Windows cmd.exe and PowerShell")
|
|
def test_per_user_python_discovery_with_ampersand_and_exit_propagation(self) -> None:
|
|
with tempfile.TemporaryDirectory() as td:
|
|
root = Path(td)
|
|
repo = root / "repo"
|
|
(repo / "tools").mkdir(parents=True)
|
|
shutil.copy2(BUILD_BAT, repo / "Build.bat")
|
|
shutil.copy2(MODS_BAT, repo / "Mods.bat")
|
|
(repo / "make_game.py").write_text("# stub\n", encoding="utf-8", newline="\n")
|
|
(repo / "tools" / "leonida_mods.py").write_text("# stub\n", encoding="utf-8", newline="\n")
|
|
|
|
local_app = root / "user & test"
|
|
py_dir = local_app / "Programs" / "Python" / "Python313"
|
|
py_dir.mkdir(parents=True)
|
|
plain_stub = root / "fake_py.exe"
|
|
|
|
ps_base = ["powershell.exe", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass"]
|
|
ps_env = {**os.environ, "FAKE_PY_EXE": str(plain_stub), "FAKE_PY_CS": FAKE_PY_CS}
|
|
ps_env["PSModulePath"] = str(Path(os.environ["SystemRoot"]) / "System32/WindowsPowerShell/v1.0/Modules")
|
|
compile_cmd = (
|
|
"$ErrorActionPreference = 'Stop'; "
|
|
"Add-Type -OutputAssembly $env:FAKE_PY_EXE -OutputType ConsoleApplication -TypeDefinition $env:FAKE_PY_CS"
|
|
)
|
|
subprocess.run([*ps_base, "-Command", compile_cmd], env=ps_env, check=True, timeout=60)
|
|
shutil.move(str(plain_stub), str(py_dir / "python.exe"))
|
|
|
|
sys32 = str(Path(os.environ["SystemRoot"]) / "System32")
|
|
clean_env = {
|
|
"SystemRoot": os.environ["SystemRoot"],
|
|
"ComSpec": os.environ.get("ComSpec", os.path.join(sys32, "cmd.exe")),
|
|
"PATH": sys32,
|
|
"LOCALAPPDATA": str(local_app),
|
|
"TEMP": str(root),
|
|
"TMP": str(root),
|
|
"LEONIDA_NO_PAUSE": "1",
|
|
"LEONIDA_PY_INSTALL_DRYRUN": "1",
|
|
}
|
|
|
|
log_build = root / "build.args"
|
|
r_build = subprocess.run(
|
|
["cmd.exe", "/c", "call", str(repo / "Build.bat"), "--out", "D:\\Out Dir"],
|
|
env={**clean_env, "FAKE_PY_LOG": str(log_build), "FAKE_PY_RC": "0"},
|
|
stdin=subprocess.DEVNULL,
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=30,
|
|
)
|
|
self.assertEqual(r_build.returncode, 0, r_build.stdout + r_build.stderr)
|
|
self.assertEqual(
|
|
log_build.read_text(encoding="utf-8-sig").splitlines(),
|
|
["-I", str(repo / "make_game.py"), "--out", "D:\\Out Dir"],
|
|
)
|
|
|
|
log_mods = root / "mods.args"
|
|
r_mods = subprocess.run(
|
|
["cmd.exe", "/c", "call", str(repo / "Mods.bat"), "--check"],
|
|
env={**clean_env, "FAKE_PY_LOG": str(log_mods), "FAKE_PY_RC": "5"},
|
|
stdin=subprocess.DEVNULL,
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=30,
|
|
)
|
|
self.assertEqual(r_mods.returncode, 5, r_mods.stdout + r_mods.stderr)
|
|
self.assertEqual(
|
|
log_mods.read_text(encoding="utf-8-sig").splitlines(),
|
|
["-I", str(repo / "tools" / "leonida_mods.py"), "gui", "--check"],
|
|
)
|
|
|
|
# Without a qualifying Python, Mods.bat exits 1 cleanly without hanging on pause.
|
|
r_missing = subprocess.run(
|
|
["cmd.exe", "/c", "call", str(repo / "Mods.bat")],
|
|
env={**clean_env, "FAKE_PY_OLD": "1"},
|
|
stdin=subprocess.DEVNULL,
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=30,
|
|
)
|
|
self.assertEqual(r_missing.returncode, 1, r_missing.stdout + r_missing.stderr)
|
|
self.assertIn("Python 3.12 or newer is needed", r_missing.stdout + r_missing.stderr)
|
|
|
|
@unittest.skipUnless(POWERSHELL, "requires PowerShell")
|
|
def test_dynamic_resolution_preset_forwards_explicit_motion_blur_values(self) -> None:
|
|
preset_source = NVIDIA_LAUNCHER.with_name("play-X-1080-1440p.ps1")
|
|
stub = r"""
|
|
param([switch]$SeriesX, [string]$Lowest, [switch]$MotionBlur)
|
|
$motionBlurArgument = if ($PSBoundParameters.ContainsKey('MotionBlur')) {
|
|
$MotionBlur.IsPresent.ToString()
|
|
} else {
|
|
'omitted'
|
|
}
|
|
"$($SeriesX.IsPresent) $Lowest $motionBlurArgument"
|
|
"""
|
|
command = r"""
|
|
$ErrorActionPreference = 'Stop'
|
|
& $env:TEST_LAUNCHER_PRESET
|
|
& $env:TEST_LAUNCHER_PRESET -MotionBlur:$false
|
|
& $env:TEST_LAUNCHER_PRESET -MotionBlur
|
|
& $env:TEST_LAUNCHER_PRESET -MotionBlur:$true
|
|
"""
|
|
with tempfile.TemporaryDirectory() as td:
|
|
root = Path(td)
|
|
preset = root / preset_source.name
|
|
shutil.copyfile(preset_source, preset)
|
|
(root / NVIDIA_LAUNCHER.name).write_text(stub, encoding="utf-8", newline="\n")
|
|
result = subprocess.run(
|
|
[POWERSHELL, "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", command],
|
|
env={**os.environ, "TEST_LAUNCHER_PRESET": str(preset)},
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=30,
|
|
)
|
|
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
|
self.assertEqual(result.stdout.splitlines(),
|
|
["True 1080 omitted", "True 1080 False", "True 1080 True", "True 1080 True"])
|
|
|
|
def test_dynamic_resolution_preset_binds_motion_blur_switch(self) -> None:
|
|
powershell = shutil.which("pwsh") or shutil.which("powershell.exe")
|
|
if not powershell:
|
|
self.skipTest("requires PowerShell")
|
|
|
|
command = r"""
|
|
$ErrorActionPreference = 'Stop'
|
|
function Get-CimInstance {
|
|
param([string]$ClassName)
|
|
if ($ClassName -ne 'Win32_OperatingSystem') { throw "Unexpected CIM query: $ClassName" }
|
|
[pscustomobject]@{ BuildNumber = '26100' }
|
|
}
|
|
function Show-Preset([string[]]$Lines) {
|
|
$Lines -like 'LEONIDA_OUTPUT=*'
|
|
$Lines -like 'LEONIDA_RENDER_SCALE_MIN=*'
|
|
'PRESET_SKIPS=' + ($Lines -like 'LEONIDA_SKIP_PIPELINES=*')
|
|
}
|
|
$preset = $env:TEST_LAUNCHER_PRESET
|
|
$exe = $env:TEST_LAUNCHER_EXE
|
|
Show-Preset (& $preset -Exe $exe -NoDlss -NoPrecompile -WindowsBarMB 0 -DryRun)
|
|
Show-Preset (& $preset -Exe $exe -NoDlss -NoPrecompile -WindowsBarMB 0 -DryRun -MotionBlur:$false)
|
|
Show-Preset (& $preset -Exe $exe -NoDlss -NoPrecompile -WindowsBarMB 0 -DryRun -MotionBlur)
|
|
Show-Preset (& $preset -Exe $exe -NoDlss -NoPrecompile -WindowsBarMB 0 -DryRun -MotionBlur:$true)
|
|
"""
|
|
with tempfile.TemporaryDirectory() as td:
|
|
root = Path(td)
|
|
scripts = root / "scripts"
|
|
scripts.mkdir()
|
|
source = ROOT / "d3d12" / "scripts"
|
|
preset = scripts / "play-X-1080-1440p.ps1"
|
|
shutil.copyfile(source / preset.name, preset)
|
|
shutil.copyfile(source / "play-nvidia.ps1", scripts / "play-nvidia.ps1")
|
|
executable = root / "synthetic title.exe"
|
|
executable.touch()
|
|
env = {name: value for name, value in os.environ.items() if not name.upper().startswith("LEONIDA_")}
|
|
env.update(TEST_LAUNCHER_PRESET=str(preset), TEST_LAUNCHER_EXE=str(executable))
|
|
result = subprocess.run(
|
|
[powershell, "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", command],
|
|
env=env,
|
|
capture_output=True,
|
|
encoding="utf-8",
|
|
errors="replace",
|
|
timeout=30,
|
|
)
|
|
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
|
lines = result.stdout.splitlines()
|
|
self.assertEqual([line for line in lines if line.startswith("LEONIDA_OUTPUT=")],
|
|
["LEONIDA_OUTPUT=2560x1440"] * 4)
|
|
self.assertEqual([line for line in lines if line.startswith("LEONIDA_RENDER_SCALE_MIN=")],
|
|
["LEONIDA_RENDER_SCALE_MIN=0.75"] * 4)
|
|
skips = [line for line in lines if line.startswith("PRESET_SKIPS=")]
|
|
self.assertEqual(len(skips), 4, result.stdout)
|
|
self.assertEqual(skips[0], skips[1])
|
|
self.assertEqual(skips[2], skips[3])
|
|
self.assertNotEqual(skips[0], skips[2])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|