Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions scripts/run_guest_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
python -I scripts/run_guest_scene.py --loader L --guest G --shaders ROOT [--out DIR] [--wine WINE]
[--play-audio] [--negative-controls]

Outside Windows, --wine defaults to $WINE (e.g. UMU-Proton's files/bin/wine) and one of the two is required.

Each run renders offscreen in a fresh process and write overlay. Without --out the runs go to a temporary folder that is removed
afterwards. The summary is printed first, and on failure the last run's log tails are printed too. Audio stays offline
unless --play-audio sets LEONIDA_SCENE_AUDIO=winmm. The guest then plays the pulse synchronously with PlaySoundW.
Expand Down Expand Up @@ -324,13 +326,14 @@ def parse_args() -> argparse.Namespace:
ap.add_argument("--guest", type=Path, required=True)
ap.add_argument("--shaders", type=Path, required=True, help="shader root containing spirv/")
ap.add_argument("--out", type=Path, help="new or empty folder (default: a temporary folder, removed afterwards)")
ap.add_argument("--wine", help="run the loader through this Wine binary")
ap.add_argument("--wine", default=(os.environ.get("WINE") or None) if os.name != "nt" else None,
help="run the loader through this Wine binary (default: $WINE outside Windows)")
ap.add_argument("--wine-arg", action="append", default=[], help="argument to the Wine launcher (repeatable)")
ap.add_argument("--play-audio", action="store_true", help="LEONIDA_SCENE_AUDIO=winmm instead of offline")
ap.add_argument("--negative-controls", action="store_true", help="also run a bad scene.bin and checkpoint")
args = ap.parse_args()
if args.wine_arg and not args.wine:
ap.error("--wine-arg requires --wine")
ap.error("--wine-arg requires --wine" if os.name == "nt" else "--wine-arg requires --wine or WINE")
for name in ("loader", "guest"):
if not getattr(args, name).is_file():
ap.error(f"--{name}: no such file")
Expand All @@ -339,10 +342,10 @@ def parse_args() -> argparse.Namespace:
if args.wine:
found = args.wine if Path(args.wine).is_file() else shutil.which(args.wine)
if not found:
ap.error("--wine: not found")
ap.error(f"--wine: not found: {args.wine}")
args.wine = str(Path(found).resolve())
elif os.name != "nt":
ap.error("--wine is required outside Windows")
ap.error("--wine (or WINE) is required outside Windows")
if args.out and args.out.exists() and (not args.out.is_dir() or any(args.out.iterdir())):
ap.error("--out must be new or an empty folder")
args.loader, args.guest, args.shaders = args.loader.resolve(), args.guest.resolve(), args.shaders.resolve()
Expand Down
122 changes: 122 additions & 0 deletions tests/test_run_guest_scene_wine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
"""run_guest_scene.py takes the Wine to use from $WINE when --wine is not given."""
import contextlib
import importlib.util
import io
import os
from pathlib import Path
import stat
import sys
import tempfile
from types import SimpleNamespace
import unittest
from unittest import mock

path = Path(__file__).resolve().parents[1] / "scripts" / "run_guest_scene.py"
spec = importlib.util.spec_from_file_location("run_guest_scene_wine", path)
runner = importlib.util.module_from_spec(spec)
spec.loader.exec_module(runner)


@unittest.skipIf(os.name == "nt", "the Wine checks only apply outside Windows")
class RunGuestSceneWineTest(unittest.TestCase):
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.root = Path(tmp.name)
self.loader, self.guest = self.root / "loader.exe", self.root / "guest.exe"
self.loader.write_bytes(b"MZ")
self.guest.write_bytes(b"MZ")
(self.root / "shaders" / "spirv").mkdir(parents=True)
self.wine = self.root / "umu-proton" / "files" / "bin" / "wine"
self.wine.parent.mkdir(parents=True)
self.wine.write_text("#!/bin/sh\n")
self.wine.chmod(self.wine.stat().st_mode | stat.S_IXUSR)

def parse(self, *extra, env=None):
argv = ["run_guest_scene.py", "--loader", str(self.loader), "--guest", str(self.guest),
"--shaders", str(self.root / "shaders"), *extra]
environ = {k: v for k, v in os.environ.items() if k != "WINE"}
environ.update(env or {})
err = io.StringIO()
with mock.patch.object(sys, "argv", argv), mock.patch.dict(os.environ, environ, clear=True), \
contextlib.redirect_stderr(err):
try:
return runner.parse_args(), err.getvalue()
except SystemExit as e:
return e.code, err.getvalue()

def test_wine_from_environment(self):
args, _ = self.parse(env={"WINE": str(self.wine)})
self.assertEqual(args.wine, str(self.wine.resolve()))

def test_wine_args_with_wine_from_environment(self):
args, _ = self.parse("--wine-arg=--debug", env={"WINE": str(self.wine)})
self.assertEqual((args.wine, args.wine_arg), (str(self.wine.resolve()), ["--debug"]))

def test_option_overrides_environment(self):
other = self.root / "wine-other"
other.write_text("#!/bin/sh\n")
args, _ = self.parse("--wine", str(other), env={"WINE": str(self.wine)})
self.assertEqual(args.wine, str(other.resolve()))

def test_missing_wine_still_refused_outside_windows(self):
for env in ({}, {"WINE": ""}):
with self.subTest(env=env):
code, err = self.parse(env=env)
self.assertEqual(code, 2)
self.assertIn("--wine (or WINE) is required outside Windows", err)

def test_wine_from_environment_that_does_not_exist(self):
code, err = self.parse(env={"WINE": str(self.root / "nowhere" / "wine")})
self.assertEqual(code, 2)
self.assertIn("--wine: not found", err)


class RunGuestSceneWindowsEnvTest(unittest.TestCase):
"""On native Windows, an ambient WINE env var must not hijack a native launch."""

def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.root = Path(tmp.name)
self.loader, self.guest = self.root / "loader.exe", self.root / "guest.exe"
self.loader.write_bytes(b"MZ")
self.guest.write_bytes(b"MZ")
(self.root / "shaders" / "spirv").mkdir(parents=True)
self.wine = self.root / "wine.exe"
self.wine.write_bytes(b"MZ")

def parse_as_windows(self, *extra, env=None):
argv = ["run_guest_scene.py", "--loader", str(self.loader), "--guest", str(self.guest),
"--shaders", str(self.root / "shaders"), *extra]
environ = {k: v for k, v in os.environ.items() if k != "WINE"}
environ.update(env or {})
err = io.StringIO()
with mock.patch.object(runner, "os", SimpleNamespace(name="nt", environ=environ)), \
mock.patch.object(sys, "argv", argv), \
contextlib.redirect_stderr(err):
try:
return runner.parse_args(), err.getvalue()
except SystemExit as e:
return e.code, err.getvalue()

def test_ambient_wine_is_ignored_on_windows(self):
for value in (str(self.wine), str(self.root / "missing-wine.exe")):
with self.subTest(WINE=value):
args, err = self.parse_as_windows(env={"WINE": value})
self.assertEqual(err, "")
self.assertIsNone(args.wine)

def test_explicit_wine_flag_still_works_on_windows(self):
args, err = self.parse_as_windows("--wine", str(self.wine), env={"WINE": str(self.root / "missing.exe")})
self.assertEqual(err, "")
self.assertEqual(args.wine, str(self.wine.resolve()))

def test_wine_arg_requires_explicit_wine_on_windows(self):
code, err = self.parse_as_windows("--wine-arg=--debug", env={"WINE": str(self.wine)})
self.assertEqual(code, 2)
self.assertIn("--wine-arg requires --wine", err)


if __name__ == "__main__":
unittest.main()