diff --git a/scripts/run_guest_scene.py b/scripts/run_guest_scene.py index 60d5ded90f..2e8428118f 100644 --- a/scripts/run_guest_scene.py +++ b/scripts/run_guest_scene.py @@ -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] +--wine defaults to $WINE (e.g. UMU-Proton's files/bin/wine); outside Windows 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. @@ -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, + help="run the loader through this Wine binary (default: $WINE)") 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 or WINE") for name in ("loader", "guest"): if not getattr(args, name).is_file(): ap.error(f"--{name}: no such file") @@ -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() diff --git a/tests/test_run_guest_scene_wine.py b/tests/test_run_guest_scene_wine.py new file mode 100644 index 0000000000..7ee61ce8da --- /dev/null +++ b/tests/test_run_guest_scene_wine.py @@ -0,0 +1,75 @@ +"""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 +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) + + +if __name__ == "__main__": + unittest.main()