Files
mandy/tests/test_shell_launchers.py
bhimio rice 5a8af54cbd build(linux): add Build.sh and Mods.sh, the shell counterparts of Build.bat and Mods.bat
Linux and macOS had no one-command entry point: Build.bat and Mods.bat are
cmd.exe scripts. Build.sh and Mods.sh find Python 3.12 or newer (the
versioned python3.N first, since a distribution's python3 can be older)
and run make_game.py or the mod manager's window with -I, passing every
argument through. They install and download nothing.

tests/test_shell_launchers.py runs both against stub interpreters on a
private PATH. The mod manager's missing-Tkinter message now names the
distribution package off Windows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 20:51:04 -05:00

115 lines
5.1 KiB
Python

"""Build.sh and Mods.sh, the Linux and macOS counterparts of Build.bat and Mods.bat, start the right Python.
python -I tests/test_shell_launchers.py
Each launcher is run with a PATH that holds only stub interpreters: shell scripts that answer the version check with
the version they are given and otherwise record their arguments. The launcher must pick the first one that is 3.12
or newer, run make_game.py (or the mod manager) through it with -I and every argument unchanged, from any folder,
pass the exit code back, and stop with a message when there is none. POSIX only (/bin/sh); no Python is started
for real, no compiler and no game files.
"""
import os
import shutil
import subprocess
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
LAUNCHERS = {"Build.sh": ["make_game.py"], "Mods.sh": ["tools/leonida_mods.py", "gui"]}
STUB = """#!/bin/sh
if [ "$1" = -c ]; then
exit {ok}
fi
for a in "$@"; do printf '%s\\n' "$a"; done > "$LEONIDA_STUB_LOG.{name}"
exit {status}
"""
@unittest.skipIf(os.name == "nt" or not os.path.exists("/bin/sh"), "POSIX shell launchers")
class ShellLauncherTest(unittest.TestCase):
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.tmp = Path(tmp.name)
self.bin = self.tmp / "bin"
self.bin.mkdir()
self.log = self.tmp / "log"
# PATH holds the stubs and the one external command the launchers run, dirname (cd, pwd, command -v and
# printf are shell builtins), so the machine's own pythons in /usr/bin can't be picked instead.
tools = self.tmp / "tools"
tools.mkdir()
dirname = shutil.which("dirname")
if not dirname:
self.skipTest("no dirname")
(tools / "dirname").symlink_to(dirname)
self.path = os.pathsep.join([str(self.bin), str(tools)])
def stub(self, name, ok=True, status=0):
path = self.bin / name
path.write_text(STUB.format(ok=0 if ok else 1, name=name, status=status), encoding="utf-8", newline="\n")
path.chmod(0o755)
def run_launcher(self, launcher, *args, cwd=None):
env = {"PATH": self.path, "LEONIDA_STUB_LOG": str(self.log), "HOME": str(self.tmp)}
return subprocess.run(["/bin/sh", str(ROOT / launcher), *args], cwd=cwd or self.tmp, env=env,
capture_output=True, text=True, timeout=60)
def recorded(self, name):
return (self.tmp / f"log.{name}").read_text(encoding="utf-8").splitlines()
def test_the_first_new_enough_python_runs_the_tool_with_every_argument(self):
for launcher, tool in LAUNCHERS.items():
with self.subTest(launcher=launcher):
for name in ("python3.14", "python3.13", "python3.12", "python3", "python"):
(self.bin / name).unlink(missing_ok=True)
self.stub("python3.13", ok=False) # too old: skipped
self.stub("python3.12")
self.stub("python3") # also new enough, but later in the order
args = ["--out", "/mnt/a b/Leonida", "", "it's", "$HOME", "*"]
p = self.run_launcher(launcher, *args)
self.assertEqual(p.returncode, 0, p.stderr)
want = ["-I", str(ROOT / tool[0]), *tool[1:], *args]
self.assertEqual(self.recorded("python3.12"), want)
self.assertFalse((self.tmp / "log.python3").exists())
def test_python3_is_used_when_there_is_no_versioned_name(self):
self.stub("python3")
p = self.run_launcher("Build.sh", "--check")
self.assertEqual(p.returncode, 0, p.stderr)
self.assertEqual(self.recorded("python3"), ["-I", str(ROOT / "make_game.py"), "--check"])
def test_the_exit_code_comes_back(self):
self.stub("python3", status=7)
self.assertEqual(self.run_launcher("Build.sh").returncode, 7)
def test_started_from_another_folder(self):
self.stub("python3")
elsewhere = self.tmp / "elsewhere"
elsewhere.mkdir()
p = self.run_launcher("Build.sh", "--find", cwd=elsewhere)
self.assertEqual(p.returncode, 0, p.stderr)
self.assertEqual(self.recorded("python3")[1], str(ROOT / "make_game.py"))
def test_without_a_new_enough_python_it_says_so(self):
self.stub("python3", ok=False)
for launcher in LAUNCHERS:
with self.subTest(launcher=launcher):
p = self.run_launcher(launcher)
self.assertEqual(p.returncode, 1)
self.assertIn("Python 3.12 or newer is needed", p.stderr)
self.assertFalse((self.tmp / "log.python3").exists())
def test_the_launchers_are_executable_and_lf(self):
for launcher in LAUNCHERS:
with self.subTest(launcher=launcher):
path = ROOT / launcher
self.assertTrue(os.access(path, os.X_OK), f"{launcher} lost its executable bit")
self.assertNotIn(b"\r", path.read_bytes())
self.assertTrue(path.read_bytes().startswith(b"#!/bin/sh\n"))
if __name__ == "__main__":
unittest.main()