refactor: split the installer and launcher tests

test_install.py becomes eleven modules by subject, test_install_selection.py
gives its pack parity classes to test_install_pack_parity.py, and
test_handheld_launchers.py becomes three. Shared helpers live in
tests/installsupport.py and tests/handheldsupport.py. Test ids are unchanged.
This commit is contained in:
Abdessamad Derraz committed 2026-11-24 07:36:36 +01:00
1 parent 623ef5cabe
commit 5451bc5b91
25 files changed
+11434 -11024

No files matched your search

+1 -1
View File
@@ -4,7 +4,7 @@
Usage:
python scripts/run_tests.py # every module, a few at a time
python scripts/run_tests.py --jobs 1 # one at a time, as discover runs
python scripts/run_tests.py tests.test_install tests.test_e2e_region
python scripts/run_tests.py tests.test_install_detection tests.test_e2e_region
unittest's discovery runs 8,500 tests in one process: about 40 minutes
here and 85 on the CI runner, and a module that left a variable or a flag
+568
View File
@@ -0,0 +1,568 @@
"""What the handheld launcher tests share: a simulated device, its fake firmware tools and the pad models.
A device is simulated with a pseudo-terminal standing for its console, a
fake install.py that records how it was started, and fakes for what the
firmware provides (gptokeyb, setfont, chvt, curl, foot, xterm, muxterm,
PortMaster's control.txt and muOS's retro.sh), all on PATH or in a folder
the launcher reads. What only a device shows (the text reaching the screen,
the pad reaching the console, a compositor covering it) is not exercised.
"""
from __future__ import annotations
import tests
from tests import scratch_dir
import contextlib
import importlib.util
import json
import os
import pty
import re
import shutil
import signal
import struct
import subprocess
import sys
import termios
import time
import unittest
import zipfile
from pathlib import Path
from installer_build import port
REPO_ROOT = Path(__file__).resolve().parent.parent
PORTS = REPO_ROOT / "ports"
_spec = importlib.util.spec_from_file_location("install", REPO_ROOT / "install.py")
install = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(install)
def read_gptk(path: Path) -> dict[str, list[str]]:
"""key -> values, as gptokeyb's parseConfigFile reads `key = value`."""
mapping: dict[str, list[str]] = {}
for line in path.read_text(encoding="utf-8").splitlines():
line = line.strip()
if not line or line.startswith(("#", "[")):
continue
key, sep, value = line.partition(" = ")
assert sep, line
mapping.setdefault(key, []).append(value.split("#")[0].strip())
return mapping
def sdl_vidpid(bus: int, vendor: int, product: int, version: int, name: str) -> tuple[int, int]:
"""The vendor and product SDL 2 reads back from the GUID it builds for
a device: SDL_CreateJoystickGUID, then SDL_GetJoystickGUIDInfo
(libsdl-org/SDL release-2.30.x src/joystick/SDL_joystick.c). A vendor of
0 makes the GUID carry the name instead of the ids."""
data = bytearray(16)
struct.pack_into("<HH", data, 0, bus, 0x5a5a)
if vendor:
struct.pack_into("<HHHHH", data, 4, vendor, 0, product, 0, version)
else:
raw = name.encode()[:11]
data[4:4 + len(raw)] = raw
words = struct.unpack("<8H", bytes(data))
if (words[0] < 0x20 or words[0] == 0xFF) and words[3] == 0 and words[5] == 0:
return words[2], words[4]
return 0, 0
def sdl_vidpid_list(hint: str) -> list[int]:
"""SDL_LoadVIDPIDListFromHint: every pair of 0x numbers."""
entries = []
for vendor, product in re.findall(r"0x([0-9a-fA-F]+)[^x]*?0x([0-9a-fA-F]+)", hint):
entries.append((int(vendor, 16) & 0xFFFF) << 16 | (int(product, 16) & 0xFFFF))
return entries
# Input devices a muOS device lists in /proc/bus/input/devices: the H700
# pad before 2026-08-28 (gpio-keys, 0001:0001, MustardOS internal
# f6a14e3121 device/rg35xx-plus/control/gamecontrollerdb/modern.txt), the
# muinput pad after it (0x756d), a USB and a Bluetooth pad, gptokeyb2's
# uinput keyboard, and pads whose vendor is 0.
PADS = [
(0x19, 0x0001, 0x0001, 0x0100, "Deeplay-keys"),
(0x19, 0x0001, 0x0001, 0x0100, "muOS-Keys"),
(0x06, 0x756d, 0x6b6d, 0x0001, "muOS-Keys"),
(0x03, 0x045e, 0x028e, 0x0114, "Microsoft X-Box 360 pad"),
(0x05, 0x057e, 0x2009, 0x8111, "Pro Controller"),
(0x06, 0x0000, 0x0000, 0x0000, "Fake Keyboard"),
]
PADS += [(bus, 0, product, 1, "pad"[:length] + "x" * max(0, length - 3))
for bus in (0x03, 0x05, 0x06, 0x19, 0xFF) for product in (0, 1, 0x2009)
for length in range(13)]
PADS += [(bus, vendor, product, 1, "pad")
for bus in (0x03, 0x05, 0x19) for vendor in (1, 0x0079, 0xFFFF)
for product in (0, 1, 0xFFFF)]
def assert_muxterm_hides_every_pad(test: unittest.TestCase, hint: str) -> None:
"""muxterm reads the pad itself (T/input.c:303-317) while gptokeyb2
sends its keys, so every button would act twice. The launchers set
SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT, which hides from SDL every
device it does not name (SDL_gamecontroller.c:2145-2150, applied to the
joystick layer by SDL_ShouldIgnoreJoystick, SDL_joystick.c:3006)."""
allowed = sdl_vidpid_list(hint)
# An empty list allows every device.
test.assertTrue(allowed, hint)
for pad in PADS:
vendor, product = sdl_vidpid(*pad)
with test.subTest(pad=pad):
test.assertNotIn(vendor << 16 | product, allowed)
def assert_muxterm_mapping(test: unittest.TestCase, args: list[str], shared: Path) -> None:
"""muxterm keeps PgUp and PgDn for its scrollback and never writes them
to the terminal (T/input.c:352-360): in muxterm, L1 and R1 send nothing,
so they cannot scroll the interface away. Every other button is
retrobios.gptk's."""
test.assertEqual(args[-2], "-c")
mapping = read_gptk(Path(args[-1]))
expected = read_gptk(shared)
expected["l1"] = expected["r1"] = ['\\"']
test.assertEqual(mapping, expected)
FAKE_INSTALL = r'''
import json, os, signal, subprocess, sys, time
calls = os.environ["FAKE_CALLS"]
state = os.environ["FAKE_STATE"]
def tty(fd):
try:
return os.ttyname(fd)
except OSError:
return ""
def note(name, text=""):
with open(os.path.join(state, name), "a") as fh:
fh.write(text + "\n")
if "--engine" in sys.argv:
# The engine the interface starts in its own session: it writes its
# summary on SIGINT, as install.py does, and records any other signal.
def summary(signum, _frame):
note("engine.log", "summary after SIGINT")
sys.exit(130)
def other(signum, _frame):
note("engine.log", f"signal {signum}")
sys.exit(128 + signum)
signal.signal(signal.SIGINT, summary)
signal.signal(signal.SIGTERM, other)
with open(os.path.join(state, "engine.pid"), "w") as fh:
fh.write(str(os.getpid()))
note("engine.ready")
time.sleep(60)
sys.exit(0)
with open(calls, "a") as fh:
fh.write(json.dumps({"argv0": sys.argv[0], "args": sys.argv[1:], "stdin": tty(0),
"stdout": tty(1), "term": os.environ.get("TERM", ""),
"pythonhome": os.environ.get("PYTHONHOME", ""),
"base_url": os.environ.get("RETROBIOS_BASE_URL", ""),
"offline": os.environ.get("RETROBIOS_INSTALL_OFFLINE", ""),
"idle_exit": os.environ.get("RETROBIOS_IDLE_EXIT"),
"pad": os.environ.get("RETROBIOS_PAD")}) + "\n")
if "--check" in sys.argv:
# install.py --check --events: detection is local, then the file list.
# With RETROBIOS_INSTALL_OFFLINE the file list stops the run at once
# with reason offline. Otherwise it is downloaded: a closed loopback
# port refuses at once, any other address is three tries of 30 s on a
# device without a network (FAKE_CURL, the probe that found it so).
offline = os.environ.get("RETROBIOS_INSTALL_OFFLINE", "") not in ("", "0")
base_url = os.environ.get("RETROBIOS_BASE_URL", "")
no_network = os.environ.get("FAKE_CURL", "0") in ("5", "6", "7", "28")
if os.environ.get("FAKE_DETECT_HANG"):
time.sleep(60)
print(json.dumps({"event": "start", "mode": "check"}), flush=True)
print(json.dumps({"event": "detect", "os": "linux", "os_name": "Fake OS",
"platforms": [{"platform": os.environ.get("FAKE_PLATFORM", "retroarch"),
"name": os.environ.get("FAKE_PLATFORM_NAME", "RetroArch"),
"bios_dir": "/roms/bios"}]}), flush=True)
if no_network and not offline and not base_url.startswith("http://127.0.0.1"):
time.sleep(90)
print(json.dumps({"event": "error", "stage": "manifest",
"reason": "offline" if offline else "network",
"platform": os.environ.get("FAKE_PLATFORM", "retroarch"),
"message": "this run makes no network request (--offline)" if offline
else "unreachable"}))
print(json.dumps({"event": "end", "code": 1}))
sys.exit(1)
if os.environ.get("FAKE_REFUSE"):
print("install.py: error: --tui cannot open the full-screen interface here: "
"the terminal is 30x10, smaller than 40x12", file=sys.stderr)
sys.exit(2)
if os.environ.get("FAKE_RAW") and os.isatty(0):
import tty as _tty
_tty.setraw(0)
if os.environ.get("FAKE_DRAIN") and os.isatty(0):
# What the console still holds when the interface starts: bytes a
# question left behind are read as the interface's first keys.
# TCSANOW, as install.py sets its mode: TCSAFLUSH would discard them.
import select as _select
import termios as _termios
import tty as _tty
_tty.setraw(0, _termios.TCSANOW)
left = b""
while _select.select([0], [], [], 0.3)[0]:
chunk = os.read(0, 64)
if not chunk:
break
left += chunk
note("ui.input", left.hex())
if os.environ.get("FAKE_HOLD"):
# The interface up and waiting for a key until a signal ends it. With
# FAKE_ENGINE an install runs, and SIGTERM gives it 2 s to say what it
# wrote, as install.py's run_tui does. FAKE_NO_HANDLER is a signal that
# comes before run_tui installs its handlers.
engine = None
if os.environ.get("FAKE_ENGINE"):
engine = subprocess.Popen([sys.executable, sys.argv[0], "--events", "--engine"],
start_new_session=True)
while not os.path.exists(os.path.join(state, "engine.ready")):
time.sleep(0.05)
def stop(signum, _frame):
note("ui.log", f"signal {signum}")
if engine is not None:
engine.send_signal(signal.SIGINT)
try:
engine.wait(2)
except subprocess.TimeoutExpired:
engine.kill()
print("RetroBIOS: the install was stopped, 2 files written.")
if os.environ.get("FAKE_TERM_ERROR"):
print("Error: " + os.environ["FAKE_TERM_ERROR"], file=sys.stderr)
sys.exit(128 + signum)
if os.environ.get("FAKE_IGNORE_TERM"):
# An interface stuck where its signal handler never runs.
signal.signal(signal.SIGTERM, signal.SIG_IGN)
elif not os.environ.get("FAKE_NO_HANDLER"):
signal.signal(signal.SIGTERM, stop)
signal.signal(signal.SIGHUP, stop)
with open(os.path.join(state, "ui.pid"), "w") as fh:
fh.write(str(os.getpid()))
note("ui.ready")
time.sleep(60)
sys.exit(0)
if os.environ.get("FAKE_IDLE_ERROR"):
# RETROBIOS_IDLE_EXIT on an error screen: the idle line, then the error.
print("RetroBIOS: No button pressed, closing.", file=sys.stderr)
print("Error: " + os.environ["FAKE_IDLE_ERROR"], file=sys.stderr)
if os.environ.get("FAKE_QUIT"):
# q or Quit on the screen: the farewell on stderr, code 2.
print("RetroBIOS: quit. Nothing was installed.", file=sys.stderr)
if os.environ.get("FAKE_ERROR"):
print("Error: " + os.environ["FAKE_ERROR"], file=sys.stderr)
sys.exit(int(os.environ.get("FAKE_CODE", "0")))
'''
# The real pgrep and pkill: Device puts a pgrep on PATH that answers for the
# compositor check.
PGREP = shutil.which("pgrep", path="/usr/bin:/bin") or "pgrep"
PKILL = shutil.which("pkill", path="/usr/bin:/bin") or "pkill"
# gptokeyb as PortsMaster/gptokeyb runs it: kill mode takes the argument
# after the flag (`-1`, `-k` or `-sudokill`) as its target. Start + Select
# (FAKE_PRESS, once FAKE_PRESS_AT exists, by default once the interface is
# up) is doKillMode in src/util.cpp: pkill -f on the target, which also
# matches gptokeyb's own command line (SDL turns that SIGTERM into an event
# it does not read there), 3 s, then pkill -9 -f and exit. `-sudokill` runs
# both through sudo. Stopped otherwise by a SIGTERM, which it records.
# FAKE_MAPPER_DIES is a mapper that cannot start (no /dev/uinput).
FAKE_MAPPER = f'''#!/bin/bash
printf '%s\\n' "$@" > "$FAKE_STATE/mapper.args"
echo $$ > "$FAKE_STATE/mapper.pid"
printf '%s\\n' "${{SDL_GAMECONTROLLERCONFIG_FILE:-}}" > "$FAKE_STATE/mapper.db"
if [ -n "${{FAKE_MAPPER_DIES:-}}" ]; then
echo "gptokeyb: cannot open /dev/uinput" >&2
exit 1
fi
name="" sudo=""
while [ $# -gt 0 ]; do
case "$1" in
-1|-k) name="$2"; shift 2 ;;
-sudokill) name="$2" sudo="sudo"; shift 2 ;;
-c) shift 2 ;;
*) shift ;;
esac
done
killing=""
trap '[ -n "$killing" ] || {{ echo stopped > "$FAKE_STATE/mapper.stopped"; exit 0; }}' TERM
until [ -n "${{FAKE_PRESS:-}}" ] && [ -e "$FAKE_STATE/${{FAKE_PRESS_AT:-ui.ready}}" ]; do sleep 0.1; done
killing=1
echo "kill $name" >> "$FAKE_STATE/mapper.log"
$sudo {PKILL} -f "$name"
sleep 3
if {PGREP} -f "$name" > /dev/null; then $sudo {PKILL} -9 -f "$name"; fi
exit 0
'''
# chvt brings the console to the front, so RB_VT_ACTIVE then names it. A
# compositor that keeps the display (FAKE_CHVT_REFUSED) leaves chvt waiting
# in VT_WAITACTIVE and the console in front unchanged.
FAKE_CHVT = """#!/bin/sh
# A loaded machine starts the process late (FAKE_CHVT_DELAY seconds).
[ -n "$FAKE_CHVT_DELAY" ] && sleep "$FAKE_CHVT_DELAY"
printf '%s\\n' "chvt $*" >> "$FAKE_STATE/tools.log"
[ -n "$FAKE_CHVT_REFUSED" ] && exec sleep 30
[ -n "$RB_VT_ACTIVE" ] && printf 'tty%s\\n' "$1" > "$RB_VT_ACTIVE"
exit 0
"""
FAKE_SETFONT = """#!/bin/sh
printf '%s\\n' "setfont $*" >> "$FAKE_STATE/tools.log"
while [ $# -gt 0 ]; do
[ "$1" = "-O" ] && { [ -n "$FAKE_NO_SAVE" ] && exit 1; printf 'font' > "$2"; }
shift
done
"""
# A terminal app: records how it was started and the SDL hint it was given,
# then runs its command. FAKE_TERM_FAILS is one that cannot open its window.
FAKE_TERMAPP = """#!/bin/sh
printf '%s\\n' "$(basename "$0") $*" >> "$FAKE_STATE/tools.log"
printf '%s\\n' "${SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT:-}" > "$FAKE_STATE/termapp.sdl"
printf '%s\\n' "${SDL_GAMECONTROLLER_IGNORE_DEVICES:-}" > "$FAKE_STATE/termapp.sdl_ignore"
echo $$ > "$FAKE_STATE/termapp.pid"
if [ -n "$FAKE_TERM_FAILS" ]; then
echo "$(basename "$0"): Can't open display" >&2
exit 1
fi
while [ $# -gt 0 ]; do
case "$1" in
--|-e) shift; break ;;
*) shift ;;
esac
done
exec "$@"
"""
class Device:
"""A simulated handheld: fake tools on PATH and a pty as its console."""
def __init__(self, test: unittest.TestCase, **env: str):
self.test = test
self.root = scratch_dir(test)
self.state = self.root / "state"
self.state.mkdir()
# Registered before every other cleanup, so it runs after them.
test.addCleanup(self._sweep)
self.bin = self.root / "bin"
self.bin.mkdir()
master, slave = pty.openpty()
test.addCleanup(os.close, master)
test.addCleanup(os.close, slave)
termios.tcsetattr(slave, termios.TCSANOW, termios.tcgetattr(slave))
import fcntl
# Wide enough that no path in a message is folded.
fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack("HHHH", 50, 250, 0, 0))
self.master, self.slave = master, slave
self.console = os.ttyname(slave)
self.before = termios.tcgetattr(slave)
(self.bin / "python3").symlink_to(shutil.which("python3.8") or sys.executable)
self.tool("gptokeyb", FAKE_MAPPER)
self.tool("setfont", FAKE_SETFONT)
self.tool("chvt", FAKE_CHVT)
# A probe that cannot connect is offline (curl exit 7).
self.tool("curl", '#!/bin/sh\nexit "${FAKE_CURL:-0}"\n')
self.tool("pgrep", '#!/bin/sh\nexit "${FAKE_PGREP:-1}"\n')
# A runner's real sudo needs no password: the Tools entry then ran
# the fake mapper as root, with a reset environment, and recorded
# nothing. A device has no sudo unless a test gives it one.
self.tool("sudo", "#!/bin/sh\nexit 1\n")
fonts = self.root / "fonts"
fonts.mkdir()
for name in ("Lat7-TerminusBold32x16", "Lat7-TerminusBold22x11",
"Lat7-TerminusBold20x10", "Lat7-Terminus16"):
(fonts / f"{name}.psf.gz").write_bytes(b"")
(self.root / "fb").write_text("640,480\n")
environ = {k: v for k, v in os.environ.items()
if k not in ("DISPLAY", "WAYLAND_DISPLAY", "RETROBIOS_BASE_URL",
"RETROBIOS_INSTALL_OFFLINE", "RETROBIOS_IDLE_EXIT",
"RETROBIOS_PAD", "XAUTHORITY", "XDG_RUNTIME_DIR")}
self.env = {
**environ,
"PATH": f"{self.bin}{os.pathsep}{os.environ['PATH']}",
"HOME": str(self.root),
"XDG_RUNTIME_DIR": str(self.root / "runtime"),
"FAKE_STATE": str(self.state),
"FAKE_CALLS": str(self.state / "calls.jsonl"),
"RB_FONTS": str(fonts),
"RB_FB": str(self.root / "fb"),
"RB_MESSAGE_WAIT": "1",
"TERM": "dumb",
# The ArkOS marker is a plymouth theme the host may carry too
# (GitHub's Ubuntu 24.04 runner does): no ArkOS unless a test says so.
"RB_ARK_MARKER": str(self.root / "no-ark-marker"),
**env,
}
test.addCleanup(self._reap)
def tool(self, name: str, text: str) -> Path:
path = self.bin / name
# python3 starts as a link to the real interpreter: never write through it.
if path.is_symlink() or path.exists():
path.unlink()
path.write_text(text)
path.chmod(0o755)
return path
def unpack(self, archive: str, into: Path) -> Path:
"""The launcher archive extracted where the firmware keeps it, with
the fake installer in place of install.py."""
into.mkdir(parents=True, exist_ok=True)
built = port.build_archive(archive, self.root / "dist" / archive)
with zipfile.ZipFile(built) as zf:
zf.extractall(into)
for info in zf.infolist():
mode = info.external_attr >> 16
if mode:
(into / info.filename).chmod(mode & 0o777)
for found in into.rglob("install.py"):
found.write_text(FAKE_INSTALL)
return into
def run(self, argv: list[str], stdio_tty: bool = False, keys: str = "", **env: str):
"""Run a launcher with *keys* waiting on the console as buttons
pressed. Its console output is read from the pty."""
if keys:
os.write(self.master, keys.encode())
stdin = self.slave if stdio_tty else subprocess.DEVNULL
stdout = self.slave if stdio_tty else subprocess.PIPE
start = time.monotonic()
proc = subprocess.run(argv, env={**self.env, **env}, stdin=stdin, stdout=stdout,
stderr=subprocess.PIPE, timeout=120, check=False)
self.elapsed = time.monotonic() - start
self.screen = self._drain()
return proc
def spawn(self, argv: list[str], **env: str) -> subprocess.Popen:
proc = subprocess.Popen(argv, env={**self.env, **env}, stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
self.test.addCleanup(lambda: proc.poll() is None and proc.kill())
return proc
def wait_for(self, name: str, timeout: float = 20) -> None:
end = time.monotonic() + timeout
while not (self.state / name).exists():
self.test.assertLess(time.monotonic(), end, f"{name} never appeared")
time.sleep(0.05)
def _drain(self) -> str:
chunks = []
os.set_blocking(self.master, False)
while True:
try:
chunk = os.read(self.master, 65536)
except (BlockingIOError, OSError):
break
if not chunk:
break
chunks.append(chunk)
return b"".join(chunks).decode("utf-8", "replace")
def _reap(self) -> None:
"""A fake the launcher failed to stop must not outlive the test."""
for name in ("mapper.pid", "ui.pid", "engine.pid"):
path = self.state / name
if path.exists():
with contextlib.suppress(ProcessLookupError, ValueError):
os.kill(int(path.read_text()), signal.SIGKILL)
def _left(self) -> list[int]:
"""The processes a run on this device started that still run: each
carries the device's FAKE_STATE in its environment."""
marker = f"FAKE_STATE={self.state}".encode()
found = []
for entry in Path("/proc").iterdir():
if not entry.name.isdigit() or int(entry.name) == os.getpid():
continue
try:
environ = (entry / "environ").read_bytes()
except OSError:
continue
if marker in environ.split(b"\0"):
found.append(int(entry.name))
return found
def _sweep(self) -> None:
"""No process outlives its test. A launcher's stop sentinel polls
every 5 s, so what is still running 6 s on was left behind: it is
killed, and the test fails naming it, instead of the leak running
into the tests after it."""
end = time.monotonic() + 6
while (left := self._left()) and time.monotonic() < end:
time.sleep(0.1)
named = []
for pid in left:
try:
argv = Path(f"/proc/{pid}/cmdline").read_bytes().replace(b"\0", b" ")
os.kill(pid, signal.SIGKILL)
except (ProcessLookupError, FileNotFoundError):
continue
named.append(f"{pid} {argv.decode('utf-8', 'replace').strip()}")
self.test.assertEqual(named, [], "processes left running after the test")
def calls(self) -> list[dict]:
path = self.state / "calls.jsonl"
if not path.exists():
return []
return [json.loads(line) for line in path.read_text().splitlines()]
def tui_calls(self) -> list[dict]:
return [c for c in self.calls() if "--tui" in c["args"]]
def lines(self, name: str) -> list[str]:
path = self.state / name
return path.read_text().splitlines() if path.exists() else []
def tools(self) -> list[str]:
return self.lines("tools.log")
def mapper_args(self) -> list[str]:
return self.lines("mapper.args")
def assert_mapper_stopped(self) -> None:
"""The launcher stopped gptokeyb itself, before it exited."""
pid = int((self.state / "mapper.pid").read_text())
stopped = (self.state / "mapper.stopped").exists()
if not stopped:
with contextlib.suppress(ProcessLookupError):
os.kill(pid, signal.SIGKILL)
self.test.assertTrue(stopped, "gptokeyb was left running")
def assert_mapper_gone(self) -> None:
"""gptokeyb ended, by its own kill mode or by the launcher."""
pid = int((self.state / "mapper.pid").read_text())
end = time.monotonic() + 5
while Path(f"/proc/{pid}").exists() and time.monotonic() < end:
time.sleep(0.05)
self.test.assertFalse(Path(f"/proc/{pid}").exists(), "gptokeyb was left running")
def assert_console_restored(self) -> None:
self.test.assertEqual(termios.tcgetattr(self.slave), self.before)
def wait_until(test: unittest.TestCase, check, timeout: float, what: str) -> None:
end = time.monotonic() + timeout
while not check():
test.assertLess(time.monotonic(), end, what)
time.sleep(0.1)
+120
View File
@@ -0,0 +1,120 @@
"""What the installer test modules share: install.py loaded once, loopback runs and served trees."""
from __future__ import annotations
import tests
from tests import loopback
from tests import scratch_dir
import hashlib
import importlib.util
import io
import json
import os
import subprocess
import sys
import unittest
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent.parent
_spec = importlib.util.spec_from_file_location("install", REPO_ROOT / "install.py")
install = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(install)
def reported(call, *args):
"""call(*args), a refusal said as the command says it: a library
function raises it, and the command prints it."""
try:
return call(*args)
except install.Refusal as refusal:
refusal.report()
raise
class _LoopbackRun:
"""install.py against the repository's own manifests over loopback."""
def serve(self, test: unittest.TestCase) -> dict:
base = loopback.serve(test, REPO_ROOT)
self.home = scratch_dir(test)
return {
**os.environ, "HOME": str(self.home), "RETROBIOS_OS": "linux",
"RETROBIOS_BASE_URL": base,
}
def _install_run(env: dict, *argv: str, **kwargs) -> subprocess.CompletedProcess:
return subprocess.run(
[sys.executable, "install.py", *argv], cwd=REPO_ROOT, env=env,
capture_output=True, text=True, timeout=300, check=False, **kwargs,
)
class _FakeResponse:
"""What urlopen returns, serving one payload from its start."""
def __init__(self, payload: bytes) -> None:
self.status = 200
self.headers = {"Content-Length": str(len(payload))}
self._body = io.BytesIO(payload)
def __enter__(self) -> _FakeResponse:
return self
def __exit__(self, *_exc: object) -> None:
return None
def read(self, size: int = -1) -> bytes:
return self._body.read(size)
class _SyntheticTree:
"""A served tree of four BIOS files over loopback, one with no system,
and a BIOS folder holding one of them, another with other bytes, and
neither of the last two."""
FILES = {
"bios/A/a.bin": b"alpha" * 20,
"bios/A/b.bin": b"bravo" * 30,
"bios/B/c.bin": b"charlie" * 10,
"bios/X/d.bin": b"delta" * 5,
}
def serve_tree(self, test: unittest.TestCase) -> dict:
root = scratch_dir(test)
files = []
for path, data in self.FILES.items():
(root / path).parent.mkdir(parents=True, exist_ok=True)
(root / path).write_bytes(data)
name = Path(path).name
systems = {"a.bin": ["sys-a"], "b.bin": ["sys-a"], "c.bin": ["sys-b"]}.get(name)
entry = {"dest": name, "repo_path": path, "size": len(data),
"sha1": hashlib.sha1(data).hexdigest(), "cores": None}
if systems:
entry["systems"] = systems
files.append(entry)
manifest = {
"manifest_version": 2, "platform": "retroarch", "display_name": "RetroArch",
"files": files, "system_names": {"sys-a": ["alpha"]},
"omitted_files": [{"dest": "e.bin", "name": "e.bin", "system": "sys-b",
"required": True, "reason": "not_found",
"systems": ["sys-b"]}],
}
(root / "install").mkdir()
(root / "install" / "retroarch.json").write_text(json.dumps(manifest))
base = loopback.serve(test, root)
self.home = scratch_dir(test)
self.bios = self.home / "bios"
self.bios.mkdir()
(self.bios / "a.bin").write_bytes(self.FILES["bios/A/a.bin"])
(self.bios / "b.bin").write_bytes(b"other bytes")
return {
**os.environ, "HOME": str(self.home), "RETROBIOS_OS": "linux",
"RETROBIOS_BASE_URL": base,
}
def _events(proc: subprocess.CompletedProcess) -> list[dict]:
"""The --events stream, every stdout line a JSON object."""
return [json.loads(line) for line in proc.stdout.splitlines()]
+681
View File
@@ -0,0 +1,681 @@
"""The Batocera port, the muOS app and the tools entry, run on a simulated device, and the device's own checks."""
from __future__ import annotations
import tests
import contextlib
import os
import re
import shutil
import signal
import subprocess
import unittest
from pathlib import Path
from installer_build import port
from tests.handheldsupport import (
PORTS,
read_gptk,
assert_muxterm_hides_every_pad,
assert_muxterm_mapping,
PGREP,
PKILL,
FAKE_TERMAPP,
Device,
wait_until,
)
# gptokeyb2 as PortsMaster/gptokeyb2 src/util.c kills: SIGKILL only, by
# process name (pkill -9 NAME) unless -X, then pgrep -f and kill -9 on every
# match but its own ancestry and children. It quits after a kill, and keeps
# running when it has no process name. FAKE_MAPPER_STAYS is gptokeyb2
# before 2026-10-05, which keeps running after a -X kill (an inner
# `int status` shadows the result, 23581b62ec src/util.c:486-519).
FAKE_GPTOKEYB2 = f'''#!/bin/bash
printf '%s\\n' "$@" > "$FAKE_STATE/mapper.args"
echo $$ > "$FAKE_STATE/mapper.pid"
if [ -n "${{FAKE_MAPPER_DIES:-}}" ]; then
echo "gptokeyb2: cannot open /dev/uinput" >&2
exit 1
fi
want_kill="" name=""
while [ $# -gt 0 ]; do
case "$1" in
-X) want_kill=1; shift ;;
-c) shift 2 ;;
-*) shift ;;
*) [ -z "$name" ] && name="$1"; shift ;;
esac
done
trap 'echo stopped > "$FAKE_STATE/mapper.stopped"; exit 0' TERM
until [ -n "${{FAKE_PRESS:-}}" ] && [ -e "$FAKE_STATE/${{FAKE_PRESS_AT:-ui.ready}}" ]; do sleep 0.1; done
echo "kill $name" >> "$FAKE_STATE/mapper.log"
if [ -z "$name" ]; then
while :; do sleep 0.1; done
fi
if [ -n "$want_kill" ]; then
ours=" $$ "
p=$$
while [ "$p" -gt 1 ]; do
p=$(awk '{{print $4}}' "/proc/$p/stat" 2>/dev/null)
[ -n "$p" ] || break
ours="$ours$p "
done
for pid in $({PGREP} -f "$name"); do
case "$ours" in *" $pid "*) continue ;; esac
[ "$(awk '{{print $4}}' "/proc/$pid/stat" 2>/dev/null)" = "$$" ] && continue
kill -9 "$pid" 2>/dev/null
done
else
{PKILL} -9 "$name"
fi
if [ -n "${{FAKE_MAPPER_STAYS:-}}" ]; then
while :; do sleep 0.1; done
fi
exit 0
'''
# ROCKNIX's /usr/bin/sudo: a stub that runs nothing (ROCKNIX distribution
# packages/sysutils/busybox/scripts/sudo).
ROCKNIX_SUDO = """#!/bin/sh
printf '%s\\n' "sudo $*" >> "$FAKE_STATE/tools.log"
echo " You are 'root' so 'sudo' is not required or supported"
exit 1
"""
# sudo with env_reset: the environment is dropped but for the variables
# --preserve-env names.
FAKE_SUDO = """#!/bin/bash
printf '%s\\n' "sudo $*" >> "$FAKE_STATE/tools.log"
keep=""
while [ $# -gt 0 ]; do
case "$1" in
--preserve-env=*) keep="${1#--preserve-env=}"; shift ;;
-*) shift ;;
*) break ;;
esac
done
case ",$keep," in
*,SDL_GAMECONTROLLERCONFIG_FILE,*) ;;
*) unset SDL_GAMECONTROLLERCONFIG_FILE ;;
esac
exec "$@"
"""
class TheBatoceraPort(unittest.TestCase):
def device(self, **env: str) -> tuple[Device, Path]:
device = Device(self, **env)
ports = device.unpack(port.BATOCERA_ZIP, device.root / "userdata" / "roms" / "ports")
return device, ports
def console_switch(self, device: Device) -> dict[str, str]:
number = re.fullmatch(r"/dev/pts/(\d+)", device.console)
self.assertIsNotNone(number)
self.number = number.group(1)
# The console in front before the switch: never the pty the test
# was given, which was /dev/pts/2 on a busy machine, so the launcher
# found itself already in front and switched nowhere.
self.home = "3" if self.number == "2" else "2"
(device.root / "active").write_text(f"tty{self.home}\n")
return {"RB_VT_ACTIVE": str(device.root / "active"), "RB_VT_PREFIX": "/dev/pts/",
"RB_VT_SWITCH": self.number}
def test_xterm_in_the_x11_session(self):
device, ports = self.device(DISPLAY=":0")
device.tool("xterm", FAKE_TERMAPP)
proc = device.run(["/bin/bash", str(ports / "RetroBIOS.sh")])
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
[call] = device.tui_calls()
self.assertEqual(call["args"], ["--tui"])
self.assertEqual(device.tools(),
[f"xterm -fullscreen -e bash {ports}/retrobios/tui.txt here"])
# evmapy reads RetroBIOS.sh.keys: no gptokeyb is started.
self.assertEqual(device.mapper_args(), [])
self.assertEqual(call["pad"], "evmapy")
self.assertEqual(call["idle_exit"], "300")
def test_the_x_cookie_goes_with_the_display(self):
"""S31emulationstation starts X with XAUTHORITY=/var/lib/.Xauthority:
without it xterm is refused the display."""
device, ports = self.device()
device.tool("xterm", FAKE_TERMAPP + "")
socket_dir = device.root / "x11"
socket_dir.mkdir()
import socket as _socket
sock = _socket.socket(_socket.AF_UNIX)
# A socket address holds 108 bytes, fewer than a scratch folder can.
with contextlib.chdir(socket_dir):
sock.bind("X0")
self.addCleanup(sock.close)
cookie = device.root / "Xauthority"
cookie.write_text("")
device.tool("xterm", '#!/bin/sh\nprintf "%s %s\\n" "$DISPLAY" "$XAUTHORITY" '
'> "$FAKE_STATE/x.env"\n' + FAKE_TERMAPP.split("\n", 1)[1])
proc = device.run(["/bin/bash", str(ports / "RetroBIOS.sh")],
RB_X_SOCKET=str(socket_dir / "X0"), RB_XAUTH=str(cookie))
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
self.assertEqual(device.lines("x.env"), [f":0 {cookie}"])
def test_console_switch_when_no_terminal_app(self):
device, ports = self.device(FAKE_PGREP="0")
proc = device.run(["/bin/bash", str(ports / "RetroBIOS.sh")], **self.console_switch(device))
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
[call] = device.tui_calls()
self.assertEqual(call["stdin"], device.console)
chvt = [line for line in device.tools() if line.startswith("chvt")]
self.assertEqual(chvt, [f"chvt {self.number}", f"chvt {self.home}"])
device.assert_console_restored()
def test_a_refused_console_switch_shows_nothing_on_an_unseen_console(self, **slow):
"""The compositor keeps the display: chvt waits and the console in
front stays the same. The launcher switches back and stops, instead
of an interface no key reaches."""
device, ports = self.device(FAKE_PGREP="0")
proc = device.run(["/bin/bash", str(ports / "RetroBIOS.sh")], FAKE_CHVT_REFUSED="1",
**self.console_switch(device), **slow)
self.assertEqual(proc.returncode, 125)
self.assertLess(device.elapsed, 20)
self.assertEqual(device.tui_calls(), [])
chvt = [line for line in device.tools() if line.startswith("chvt")]
self.assertEqual(chvt, [f"chvt {self.number}", f"chvt {self.home}"])
self.assertEqual((device.root / "active").read_text(), f"tty{self.home}\n")
log = (ports / "retrobios" / "log.txt").read_text()
self.assertIn(f"console {self.number} did not come to the front", log)
self.assertNotIn("\x1bc", device.screen)
def test_the_switch_back_is_waited_for_on_a_slow_machine(self):
"""The console to go back to was already in front, so rb_chvt
returned before its chvt had even started, and the launcher ended
with the switch back still pending: under load the log missed
"chvt 2". A chvt that starts 1.2 s late made it fail every time."""
self.test_a_refused_console_switch_shows_nothing_on_an_unseen_console(
FAKE_CHVT_DELAY="1.2")
def test_a_slow_console_switch_still_comes_back(self):
device, ports = self.device(FAKE_PGREP="0")
proc = device.run(["/bin/bash", str(ports / "RetroBIOS.sh")], FAKE_CHVT_DELAY="1.2",
**self.console_switch(device))
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
chvt = [line for line in device.tools() if line.startswith("chvt")]
self.assertEqual(chvt, [f"chvt {self.number}", f"chvt {self.home}"])
self.assertEqual((device.root / "active").read_text(), f"tty{self.home}\n")
def test_xterm_that_cannot_open_hands_over_to_the_console(self):
device, ports = self.device(DISPLAY=":0", FAKE_PGREP="0")
device.tool("xterm", FAKE_TERMAPP)
proc = device.run(["/bin/bash", str(ports / "RetroBIOS.sh")], FAKE_TERM_FAILS="1",
**self.console_switch(device))
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
[call] = device.tui_calls()
self.assertEqual(call["stdin"], device.console)
self.assertTrue(device.tools()[0].startswith("xterm -fullscreen -e bash"))
class TheMuosApp(unittest.TestCase):
def device(self, muxterm: bool, gptokeyb2: bool = True) -> tuple[Device, Path]:
device = Device(self)
apps = device.unpack(port.MUOS_APP, device.root / "mmc" / "MUOS" / "application")
muos = device.root / "opt-muos"
(muos / "script" / "var").mkdir(parents=True)
(muos / "script" / "var" / "retro.sh").write_text(
'SETUP_APP() { printf "SETUP_APP %s\\n" "$1" >> "$FAKE_STATE/tools.log"; }\n'
'GET_VAR() { case "$2" in\n'
f' storage/rom/mount) echo "{device.root / "mmc"}" ;;\n'
f' storage/sdcard/mount) echo "{device.root / "sdcard"}" ;;\n'
' *) echo "" ;;\nesac; }\n')
if muxterm:
(muos / "frontend").mkdir()
shutil.copy(device.tool("muxterm", FAKE_TERMAPP), muos / "frontend" / "muxterm")
pm = device.root / "mmc" / "MUOS" / "PortMaster"
pm.mkdir(parents=True)
if gptokeyb2:
(pm / "gptokeyb2").write_text(FAKE_GPTOKEYB2)
(pm / "gptokeyb2").chmod(0o755)
device.env["RB_MUOS"] = str(muos)
return device, apps / "RetroBIOS"
def test_in_muxterm(self):
device, app = self.device(muxterm=True)
# muOS starts an app as <app>/mux_launch.sh <app>.
proc = device.run([str(app / "mux_launch.sh"), str(app)])
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
[call] = device.tui_calls()
self.assertEqual(call["args"], ["--tui"])
self.assertEqual(device.tools(), ["SETUP_APP retrobios",
f"muxterm --gl -- bash {app}/tui.txt here"])
# muxterm leaves the pad to gptokeyb2, which sends each button once
# with the interface's mapping: Y is Tab (Options), not muxterm's Space.
[hint] = device.lines("termapp.sdl")
assert_muxterm_hides_every_pad(self, hint)
args = device.mapper_args()
self.assertEqual(args[0], "-X")
self.assertRegex(args[1], r"^retrobios-stop-\d+x$")
assert_muxterm_mapping(self, args, app / "retrobios.gptk")
self.assertEqual(read_gptk(Path(args[-1]))["y"], ["tab"])
self.assertEqual(call["pad"], "gptokeyb_muxterm")
self.assertEqual(call["idle_exit"], "300")
device.assert_mapper_stopped()
def test_muxterm_alone_without_gptokeyb2(self):
device, app = self.device(muxterm=True, gptokeyb2=False)
proc = device.run([str(app / "mux_launch.sh"), str(app)])
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
[call] = device.tui_calls()
self.assertEqual(call["pad"], "muxterm")
self.assertEqual(call["idle_exit"], "300")
# muxterm then reads the pad itself.
self.assertEqual(device.lines("termapp.sdl"), [""])
self.assertEqual(device.mapper_args(), [])
def test_start_select_ends_the_run_in_muxterm(self):
"""gptokeyb2 kills with SIGKILL: -X reaches the sentinel, and the
interface is asked to stop as on the other routes."""
device, app = self.device(muxterm=True)
proc = device.run([str(app / "mux_launch.sh"), str(app)], FAKE_HOLD="1",
FAKE_PRESS="1", RB_MESSAGE_WAIT="300")
self.assertEqual(proc.returncode, 143, proc.stderr.decode())
self.assertLess(device.elapsed, 10)
self.assertEqual(device.lines("ui.log"), ["signal 15"])
device.assert_mapper_gone()
def test_start_select_ends_the_run_on_the_console(self):
device, app = self.device(muxterm=False)
proc = device.run([str(app / "mux_launch.sh"), str(app)], FAKE_HOLD="1",
FAKE_PRESS="1", RB_CONSOLE=device.console, RB_MESSAGE_WAIT="300")
self.assertEqual(proc.returncode, 143, proc.stderr.decode())
self.assertLess(device.elapsed, 10)
self.assertEqual(device.lines("ui.log"), ["signal 15"])
device.assert_console_restored()
def test_start_select_with_a_gptokeyb2_that_keeps_running(self):
"""gptokeyb2 before 2026-10-05 keeps running after its -X kill: only
the sentinel's end says Start + Select was pressed."""
device, app = self.device(muxterm=True)
proc = device.run([str(app / "mux_launch.sh"), str(app)], FAKE_HOLD="1",
FAKE_PRESS="1", FAKE_MAPPER_STAYS="1", RB_MESSAGE_WAIT="300")
self.assertEqual(proc.returncode, 143, proc.stderr.decode())
self.assertLess(device.elapsed, 10)
self.assertEqual(device.lines("ui.log"), ["signal 15"])
self.assertIn("Start + Select", (app / "log.txt").read_text())
device.assert_mapper_stopped()
def test_a_gptokeyb2_that_cannot_start_is_named(self):
device, app = self.device(muxterm=False)
proc = device.run([str(app / "mux_launch.sh"), str(app)], FAKE_MAPPER_DIES="1",
RB_CONSOLE=device.console, RB_DEAD_WAIT="1")
self.assertEqual(proc.returncode, 1)
self.assertEqual(device.tui_calls(), [])
screen = " ".join(device.screen.split())
self.assertIn("gptokeyb2, which turns the buttons into keys, stopped as soon as it "
"started", screen)
def test_an_arrow_at_the_question_leaves_nothing_behind(self):
"""An arrow is three bytes: the first answers the question, the rest
must not reach the interface as two more keys."""
device, app = self.device(muxterm=False)
_sd1, sd2, _pack = self.two_cards(device)
proc = device.run([str(app / "mux_launch.sh"), str(app)], keys="\x1b[B",
FAKE_CURL="7", FAKE_DRAIN="1", RB_CONSOLE=device.console,
RB_BIOS_DEST=str(sd2))
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
[call] = device.tui_calls()
self.assertNotIn("--from", call["args"])
self.assertEqual(device.lines("ui.input"), [""])
def test_start_select_at_the_question_starts_no_interface(self):
device, app = self.device(muxterm=False)
_sd1, sd2, _pack = self.two_cards(device)
proc = device.run([str(app / "mux_launch.sh"), str(app)], FAKE_CURL="7",
FAKE_PRESS="1", FAKE_PRESS_AT="calls.jsonl", RB_MESSAGE_WAIT="300",
RB_CONSOLE=device.console, RB_BIOS_DEST=str(sd2))
self.assertEqual(proc.returncode, 143, proc.stderr.decode())
self.assertLess(device.elapsed, 10)
self.assertEqual(device.tui_calls(), [])
# Never started: an interface started then stopped at once would
# end the same way, before Python records anything.
self.assertFalse((app / "run" / "tui.err").exists())
def test_on_the_console_without_muxterm(self):
device, app = self.device(muxterm=False)
proc = device.run([str(app / "mux_launch.sh"), str(app)], RB_CONSOLE=device.console)
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
[call] = device.tui_calls()
self.assertEqual(call["stdin"], device.console)
self.assertEqual(device.mapper_args()[3], str(app / "retrobios.gptk"))
device.assert_console_restored()
def test_offline_reads_a_pack_at_the_card_root(self):
device, app = self.device(muxterm=True)
pack = device.root / "mmc" / "RetroArch_Lakka_v1.22.2_BIOS_Pack.zip"
pack.write_bytes(b"")
device.run([str(app / "mux_launch.sh"), str(app)], FAKE_CURL="7")
[call] = device.tui_calls()
self.assertEqual(call["args"][:3], ["--tui", "--from-pack", str(pack)])
def test_offline_names_the_pack_that_serves_muos(self):
"""muOS runs RetroArch, and install.py reads it as retroarch: the
RetroArch pack serves it."""
device, app = self.device(muxterm=True)
proc = device.run([str(app / "mux_launch.sh"), str(app)], FAKE_CURL="7")
self.assertEqual(proc.returncode, 1)
# muxterm draws what its child writes: here the launcher's stdout.
screen = " ".join(proc.stdout.decode().split())
self.assertIn("download the RetroArch BIOS pack (a file whose name starts with "
"RetroArch_ and ends in _BIOS_Pack.zip)", screen)
def test_offline_takes_no_pack_named_for_another_system(self):
device, app = self.device(muxterm=True)
(device.root / "mmc" / "Batocera_43.1_BIOS_Pack.zip").write_bytes(b"")
proc = device.run([str(app / "mux_launch.sh"), str(app)], FAKE_CURL="7")
self.assertEqual(proc.returncode, 1)
self.assertEqual(device.tui_calls(), [])
def test_offline_reads_a_pack_on_the_second_card(self):
device, app = self.device(muxterm=True)
(device.root / "sdcard").mkdir()
pack = device.root / "sdcard" / "RetroArch_Lakka_v1.22.2_BIOS_Pack.zip"
pack.write_bytes(b"")
device.run([str(app / "mux_launch.sh"), str(app)], FAKE_CURL="7")
[call] = device.tui_calls()
self.assertEqual(call["args"][:3], ["--tui", "--from-pack", str(pack)])
def two_cards(self, device: Device) -> tuple[Path, Path, Path]:
"""SD1 and SD2 both hold MUOS/bios, SD2's is the one muOS binds."""
sd1 = device.root / "mmc" / "MUOS" / "bios"
sd2 = device.root / "sdcard" / "MUOS" / "bios"
sd1.mkdir(parents=True)
sd2.mkdir(parents=True)
(sd1 / "scph5501.bin").write_bytes(b"x")
(sd2 / "gba_bios.bin").write_bytes(b"y")
pack = device.root / "mmc" / "RetroArch_Lakka_v1.22.2_BIOS_Pack.zip"
pack.write_bytes(b"")
return sd1, sd2, pack
def test_offline_offers_the_other_card(self):
device, app = self.device(muxterm=False)
sd1, sd2, pack = self.two_cards(device)
proc = device.run([str(app / "mux_launch.sh"), str(app)], keys="\r", FAKE_CURL="7",
RB_CONSOLE=device.console, RB_BIOS_DEST=str(sd2))
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
self.assertIn("Copy the BIOS files from the other card?", device.screen)
self.assertIn(str(sd1), device.screen)
[call] = device.tui_calls()
self.assertEqual(call["args"], ["--tui", "--from", str(sd1), "--from-pack", str(pack),
"--platform", "retroarch"])
def test_b_declines_the_other_card(self):
device, app = self.device(muxterm=False)
_sd1, sd2, pack = self.two_cards(device)
device.run([str(app / "mux_launch.sh"), str(app)], keys="\x1b", FAKE_CURL="7",
RB_CONSOLE=device.console, RB_BIOS_DEST=str(sd2))
[call] = device.tui_calls()
self.assertEqual(call["args"], ["--tui", "--from-pack", str(pack),
"--platform", "retroarch"])
def test_the_card_the_system_reads_is_never_offered(self):
device, app = self.device(muxterm=False)
sd1, _sd2, _pack = self.two_cards(device)
shutil.rmtree(device.root / "sdcard")
device.run([str(app / "mux_launch.sh"), str(app)], FAKE_CURL="7",
RB_CONSOLE=device.console, RB_BIOS_DEST=str(sd1))
self.assertNotIn("other card", device.screen)
[call] = device.tui_calls()
self.assertNotIn("--from", call["args"])
def test_online_asks_nothing(self):
device, app = self.device(muxterm=False)
_sd1, sd2, _pack = self.two_cards(device)
device.run([str(app / "mux_launch.sh"), str(app)],
RB_CONSOLE=device.console, RB_BIOS_DEST=str(sd2))
self.assertNotIn("other card", device.screen)
[call] = device.tui_calls()
self.assertEqual(call["args"], ["--tui"])
class TheToolsEntry(unittest.TestCase):
def device(self, **env: str) -> tuple[Device, Path]:
device = Device(self, **env)
tools = device.unpack(port.TOOLS_ZIP, device.root / "tools")
device.env["RB_ROOT"] = str(device.root / "fs")
return device, tools
def arkos(self, device: Device) -> Path:
fs = device.root / "fs"
(fs / "opt" / "system").mkdir(parents=True)
(fs / "opt" / "inttools").mkdir(parents=True)
shutil.copy(device.bin / "gptokeyb", fs / "opt" / "inttools" / "gptokeyb")
return fs
def test_arkos_console_one(self):
device, tools = self.device()
self.arkos(device)
proc = device.run(["bash", str(tools / "RetroBIOS.sh")], RB_CONSOLE=device.console)
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
[call] = device.tui_calls()
self.assertEqual((call["stdin"], call["term"]), (device.console, "linux"))
self.assertEqual(call["pad"], "gptokeyb")
self.assertEqual(call["idle_exit"], "300")
args = device.mapper_args()
self.assertEqual(args[0], "-1")
self.assertRegex(args[1], r"^retrobios-stop-\d+x$")
self.assertEqual(args[2:], ["-c", str(tools / "retrobios" / "retrobios.gptk")])
device.assert_mapper_stopped()
device.assert_console_restored()
def test_the_previous_log_is_kept(self):
device, tools = self.device()
self.arkos(device)
log = tools / "retrobios" / "log.txt"
log.write_text("the failure\n")
device.run(["bash", str(tools / "RetroBIOS.sh")], RB_CONSOLE=device.console)
self.assertEqual((tools / "retrobios" / "log.prev.txt").read_text(), "the failure\n")
self.assertNotIn("the failure", log.read_text())
def test_arkos_sudo_keeps_the_controller_list(self):
"""sudo's env_reset would drop SDL_GAMECONTROLLERCONFIG_FILE, and a
pad SDL does not know sends nothing."""
device, tools = self.device()
fs = self.arkos(device)
db = fs / "opt" / "inttools" / "gamecontrollerdb.txt"
db.write_text("")
device.tool("sudo", FAKE_SUDO)
proc = device.run(["bash", str(tools / "RetroBIOS.sh")], RB_CONSOLE=device.console)
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
self.assertEqual(device.lines("mapper.db"), [str(db)])
started = [t for t in device.tools() if "gptokeyb" in t]
self.assertTrue(started[0].startswith(
"sudo --preserve-env=SDL_GAMECONTROLLERCONFIG_FILE "), started)
def test_start_select_on_the_console(self):
device, tools = self.device()
self.arkos(device)
proc = device.run(["bash", str(tools / "RetroBIOS.sh")], RB_CONSOLE=device.console,
FAKE_HOLD="1", FAKE_PRESS="1", RB_MESSAGE_WAIT="300")
self.assertEqual(proc.returncode, 143, proc.stderr.decode())
self.assertLess(device.elapsed, 10)
self.assertEqual(device.lines("ui.log"), ["signal 15"])
self.assertNotIn("Press A", device.screen)
device.assert_mapper_gone()
device.assert_console_restored()
def test_arkos_other_card_without_a_pack_needs_one(self):
"""--from needs the list of files, which an offline run takes from a
pack: the message says so."""
device, tools = self.device(FAKE_CURL="7")
fs = self.arkos(device)
roms, roms2 = device.root / "roms" / "bios", device.root / "roms2" / "bios"
roms.mkdir(parents=True)
roms2.mkdir(parents=True)
(roms / "bios.bin").write_bytes(b"x")
cfg = fs / "home" / "ark" / ".config" / "retroarch"
cfg.mkdir(parents=True)
(cfg / "retroarch.cfg").write_text(f'system_directory = "{roms2}"\n')
proc = device.run(["bash", str(tools / "RetroBIOS.sh")], keys="\r",
RB_CONSOLE=device.console, RB_OTHER_CARDS=f"{roms}:{roms2}")
self.assertEqual(proc.returncode, 1)
screen = " ".join(device.screen.split())
self.assertIn(f"Copy the BIOS files from the other card? {roms} holds files", screen)
self.assertIn("with it, the files on the other card are copied too", screen)
self.assertEqual(device.tui_calls(), [])
def test_amberelec_console(self):
device, tools = self.device()
etc = device.root / "fs" / "etc"
etc.mkdir(parents=True)
(etc / "os-release").write_text('NAME="AmberELEC"\n')
proc = device.run(["bash", str(tools / "RetroBIOS.sh")], RB_CONSOLE=device.console)
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
[call] = device.tui_calls()
self.assertEqual(call["stdin"], device.console)
self.assertEqual(device.mapper_args()[0], "-1")
def rocknix(self, device: Device) -> None:
etc = device.root / "fs" / "etc"
etc.mkdir(parents=True)
(etc / "os-release").write_text('NAME="ROCKNIX"\n')
device.tool("sudo", ROCKNIX_SUDO)
def control_ini(self, device: Device) -> None:
"""control-gen_init.sh writes control.ini as control-gen.cpp:31
does: `ESUDOKILL="-sudokill"`, and GPTOKEYB with it."""
folder = device.root / "fs" / "storage" / ".config" / "gptokeyb"
shutil.copy(device.bin / "gptokeyb", device.root / "gptokeyb-rocknix")
device.tool("control-gen_init.sh", f'''#!/bin/sh
mkdir -p "{folder}"
cat > "{folder}/control.ini" <<'INI'
controlfolder="{device.root}"
ESUDO="sudo"
ESUDOKILL="-sudokill"
export SDL_GAMECONTROLLERCONFIG_FILE="$controlfolder/gamecontrollerdb.txt"
get_controls() {{
DEVICE="03000000"
param_device="fake pad"
}}
GPTOKEYB="$controlfolder/gptokeyb-rocknix $ESUDOKILL"
INI
''')
def test_rocknix_runs_in_the_terminal_tools_opens(self):
device, tools = self.device()
self.rocknix(device)
proc = device.run(["bash", str(tools / "RetroBIOS.sh")], stdio_tty=True, TERM="xterm")
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
[call] = device.tui_calls()
self.assertEqual((call["stdin"], call["stdout"]), (device.console, device.console))
self.assertEqual(call["term"], "xterm")
self.assertEqual(call["pad"], "gptokeyb")
self.assertEqual(call["idle_exit"], "300")
# The terminal app owns its window: no console reset, no font.
self.assertNotIn("\x1bc", device.screen)
self.assertEqual([t for t in device.tools() if t.startswith("setfont")], [])
def test_rocknix_loads_the_controller_list_its_own_tools_load(self):
"""control-gen_init.sh writes control.ini, which names the pad's
controller list and the GPTOKEYB to start (ROCKNIX fex-emu's Start
FEXConfig.sh). Its ESUDO is not this launcher's."""
device, tools = self.device()
self.rocknix(device)
self.control_ini(device)
proc = device.run(["bash", str(tools / "RetroBIOS.sh")], stdio_tty=True, TERM="xterm")
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
self.assertEqual(device.lines("mapper.args")[0], "-1")
self.assertRegex(device.mapper_args()[1], r"^retrobios-stop-\d+x$")
self.assertEqual(device.lines("mapper.db"), [f"{device.root}/gamecontrollerdb.txt"])
started = [t for t in device.tools() if t.startswith("sudo")]
self.assertEqual(started, [])
# Stopped without the sudo control.ini names.
device.assert_mapper_stopped()
def test_rocknix_start_select_with_its_controller_setup(self):
"""control.ini's `-sudokill` runs the kill through sudo, which on
ROCKNIX runs nothing: the mapper is started with -1, and Start +
Select ends the run at once instead of 3 s later through the
liveness watch."""
device, tools = self.device()
self.rocknix(device)
self.control_ini(device)
proc = device.run(["bash", str(tools / "RetroBIOS.sh")], stdio_tty=True, TERM="xterm",
FAKE_HOLD="1", FAKE_PRESS="1", RB_MESSAGE_WAIT="300")
self.assertEqual(proc.returncode, 143, proc.stderr.decode())
self.assertLess(device.elapsed, 10)
self.assertEqual(device.lines("ui.log"), ["signal 15"])
log = (tools / "retrobios" / "log.txt").read_text()
self.assertIn("Start + Select: the interface is asked to stop", log)
self.assertNotIn("the controller mapper stopped", log)
self.assertNotIn("You are 'root'", log)
device.assert_mapper_gone()
def test_rocknix_offline_names_no_pack_the_releases_lack(self):
"""No release pack spells ROCKNIX, and install.py refuses one named
for another system: the RetroArch pack on the card is not offered,
and the message gives the computer route."""
device, tools = self.device(FAKE_CURL="7")
self.rocknix(device)
(tools / "retrobios" / "RetroArch_v1.22.2_BIOS_Pack.zip").write_bytes(b"")
proc = device.run(["bash", str(tools / "RetroBIOS.sh")], stdio_tty=True, TERM="xterm",
FAKE_PLATFORM="rocknix", FAKE_PLATFORM_NAME="ROCKNIX")
self.assertEqual(proc.returncode, 1)
self.assertEqual(device.tui_calls(), [])
screen = " ".join(device.screen.split())
self.assertIn("the releases carry no BIOS pack for ROCKNIX", screen)
self.assertIn("--dest", screen)
self.assertNotIn("download", screen)
def test_rocknix_start_select_in_foot(self):
device, tools = self.device()
self.rocknix(device)
proc = device.run(["bash", str(tools / "RetroBIOS.sh")], stdio_tty=True, TERM="xterm",
FAKE_HOLD="1", FAKE_PRESS="1", FAKE_TERM_ERROR="unreachable",
RB_MESSAGE_WAIT="300")
self.assertEqual(proc.returncode, 143, proc.stderr.decode())
self.assertLess(device.elapsed, 10)
self.assertNotIn("Press A", device.screen)
device.assert_mapper_gone()
class TheSweep(unittest.TestCase):
def test_a_process_left_running_fails_its_test(self):
started = []
class Leaky(unittest.TestCase):
def runTest(inner):
device = Device(inner)
started.append(subprocess.Popen(["sleep", "60"], env=device.env,
stdin=subprocess.DEVNULL))
result = unittest.TestResult()
Leaky().run(result)
[proc] = started
self.assertEqual(proc.wait(timeout=5), -signal.SIGKILL)
[(_, report)] = result.failures
self.assertIn(f"{proc.pid} sleep 60", report)
class TheLiveness(unittest.TestCase):
def test_a_zombie_is_not_running(self):
"""A process that exited and that its parent has not reaped still
has /proc/PID: rb_alive reads its state."""
pid = os.fork()
if pid == 0:
os._exit(0)
self.addCleanup(os.waitpid, pid, 0)
wait_until(self, lambda: Path(f"/proc/{pid}/stat").read_text().split(") ")[1][0] == "Z",
5, "no zombie")
proc = subprocess.run(
["bash", "-c", 'source "$1"; rb_alive "$2" && echo alive || echo gone', "bash",
str(PORTS / "shared" / "tui.txt"), str(pid)],
capture_output=True, text=True, check=False)
self.assertEqual(proc.stdout.strip(), "gone", proc.stderr)
proc = subprocess.run(
["bash", "-c", 'source "$1"; rb_alive "$$" && echo alive || echo gone', "bash",
str(PORTS / "shared" / "tui.txt")],
capture_output=True, text=True, check=False)
self.assertEqual(proc.stdout.strip(), "alive", proc.stderr)
if __name__ == "__main__":
unittest.main()
File diff suppressed because it is too large. Load diff
+659
View File
@@ -0,0 +1,659 @@
"""RetroBIOS.sh from the PortMaster archive, run on a simulated device with a fake PortMaster."""
from __future__ import annotations
import tests
import json
import os
import shutil
import signal
import sys
import unittest
from pathlib import Path
from installer_build import port
from tests.handheldsupport import (
REPO_ROOT,
install,
assert_muxterm_hides_every_pad,
assert_muxterm_mapping,
FAKE_INSTALL,
FAKE_TERMAPP,
Device,
wait_until,
)
FAKE_RECORDER = """#!/bin/sh
printf '%s\\n' "$(basename "$0") $*" >> "$FAKE_STATE/tools.log"
"""
def sentinels(token: str) -> list[int]:
"""The Start + Select sentinels of a run: `bash -c SCRIPT TOKEN`, where
the mapper carries the token after its own path."""
found = []
for entry in Path("/proc").iterdir():
if not entry.name.isdigit():
continue
try:
argv = (entry / "cmdline").read_bytes().split(b"\0")
except OSError:
continue
if len(argv) > 3 and argv[1] == b"-c" and argv[3] == token.encode():
found.append(int(entry.name))
return found
CONTROL_TXT = '''
directory="{roms}"
ESUDO=""
ESUDOKILL="-1"
CUR_TTY="{console}"
GPTOKEYB="$controlfolder/gptokeyb $ESUDOKILL"
get_controls() {{ sdl_controllerconfig="fake"; }}
'''
DIALOG_TXT = '''
PM_PID=""
PM_DONE="$FAKE_STATE/pm_done"
trace() { printf '%s\\n' "$*" >> "$FAKE_STATE/dialog.log"; }
PortMasterIPCheck() { echo 192.168.0.1; }
PortMasterDialogInit() { trace "INIT $*"; }
PortMasterDialogExit() { trace EXIT; }
PortMasterDialog() { trace "CMD $*"; }
PortMasterDialogResult() { trace "ASK $*"; echo quit; }
pm_finish() { trace FINISH; }
'''
class ThePortMasterPort(unittest.TestCase):
"""RetroBIOS.sh from the PortMaster archive, on a fake PortMaster."""
def device(self, console: str | None = None, **env: str) -> Device:
device = Device(self, **env)
roms = device.root / "roms"
self.gamedir = device.unpack(port.PORT_ZIP, roms / "ports") / "retrobios"
control = device.root / "xdg" / "PortMaster"
control.mkdir(parents=True)
self.control = control
(control / "control.txt").write_text(
CONTROL_TXT.format(roms=str(roms).lstrip("/"), console=console or device.console))
(control / "PortMasterDialog.txt").write_text(DIALOG_TXT)
shutil.copy(device.bin / "gptokeyb", control / "gptokeyb")
device.env["XDG_DATA_HOME"] = str(device.root / "xdg")
# No muxterm on this device unless a test puts one there.
device.env["RB_MUXTERM"] = str(device.root / "no-muxterm")
self.script = str(roms / "ports" / "RetroBIOS.sh")
return device
def dialog(self, device: Device) -> list[str]:
return device.lines("dialog.log")
def test_online_on_the_console(self):
device = self.device()
proc = device.run(["bash", self.script], FAKE_RAW="1", FAKE_CODE="0")
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
[call] = device.tui_calls()
self.assertEqual(call["args"], ["--tui"])
self.assertEqual((call["stdin"], call["stdout"]), (device.console, device.console))
self.assertEqual(call["term"], "linux")
installer = str(self.gamedir / "install.py")
self.assertEqual(call["argv0"], installer)
self.assertEqual(call["pad"], "gptokeyb")
self.assertEqual(call["idle_exit"], "300")
# Kill mode on the Start + Select sentinel, never on the installer's
# path, which the engine's command line carries too.
args = device.mapper_args()
self.assertEqual(args[0], "-1")
self.assertRegex(args[1], r"^retrobios-stop-\d+x$")
self.assertEqual(args[2:], ["-c", str(self.gamedir / "retrobios.gptk")])
device.assert_mapper_stopped()
self.assertEqual(sentinels(args[1]), [])
device.assert_console_restored()
# Reset when the console is taken and again when it is handed back.
self.assertEqual(device.screen.count("\x1bc"), 2)
self.assertLess(device.screen.index("Checking the connection"),
device.screen.rindex("\x1bc"))
saved = str(self.gamedir / "run" / "font.psf")
fonts = device.env["RB_FONTS"]
self.assertEqual(device.tools(), [
f"setfont -C {device.console} -O {saved} {fonts}/Lat7-TerminusBold22x11.psf.gz",
f"setfont -C {device.console} {saved}",
])
# PortMaster's own end of a port, after the interface, no screens.
self.assertEqual(self.dialog(device), ["FINISH"])
def test_the_interface_status_is_the_port_status(self):
device = self.device()
proc = device.run(["bash", self.script], FAKE_CODE="3", FAKE_ERROR="the folder is read-only")
self.assertEqual(proc.returncode, 3)
self.assertIn("Error: the folder is read-only", device.screen)
self.assertIn("log.txt", device.screen)
def test_a_quit_chosen_shows_no_message(self):
"""A quit chosen exits 2, below 128, and the
first line of tui.err went into a message box waiting for A."""
device = self.device()
proc = device.run(["bash", self.script], FAKE_QUIT="1", FAKE_CODE="2",
RB_MESSAGE_WAIT="300")
self.assertEqual(proc.returncode, 2)
self.assertLess(device.elapsed, 15)
self.assertNotIn("Nothing was installed", device.screen)
self.assertNotIn("Press A", device.screen)
device.assert_console_restored()
def test_the_quit_line_is_the_one_install_py_writes(self):
state = install.TuiState(os_type="linux", home=Path.home())
state.quit_asked, state.exit_code = True, install.QUIT_CODE
self.assertTrue(install.tui_quit_chosen(state))
line = install.tui_farewell(state)[0]
tui = (REPO_ROOT / "ports" / "shared" / "tui.txt").read_text(encoding="utf-8")
self.assertIn(f'= "{line}" ]', tui)
self.assertIn(f'print("{line}", file=sys.stderr)', FAKE_INSTALL)
def test_offline_installs_from_a_pack_on_the_card(self):
device = self.device(FAKE_CURL="7")
roms = device.root / "roms"
(roms / "Batocera_43.1_BIOS_Pack.zip").write_bytes(b"")
pack = self.gamedir / "RetroArch_Lakka_v1.22.2_BIOS_Pack.zip"
pack.write_bytes(b"")
proc = device.run(["bash", self.script])
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
[call] = device.tui_calls()
self.assertEqual(call["args"], ["--tui", "--from-pack", str(pack),
"--platform", "retroarch"])
def test_offline_prefers_the_pack_of_this_system(self):
device = self.device(FAKE_CURL="6")
(self.gamedir / "Batocera_43.1_BIOS_Pack.zip").write_bytes(b"")
pack = device.root / "roms" / "RetroArch_Lakka_v1.22.2_BIOS_Pack.part1of2.zip"
pack.write_bytes(b"")
device.run(["bash", self.script])
[call] = device.tui_calls()
self.assertEqual(call["args"][1:3], ["--from-pack", str(pack)])
def test_offline_without_a_pack_says_where_to_put_one(self):
device = self.device(FAKE_CURL="7")
proc = device.run(["bash", self.script])
self.assertEqual(proc.returncode, 1)
self.assertEqual(device.tui_calls(), [])
screen = " ".join(device.screen.split())
self.assertIn("No network connection", screen)
self.assertIn("RetroArch", screen)
self.assertIn(str(self.gamedir), screen)
self.assertIn("github.com/Abdess/retrobios/releases/latest", screen)
device.assert_mapper_stopped()
device.assert_console_restored()
def test_offline_says_so_at_once_and_bounds_the_detection(self):
"""A detection run that hangs is stopped after RB_DETECT_LIMIT, with
a line on the console from the start."""
device = self.device(FAKE_CURL="28")
pack = self.gamedir / "RetroArch_Lakka_v1.22.2_BIOS_Pack.zip"
pack.write_bytes(b"")
proc = device.run(["bash", self.script], FAKE_DETECT_HANG="1", RB_DETECT_LIMIT="2")
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
self.assertLess(device.elapsed, 15)
screen = device.screen
self.assertIn("Checking the connection", screen)
self.assertIn("No connection. Looking for the BIOS folder", screen)
[call] = device.tui_calls()
# The detection was stopped: no platform, the first pack.
self.assertEqual(call["args"], ["--tui", "--from-pack", str(pack)])
log = (self.gamedir / "log.txt").read_text()
self.assertIn("the detection run took over 2 s and was stopped", log)
def test_the_detection_is_told_to_make_no_request(self):
"""RETROBIOS_INSTALL_OFFLINE: install.py detects, then stops where
the file list would download, at once and with reason offline
(install.py _network_allowed). The interface run does not inherit
it, and no address is borrowed for the detection."""
device = self.device(FAKE_CURL="7")
pack = self.gamedir / "RetroArch_Lakka_v1.22.2_BIOS_Pack.zip"
pack.write_bytes(b"")
proc = device.run(["bash", self.script])
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
self.assertLess(device.elapsed, 15)
[check] = [c for c in device.calls() if "--check" in c["args"]]
self.assertEqual(check["args"], ["--check", "--events"])
self.assertEqual((check["offline"], check["base_url"]), ("1", ""))
detected = (self.gamedir / "run" / "detect.jsonl").read_text().splitlines()
self.assertEqual([json.loads(line)["event"] for line in detected],
["start", "detect", "error", "end"])
self.assertEqual(json.loads(detected[2])["reason"], "offline")
[call] = device.tui_calls()
self.assertEqual(call["args"], ["--tui", "--from-pack", str(pack),
"--platform", "retroarch"])
self.assertEqual((call["offline"], call["base_url"]), ("", ""))
self.assertNotIn("took over", (self.gamedir / "log.txt").read_text())
def test_a_certificate_failure_is_not_offline(self):
# curl 60: the network answered, install.py reports the certificate.
device = self.device(FAKE_CURL="60")
device.run(["bash", self.script])
[call] = device.tui_calls()
self.assertEqual(call["args"], ["--tui"])
def test_an_old_python_is_told_on_the_console(self):
device = self.device()
device.tool("python3", '#!/bin/sh\n[ "$1" = "--version" ] && echo "Python 3.7.3"\nexit 1\n')
device.tool("uname", "#!/bin/sh\necho armv7l\n")
proc = device.run(["bash", self.script])
self.assertEqual(proc.returncode, 1)
screen = " ".join(device.screen.split())
self.assertIn("Python 3.8 or newer is needed", screen)
self.assertIn("3.7.3", screen)
self.assertIn("--dest", screen)
self.assertEqual(device.calls(), [])
def _python_without_ssl(self, device) -> None:
"""A python3 3.8 or newer whose ssl import fails, as on a CFW image
built without OpenSSL."""
real = shutil.which("python3.8") or sys.executable
shadow = device.root / "no-ssl"
shadow.mkdir()
(shadow / "ssl.py").write_text('raise ImportError("built without ssl")\n')
device.tool("python3", f'#!/bin/sh\nPYTHONPATH="{shadow}" exec {real} "$@"\n')
def test_a_python_without_ssl_is_told_on_the_console(self):
"""Only the version was probed: a python3 without ssl was chosen and
every download failed as if the network were down."""
device = self.device()
self._python_without_ssl(device)
device.tool("uname", "#!/bin/sh\necho armv7l\n")
proc = device.run(["bash", self.script])
self.assertEqual(proc.returncode, 1)
screen = " ".join(device.screen.split())
self.assertIn("built without a module install.py needs", screen)
self.assertEqual(device.calls(), [])
def test_a_python_without_ssl_gives_way_to_the_runtime(self):
device = self.device()
self._python_without_ssl(device)
real = shutil.which("python3.8") or sys.executable
device.tool("uname", "#!/bin/sh\necho aarch64\n")
device.tool("mount", '#!/bin/sh\nprintf "mount %s\\n" "$*" >> "$FAKE_STATE/tools.log"\n'
'mkdir -p "$2/bin"\n'
f'printf \'#!/bin/sh\\nunset PYTHONHOME\\nexec {real} "$@"\\n\' > "$2/bin/python3"\n'
'chmod +x "$2/bin/python3"\n')
device.tool("umount", FAKE_RECORDER)
(self.control / "libs").mkdir()
(self.control / "libs" / "python_3.11.squashfs").write_bytes(b"")
proc = device.run(["bash", self.script])
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
[call] = device.tui_calls()
self.assertTrue(any(t.startswith("mount") for t in device.tools()))
def test_the_runtime_is_mounted_then_unmounted(self):
"""ArkOS's Python 3.7: PortMaster's python_3.11 runtime, mounted for
the run and unmounted after it."""
device = self.device()
real = shutil.which("python3.8") or sys.executable
device.tool("python3", '#!/bin/sh\n[ "$1" = "--version" ] && echo "Python 3.7.3"\nexit 1\n')
device.tool("uname", "#!/bin/sh\necho aarch64\n")
# The runtime's python, a stand-in that ignores the PYTHONHOME the
# launcher sets for the real one.
device.tool("mount", '#!/bin/sh\nprintf "mount %s\\n" "$*" >> "$FAKE_STATE/tools.log"\n'
'mkdir -p "$2/bin"\n'
f'printf \'#!/bin/sh\\nunset PYTHONHOME\\nexec {real} "$@"\\n\' > "$2/bin/python3"\n'
'chmod +x "$2/bin/python3"\n')
device.tool("umount", FAKE_RECORDER)
(self.control / "libs").mkdir()
squashfs = self.control / "libs" / "python_3.11.squashfs"
squashfs.write_bytes(b"")
proc = device.run(["bash", self.script])
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
self.assertEqual(len(device.tui_calls()), 1)
runtime = str(device.root / "python_3.11")
mounts = [t for t in device.tools() if t.startswith(("mount", "umount"))]
self.assertEqual(mounts, [f"umount {runtime}", f"mount {squashfs} {runtime}",
f"umount {runtime}"])
def test_a_refused_console_falls_back_to_portmaster_screens(self):
device = self.device()
device.run(["bash", self.script], FAKE_REFUSE="1")
self.assertEqual(len(device.tui_calls()), 1)
dialog = self.dialog(device)
self.assertEqual(dialog[0], "INIT no-check")
self.assertIn("ASK selection_list --register=notice --want-description --want-cancel",
dialog)
device.assert_mapper_stopped()
device.assert_console_restored()
def test_a_console_this_user_cannot_open_is_opened_up(self):
"""PortMaster's control.txt chmods the consoles only under sudo: the
launcher does it for the one it draws on."""
device = self.device()
os.chmod(device.console, 0o000)
self.addCleanup(os.chmod, device.console, 0o620)
proc = device.run(["bash", self.script])
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
[call] = device.tui_calls()
self.assertEqual(call["stdin"], device.console)
self.assertEqual(os.stat(device.console).st_mode & 0o777, 0o666)
def test_under_a_compositor_without_a_terminal_app_the_screens_open(self):
device = self.device(FAKE_PGREP="0")
device.run(["bash", self.script])
self.assertEqual(device.tui_calls(), [])
self.assertEqual(self.dialog(device)[0], "INIT no-check")
self.assertEqual(device.mapper_args(), [])
def test_arkos_offers_the_card_portmaster_does_not_run_from(self):
device = self.device(FAKE_CURL="7")
marker = device.root / "text.plymouth"
marker.write_text("")
roms = device.root / "roms" / "bios"
other = device.root / "roms2" / "bios"
roms.mkdir(parents=True)
other.mkdir(parents=True)
(roms / "a.bin").write_bytes(b"x")
(other / "b.bin").write_bytes(b"y")
pack = self.gamedir / "RetroArch_Lakka_v1.22.2_BIOS_Pack.zip"
pack.write_bytes(b"")
device.run(["bash", self.script], keys="\r", RB_ARK_MARKER=str(marker),
RB_OTHER_CARDS=f"{roms}:{other}",
RB_RETROARCH_CFG=str(device.root / "none.cfg"))
[call] = device.tui_calls()
# The port's games card is roms here: its bios is the destination.
self.assertEqual(call["args"][:3], ["--tui", "--from", str(other)])
def test_arkos_destination_is_the_folder_retroarch_reads(self):
"""control.txt names roms2 as the port's card whenever a second card
is mounted: RetroArch's system_directory decides."""
device = self.device(FAKE_CURL="7")
marker = device.root / "text.plymouth"
marker.write_text("")
roms = device.root / "roms" / "bios"
other = device.root / "roms2" / "bios"
roms.mkdir(parents=True)
other.mkdir(parents=True)
(roms / "a.bin").write_bytes(b"x")
(other / "b.bin").write_bytes(b"y")
cfg = device.root / "retroarch.cfg"
cfg.write_text(f'system_directory = "{other}"\n')
(self.gamedir / "RetroArch_Lakka_v1.22.2_BIOS_Pack.zip").write_bytes(b"")
device.run(["bash", self.script], keys="\r", RB_ARK_MARKER=str(marker),
RB_OTHER_CARDS=f"{roms}:{other}", RB_RETROARCH_CFG=str(cfg))
[call] = device.tui_calls()
self.assertEqual(call["args"][:3], ["--tui", "--from", str(roms)])
def test_under_a_compositor_foot_shows_the_interface(self):
device = self.device(WAYLAND_DISPLAY="wayland-1")
device.tool("foot", FAKE_TERMAPP)
proc = device.run(["bash", self.script], FAKE_CODE="4")
self.assertEqual(proc.returncode, 4)
[call] = device.tui_calls()
self.assertEqual(call["args"], ["--tui"])
self.assertEqual(device.tools(), [f"foot -F -- bash {self.gamedir}/tui.txt here"])
self.assertRegex(device.mapper_args()[1], r"^retrobios-stop-\d+x$")
device.assert_mapper_stopped()
def test_a_terminal_app_that_cannot_open_hands_over(self):
"""foot refused the display: the interface never ran, and the port
opens PortMaster's screens instead of ending on a status of 1."""
device = self.device(WAYLAND_DISPLAY="wayland-1")
device.tool("foot", FAKE_TERMAPP)
proc = device.run(["bash", self.script], FAKE_TERM_FAILS="1")
self.assertEqual(device.tui_calls(), [])
self.assertEqual(self.dialog(device)[0], "INIT no-check", proc.stderr.decode())
log = (self.gamedir / "log.txt").read_text()
self.assertIn("foot ended with status 1 before the interface ran", log)
device.assert_mapper_stopped()
def test_on_muos_the_interface_opens_in_muxterm(self):
"""mod_muOS.txt sets CUR_TTY to /dev/null: muxterm shows the
interface, told to leave the pad to gptokeyb."""
device = self.device(console="/dev/null")
muxterm = device.tool("muxterm", FAKE_TERMAPP)
proc = device.run(["bash", self.script], RB_MUXTERM=str(muxterm))
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
[call] = device.tui_calls()
self.assertEqual(call["args"], ["--tui"])
self.assertEqual(device.tools(), [f"muxterm --gl -- bash {self.gamedir}/tui.txt here"])
[hint] = device.lines("termapp.sdl")
assert_muxterm_hides_every_pad(self, hint)
assert_muxterm_mapping(self, device.mapper_args(), self.gamedir / "retrobios.gptk")
self.assertEqual(call["pad"], "gptokeyb_muxterm")
self.assertEqual(call["idle_exit"], "300")
self.assertEqual(self.dialog(device), ["FINISH"])
def test_on_muos_without_muxterm_the_log_says_why_the_screens_open(self):
device = self.device(console="/dev/null")
device.run(["bash", self.script])
self.assertEqual(device.tui_calls(), [])
self.assertEqual(self.dialog(device)[0], "INIT no-check")
log = (self.gamedir / "log.txt").read_text()
self.assertIn("/dev/null is not a console", log)
def test_start_select_lets_the_install_say_what_it_wrote(self):
"""gptokeyb's kill reaches the sentinel only: the interface is asked
to stop, gives the running install its SIGINT and 2 s, and writes
its result. The engine never sees gptokeyb's SIGTERM or SIGKILL."""
device = self.device()
proc = device.run(["bash", self.script], FAKE_HOLD="1", FAKE_ENGINE="1",
FAKE_PRESS="1", RB_MESSAGE_WAIT="300")
self.assertEqual(proc.returncode, 143, proc.stderr.decode())
self.assertLess(device.elapsed, 15)
self.assertEqual(device.lines("ui.log"), ["signal 15"])
self.assertEqual(device.lines("engine.log"), ["summary after SIGINT"])
self.assertIn("RetroBIOS: the install was stopped, 2 files written.", device.screen)
self.assertNotIn("Press A", device.screen)
device.assert_mapper_gone()
device.assert_console_restored()
self.assertEqual(self.dialog(device), ["FINISH"])
def test_start_select_on_an_error_screen_returns_at_once(self):
"""The interface leaves an error screen with its farewell on stderr,
and gptokeyb is gone: no message may wait for a button."""
device = self.device()
proc = device.run(["bash", self.script], FAKE_HOLD="1", FAKE_PRESS="1",
FAKE_TERM_ERROR="could not reach the file list",
RB_MESSAGE_WAIT="300")
self.assertEqual(proc.returncode, 143)
self.assertLess(device.elapsed, 10)
self.assertNotIn("Press A", device.screen)
device.assert_mapper_gone()
device.assert_console_restored()
def test_an_idle_close_on_an_error_names_the_error_at_once(self):
"""The interface closed for want of a button: the mapper never
opened the pad, so no A can close a message. The error it closed is
named, for RB_DEAD_WAIT seconds, not the idle line for 300."""
device = self.device()
proc = device.run(["bash", self.script], FAKE_IDLE_ERROR="could not reach the file list",
FAKE_CODE="1", RB_MESSAGE_WAIT="300", RB_DEAD_WAIT="1")
self.assertEqual(proc.returncode, 1)
self.assertLess(device.elapsed, 10)
self.assertIn("could not reach the file list", device.screen)
self.assertNotIn("No button pressed", device.screen)
device.assert_console_restored()
def test_the_idle_line_is_the_one_install_py_writes(self):
"""The launcher keyed on the words without install.py's prefix, and
its fake printed them the same way: the test passed, the device
still waited 300 s."""
state = install.TuiState(os_type="linux", home=Path.home())
state.idle_closed = True
line = install._farewell_lines(state)[0]
tui = (REPO_ROOT / "ports" / "shared" / "tui.txt").read_text(encoding="utf-8")
self.assertIn(f'= "{line}" ]', tui)
self.assertIn(f'print("{line}", file=sys.stderr)', FAKE_INSTALL)
def test_start_select_before_the_interface_handles_signals(self):
"""A SIGTERM before run_tui installs its handlers ends Python on the
default action, status 143."""
device = self.device()
proc = device.run(["bash", self.script], FAKE_HOLD="1", FAKE_PRESS="1",
FAKE_NO_HANDLER="1", RB_MESSAGE_WAIT="300")
self.assertEqual(proc.returncode, 143)
self.assertLess(device.elapsed, 10)
self.assertNotIn("Press A", device.screen)
device.assert_console_restored()
def test_a_mapper_that_dies_at_once_starts_no_interface(self):
device = self.device()
proc = device.run(["bash", self.script], FAKE_MAPPER_DIES="1", RB_MESSAGE_WAIT="300",
RB_DEAD_WAIT="1")
self.assertEqual(proc.returncode, 1)
self.assertLess(device.elapsed, 10)
self.assertEqual(device.tui_calls(), [])
screen = " ".join(device.screen.split())
self.assertIn("The controller cannot be read", screen)
self.assertIn("log.txt", screen)
log = (self.gamedir / "log.txt").read_text()
self.assertIn("cannot open /dev/uinput", log)
device.assert_console_restored()
def test_a_mapper_that_dies_later_ends_the_interface(self):
"""Nothing moves the interface once gptokeyb is gone: it is asked to
stop instead of waiting for a key forever."""
device = self.device()
proc = device.spawn(["bash", self.script], FAKE_HOLD="1", RB_DEAD_WAIT="1",
RB_MESSAGE_WAIT="300")
device.wait_for("ui.ready")
os.kill(int((device.state / "mapper.pid").read_text()), signal.SIGKILL)
self.assertEqual(proc.wait(timeout=20), 143)
self.assertEqual(device.lines("ui.log"), ["signal 15"])
# The interface disappears: the console says why, for RB_DEAD_WAIT.
screen = " ".join(device._drain().split())
self.assertIn("gptokeyb, which turns the buttons into keys, stopped during the run",
screen)
device.assert_console_restored()
def test_start_select_with_an_interface_that_ignores_sigterm(self):
"""An interface that does not stop within RB_STOP_GRACE is killed,
and the engine it started in its own session with it."""
device = self.device()
proc = device.run(["bash", self.script], FAKE_HOLD="1", FAKE_ENGINE="1",
FAKE_IGNORE_TERM="1", FAKE_PRESS="1", RB_STOP_GRACE="1",
RB_MESSAGE_WAIT="300")
self.assertEqual(proc.returncode, 137, proc.stderr.decode())
self.assertLess(device.elapsed, 15)
engine = int((device.state / "engine.pid").read_text())
wait_until(self, lambda: not Path(f"/proc/{engine}").exists(), 5,
"the engine outlived the interface")
log = (self.gamedir / "log.txt").read_text()
self.assertIn("the interface did not stop within 1 s", log)
device.assert_console_restored()
def test_an_interface_that_ignores_sigterm_without_pkill(self):
"""With no working pkill, the engines cannot be found: the interface
itself is still killed after RB_STOP_GRACE."""
device = self.device()
device.tool("pkill", "#!/bin/sh\necho 'pkill: applet not found' >&2\nexit 127\n")
proc = device.run(["bash", self.script], FAKE_HOLD="1", FAKE_IGNORE_TERM="1",
FAKE_PRESS="1", RB_STOP_GRACE="1", RB_MESSAGE_WAIT="300")
self.assertEqual(proc.returncode, 137, proc.stderr.decode())
self.assertLess(device.elapsed, 15)
ui = int((device.state / "ui.pid").read_text())
self.assertFalse(Path(f"/proc/{ui}").exists())
def test_the_sentinel_ends_when_the_launcher_is_killed(self):
"""SIGKILL leaves the launcher no trap: the sentinel ends by itself
once the launcher is gone."""
device = self.device()
proc = device.spawn(["bash", self.script], FAKE_HOLD="1")
device.wait_for("ui.ready")
token = device.mapper_args()[1]
self.assertEqual(len(sentinels(token)), 1)
proc.kill()
proc.wait(timeout=5)
wait_until(self, lambda: not sentinels(token), 10, "the sentinel outlived the launcher")
def test_a_closed_terminal_ends_the_run(self):
"""The terminal app closed (muxterm's Menu, foot's window): the run
inside it stops the interface and ends the run, and no other route
or screen opens after it."""
device = self.device(WAYLAND_DISPLAY="wayland-1")
device.tool("foot", FAKE_TERMAPP)
proc = device.spawn(["bash", self.script], FAKE_HOLD="1")
device.wait_for("ui.ready")
# FAKE_TERMAPP execs its command: its pid is the run inside it.
os.kill(int((device.state / "termapp.pid").read_text()), signal.SIGHUP)
self.assertEqual(proc.wait(timeout=20), 143)
self.assertEqual(device.lines("ui.log"), ["signal 15"])
self.assertEqual(self.dialog(device), ["FINISH"])
device.assert_mapper_stopped()
def test_sigterm_to_the_launcher_stops_the_interface_and_restores(self):
"""A firmware that signals the launcher alone: the trap stops the
interface, the mapper and puts the console back."""
device = self.device()
proc = device.spawn(["bash", self.script], FAKE_HOLD="1", FAKE_RAW="1")
device.wait_for("ui.ready")
proc.send_signal(signal.SIGTERM)
self.assertEqual(proc.wait(timeout=20), 143)
self.assertEqual(device.lines("ui.log"), ["signal 15"])
device.assert_mapper_stopped()
device.assert_console_restored()
def test_the_font_is_put_back_without_setfont_o(self):
"""busybox setfont has no -O: on ArkOS the font is changed only when
the Lat7-Terminus20x10 its tools restore exists, and that one is put
back."""
device = self.device(FAKE_NO_SAVE="1")
fonts = device.env["RB_FONTS"]
(Path(fonts) / "Lat7-Terminus20x10.psf.gz").write_bytes(b"")
marker = device.root / "text.plymouth"
marker.write_text("")
device.run(["bash", self.script], RB_ARK_MARKER=str(marker))
chosen = f"{fonts}/Lat7-TerminusBold22x11.psf.gz"
saved = str(self.gamedir / "run" / "font.psf")
self.assertEqual(device.tools(), [
f"setfont -C {device.console} -O {saved} {chosen}",
f"setfont -C {device.console} {chosen}",
f"setfont -C {device.console} {fonts}/Lat7-Terminus20x10.psf.gz",
])
def test_elsewhere_a_font_that_cannot_be_saved_is_left(self):
"""Lat7-Terminus20x10 is ArkOS's font: put back on another device,
it left the console in a font it never had."""
device = self.device(FAKE_NO_SAVE="1")
(Path(device.env["RB_FONTS"]) / "Lat7-Terminus20x10.psf.gz").write_bytes(b"")
device.run(["bash", self.script])
self.assertEqual(len([t for t in device.tools() if t.startswith("setfont")]), 1)
def test_without_the_fallback_font_the_font_is_left(self):
device = self.device(FAKE_NO_SAVE="1")
device.run(["bash", self.script])
self.assertEqual(len([t for t in device.tools() if t.startswith("setfont")]), 1)
def test_a_read_only_card_runs_from_the_runtime_folder(self):
device = self.device()
self.gamedir.chmod(0o555)
self.addCleanup(self.gamedir.chmod, 0o755)
proc = device.run(["bash", self.script])
self.assertEqual(proc.returncode, 0, proc.stderr.decode())
self.assertEqual(len(device.tui_calls()), 1)
spare = device.root / "runtime" / "retrobios"
log = (spare / "log.txt").read_text()
self.assertIn(f"{self.gamedir} cannot be written", log)
self.assertTrue((spare / "run" / "status").exists())
def test_nothing_writable_is_said_on_the_console(self):
device = self.device()
self.gamedir.chmod(0o555)
self.addCleanup(self.gamedir.chmod, 0o755)
runtime = device.root / "runtime"
runtime.mkdir(mode=0o555)
self.addCleanup(runtime.chmod, 0o755)
proc = device.run(["bash", self.script], RB_DEAD_WAIT="2")
self.assertEqual(proc.returncode, 1)
self.assertEqual(device.calls(), [])
self.assertIn("the SD card may be read-only or full", " ".join(device.screen.split()))
# Left on the console long enough to read before the menu redraws.
self.assertGreaterEqual(device.elapsed, 2)
def test_the_previous_log_is_kept(self):
device = self.device()
(self.gamedir / "log.txt").write_text("the failure\n")
device.run(["bash", self.script])
self.assertEqual((self.gamedir / "log.prev.txt").read_text(), "the failure\n")
self.assertNotIn("the failure", (self.gamedir / "log.txt").read_text())
if __name__ == "__main__":
unittest.main()
+344
View File
@@ -0,0 +1,344 @@
"""The handheld launchers' scripts, controller mappings, archives and the pages that describe them."""
from __future__ import annotations
import tests
from tests import scratch_dir
import hashlib
import json
import os
import re
import shutil
import subprocess
import unittest
import zipfile
import yaml
from installer_build import pins, port
from tests.handheldsupport import (
REPO_ROOT,
PORTS,
install,
read_gptk,
sdl_vidpid,
sdl_vidpid_list,
assert_muxterm_hides_every_pad,
)
SCRIPTS = [
PORTS / "shared" / "tui.txt",
PORTS / "RetroBIOS" / "RetroBIOS.sh",
PORTS / "RetroBIOS" / "retrobios" / "screens.txt",
PORTS / "batocera" / "RetroBIOS.sh",
PORTS / "muos" / "mux_launch.sh",
PORTS / "tools" / "RetroBIOS.sh",
]
# What each button does in the interface (wiki/installer.md, the full-screen
# interface): the convergent menu mapping of handheld_runtime.md.
ROLES = {
"a": "enter", "start": "enter", "b": "esc", "back": "esc", "x": "space",
"y": "tab", "l1": "pageup", "r1": "pagedown",
"up": "up", "down": "down", "left": "left", "right": "right",
}
# What a Linux console or a terminal sends for each key gptokeyb and evmapy
# emit, and the key the interface reads from it.
SEQUENCES = {
"enter": "\r", "esc": "\x1b", "space": " ", "tab": "\t",
"pageup": "\x1b[5~", "pagedown": "\x1b[6~",
"up": "\x1b[A", "down": "\x1b[B", "left": "\x1b[D", "right": "\x1b[C",
}
INTERFACE_KEYS = {
"enter": "enter", "esc": "esc", "space": "char", "tab": "tab",
"pageup": "pgup", "pagedown": "pgdn",
"up": "up", "down": "down", "left": "left", "right": "right",
}
# gptokeyb's char_to_keycode (PortsMaster/gptokeyb src/util.cpp) names.
GPTOKEYB_NAMES = set(SEQUENCES) | {"backspace", '\\"'}
class EveryScriptParses(unittest.TestCase):
def test_bash_n(self):
for script in SCRIPTS:
with self.subTest(script=str(script.relative_to(REPO_ROOT))):
proc = subprocess.run(["bash", "-n", str(script)], capture_output=True,
text=True, check=False)
self.assertEqual(proc.returncode, 0, proc.stderr)
def test_shellcheck(self):
"""CI provides shellcheck (validate.yml checks it), so there it may
not skip."""
if shutil.which("shellcheck") is None:
if os.environ.get("CI"):
self.fail("CI must provide shellcheck for the handheld launchers")
self.skipTest("shellcheck is not installed")
proc = subprocess.run(
["shellcheck", "--shell=bash", "--severity=style", "--external-sources",
"--source-path=SCRIPTDIR", *map(str, SCRIPTS)],
capture_output=True, text=True, check=False)
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
def test_launchers_are_executable(self):
for script in (PORTS / "RetroBIOS" / "RetroBIOS.sh", PORTS / "batocera" / "RetroBIOS.sh",
PORTS / "muos" / "mux_launch.sh", PORTS / "tools" / "RetroBIOS.sh"):
with self.subTest(script=str(script.relative_to(REPO_ROOT))):
self.assertTrue(script.stat().st_mode & 0o111)
self.assertTrue(script.read_text().startswith("#!/bin/bash\n"))
def test_muos_headers(self):
"""muOS's frontend reads the HELP, ICON and GRID lines of an app
(MustardOS internal share/application/*/mux_launch.sh)."""
head = (PORTS / "muos" / "mux_launch.sh").read_text().splitlines()[1:4]
self.assertEqual([line.split(":")[0] for line in head], ["# HELP", "# ICON", "# GRID"])
self.assertTrue(all(line.split(":", 1)[1].strip() for line in head))
class ControllerMappings(unittest.TestCase):
def _decodes(self, key: str) -> str:
decoder = install.KeyDecoder()
keys = decoder.feed(SEQUENCES[key]) + decoder.flush()
self.assertEqual(len(keys), 1, key)
return keys[0].name
def test_gptk_maps_every_button_the_interface_reads(self):
mapping = read_gptk(PORTS / "shared" / "retrobios.gptk")
for button, key in ROLES.items():
with self.subTest(button=button):
self.assertEqual(mapping[button][0], key)
self.assertEqual(self._decodes(key), INTERFACE_KEYS[key])
for values in mapping.values():
self.assertIn(values[0], GPTOKEYB_NAMES | {"repeat"})
self.assertTrue(set(values[1:]) <= {"repeat"})
self.assertEqual(mapping["up"], ["up", "repeat"])
self.assertEqual(mapping["down"], ["down", "repeat"])
self.assertEqual(mapping["guide"], ['\\"'])
def test_evmapy_keys_map_the_same_buttons(self):
"""Batocera's evmapy names L1 and R1 pageup and pagedown, and Select
select (batocera-pygame evmapy.keys)."""
data = json.loads((PORTS / "batocera" / "RetroBIOS.sh.keys").read_text())
actions = {a["trigger"]: a for a in data["actions_player1"]}
triggers = {"a": "a", "start": "start", "b": "b", "back": "select", "x": "x",
"y": "y", "l1": "pageup", "r1": "pagedown", "up": "up",
"down": "down", "left": "left", "right": "right"}
for button, trigger in triggers.items():
with self.subTest(button=button):
action = actions[trigger]
self.assertEqual(action["type"], "key")
self.assertEqual(action["target"], [f"KEY_{ROLES[button].upper()}"])
for stick in ("up", "down", "left", "right"):
self.assertEqual(actions[f"joystick1{stick}"]["target"], [f"KEY_{stick.upper()}"])
self.assertEqual(len(actions), len(data["actions_player1"]))
def test_x_ticks_and_y_opens_the_options_on_every_route(self):
"""The interface reads Space as Tick and Tab as Options: a mapping
that sends Y as Space activates the focused row instead."""
self.assertEqual(self._decodes("space"), "char")
self.assertEqual(self._decodes("tab"), "tab")
gptk = read_gptk(PORTS / "shared" / "retrobios.gptk")
self.assertEqual((gptk["x"], gptk["y"]), (["space"], ["tab"]))
keys = json.loads((PORTS / "batocera" / "RetroBIOS.sh.keys").read_text())
targets = {a["trigger"]: a["target"] for a in keys["actions_player1"]}
self.assertEqual((targets["x"], targets["y"]), (["KEY_SPACE"], ["KEY_TAB"]))
# muxterm sends Y as Space on its own (MustardOS/terminal input.c):
# where gptokeyb runs beside it, muxterm is told to leave the pad
# (assert_muxterm_hides_every_pad).
for launcher in (PORTS / "muos" / "mux_launch.sh", PORTS / "RetroBIOS" / "RetroBIOS.sh"):
self.assertIn("rb_muxterm ", launcher.read_text())
self.assertFalse((PORTS / "muos" / "muxterm.gptk").exists())
class TheSdlModel(unittest.TestCase):
def test_it_reads_the_ids_as_sdl_does(self):
self.assertEqual(sdl_vidpid(0x19, 1, 1, 0x100, "Deeplay-keys"), (1, 1))
self.assertEqual(sdl_vidpid(0x05, 0, 0x2009, 1, "pad"), (0, 0))
self.assertEqual(sdl_vidpid(0x05, 0, 0x2009, 1, "ab"), (0x6261, 0))
self.assertEqual(sdl_vidpid_list("0x0001/0x0001, 0x045e/0x028e"),
[0x00010001, 0x045e028e])
def test_the_first_hint_allowed_the_h700_pad(self):
with self.assertRaises(AssertionError):
assert_muxterm_hides_every_pad(unittest.TestCase(), "0x0001/0x0001")
class TheArchives(unittest.TestCase):
LAYOUT = {
port.PORT_ZIP: ("RetroBIOS.sh", "retrobios"),
port.BATOCERA_ZIP: ("RetroBIOS.sh", "retrobios"),
port.MUOS_APP: (None, "RetroBIOS"),
port.TOOLS_ZIP: ("RetroBIOS.sh", "retrobios"),
}
def setUp(self):
self.out = scratch_dir(self)
def test_every_release_archive_is_built(self):
self.assertEqual(set(port.ARCHIVES), set(self.LAYOUT))
def test_layout(self):
for name, (script, folder) in self.LAYOUT.items():
with self.subTest(archive=name):
with zipfile.ZipFile(port.build_archive(name, self.out / name)) as zf:
names = zf.namelist()
modes = {n: zf.getinfo(n).external_attr >> 16 for n in names}
self.assertEqual(names, sorted(names))
top = [n for n in names if "/" not in n]
self.assertEqual({n.split("/")[0] for n in names if "/" in n}, {folder})
for member in ("install.py", "tui.txt", "rbevents.py",
"licenses/LICENSE-retrobios.txt"):
self.assertIn(f"{folder}/{member}", names)
launcher = script or f"{folder}/mux_launch.sh"
self.assertTrue(modes[launcher] & 0o111, launcher)
if name == port.BATOCERA_ZIP:
self.assertEqual(top, ["RetroBIOS.sh", "RetroBIOS.sh.keys"])
self.assertNotIn("retrobios/retrobios.gptk", names)
elif script:
self.assertEqual(top, [script])
self.assertIn(f"{folder}/retrobios.gptk", names)
else:
self.assertEqual(top, [])
self.assertIn("RetroBIOS/retrobios.gptk", names)
def test_the_muos_icon_is_the_glyph_its_header_names(self):
"""muOS reads glyph/<ICON>.png in the application's folder, and the
muxapp named an icon it did not carry."""
header = (REPO_ROOT / "ports" / "muos" / "mux_launch.sh").read_text()
icon = re.search(r"^# ICON: (\S+)$", header, re.MULTILINE).group(1)
name = port.MUOS_APP
with zipfile.ZipFile(port.build_archive(name, self.out / name)) as zf:
data = zf.read(f"RetroBIOS/glyph/{icon}.png")
self.assertEqual(data[:8], b"\x89PNG\r\n\x1a\n")
def test_no_tools_support_file_is_a_menu_entry(self):
"""EmulationStation lists a folder only when it holds a file of the
system's extension (.sh for Tools and Ports): batocera-emulationstation
and EmulationStation-fcamod SystemData.cpp drop a folder with none.
The support folders must keep none, or each shows up in the menu."""
for name in (port.TOOLS_ZIP, port.PORT_ZIP, port.BATOCERA_ZIP):
with self.subTest(archive=name):
with zipfile.ZipFile(port.build_archive(name, self.out / name)) as zf:
inside = [n for n in zf.namelist()
if "/" in n and n.lower().endswith(".sh")]
self.assertEqual(inside, [])
def test_each_carries_the_pinned_installer(self):
pinned = pins.pinned_sha256("install.sh")
for name, (_script, folder) in self.LAYOUT.items():
with self.subTest(archive=name):
with zipfile.ZipFile(port.build_archive(name, self.out / name)) as zf:
bundled = hashlib.sha256(zf.read(f"{folder}/install.py")).hexdigest()
self.assertEqual(bundled, pinned)
def test_reproducible(self):
for name in port.ARCHIVES:
with self.subTest(archive=name):
first = port.build_archive(name, self.out / f"a-{name}").read_bytes()
second = port.build_archive(name, self.out / f"b-{name}").read_bytes()
self.assertEqual(first, second)
def test_each_names_the_release_packs(self):
"""packs.txt names the platforms a release publishes a pack for, as
the registry marks them, and the pack's name for each."""
registry = yaml.safe_load((REPO_ROOT / "platforms" / "_registry.yml").read_text())
released = {name for name, entry in registry["platforms"].items()
if entry.get("release_pack") is True}
labels = port.release_pack_labels()
self.assertEqual(set(labels), released)
self.assertEqual(labels["retroarch"], "RetroArch")
expected = "".join(f"{p}\t{label}\n" for p, label in sorted(labels.items()))
for name, (_script, folder) in self.LAYOUT.items():
with self.subTest(archive=name), \
zipfile.ZipFile(port.build_archive(name, self.out / name)) as zf:
self.assertEqual(zf.read(f"{folder}/packs.txt").decode(), expected)
def test_an_unpinned_installer_is_refused(self):
root = scratch_dir(self)
for name in ("install.py", "install.sh", "LICENSE", "NOTICE"):
shutil.copy(REPO_ROOT / name, root / name)
shutil.copytree(PORTS, root / "ports")
with (root / "install.py").open("a") as fh:
fh.write("\n")
for name in port.ARCHIVES:
with self.subTest(archive=name), self.assertRaises(ValueError):
port.build_archive(name, self.out / name, root)
def test_the_site_serves_each_and_the_release_none(self):
"""Each archive carries install.py: the site serves it from its own
commit (sitehandheld.py), and the release steps never build one."""
text = (REPO_ROOT / "wiki" / "release-process.md").read_text(encoding="utf-8")
self.assertNotIn("scripts/build_port.py", text)
installer = (REPO_ROOT / "wiki" / "installer.md").read_text(encoding="utf-8")
for name in port.ARCHIVES:
with self.subTest(archive=name):
self.assertNotIn(name, text)
self.assertIn(f"](../get/handheld/{name})", installer)
class TheDocs(unittest.TestCase):
def test_the_offline_packs_named_are_the_ones_that_serve(self):
"""The wiki names the pack each firmware takes offline: the release
pack of the platform install.py detects it as, or none."""
labels = port.release_pack_labels()
served = {}
for rule in install._EMBEDDED_RULES:
served.setdefault(rule.name, labels.get(rule.manifest))
for name in ("muOS", "ArkOS", "dArkOS", "JELOS"):
self.assertEqual(served[name], "RetroArch", name)
self.assertEqual(served["Batocera"], "Batocera")
for name in ("ROCKNIX", "KNULLI", "AmberELEC"):
self.assertIsNone(served[name], name)
text = (REPO_ROOT / "wiki" / "installer.md").read_text(encoding="utf-8")
self.assertIn("The releases carry no pack for ROCKNIX,\nKnulli or AmberELEC", text)
def test_every_pad_value_is_documented(self):
"""RETROBIOS_PAD is a contract the interface reads: each value tui.txt
names has its row in the handheld section."""
text = (REPO_ROOT / "wiki" / "installer.md").read_text(encoding="utf-8")
tui = (PORTS / "shared" / "tui.txt").read_text()
for value in ("gptokeyb", "muxterm", "evmapy", "miyoo"):
self.assertIn(f"| `{value}` |", text)
self.assertIn(value, tui)
def handheld(self) -> str:
text = (REPO_ROOT / "wiki" / "installer.md").read_text(encoding="utf-8")
start = text.index("## On a handheld")
end = text.find("\n## ", start + 1)
return text[start:end if end != -1 else len(text)]
def test_the_idle_limit_is_documented(self):
"""Every launcher sets RETROBIOS_IDLE_EXIT for the interface: a
mapper that runs but never opens the pad leaves no button to quit."""
self.assertIn("`RETROBIOS_IDLE_EXIT`", self.handheld())
self.assertIn("RETROBIOS_IDLE_EXIT", (PORTS / "shared" / "tui.txt").read_text())
def test_no_page_buttons_are_promised_in_muxterm(self):
"""muxterm keeps PgUp and PgDn for its scrollback (T/input.c:352-360)."""
readme = " ".join((PORTS / "muos" / "README.md").read_text().split())
self.assertNotIn("L1 and R1 to page", readme)
self.assertIn("L1 and R1 do nothing in muxterm", readme)
rows = [line for line in self.handheld().splitlines() if line.startswith("| L1, R1 |")]
self.assertEqual(len(rows), 1)
self.assertIn("not in muxterm", rows[0])
def test_muos_paths_are_the_card_muos_mounts(self):
"""muOS mounts the first card at /mnt/mmc (MustardOS internal
mux_launch.sh, GET_VAR storage/rom/mount)."""
text = (REPO_ROOT / "wiki" / "installer.md").read_text(encoding="utf-8")
start = text.index("## On a handheld")
end = text.find("\n## ", start + 1)
section = text[start:end if end != -1 else len(text)]
self.assertNotIn("/mmc/MUOS", section.replace("/mnt/mmc", ""))
self.assertIn("`MUOS/PortMaster/autoinstall/`", section)
if __name__ == "__main__":
unittest.main()
File diff suppressed because it is too large. Load diff
+760
View File
@@ -0,0 +1,760 @@
"""The bootstraps and the installer they fetch: Python 3.8 and the stdlib, HTTPS, pins and the one-liners."""
from __future__ import annotations
import tests
from tests import loopback
from tests import scratch_dir
import contextlib
import io
import json
import os
import shlex
import shutil
import socket
import subprocess
import sys
import tempfile
import time
import unittest
import unittest.mock
from pathlib import Path
from tests.netguard import module_guard
from tests.installsupport import REPO_ROOT, install, reported
# No connection off the loopback and no card of the host reaches a test:
# the module fails at its end otherwise.
setUpModule, tearDownModule = module_guard("test_install_bootstrap")
def _parenthesized_withs(tokens: list) -> list[str]:
import tokenize
found = []
for i, tok in enumerate(tokens):
if tok.type == tokenize.NAME and tok.string == "with" and i + 1 < len(tokens):
if tokens[i + 1].string != "(":
continue
depth = 0
for inner in tokens[i + 1:]:
if inner.string in "([{" and inner.type == tokenize.OP:
depth += 1
elif inner.string in ")]}" and inner.type == tokenize.OP:
depth -= 1
if depth == 0:
break
elif depth == 1 and inner.type == tokenize.NAME and inner.string == "as":
found.append(f"line {tok.start[0]}: parenthesized with")
break
return found
def _nested_fstrings(tokens: list) -> list[str]:
import tokenize
found: list[str] = []
fstring_start = getattr(tokenize, "FSTRING_START", None)
if fstring_start is None:
return found
stack = []
for tok in tokens:
if tok.type == fstring_start:
if stack:
found.append(f"line {tok.start[0]}: f-string inside an f-string")
stack.append(tok.string[-1])
elif tok.type == tokenize.FSTRING_END:
stack.pop()
elif stack and tok.type == tokenize.STRING and stack[-1] in tok.string:
found.append(f"line {tok.start[0]}: f-string reuses its quote")
elif stack and tok.type == tokenize.FSTRING_MIDDLE:
continue
elif stack and tok.type == tokenize.ERRORTOKEN and tok.string == "\\":
found.append(f"line {tok.start[0]}: backslash in an f-string field")
return found
class InstallerStaysOnPython38AndTheStdlib(unittest.TestCase):
"""install.py runs where the bootstraps let it run: Python 3.8, stdlib only.
The suite runs on 3.12 with pyyaml installed, so a removeprefix() or an
import of yaml kept every test green and died at the user's first run,
after the bootstrap had told them 3.8 was enough.
"""
SOURCE = (REPO_ROOT / "install.py").read_text(encoding="utf-8")
def test_the_grammar_is_python_38(self):
import ast
ast.parse(self.SOURCE, filename="install.py", feature_version=(3, 8))
def test_every_import_is_standard_library(self):
import ast
import sys
tree = ast.parse(self.SOURCE)
imported = set()
for node in ast.walk(tree):
if isinstance(node, ast.Import):
imported.update(alias.name.split(".")[0] for alias in node.names)
elif isinstance(node, ast.ImportFrom) and node.level == 0 and node.module:
imported.add(node.module.split(".")[0])
self.assertEqual(sorted(imported - set(sys.stdlib_module_names)), [])
def test_no_api_newer_than_38(self):
newer = (".removeprefix(", ".removesuffix(", "zoneinfo", "graphlib",
"functools.cache(", ".bit_count(", "math.lcm(", "math.nextafter(",
"tomllib", ".is_relative_to(", ".with_stem(", ".hardlink_to(",
"itertools.pairwise(", "str.removeprefix", "types.NoneType")
used = [api for api in newer if api in self.SOURCE]
self.assertEqual(used, [])
def test_a_python_38_interpreter_compiles_and_imports_it(self):
"""ast.parse(feature_version=(3, 8)) on 3.12 accepts parenthesized
context managers and PEP 701 f-strings: only a 3.8 interpreter says."""
import shutil
import subprocess
python38 = shutil.which("python3.8")
if python38 is None:
self.skipTest("no python3.8 on PATH, the static checks below still run")
code = (
"import importlib.util; "
"spec = importlib.util.spec_from_file_location('install', 'install.py'); "
"spec.loader.exec_module(importlib.util.module_from_spec(spec))"
)
proc = subprocess.run(
[python38, "-I", "-c", code], cwd=REPO_ROOT,
capture_output=True, text=True, timeout=60, check=False,
)
self.assertEqual(proc.returncode, 0, proc.stderr)
def test_a_python_38_interpreter_runs_it_end_to_end(self):
"""An import proves the grammar, not the calls a function makes.
dict | dict, zip(strict=True) or functools.cache import cleanly on
3.8 and raise the first time their path runs: listing, narrowing by
system, region and target, installing and checking all run here, on
the repository's own manifests served over loopback. CI installs the
interpreter, so there it may not skip.
"""
import shutil
import subprocess
python38 = shutil.which("python3.8")
if python38 is None:
if os.environ.get("CI"):
self.fail("CI must provide python3.8 for the installer's floor")
self.skipTest("no python3.8 on PATH, the static checks below still run")
base = loopback.serve(self, REPO_ROOT)
home = Path(tempfile.mkdtemp())
self.addCleanup(shutil.rmtree, home, True)
env = {
**os.environ,
"HOME": str(home),
"RETROBIOS_OS": "linux",
"RETROBIOS_BASE_URL": base,
}
dest = home / "system"
base = [python38, "-I", "install.py", "--platform", "retroarch", "--no-input"]
for extra in (
["--list-systems"],
["--list-cores"],
["--list-targets"],
["--dest", str(dest), "--system", "atari-lynx", "--region", "us",
"--target", "switch", "--yes"],
["--dest", str(dest), "--system", "atari-lynx", "--check"],
):
with self.subTest(args=extra):
proc = subprocess.run(
base + extra, cwd=REPO_ROOT, env=env,
capture_output=True, text=True, timeout=300, check=False,
)
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
self.assertNotIn("Traceback", proc.stderr)
self.assertTrue((dest / "lynxboot.img").is_file())
# A check is scripted as `--check && launch`: missing files fail it.
proc = subprocess.run(
base + ["--dest", str(home / "empty"), "--system", "atari-lynx", "--check"],
cwd=REPO_ROOT, env=env, capture_output=True, text=True, timeout=300,
check=False,
)
self.assertEqual(proc.returncode, 3, proc.stdout + proc.stderr)
self.assertIn("to download.", proc.stdout)
def test_no_syntax_a_312_parser_lets_through(self):
"""Parenthesized context managers (3.9) and f-strings reusing their
own quote or a backslash inside a replacement field (3.12)."""
import io
import tokenize
tokens = list(tokenize.generate_tokens(io.StringIO(self.SOURCE).readline))
found = _parenthesized_withs(tokens) + _nested_fstrings(tokens)
self.assertEqual(found, [])
def test_no_runtime_generic_builtin(self):
"""dict[str, Path] outside an annotation runs on 3.9 and later only."""
import ast
tree = ast.parse(self.SOURCE)
annotations = set()
for node in ast.walk(tree):
for field in ("annotation", "returns"):
sub = getattr(node, field, None)
if sub is not None:
annotations.update(id(n) for n in ast.walk(sub))
generic = {"dict", "list", "tuple", "set", "frozenset", "type"}
used = sorted(
f"line {node.lineno}: {node.value.id}[...]"
for node in ast.walk(tree)
if isinstance(node, ast.Subscript)
and isinstance(node.value, ast.Name)
and node.value.id in generic
and id(node) not in annotations
)
self.assertEqual(used, [])
def test_no_local_is_assigned_and_never_read(self):
"""ruff --select F in py38 mode is the installer's lint gate, and an
unread `appdata` in _os_default_dests made it exit 1. The suite has
no ruff, so the rule is read from the syntax tree."""
import ast
tree = ast.parse((Path(__file__).resolve().parents[1] / "install.py").read_text())
unread = []
for func in ast.walk(tree):
if not isinstance(func, (ast.FunctionDef, ast.AsyncFunctionDef)):
continue
stored, loaded = {}, set()
for node in ast.walk(func):
if isinstance(node, ast.Name):
if isinstance(node.ctx, ast.Store):
stored.setdefault(node.id, node.lineno)
else:
loaded.add(node.id)
nested = {
n.id for inner in ast.walk(func) if inner is not func
and isinstance(inner, (ast.FunctionDef, ast.Lambda))
for n in ast.walk(inner) if isinstance(n, ast.Name)
}
unread += [
f"{func.name}:{line} {name}" for name, line in stored.items()
if name not in loaded | nested and not name.startswith("_")
and not any(
isinstance(g, (ast.Global, ast.Nonlocal)) and name in g.names
for g in ast.walk(func)
)
]
self.assertEqual(unread, [])
# Nor a module rebound: a loop variable named `platform` shadowed the
# platform module in _detect_by_path (F402).
modules = {
alias.asname or alias.name.split(".")[0]
for node in tree.body if isinstance(node, ast.Import)
for alias in node.names
}
rebound = sorted(
f"{node.id}:{node.lineno}" for node in ast.walk(tree)
if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Store)
and node.id in modules
)
self.assertEqual(rebound, [])
class TestBaseUrlScheme(unittest.TestCase):
"""The bootstraps insist on HTTPS, so the installer must not be laxer."""
def test_https_is_accepted(self):
self.assertEqual(
install._checked_base_url("https://example.test/repo"),
"https://example.test/repo",
)
def test_plain_http_is_refused(self):
with self.assertRaises(ValueError):
install._checked_base_url("http://example.test/repo")
def test_local_file_scheme_is_refused(self):
with self.assertRaises(ValueError):
install._checked_base_url("file:///etc")
def test_a_refused_base_is_said_by_main_and_ends_on_code_1(self):
with unittest.mock.patch.dict(os.environ, {"RETROBIOS_BASE_URL": "http://example.test"}), \
unittest.mock.patch.object(sys, "argv", ["install.py", "--help"]), \
contextlib.redirect_stderr(io.StringIO()) as err, \
self.assertRaises(SystemExit) as stopped:
install.main()
self.assertEqual(stopped.exception.code, 1)
self.assertEqual(err.getvalue(),
"Error: RETROBIOS_BASE_URL must use HTTPS, got 'http://example.test'\n")
def test_an_empty_variable_is_unset(self):
# Set but empty, the base killed even an import of install.py, where
# every other reading of it takes it as unset.
with unittest.mock.patch.dict(os.environ, {"RETROBIOS_BASE_URL": "",
"RETROBIOS_REF": ""}):
remote = install.remote_from_env()
self.assertEqual(remote, install.Remote(
install.DEFAULT_RELEASE_REF, install.default_base_url(install.DEFAULT_RELEASE_REF),
False))
def test_loopback_http_is_allowed_for_end_to_end_runs(self):
for url in (
"http://127.0.0.1:8080/repo",
"http://localhost:8080/repo",
"http://[::1]:8080/repo",
):
self.assertEqual(install._checked_base_url(url), url)
class NoPythonNamesThisSystemsCommand(unittest.TestCase):
"""Without Python the bootstrap said `sudo apt install python3` on every
system: Termux has no sudo and installs with `pkg install python`, and
macOS has no apt."""
def _run(self, extra_env: dict, uname: str = "Linux") -> subprocess.CompletedProcess:
root = scratch_dir(self)
fake_bin = root / "bin"
fake_bin.mkdir()
for tool in ("sed", "tr", "dirname", "cat"):
found = shutil.which(tool)
if found:
(fake_bin / tool).symlink_to(found)
(fake_bin / "uname").write_text(f"#!/bin/sh\necho {uname}\n", encoding="utf-8")
(fake_bin / "uname").chmod(0o755)
# No terminal: the Termux offer is a question asked on /dev/tty.
return subprocess.run(
["/bin/sh", str(REPO_ROOT / "install.sh")], cwd=root,
env={"PATH": str(fake_bin), "HOME": str(root), **extra_env},
capture_output=True, text=True, timeout=30, check=False,
start_new_session=True,
)
def test_termux_is_told_pkg(self):
proc = self._run({"TERMUX_VERSION": "0.118",
"PREFIX": "/data/data/com.termux/files/usr"})
self.assertEqual(proc.returncode, 1)
self.assertIn("Install it with: pkg install python", proc.stderr)
self.assertNotIn("sudo", proc.stderr)
def test_macos_is_told_xcode_select(self):
proc = self._run({}, uname="Darwin")
self.assertEqual(proc.returncode, 1)
self.assertIn("Install it with: xcode-select --install", proc.stderr)
self.assertNotIn("apt", proc.stderr)
class ABootstrapPythonCanDoHttps(unittest.TestCase):
"""find_python checked the version only: a python3 built without ssl
was chosen over a full python, and every download then failed."""
def _bin(self) -> tuple[Path, Path]:
root = scratch_dir(self)
fake_bin = root / "bin"
fake_bin.mkdir()
for tool in ("sed", "tr", "dirname", "cat", "awk", "uname"):
found = shutil.which(tool)
if found:
(fake_bin / tool).symlink_to(found)
shadow = root / "no-ssl"
shadow.mkdir()
(shadow / "ssl.py").write_text('raise ImportError("built without ssl")\n')
(fake_bin / "python3").write_text(
f'#!/bin/sh\nPYTHONPATH="{shadow}" exec {shlex.quote(sys.executable)} "$@"\n')
(fake_bin / "python3").chmod(0o755)
(root / "install.py").write_text("import ssl; print('installer ran')\n")
shutil.copy(REPO_ROOT / "install.sh", root / "install.sh")
return root, fake_bin
def _run(self, root: Path, fake_bin: Path) -> subprocess.CompletedProcess:
return subprocess.run(
["/bin/sh", str(root / "install.sh")], cwd=root,
env={"PATH": str(fake_bin), "HOME": str(root)},
capture_output=True, text=True, timeout=30, check=False,
stdin=subprocess.DEVNULL, start_new_session=True,
)
def test_a_full_python_is_preferred(self):
root, fake_bin = self._bin()
(fake_bin / "python").symlink_to(sys.executable)
proc = self._run(root, fake_bin)
self.assertEqual(proc.returncode, 0, proc.stderr)
self.assertIn("installer ran", proc.stdout)
def test_an_old_python_is_named_with_its_version(self):
"""Debian Buster's python3 3.7 got "Install it with: sudo apt install
python3", which apt answers with "already the newest version"."""
root, fake_bin = self._bin()
(fake_bin / "python3").write_text(
'#!/bin/sh\ncase "$2" in *print*) echo 3.7.3 ;; esac\nexit 1\n')
proc = self._run(root, fake_bin)
self.assertEqual(proc.returncode, 1)
self.assertIn("Python 3.8 or newer is required, and python3 here is 3.7.3.",
proc.stderr)
self.assertNotIn("Install it with: sudo apt", proc.stderr)
def test_the_missing_module_is_named(self):
root, fake_bin = self._bin()
proc = self._run(root, fake_bin)
self.assertEqual(proc.returncode, 1)
self.assertNotIn("installer ran", proc.stdout)
self.assertIn("python3 is built without a module the installer needs", proc.stderr)
@unittest.skipUnless(shutil.which("pwsh") and shutil.which("script"), "needs PowerShell and a terminal")
class WindowsWithoutPythonIsOfferedWinget(unittest.TestCase):
"""A Windows machine without Python was told to install it, open a new
window and run the command again, while winget, present on Windows 10
and 11, could do it from the bootstrap."""
def _folder(self) -> tuple[Path, Path]:
folder = scratch_dir(self)
shutil.copy(REPO_ROOT / "install.ps1", folder / "install.ps1")
(folder / "install.py").write_text("print('installer ran')\n")
fake_bin = folder / "bin"
fake_bin.mkdir()
python = shutil.which("python3") or sys.executable
(fake_bin / "winget").write_text(
f"#!/bin/sh\necho \"winget $*\"\n{shutil.which('ln')} -s {shlex.quote(python)} "
f"{shlex.quote(str(fake_bin / 'python3'))}\n"
)
(fake_bin / "winget").chmod(0o755)
return folder, fake_bin
def _run(self, folder: Path, fake_bin: Path, answer: str) -> subprocess.CompletedProcess:
pwsh = shutil.which("pwsh")
command = (f"{shlex.quote(pwsh)} -NoProfile -File "
f"{shlex.quote(str(folder / 'install.ps1'))}")
return subprocess.run(
[shutil.which("script"), "-qec", command, "/dev/null"], input=answer,
env={**os.environ, "PATH": f"{fake_bin}{os.pathsep}/usr/bin/nonexistent"},
capture_output=True, text=True, timeout=120, check=False,
)
def test_yes_installs_and_carries_on(self):
folder, fake_bin = self._folder()
proc = self._run(folder, fake_bin, "y\n")
self.assertIn("Install Python 3.12 now with winget?", proc.stdout)
self.assertIn("winget install --id Python.Python.3.12", proc.stdout)
self.assertIn("installer ran", proc.stdout)
self.assertEqual(proc.returncode, 0, proc.stdout)
def test_no_names_the_command(self):
folder, fake_bin = self._folder()
proc = self._run(folder, fake_bin, "n\n")
self.assertNotIn("installer ran", proc.stdout)
self.assertIn("winget install Python.Python.3.12", proc.stdout)
self.assertFalse((fake_bin / "python3").exists())
@unittest.skipUnless(shutil.which("script"), "needs a terminal")
class TermuxStorageIsSetUpByTheBootstrap(unittest.TestCase):
"""The installer told an Android user to run termux-setup-storage and
start the whole run over: the bootstrap runs it and waits for ~/storage."""
def _run(self, storage_exists: bool) -> tuple[subprocess.CompletedProcess, Path]:
root = scratch_dir(self)
shutil.copy(REPO_ROOT / "install.sh", root / "install.sh")
(root / "install.py").write_text("print('installer ran')\n")
home = root / "home"
home.mkdir()
if storage_exists:
(home / "storage").mkdir()
fake_bin = root / "bin"
fake_bin.mkdir()
for tool in ("sed", "tr", "dirname", "cat", "awk", "uname", "sleep", "mkdir"):
found = shutil.which(tool)
if found:
(fake_bin / tool).symlink_to(found)
(fake_bin / "python3").symlink_to(sys.executable)
# The app creates ~/storage once Android's dialog is answered.
(fake_bin / "termux-setup-storage").write_text(
f'#!/bin/sh\necho ran >> "{root}/setup.log"\nmkdir "$HOME/storage"\n')
(fake_bin / "termux-setup-storage").chmod(0o755)
command = (f"PATH={shlex.quote(str(fake_bin))} HOME={shlex.quote(str(home))} "
f"TERMUX_VERSION=0.118 /bin/sh {shlex.quote(str(root / 'install.sh'))}")
proc = subprocess.run(
[shutil.which("script"), "-qec", command, "/dev/null"],
capture_output=True, text=True, timeout=120, stdin=subprocess.DEVNULL, check=False)
return proc, root
def test_it_runs_once_then_the_installer(self):
proc, root = self._run(storage_exists=False)
self.assertEqual(proc.returncode, 0, proc.stdout)
self.assertEqual((root / "setup.log").read_text(), "ran\n")
self.assertIn("allow it", proc.stdout)
self.assertIn("installer ran", proc.stdout)
def test_a_set_up_storage_is_left_alone(self):
proc, root = self._run(storage_exists=True)
self.assertEqual(proc.returncode, 0, proc.stdout)
self.assertFalse((root / "setup.log").exists())
self.assertIn("installer ran", proc.stdout)
@unittest.skipUnless(shutil.which("pwsh"), "needs PowerShell")
class APyLauncherWithoutARuntimeIsPassed(unittest.TestCase):
"""findPython returned the py launcher as soon as it existed: one left
with no usable runtime hid a Python 3.12 on PATH, and the bootstrap
offered a second install or gave up."""
def test_python_on_path_runs_the_installer(self):
folder = scratch_dir(self)
shutil.copy(REPO_ROOT / "install.ps1", folder / "install.ps1")
(folder / "install.py").write_text("print('installer ran')\n")
fake_bin = folder / "bin"
fake_bin.mkdir()
(fake_bin / "py").write_text("#!/bin/sh\necho 'No installed Python found!' >&2\nexit 1\n")
(fake_bin / "py").chmod(0o755)
(fake_bin / "python3").symlink_to(sys.executable)
proc = subprocess.run(
[shutil.which("pwsh"), "-NoProfile", "-File", str(folder / "install.ps1")],
env={**os.environ, "PATH": f"{fake_bin}{os.pathsep}/usr/bin/nonexistent"},
capture_output=True, text=True, timeout=120, stdin=subprocess.DEVNULL, check=False)
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
self.assertIn("installer ran", proc.stdout)
@unittest.skipUnless(shutil.which("pwsh"), "needs PowerShell")
class APowerShellDownloadThatNeverAnswersEnds(unittest.TestCase):
"""Invoke-WebRequest had no timeout: behind a server that takes the
connection and never answers, the window waited forever with no line."""
def test_a_silent_server_ends_on_the_network_advice(self):
folder = scratch_dir(self)
shutil.copy(REPO_ROOT / "install.ps1", folder / "install.ps1")
with socket.socket() as listener:
listener.bind(("127.0.0.1", 0))
listener.listen(4)
port = listener.getsockname()[1]
started = time.monotonic()
proc = subprocess.run(
["pwsh", "-NoProfile", "-File", str(folder / "install.ps1"), "--check"],
env={**os.environ,
"RETROBIOS_INSTALL_URL": f"https://127.0.0.1:{port}/install.py"},
capture_output=True, text=True, timeout=150, stdin=subprocess.DEVNULL, check=False)
self.assertLess(time.monotonic() - started, 120)
self.assertNotEqual(proc.returncode, 0)
self.assertIn("could not be downloaded", " ".join((proc.stdout + proc.stderr).split()))
class ThePowerShellWrapperPassesTheStatusOn(unittest.TestCase):
"""Every non-zero exit became a red "installer failed" exception and
code 1: a --check that found work read as a crash, 2 and 130 were lost."""
@unittest.skipUnless(shutil.which("pwsh"), "needs PowerShell")
def test_the_installer_code_comes_back_unchanged(self):
folder = scratch_dir(self)
shutil.copy(REPO_ROOT / "install.ps1", folder / "install.ps1")
for code in (0, 1, 2, 130):
with self.subTest(code=code):
(folder / "install.py").write_text(f"import sys\nsys.exit({code})\n")
proc = subprocess.run(
["pwsh", "-NoProfile", "-File", str(folder / "install.ps1")],
capture_output=True, text=True, timeout=120,
check=False,
)
self.assertEqual(proc.returncode, code, proc.stderr)
self.assertNotIn("installer failed", proc.stdout + proc.stderr)
class ThePowerShellWrapperPassesEveryArgument(unittest.TestCase):
"""A [Parameter()] attribute made install.ps1 an advanced script, whose
common parameters took `-v` (read as `-Verbose`), `-vb`, `-d` and `-ea`:
the documented `-v` never reached install.py."""
@unittest.skipUnless(shutil.which("pwsh"), "needs PowerShell")
def test_short_flags_reach_the_installer(self):
folder = scratch_dir(self)
shutil.copy(REPO_ROOT / "install.ps1", folder / "install.ps1")
(folder / "install.py").write_text(
"import json, sys\nprint(json.dumps(sys.argv[1:]))\n"
)
for args in (["-v", "--check"], ["-vb"], ["-Verbose"], ["-d", "-ea", "x"],
["--platform", "a b"], []):
with self.subTest(args=args):
proc = subprocess.run(
["pwsh", "-NoProfile", "-File", str(folder / "install.ps1"), *args],
capture_output=True, text=True, timeout=120,
check=False,
)
self.assertEqual(proc.returncode, 0, proc.stderr)
self.assertEqual(json.loads(proc.stdout), args)
class ThePagesQuoteTheBootstrapsOwnWords(unittest.TestCase):
"""The installer page and the FAQ quoted "install.py SHA-256 mismatch",
a message the bootstraps no longer print."""
def test_the_quoted_message_is_printed(self):
quoted = "does not match its expected"
for wrapper in ("install.sh", "install.ps1"):
self.assertIn(quoted, (REPO_ROOT / wrapper).read_text(encoding="utf-8"))
for page in ("wiki/installer.md", "wiki/faq.md"):
text = (REPO_ROOT / page).read_text(encoding="utf-8")
self.assertNotIn("SHA-256 mismatch`", text)
self.assertIn(quoted, " ".join(text.split()))
class TheInstallerFitsTheOneLiners(unittest.TestCase):
"""install.sh and install.ps1 refuse an installer over their cap, 8 MiB:
the SHA-256 pin refuses any changed byte, and the cap only stops a page
served in its place. install.py grew from 596 KB to 1.1 MB in twelve
days: past REVIEW_BYTES its growth is looked at before it goes on."""
# The size past which install.py's growth is reviewed, well below the cap.
REVIEW_BYTES = 3 * 1024 ** 2
def _caps(self) -> dict[str, int]:
import re
shell = (REPO_ROOT / "install.sh").read_text(encoding="utf-8")
ps1 = (REPO_ROOT / "install.ps1").read_text(encoding="utf-8")
units = {"": 1, "KB": 1024, "MB": 1024 ** 2}
number, unit = re.search(r"\$maximumInstallerBytes = (\d+)(KB|MB)?", ps1).groups()
return {"install.sh": int(re.search(r"^MAX_INSTALLER_BYTES=(\d+)$", shell, re.M)[1]),
"install.ps1": int(number) * units[unit or ""]}
def test_both_one_liners_accept_the_same_size(self):
self.assertEqual(set(self._caps().values()), {8 * 1024 ** 2})
def test_install_py_stays_below_the_review_size(self):
size = (REPO_ROOT / "install.py").stat().st_size
self.assertLess(self.REVIEW_BYTES, min(self._caps().values()))
self.assertLess(
size, self.REVIEW_BYTES,
f"install.py is {size} bytes, past the {self.REVIEW_BYTES} that call for a "
"look at its growth: shrink it, or raise REVIEW_BYTES once reviewed")
def test_the_refusal_names_a_cause_and_a_way_out(self):
for script in ("install.sh", "install.ps1"):
text = " ".join((REPO_ROOT / script).read_text(encoding="utf-8").split())
with self.subTest(script=script):
self.assertIn("this script accepts", text)
self.assertIn("open any web page to sign in", text)
class AnOverrideIsNamedWhenItIsTheCause(unittest.TestCase):
"""A RETROBIOS_INSTALL_SHA256 of the user's own read as a cut download,
and a dead RETROBIOS_BASE_URL as a network fault naming no host."""
def test_an_unreachable_base_url_is_named(self):
error = install.urllib.error.URLError("timed out")
with unittest.mock.patch.dict(os.environ, {"RETROBIOS_BASE_URL": "https://mirror.invalid"}), \
unittest.mock.patch.object(install, "_fetch_json", side_effect=error), \
contextlib.redirect_stderr(io.StringIO()) as err, \
self.assertRaises(SystemExit):
reported(install.fetch_manifest, "retroarch")
text = err.getvalue()
self.assertIn("Address: https://mirror.invalid/install/retroarch.json", text)
self.assertIn("RETROBIOS_BASE_URL is set", text)
def test_a_pinned_fingerprint_is_named(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
fake_bin = root / "bin"
fake_bin.mkdir()
(fake_bin / "curl").write_text(
"#!/bin/sh\noutput=\n"
"while [ \"$#\" -gt 0 ]; do\n"
" if [ \"$1\" = --output ]; then output=$2; shift 2; else shift; fi\n"
"done\n"
"echo 'print(1)' > \"$output\"\n")
(fake_bin / "curl").chmod(0o755)
env = {**os.environ, "PATH": f"{fake_bin}{os.pathsep}{os.environ['PATH']}",
"TMPDIR": str(root), "RETROBIOS_INSTALL_SHA256": "0" * 64}
proc = subprocess.run(["sh", "-s"], cwd=root, env=env, capture_output=True,
input=(REPO_ROOT / "install.sh").read_text(),
text=True, timeout=30, check=False)
self.assertEqual(proc.returncode, 1)
self.assertIn("RETROBIOS_INSTALL_SHA256 is set", proc.stderr)
self.assertNotIn("cut short", proc.stderr)
ps1 = (REPO_ROOT / "install.ps1").read_text()
self.assertIn('"RETROBIOS_INSTALL_URL", "RETROBIOS_INSTALL_SHA256"', ps1)
@unittest.skipUnless(shutil.which("pwsh"), "needs PowerShell")
class ThePowerShellOneLinerLeavesTheSessionAlone(unittest.TestCase):
"""irm | iex left $ErrorActionPreference = Stop and the script's
variables in the user's session: their next error stopped everything."""
def test_nothing_is_left_behind(self):
script = (
"Get-Content install.ps1 -Raw | Invoke-Expression; "
"\"EAP=$ErrorActionPreference temporary=[$temporary] installer=[$installer] "
"bootstrap=[$env:RETROBIOS_BOOTSTRAP]\""
)
# Refused before any request, so the run needs no network.
env = {**os.environ, "RETROBIOS_INSTALL_URL": "http://127.0.0.1/install.py"}
env.pop("RETROBIOS_BOOTSTRAP", None)
proc = subprocess.run(["pwsh", "-NoProfile", "-Command", script], cwd=REPO_ROOT,
env=env, capture_output=True, text=True, timeout=120, check=False)
self.assertIn("must use HTTPS", proc.stderr)
self.assertIn("EAP=Continue temporary=[] installer=[] bootstrap=[]", proc.stdout)
class AStrippedPythonIsNamed(unittest.TestCase):
"""A Python built without ssl blamed the network three times, and one
without zlib or unicodedata stopped on a ModuleNotFoundError traceback."""
def _run(self, hidden: str, *argv: str) -> subprocess.CompletedProcess:
code = (f"import runpy, sys; sys.modules[{hidden!r}] = None; "
"sys.argv = sys.argv[1:]; "
"runpy.run_path(sys.argv[0], run_name='__main__')")
env = {key: value for key, value in os.environ.items()
if key not in ("RETROBIOS_BASE_URL", install.OFFLINE_VARIABLE)}
return subprocess.run(
[sys.executable, "-c", code, str(REPO_ROOT / "install.py"), *argv],
env=env, capture_output=True, text=True, timeout=60,
stdin=subprocess.DEVNULL, check=False)
def test_a_missing_c_module_is_named(self):
for hidden in ("zlib", "unicodedata"):
with self.subTest(module=hidden):
proc = self._run(hidden, "--help")
self.assertEqual(proc.returncode, 1)
self.assertNotIn("Traceback", proc.stderr)
self.assertIn(f"built without its {hidden} module", proc.stderr)
def test_no_ssl_is_said_once_not_retried(self):
with tempfile.TemporaryDirectory() as dest:
proc = self._run("ssl", "--platform", "retroarch", "--dest", dest,
"--check", "--no-input")
self.assertEqual(proc.returncode, 1)
self.assertIn("built without its ssl module and cannot use HTTPS", proc.stderr)
self.assertNotIn("trying again", proc.stderr)
self.assertNotIn("HTTPS_PROXY", proc.stderr)
class APackageHintNamesThePackageCommand(unittest.TestCase):
"""Run from the retrobios package, a hint said `python3 install.py`,
a file the user does not have."""
def test_the_environment_names_the_command(self):
for prefix, command in (
("/home/me/.cache/uv/archive-v0/UwL4i3xOlCEYSTnw", "uvx retrobios"),
("C:\\Users\\me\\AppData\\Local\\uv\\cache\\archive-v0\\x1", "uvx retrobios"),
("/home/me/.cache/pipx/539b8243c11ee7c", "pipx run retrobios"),
("/home/me/.local/pipx/.cache/539b8243c11ee7c", "pipx run retrobios"),
("/home/me/.local/share/pipx/venvs/retrobios", "retrobios"),
("/home/me/.local/share/uv/tools/retrobios", "retrobios"),
("/home/me/venv", "retrobios"),
):
with self.subTest(prefix=prefix):
self.assertEqual(install.package_command(prefix), command)
def test_the_launcher_is_the_package_command(self):
hint = install.Invocation()
with unittest.mock.patch.dict(os.environ, {"RETROBIOS_BOOTSTRAP": "pypi"}), \
unittest.mock.patch.object(install.sys, "prefix", "/home/me/.cache/uv/archive-v0/a"):
self.assertEqual(hint.launcher(), "uvx retrobios")
self.assertEqual(hint.launcher(elevated=True), "sudo uvx retrobios")
self.assertTrue(hint.command("retroarch", "DIR").startswith(
"uvx retrobios --platform retroarch --dest DIR"))
if __name__ == "__main__":
unittest.main()
+966
View File
@@ -0,0 +1,966 @@
"""install.py reads each setup's config files and finds RetroArch and LaunchBox where they keep their BIOS."""
from __future__ import annotations
import tests
from tests import scratch_dir
import functools
import http.server
import json
import os
import subprocess
import tempfile
import threading
import unittest
import unittest.mock
from pathlib import Path
from tests.netguard import module_guard
from tests.installsupport import REPO_ROOT, install
# No connection off the loopback and no card of the host reaches a test:
# the module fails at its end otherwise.
setUpModule, tearDownModule = module_guard("test_install_config")
class TestShellUnquote(unittest.TestCase):
def test_plain_value(self):
self.assertEqual(install._shell_unquote("/userdata/bios"), "/userdata/bios")
def test_double_quoted(self):
self.assertEqual(install._shell_unquote('"/home/deck/Emulation"'), "/home/deck/Emulation")
def test_single_quoted(self):
self.assertEqual(install._shell_unquote("'/home/deck/Emulation'"), "/home/deck/Emulation")
def test_concatenated_quoted_unquoted(self):
# EmuDeck SD card layout (issue: quote left in the middle of the path)
self.assertEqual(
install._shell_unquote('"/run/media/deck/EmuSD"/Emulation'),
"/run/media/deck/EmuSD/Emulation",
)
def test_concatenated_preserves_spaces_in_quotes(self):
self.assertEqual(
install._shell_unquote('"/run/media/deck/My Card"/Emulation'),
"/run/media/deck/My Card/Emulation",
)
def test_home_variable_expansion(self):
old = os.environ.get("HOME")
os.environ["HOME"] = "/home/deck"
try:
self.assertEqual(
install._shell_unquote('"$HOME"/Emulation'),
"/home/deck/Emulation",
)
self.assertEqual(
install._shell_unquote('"${HOME}/Emulation"'),
"/home/deck/Emulation",
)
finally:
if old is not None:
os.environ["HOME"] = old
def test_single_quotes_do_not_expand(self):
self.assertEqual(install._shell_unquote("'$HOME/x'"), "$HOME/x")
def test_trailing_comment(self):
self.assertEqual(install._shell_unquote("/data/bios # main dir"), "/data/bios")
def test_unquoted_stops_at_whitespace(self):
self.assertEqual(install._shell_unquote("/data/bios extra"), "/data/bios")
def test_tilde_expansion(self):
old = os.environ.get("HOME")
os.environ["HOME"] = "/home/deck"
try:
self.assertEqual(install._shell_unquote("~/Emulation"), "/home/deck/Emulation")
finally:
if old is not None:
os.environ["HOME"] = old
class TestParseBashVar(unittest.TestCase):
def _write(self, content: str) -> Path:
fd, path = tempfile.mkstemp(suffix=".sh")
os.close(fd)
Path(path).write_text(content, encoding="utf-8")
self.addCleanup(os.unlink, path)
return Path(path)
def test_emudeck_sd_card_concatenation(self):
path = self._write(
'emulationPath="/run/media/deck/EmuSD"/Emulation\n'
'biosPath="/run/media/deck/EmuSD"/Emulation/bios\n'
)
self.assertEqual(
install._parse_bash_var(path, "emulationPath"),
"/run/media/deck/EmuSD/Emulation",
)
self.assertEqual(
install._parse_bash_var(path, "biosPath"),
"/run/media/deck/EmuSD/Emulation/bios",
)
def test_emudeck_home_variable(self):
old = os.environ.get("HOME")
os.environ["HOME"] = "/home/deck"
try:
path = self._write('emulationPath="$HOME"/Emulation\n')
self.assertEqual(
install._parse_bash_var(path, "emulationPath"),
"/home/deck/Emulation",
)
finally:
if old is not None:
os.environ["HOME"] = old
def test_missing_key(self):
path = self._write("other=1\n")
self.assertIsNone(install._parse_bash_var(path, "emulationPath"))
def test_missing_file(self):
self.assertIsNone(
install._parse_bash_var(Path("/nonexistent/settings.sh"), "emulationPath")
)
class TestParseJsonPath(unittest.TestCase):
def _write(self, data) -> Path:
fd, path = tempfile.mkstemp(suffix=".json")
os.close(fd)
Path(path).write_text(
data if isinstance(data, str) else json.dumps(data), encoding="utf-8"
)
self.addCleanup(os.unlink, path)
return Path(path)
def test_retrodeck_rd_home_path(self):
path = self._write(
{"version": "1.0", "paths": {"rd_home_path": "/home/deck/retrodeck"}}
)
self.assertEqual(
install._parse_json_path(path, "paths", "rd_home_path"),
"/home/deck/retrodeck",
)
def test_missing_key_returns_none(self):
path = self._write({"paths": {}})
self.assertIsNone(install._parse_json_path(path, "paths", "rd_home_path"))
def test_invalid_json_returns_none(self):
path = self._write("{not json")
self.assertIsNone(install._parse_json_path(path, "paths", "rd_home_path"))
def test_empty_value_returns_none(self):
path = self._write({"paths": {"rd_home_path": ""}})
self.assertIsNone(install._parse_json_path(path, "paths", "rd_home_path"))
def test_missing_file(self):
self.assertIsNone(
install._parse_json_path(Path("/nonexistent/rd.json"), "paths", "rd_home_path")
)
class TestDefaultDests(unittest.TestCase):
def test_retrodeck_defaults_to_rdhome(self):
self.assertEqual(
install.DEFAULT_DESTS["retrodeck"], Path.home() / "retrodeck"
)
def test_known_fixed_paths(self):
self.assertEqual(install.DEFAULT_DESTS["batocera"], Path("/userdata/bios"))
self.assertEqual(
install.DEFAULT_DESTS["recalbox"], Path("/recalbox/share/bios")
)
def test_mister_defaults_to_games_on_the_sd_card(self):
self.assertEqual(
install.DEFAULT_DESTS["misterfpga"], Path("/media/fat/games")
)
class TestDefaultsFollowTheRegistry(unittest.TestCase):
"""A platform named but not detected fell back to ~/bios on every OS for
RetroArch, RetroBat, BizHawk and RomM, a folder none of them reads."""
ENV = {"USERPROFILE": "/u", "APPDATA": "/u/AppData/Roaming"}
def _expand(self, raw: str) -> Path:
text = raw.replace("$HOME", str(Path.home()))
for name, value in self.ENV.items():
text = text.replace(f"%{name}%", value)
return Path(text.replace("\\", "/"))
def test_the_default_is_a_folder_detection_looks_for(self):
import yaml
registry = yaml.safe_load(
(Path(__file__).resolve().parent.parent / "platforms" / "_registry.yml")
.read_text(encoding="utf-8")
)["platforms"]
checked = 0
with unittest.mock.patch.dict(os.environ, self.ENV):
for name, entry in registry.items():
by_os: dict[str, set[Path]] = {}
for rule in (entry.get("install") or {}).get("detect", []):
if rule.get("bios_path"):
by_os.setdefault(rule["os"], set()).add(self._expand(rule["bios_path"]))
elif rule.get("method") == "path_exists":
by_os.setdefault(rule["os"], set()).add(self._expand(rule["path"]))
elif rule.get("parse_key") == "system_directory":
config = self._expand(rule["config"])
by_os.setdefault(rule["os"], set()).add(config.parent / "system")
if name in install.PORTABLE_ONLY:
# No folder of its own to fall back on: the run stops
# and asks for --dest (test_a_portable_platform_is_not_guessed).
continue
for os_type, candidates in by_os.items():
with self.subTest(platform=name, os=os_type):
self.assertIn(install._default_dest(os_type, name), candidates)
checked += 1
# The installer's own default comes first in the registry and
# is the one the pack guide and the installer name.
first = next(
(self._expand(rule["path"]) for rule in (entry.get("install") or {}).get("detect", [])
if rule.get("os") == "windows" and rule.get("method") == "path_exists"),
None,
)
if name == "retrobat":
self.assertEqual(install._default_dest("windows", name), first)
self.assertGreater(checked, 10)
class TestLaunchboxDetection(unittest.TestCase):
"""LaunchBox references its emulators in Data/Emulators.xml."""
def _make_launchbox(self, app_path: str, title: str = "Retroarch"):
root = scratch_dir(self)
data = root / "LaunchBox" / "Data"
data.mkdir(parents=True)
xml = data / "Emulators.xml"
xml.write_text(
"<?xml version=\"1.0\" standalone=\"yes\"?>\n"
"<LaunchBox>\n"
" <Emulator>\n"
" <ID>x</ID>\n"
f" <Title>{title}</Title>\n"
f" <ApplicationPath>{app_path}</ApplicationPath>\n"
" </Emulator>\n"
"</LaunchBox>\n"
)
return root, xml
def test_relative_application_path(self):
root, xml = self._make_launchbox("Emulators\\RetroArch\\retroarch.exe")
ra_dir = root / "LaunchBox" / "Emulators" / "RetroArch"
ra_dir.mkdir(parents=True)
self.assertEqual(
install._launchbox_retroarch_system_dir(xml), ra_dir / "system"
)
def test_absolute_application_path(self):
emu_dir = scratch_dir(self) / "RetroArch"
emu_dir.mkdir(parents=True)
root, xml = self._make_launchbox(str(emu_dir / "retroarch.exe"))
self.assertEqual(
install._launchbox_retroarch_system_dir(xml), emu_dir / "system"
)
def test_no_retroarch_entry(self):
root, xml = self._make_launchbox("Emulators\\PCSX2\\pcsx2.exe", "PCSX2")
self.assertIsNone(install._launchbox_retroarch_system_dir(xml))
def test_missing_emulator_directory(self):
root, xml = self._make_launchbox("Emulators\\RetroArch\\retroarch.exe")
self.assertIsNone(install._launchbox_retroarch_system_dir(xml))
def test_stale_entry_before_installed_one(self):
root = scratch_dir(self)
data = root / "LaunchBox" / "Data"
data.mkdir(parents=True)
ra_dir = root / "LaunchBox" / "Emulators" / "RetroArch"
ra_dir.mkdir(parents=True)
xml = data / "Emulators.xml"
xml.write_text(
"<?xml version=\"1.0\" standalone=\"yes\"?>\n"
"<LaunchBox>\n"
" <Emulator>\n"
" <Title>RetroArch (old)</Title>\n"
" <ApplicationPath>Emulators\\RetroArch-old\\retroarch.exe</ApplicationPath>\n"
" </Emulator>\n"
" <Emulator>\n"
" <Title>RetroArch</Title>\n"
" <ApplicationPath>Emulators\\RetroArch\\retroarch.exe</ApplicationPath>\n"
" </Emulator>\n"
"</LaunchBox>\n"
)
self.assertEqual(
install._launchbox_retroarch_system_dir(xml), ra_dir / "system"
)
def test_missing_or_malformed_xml(self):
self.assertIsNone(
install._launchbox_retroarch_system_dir(Path("/nonexistent/Emulators.xml"))
)
root = scratch_dir(self)
bad = root / "Emulators.xml"
bad.write_text("not xml at all <<<")
self.assertIsNone(install._launchbox_retroarch_system_dir(bad))
class TestRetroarchPathExpansion(unittest.TestCase):
"""RetroArch abbreviates paths with '~' and ':' in its config."""
def _cfg(self, value: str) -> Path:
app_dir = scratch_dir(self)
(app_dir / "retroarch.cfg").write_text(f'system_directory = "{value}"\n')
return app_dir
def test_application_dir_prefix(self):
app_dir = self._cfg(":\\system")
self.assertEqual(
install._parse_retroarch_system_dir(app_dir / "retroarch.cfg"),
app_dir / "system",
)
def test_application_dir_prefix_forward_slash(self):
app_dir = self._cfg(":/system")
self.assertEqual(
install._parse_retroarch_system_dir(app_dir / "retroarch.cfg"),
app_dir / "system",
)
def test_home_prefix(self):
app_dir = self._cfg("~/bios")
self.assertEqual(
install._parse_retroarch_system_dir(app_dir / "retroarch.cfg"),
Path.home() / "bios",
)
def test_application_dir_overrides_config_dir(self):
app_dir = self._cfg(":\\system")
elsewhere = scratch_dir(self)
self.assertEqual(
install._parse_retroarch_system_dir(
app_dir / "retroarch.cfg", elsewhere
),
elsewhere / "system",
)
def test_absolute_path_untouched(self):
target = scratch_dir(self) / "bios"
app_dir = self._cfg(str(target))
self.assertEqual(
install._parse_retroarch_system_dir(app_dir / "retroarch.cfg"), target
)
class TestLaunchboxRoot(unittest.TestCase):
"""LaunchBox installs where the user chooses and keeps no registry key."""
def _make_root(self, root: Path) -> Path:
(root / "Data").mkdir(parents=True)
(root / "Data" / "Emulators.xml").write_text(
"<?xml version=\"1.0\" standalone=\"yes\"?>\n<LaunchBox></LaunchBox>\n"
)
return root
def test_shortcut_target_is_read(self):
# Byte layout of the shortcut a real install writes
lnk = scratch_dir(self) / "LaunchBox.lnk"
lnk.write_bytes(
b"L\x00\x00\x00\x01\x14\x02\x00\x00\x00\x00\x00\xc0\x00\x00\x00"
b"\x00\x00\x00\x46" + b"\x00" * 8
+ b"C:\\Games\\LaunchBox\\Core\\LaunchBox.exe\x00"
b"\x1c\x00\x00\x00LaunchBox\x00"
)
self.assertEqual(
install._lnk_target(lnk, "LaunchBox.exe"),
Path("C:/Games/LaunchBox/Core/LaunchBox.exe"),
)
def test_shortcut_without_target_is_ignored(self):
lnk = scratch_dir(self) / "LaunchBox.lnk"
lnk.write_bytes(b"L\x00\x00\x00 no target here ")
self.assertIsNone(install._lnk_target(lnk, "LaunchBox.exe"))
def test_shortcut_pointing_elsewhere_falls_back(self):
# A stale shortcut must not win over a real installation
userprofile = scratch_dir(self)
root = self._make_root(userprofile / "LaunchBox")
appdata = scratch_dir(self)
lnk_dir = (
appdata / "Microsoft" / "Windows" / "Start Menu" / "Programs" / "LaunchBox"
)
lnk_dir.mkdir(parents=True)
(lnk_dir / "LaunchBox.lnk").write_bytes(
b"L\x00\x00\x00D:\\Gone\\LaunchBox\\Core\\LaunchBox.exe\x00"
)
env = {"APPDATA": str(appdata), "USERPROFILE": str(userprofile)}
with unittest.mock.patch.dict(os.environ, env):
self.assertEqual(install.launchbox_root("windows"), root)
def test_falls_back_to_userprofile(self):
userprofile = scratch_dir(self)
root = self._make_root(userprofile / "LaunchBox")
env = {"APPDATA": str(scratch_dir(self)), "USERPROFILE": str(userprofile)}
with unittest.mock.patch.dict(os.environ, env):
self.assertEqual(install.launchbox_root("windows"), root)
def test_absent_on_linux(self):
self.assertIsNone(install.launchbox_root("linux"))
def test_nothing_installed(self):
env = {
"APPDATA": str(scratch_dir(self)),
"USERPROFILE": str(scratch_dir(self)),
}
with unittest.mock.patch.dict(os.environ, env):
self.assertIsNone(install.launchbox_root("windows"))
class TestLaunchboxBiosDirs(unittest.TestCase):
"""LaunchBox computes where each emulator it manages keeps its BIOS."""
def _make(self, entries: list[tuple[str, str]]) -> Path:
root = scratch_dir(self) / "LaunchBox"
(root / "Data").mkdir(parents=True)
body = "".join(
" <Emulator>\n"
f" <Title>{title}</Title>\n"
f" <ApplicationPath>{app}</ApplicationPath>\n"
" </Emulator>\n"
for title, app in entries
)
(root / "Data" / "Emulators.xml").write_text(
"<?xml version=\"1.0\" standalone=\"yes\"?>\n"
f"<LaunchBox>\n{body}</LaunchBox>\n"
)
for _, app in entries:
(root / Path(app.replace("\\", "/")).parent).mkdir(parents=True, exist_ok=True)
return root
def test_pcsx2_portable_default_bios(self):
root = self._make([("PCSX2", "Emulators\\PCSX2\\pcsx2.exe")])
emu = root / "Emulators" / "PCSX2"
(emu / "portable.ini").write_text("")
(emu / "inis").mkdir()
(emu / "inis" / "PCSX2.ini").write_text("[Folders]\nBios = bios\n")
self.assertEqual(install.launchbox_bios_dirs(root)["pcsx2"], emu / "bios")
def test_pcsx2_portable_absolute_bios(self):
root = self._make([("PCSX2", "Emulators\\PCSX2\\pcsx2.exe")])
emu = root / "Emulators" / "PCSX2"
elsewhere = scratch_dir(self) / "ps2bios"
(emu / "portable.ini").write_text("")
(emu / "inis").mkdir()
(emu / "inis" / "PCSX2.ini").write_text(f"Bios = {elsewhere}\n")
self.assertEqual(install.launchbox_bios_dirs(root)["pcsx2"], elsewhere)
def test_pcsx2_portable_txt_also_triggers_portable_mode(self):
root = self._make([("PCSX2", "Emulators\\PCSX2\\pcsx2.exe")])
emu = root / "Emulators" / "PCSX2"
(emu / "portable.txt").write_text("")
self.assertEqual(install.launchbox_bios_dirs(root)["pcsx2"], emu / "bios")
def test_pcsx2_portable_txt_names_a_data_root(self):
root = self._make([("PCSX2", "Emulators\\PCSX2\\pcsx2.exe")])
emu = root / "Emulators" / "PCSX2"
(emu / "portable.txt").write_text("mydata\n")
self.assertEqual(
install.launchbox_bios_dirs(root)["pcsx2"], emu / "mydata" / "bios"
)
def test_pcsx2_without_config_uses_documents(self):
root = self._make([("PCSX2", "Emulators\\PCSX2\\pcsx2.exe")])
userprofile = scratch_dir(self)
with unittest.mock.patch.dict(os.environ, {"USERPROFILE": str(userprofile)}):
self.assertEqual(
install.launchbox_bios_dirs(root)["pcsx2"],
userprofile / "Documents" / "PCSX2" / "bios",
)
def test_xemu_defaults_to_bios_folder(self):
root = self._make([("Xemu", "Emulators\\Xemu\\xemu.exe")])
emu = root / "Emulators" / "Xemu"
with unittest.mock.patch.dict(os.environ, {"APPDATA": str(scratch_dir(self))}):
self.assertEqual(install.launchbox_bios_dirs(root)["xemu"], emu / "bios")
def test_xemu_honours_configured_bootrom(self):
root = self._make([("Xemu", "Emulators\\Xemu\\xemu.exe")])
emu = root / "Emulators" / "Xemu"
configured = scratch_dir(self) / "xbox"
configured.mkdir()
(configured / "mcpx_1.0.bin").write_text("x")
(emu / "xemu.toml").write_text(
"[sys.files]\n"
f"bootrom_path = '{configured / 'mcpx_1.0.bin'}'\n"
)
self.assertEqual(install.launchbox_bios_dirs(root)["xemu"], configured)
def test_dolphin_portable_user_folder(self):
root = self._make([("Dolphin", "Emulators\\Dolphin\\Dolphin.exe")])
emu = root / "Emulators" / "Dolphin"
(emu / "portable.txt").write_text("")
self.assertEqual(install.launchbox_bios_dirs(root)["dolphin"], emu / "User")
def test_dolphin_without_portable_marker_is_absent(self):
root = self._make([("Dolphin", "Emulators\\Dolphin\\Dolphin.exe")])
(root / "Emulators" / "Dolphin" / "User").mkdir()
self.assertNotIn("dolphin", install.launchbox_bios_dirs(root))
def test_unknown_emulators_are_ignored(self):
root = self._make([("Cemu", "Emulators\\Cemu\\Cemu.exe")])
self.assertEqual(install.launchbox_bios_dirs(root), {})
def test_doctype_is_rejected(self):
root = scratch_dir(self) / "LaunchBox"
(root / "Data").mkdir(parents=True)
(root / "Data" / "Emulators.xml").write_text(
"<!DOCTYPE LaunchBox [<!ENTITY a 'x'>]>\n<LaunchBox></LaunchBox>\n"
)
self.assertEqual(install.launchbox_emulators(root / "Data" / "Emulators.xml"), {})
class RetroArchIsFoundWhereWindowsInstallsPutIt(unittest.TestCase):
"""Program Files\\RetroArch-Win64, a Steam library on another drive and
an installed ES-DE's Emulators folder all held a RetroArch the one-liner
reported as "no supported platform detected"."""
def _env(self, program_files: Path) -> None:
patcher = unittest.mock.patch.dict(
os.environ, {"ProgramFiles": str(program_files), "ProgramFiles(x86)": ""}
)
patcher.start()
self.addCleanup(patcher.stop)
@staticmethod
def _exe(folder: Path) -> Path:
folder.mkdir(parents=True)
(folder / "retroarch.exe").write_bytes(b"MZ")
return folder
def test_program_files_win64(self):
program_files = scratch_dir(self)
self._env(program_files)
found = self._exe(program_files / "RetroArch-Win64")
self.assertIn(found, install._windows_retroarch_dirs())
def test_a_scoop_install(self):
# `scoop install retroarch` puts it under ~/scoop/apps/retroarch/current,
# which none of the searched folders covered.
program_files = scratch_dir(self)
self._env(program_files)
home = scratch_dir(self)
found = self._exe(home / "scoop" / "apps" / "retroarch" / "current")
with unittest.mock.patch.dict(os.environ, {"USERPROFILE": str(home)}):
os.environ.pop("SCOOP", None)
self.assertIn(found, install._windows_retroarch_dirs())
def test_a_steam_library_on_another_drive(self):
program_files = scratch_dir(self)
library = scratch_dir(self)
steamapps = program_files / "Steam" / "steamapps"
steamapps.mkdir(parents=True)
(steamapps / "libraryfolders.vdf").write_text(
'"libraryfolders"\n{\n\t"1"\n\t{\n\t\t"path"\t\t"' + str(library) + '"\n\t}\n}\n'
)
self._env(program_files)
found = self._exe(library / "steamapps" / "common" / "RetroArch")
self.assertIn(found, install._windows_retroarch_dirs())
def test_an_installed_es_de(self):
program_files = scratch_dir(self)
self._env(program_files)
found = self._exe(program_files / "ES-DE" / "Emulators" / "RetroArch-Win64")
self.assertIn(found, install._windows_retroarch_dirs())
class TestSteamRetroarchDetection(unittest.TestCase):
"""The Steam build is a portable install under the default library."""
def test_steam_library_is_found_on_windows(self):
program_files = scratch_dir(self)
ra_dir = program_files / "Steam" / "steamapps" / "common" / "RetroArch"
ra_dir.mkdir(parents=True)
(ra_dir / "retroarch.cfg").write_text('system_directory = ":\\system"\n')
env = {"ProgramFiles(x86)": str(program_files)}
with unittest.mock.patch.dict(os.environ, env, clear=True):
found = install.detect_platforms("windows")
self.assertIn(("retroarch", ra_dir / "system"), found)
class TestLaunchboxDetectionPowershell(unittest.TestCase):
"""install.ps1 must resolve LaunchBox's portable RetroArch on its own."""
def _run(self, entries: str, seed=None, manifest: dict | None = None):
userprofile = scratch_dir(self)
ra_dir = userprofile / "LaunchBox" / "Emulators" / "RetroArch"
ra_dir.mkdir(parents=True)
data = userprofile / "LaunchBox" / "Data"
data.mkdir(parents=True)
(data / "Emulators.xml").write_text(
"<?xml version=\"1.0\" standalone=\"yes\"?>\n"
"<LaunchBox>\n"
f"{entries}"
"</LaunchBox>\n"
)
if seed is not None:
seed(userprofile)
serve_root = scratch_dir(self)
(serve_root / "install").mkdir()
payload = dict(manifest if manifest is not None else {"files": []})
payload.setdefault("manifest_version", 2)
payload.setdefault("platform", "retroarch")
payload.setdefault("files", [])
(serve_root / "install" / "retroarch.json").write_text(json.dumps(payload))
handler = functools.partial(
http.server.SimpleHTTPRequestHandler, directory=str(serve_root)
)
httpd = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
threading.Thread(target=httpd.serve_forever, daemon=True).start()
try:
env = dict(os.environ)
env.update(
USERPROFILE=str(userprofile),
APPDATA=str(scratch_dir(self)),
RETROBIOS_BASE_URL=f"http://127.0.0.1:{httpd.server_address[1]}",
# A LaunchBox layout is a Windows layout: the runner is Linux.
RETROBIOS_OS="wsl",
)
proc = subprocess.run(
[
"pwsh", "-NoProfile", "-File",
str(REPO_ROOT / "install.ps1"), "--standalone-copies", "--verbose",
],
env=env,
capture_output=True,
text=True,
timeout=60,
check=False,
)
finally:
httpd.shutdown()
httpd.server_close()
return proc, ra_dir
def _assert_resolved(self, proc, ra_dir):
self.assertIn("Found LaunchBox", proc.stdout)
self.assertIn("Found RetroArch at", proc.stdout)
self.assertIn(str(ra_dir), proc.stdout)
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
self.assertIn("Done.", proc.stdout)
def test_detects_launchbox_retroarch(self):
proc, ra_dir = self._run(
" <Emulator>\n"
" <Title>Retroarch</Title>\n"
" <ApplicationPath>Emulators\\RetroArch\\retroarch.exe</ApplicationPath>\n"
" </Emulator>\n"
)
self._assert_resolved(proc, ra_dir)
def test_stale_entry_before_installed_one(self):
proc, ra_dir = self._run(
" <Emulator>\n"
" <Title>RetroArch (old)</Title>\n"
" <ApplicationPath>Emulators\\RetroArch-old\\retroarch.exe</ApplicationPath>\n"
" </Emulator>\n"
" <Emulator>\n"
" <Title>RetroArch</Title>\n"
" <ApplicationPath>Emulators\\RetroArch\\retroarch.exe</ApplicationPath>\n"
" </Emulator>\n"
)
self._assert_resolved(proc, ra_dir)
def test_system_directory_from_config(self):
def seed(root: Path) -> None:
ra = root / "LaunchBox" / "Emulators" / "RetroArch"
(ra / "bios").mkdir(parents=True)
(ra / "retroarch.cfg").write_text('system_directory = ":\\bios"\n')
proc, ra_dir = self._run(
" <Emulator>\n"
" <Title>RetroArch</Title>\n"
" <ApplicationPath>Emulators\\RetroArch\\retroarch.exe</ApplicationPath>\n"
" </Emulator>\n",
seed=seed,
)
self.assertIn("Found LaunchBox", proc.stdout)
self.assertIn("Found RetroArch at", proc.stdout)
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
self.assertIn(str(ra_dir / "bios"), proc.stdout)
def test_standalone_copy_reaches_launchbox_emulator(self):
def seed(root: Path) -> None:
system = root / "LaunchBox" / "Emulators" / "RetroArch" / "system"
system.mkdir(parents=True)
(system / "ps2-0230a.bin").write_text("rom")
pcsx2 = root / "LaunchBox" / "Emulators" / "PCSX2"
(pcsx2 / "bios").mkdir(parents=True)
(pcsx2 / "portable.ini").write_text("")
(pcsx2 / "inis").mkdir()
(pcsx2 / "inis" / "PCSX2.ini").write_text("[Folders]\nBios = bios\n")
# In place with its hash: only a file the run verified is copied.
manifest = {
"files": [{"dest": "ps2-0230a.bin", "size": 3, "repo_path": "bios/ps2-0230a.bin",
"sha1": "a1e17a20e93e5da710685444df3ad038ac66e2c9"}],
"standalone_copies": [
{
"pattern": "ps2-*.bin",
"emulator": "pcsx2",
"targets": {"windows": []},
}
],
}
proc, ra_dir = self._run(
" <Emulator>\n"
" <Title>RetroArch</Title>\n"
" <ApplicationPath>Emulators\\RetroArch\\retroarch.exe</ApplicationPath>\n"
" </Emulator>\n"
" <Emulator>\n"
" <Title>PCSX2</Title>\n"
" <ApplicationPath>Emulators\\PCSX2\\pcsx2.exe</ApplicationPath>\n"
" </Emulator>\n",
seed=seed,
manifest=manifest,
)
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
landed = ra_dir.parent / "PCSX2" / "bios" / "ps2-0230a.bin"
self.assertTrue(landed.exists(), proc.stdout)
class TestForcedOs(unittest.TestCase):
"""RETROBIOS_OS overrides detection with a known name only."""
def test_known_name_wins(self):
with unittest.mock.patch.dict(os.environ, {"RETROBIOS_OS": "Windows"}):
self.assertEqual(install.detect_os(), "windows")
def test_unknown_name_is_ignored(self):
with unittest.mock.patch.dict(os.environ, {"RETROBIOS_OS": "amiga"}):
self.assertIn(install.detect_os(), ("linux", "wsl", "windows", "darwin"))
class TestDetectFrontends(unittest.TestCase):
"""Frontends have no BIOS directory of their own but hint at the setup."""
def test_esde_from_home_directory(self):
home = scratch_dir(self)
(home / "ES-DE").mkdir()
old = os.environ.pop("ESDE_APPDATA_DIR", None)
try:
self.assertIn("esde", install.detect_frontends("linux", home=home))
finally:
if old is not None:
os.environ["ESDE_APPDATA_DIR"] = old
def test_esde_from_env_dir(self):
home = scratch_dir(self)
appdata = scratch_dir(self)
os.environ["ESDE_APPDATA_DIR"] = str(appdata)
try:
self.assertIn("esde", install.detect_frontends("linux", home=home))
finally:
del os.environ["ESDE_APPDATA_DIR"]
def test_nothing_detected(self):
home = scratch_dir(self)
old = os.environ.pop("ESDE_APPDATA_DIR", None)
try:
self.assertEqual(install.detect_frontends("linux", home=home), [])
finally:
if old is not None:
os.environ["ESDE_APPDATA_DIR"] = old
class RetroArchWhereItReallyLives(unittest.TestCase):
"""Windows: retroarch.cfg sits beside retroarch.exe and system defaults
to <exe folder>\\system. Only %APPDATA% was read, and the fallback was a
folder RetroArch never reads. macOS: the config is under
Application Support/RetroArch/config, the system folder in Documents."""
def _exe_dir(self) -> tuple[Path, dict]:
program_files = scratch_dir(self)
exe_dir = program_files / "RetroArch"
exe_dir.mkdir()
(exe_dir / "retroarch.exe").write_bytes(b"MZ")
return exe_dir, {"ProgramFiles": str(program_files)}
def test_windows_install_folder_without_a_config(self):
exe_dir, env = self._exe_dir()
with unittest.mock.patch.dict(os.environ, env, clear=True):
found = install.detect_platforms("windows")
self.assertIn(("retroarch", exe_dir / "system"), found)
def test_windows_appdata_config_at_default_names_the_exe_folder(self):
exe_dir, env = self._exe_dir()
appdata = scratch_dir(self)
(appdata / "RetroArch").mkdir()
(appdata / "RetroArch" / "retroarch.cfg").write_text('system_directory = "default"\n')
env["APPDATA"] = str(appdata)
with unittest.mock.patch.dict(os.environ, env, clear=True):
found = install.detect_platforms("windows")
self.assertIn(("retroarch", exe_dir / "system"), found)
def test_windows_config_beside_the_exe_wins(self):
exe_dir, env = self._exe_dir()
(exe_dir / "retroarch.cfg").write_text('system_directory = "D:\\bios"\n')
with unittest.mock.patch.dict(os.environ, env, clear=True):
found = install.detect_platforms("windows")
self.assertIn(("retroarch", Path("D:\\bios")), found)
def test_macos_config_folder_and_documents_default(self):
home = scratch_dir(self)
support = home / "Library" / "Application Support" / "RetroArch"
(support / "config").mkdir(parents=True)
(support / "config" / "retroarch.cfg").write_text('system_directory = "default"\n')
with unittest.mock.patch.object(install.Path, "home", return_value=home):
found = install.detect_platforms("darwin")
self.assertIn(("retroarch", home / "Documents" / "RetroArch" / "system"), found)
(support / "portable.txt").write_text("")
found = install.detect_platforms("darwin")
self.assertIn(("retroarch", support / "system"), found)
class LinuxRetroArchFollowsXdgConfigHome(unittest.TestCase):
"""RetroArch on Linux keeps its config under $XDG_CONFIG_HOME/retroarch
when the variable is set, then falls back to ~/.retroarch.cfg
(file_path_special.c fill_pathname_application_data, configuration.c
open_default_config_file). Only ~/.config/retroarch was read: a home
with its own XDG layout detected nothing."""
def _env(self) -> tuple[Path, Path]:
home = scratch_dir(self)
xdg = scratch_dir(self)
patcher = unittest.mock.patch.dict(os.environ, {"HOME": str(home)}, clear=True)
patcher.start()
self.addCleanup(patcher.stop)
return home, xdg
def test_the_config_under_xdg_config_home_is_read(self):
home, xdg = self._env()
(xdg / "retroarch").mkdir()
(xdg / "retroarch" / "retroarch.cfg").write_text('system_directory = "/srv/bios"\n')
# A config under ~/.config is not the one this RetroArch opens.
(home / ".config" / "retroarch").mkdir(parents=True)
(home / ".config" / "retroarch" / "retroarch.cfg").write_text(
'system_directory = "/elsewhere"\n')
os.environ["XDG_CONFIG_HOME"] = str(xdg)
found = install.detect_platforms("linux")
self.assertIn(("retroarch", Path("/srv/bios")), found)
self.assertNotIn(("retroarch", Path("/elsewhere")), found)
self.assertEqual(install._os_default_dests("linux")["retroarch"],
xdg / "retroarch" / "system")
def test_home_retroarch_cfg_is_the_fallback(self):
home, xdg = self._env()
os.environ["XDG_CONFIG_HOME"] = str(xdg)
(home / ".retroarch.cfg").write_text('system_directory = "default"\n')
found = install.detect_platforms("linux")
self.assertIn(("retroarch", xdg / "retroarch" / "system"), found)
def test_without_the_variable_the_folder_is_under_dot_config(self):
home, _ = self._env()
(home / ".config" / "retroarch").mkdir(parents=True)
(home / ".config" / "retroarch" / "retroarch.cfg").write_text(
'system_directory = "default"\n')
found = install.detect_platforms("linux")
self.assertIn(("retroarch", home / ".config" / "retroarch" / "system"), found)
class RetroArchUwpIsFound(unittest.TestCase):
"""The UWP/MSIX RetroArch keeps retroarch.cfg and system/ in its
package's LocalState, but no rule looked under %LOCALAPPDATA%\\Packages."""
def _state(self) -> Path:
local = scratch_dir(self)
state = local / "Packages" / f"{install.RETROARCH_UWP_ID}_abc123" / "LocalState"
state.mkdir(parents=True)
patcher = unittest.mock.patch.dict(os.environ, {"LOCALAPPDATA": str(local)})
patcher.start()
self.addCleanup(patcher.stop)
return state
def test_the_default_is_local_state_system(self):
state = self._state()
self.assertEqual(install._retroarch_uwp_system_dir(), state / "system")
def test_a_tilde_is_local_state(self):
state = self._state()
(state / "retroarch.cfg").write_text('system_directory = "~\\bios"\n')
self.assertEqual(install._retroarch_uwp_system_dir(), state / "bios")
def test_nothing_installed(self):
with unittest.mock.patch.dict(os.environ, {"LOCALAPPDATA": str(scratch_dir(self))}):
self.assertIsNone(install._retroarch_uwp_system_dir())
class RetroArchAppImageIsPortable(unittest.TestCase):
"""The buildbot's Linux RetroArch is an AppImage beside a .home folder
the AppImage runtime makes its HOME: the run found nothing and offered
~/.config/retroarch/system, a folder this RetroArch never reads."""
def _detect(self, home: Path, env: dict | None = None) -> list:
cleared = {"HOME": str(home), "XDG_CONFIG_HOME": ""}
with unittest.mock.patch.dict(os.environ, {**cleared, **(env or {})}), \
unittest.mock.patch.object(Path, "home", return_value=home):
os.environ.pop("XDG_CONFIG_HOME")
return install.detect_platforms("linux")
def _unpack(self, home: Path) -> Path:
folder = home / "Downloads" / "RetroArch-Linux-x86_64"
base = folder / "RetroArch-Linux-x86_64.AppImage.home" / ".config" / "retroarch"
(base / "system").mkdir(parents=True)
(folder / "RetroArch-Linux-x86_64.AppImage").write_bytes(b"\x7fELF")
return base
def test_the_unpacked_archive_is_found(self):
home = scratch_dir(self)
base = self._unpack(home)
self.assertIn(("retroarch", base / "system"), self._detect(home))
def test_its_config_is_read_with_its_own_home(self):
home = scratch_dir(self)
base = self._unpack(home)
(base / "retroarch.cfg").write_text('system_directory = "~/bios"\n')
portable = base.parent.parent
self.assertIn(("retroarch", portable / "bios"), self._detect(home))
def test_a_config_folder_beside_it_wins(self):
home = scratch_dir(self)
base = self._unpack(home)
config = base.parent.parent.with_name("RetroArch-Linux-x86_64.AppImage.config")
(config / "retroarch").mkdir(parents=True)
found = self._detect(home)
self.assertIn(("retroarch", config / "retroarch" / "system"), found)
self.assertNotIn(("retroarch", base / "system"), found)
def test_a_folder_without_its_appimage_is_left(self):
home = scratch_dir(self)
base = self._unpack(home)
(base.parent.parent.with_name("RetroArch-Linux-x86_64.AppImage")).unlink()
self.assertNotIn(("retroarch", base / "system"), self._detect(home))
class SteamRetroArchOnMacIsPortable(unittest.TestCase):
"""platform_darwin.m makes the Steam build portable (HAVE_STEAM) with no
portable.txt: a default system_directory is Application Support's, and
the installer took ~/Documents/RetroArch/system."""
def test_the_steam_bundle_moves_the_base(self):
home = scratch_dir(self)
support = home / "Library" / "Application Support"
with unittest.mock.patch.dict(os.environ, {"HOME": str(home)}), \
unittest.mock.patch.object(Path, "home", return_value=home):
self.assertEqual(install._macos_retroarch_base(), home / "Documents" / "RetroArch")
(support / "Steam/steamapps/common/RetroArch/RetroArch.app").mkdir(parents=True)
self.assertEqual(install._macos_retroarch_base(), support / "RetroArch")
if __name__ == "__main__":
unittest.main()
+528
View File
@@ -0,0 +1,528 @@
"""install.py installs the data directories a pack carries, as the pack builder lays them out."""
from __future__ import annotations
import tests
from tests import scratch_dir
import hashlib
import io
import json
import os
import shutil
import unittest
import unittest.mock
import zipfile
from pathlib import Path
from tests.netguard import module_guard
from tests.installsupport import REPO_ROOT, install
# No connection off the loopback and no card of the host reaches a test:
# the module fails at its end otherwise.
setUpModule, tearDownModule = module_guard("test_install_data_trees")
class DataDirectoriesAreInstalled(unittest.TestCase):
"""The README said the installer does what the pack does, but for RetroArch
it left out 2,640 data files (Dolphin's Sys, PPSSPP's assets) and
--check still said "All files up to date"."""
def _zip(self, root: Path) -> Path:
import zipfile
archive = root / "Dolphin.zip"
with zipfile.ZipFile(archive, "w") as zf:
zf.writestr("Dolphin/Sys/GC/font.bin", b"font")
zf.writestr("Dolphin/Sys/GC/dsp.bin", b"new bytes upstream")
zf.writestr("Dolphin/Themes/x.png", b"theme")
return archive
def _entry(self) -> dict:
return {
"ref": "dolphin-sys", "dest": "dolphin-emu", "url": "https://example.invalid/D.zip",
"type": "zip", "strip_components": 1, "exclude": ["Themes"],
"systems": ["nintendo-gamecube"],
"files": {
"Sys/GC/font.bin": {"sha1": hashlib.sha1(b"font").hexdigest(), "size": 4},
"Sys/GC/dsp.bin": {"sha1": hashlib.sha1(b"old").hexdigest(), "size": 3},
"Sys/GC/gone.bin": {"sha1": "0" * 40, "size": 1},
},
}
def test_required_only_leaves_out_an_optional_tree(self):
"""A GBA and a SNES game with --for-games --required-only took
FBNeo's cheats and samples, 3,472 files no game opened."""
cheats = {**self._entry(), "ref": "fbneo-cheats", "systems": ["snes"], "optional": True}
sys_tree = {**self._entry(), "systems": ["snes"]}
manifest = {"data_directories": [cheats, sys_tree]}
files = [{"dest": "a.rom", "systems": ["snes"]}]
self.assertEqual(install.select_data_directories(manifest, files, True), [cheats, sys_tree])
self.assertEqual(install.select_data_directories(manifest, files, True, True), [sys_tree])
self.assertEqual(install.select_data_directories(manifest, files, False, True), [sys_tree])
install._check_data_directory(cheats, "data_directories[0]")
with self.assertRaises(ValueError):
install._check_data_directory({**cheats, "optional": False}, "data_directories[0]")
def test_the_archive_is_drawn_and_counted(self):
"""82 MB of archives came with no progress line, and the run ended
on "310.5 KB downloaded", the BIOS files alone."""
work = scratch_dir(self)
body = self._zip(work).read_bytes()
attempts: list[int] = []
class Response(io.BytesIO):
headers = {"Content-Length": str(len(body))}
def __enter__(self):
return self
def __exit__(self, *exc):
return False
def urlopen(_url, timeout=None):
attempts.append(1)
if len(attempts) == 1:
class Cut(Response):
def read(self, size=-1):
if self.tell():
raise ConnectionResetError("cut")
return super().read(5)
return Cut(body)
return Response(body)
progress = install.Progress(1, 0, install.Terminal(io.StringIO()), events=False)
with unittest.mock.patch.object(install.urllib.request, "urlopen", urlopen), \
unittest.mock.patch.object(install, "_network_allowed", lambda url: None), \
unittest.mock.patch.object(install, "_retry_delay", lambda *a: 0), \
unittest.mock.patch.object(install.EVENTS, "emit") as emit:
path = install._fetch_archive("https://example.invalid/D.zip", work, progress)
self.assertEqual(path.read_bytes(), body)
self.assertEqual(len(attempts), 2)
# The cut attempt's five bytes are taken back.
self.assertEqual((progress.transferred(), progress.expected_bytes),
(len(body), len(body)))
emit.assert_not_called()
def test_a_cut_archive_resumes_where_it_stopped(self):
"""A data archive cut at 90% was deleted and fetched again from
zero, on the retry and on the next run, while the run said that
nothing was lost."""
work = scratch_dir(self)
body = self._zip(work).read_bytes()
keep = work / ".dolphin-sys.archive.part"
requests: list[dict] = []
class Response(io.BytesIO):
def __init__(self, data, status, headers):
super().__init__(data)
self.status, self.headers = status, headers
def __enter__(self):
return self
def __exit__(self, *exc):
return False
def urlopen(request, timeout=None):
sent = dict(request.header_items())
requests.append(sent)
if "Range" in sent:
start = int(sent["Range"][len("bytes="):-1])
if sent.get("If-range") != '"v1"':
return Response(body, 200, {"ETag": '"v2"'})
return Response(body[start:], 206, {
"Content-Range": f"bytes {start}-{len(body) - 1}/{len(body)}",
"Content-Length": str(len(body) - start), "ETag": '"v1"'})
cut = len(body) * 9 // 10
class Cut(Response):
def read(self, size=-1):
if self.tell() >= cut:
raise ConnectionResetError("cut")
return super().read(min(size, cut - self.tell()))
return Cut(body, 200, {"Content-Length": str(len(body)), "ETag": '"v1"'})
with unittest.mock.patch.object(install.urllib.request, "urlopen", urlopen), \
unittest.mock.patch.object(install, "_network_allowed", lambda url: None), \
unittest.mock.patch.object(install, "_retry_delay", lambda *a: 0):
# The next run, after a stop: what the cut attempt wrote is kept.
with unittest.mock.patch.object(install, "MAX_RETRIES", 1), \
self.assertRaises(ConnectionResetError):
install._fetch_archive("https://example.invalid/D.zip", work, None, keep)
self.assertEqual(keep.stat().st_size, len(body) * 9 // 10)
path = install._fetch_archive("https://example.invalid/D.zip", work, None, keep)
self.assertEqual(path.read_bytes(), body)
self.assertEqual(requests[-1]["Range"], f"bytes={len(body) * 9 // 10}-")
self.assertEqual(requests[-1]["If-range"], '"v1"')
def test_a_changed_archive_is_fetched_whole(self):
work = scratch_dir(self)
body = self._zip(work).read_bytes()
keep = work / ".dolphin-sys.archive.part"
keep.write_bytes(b"older revision")
keep.with_name(keep.name + ".source").write_text(
'https://example.invalid/D.zip\n"v0"\n')
class Whole(io.BytesIO):
status, headers = 200, {"ETag": '"v1"'}
def __enter__(self):
return self
def __exit__(self, *exc):
return False
with unittest.mock.patch.object(install.urllib.request, "urlopen",
lambda request, timeout=None: Whole(body)), \
unittest.mock.patch.object(install, "_network_allowed", lambda url: None):
path = install._fetch_archive("https://example.invalid/D.zip", work, None, keep)
self.assertEqual(path.read_bytes(), body)
def test_a_tree_read_whole_leaves_no_archive(self):
work = scratch_dir(self)
archive = self._zip(work)
bios = work / "system"
bios.mkdir()
def fetch(url, directory, progress=None, keep=None):
shutil.copy(archive, keep)
keep.with_name(keep.name + ".source").write_text(f"{url}\n\"v1\"\n")
return keep
with unittest.mock.patch.object(install, "_fetch_archive", fetch):
install.install_data_directory(self._entry(), bios)
self.assertEqual([p.name for p in (bios / "dolphin-emu").iterdir()], ["Sys"])
def test_a_second_run_leaves_the_kept_archive_alone(self):
"""Two runs into one BIOS folder shared .dolphin-sys.archive.part: the
one that finished first deleted the archive the other was still
writing, and the other truncated the one being read."""
work = scratch_dir(self)
archive = self._zip(work)
bios = work / "system"
root = bios / "dolphin-emu"
root.mkdir(parents=True)
other = root / ".dolphin-sys.archive.part"
other.write_bytes(b"another run is writing this")
kept: list = []
def fetch(url, directory, progress=None, keep=None):
kept.append(keep)
copy = Path(directory) / "private.zip"
shutil.copy(archive, copy)
return copy
held = install._claim_partial(root / ".dolphin-sys.archive.lock")
self.addCleanup(held.close)
with unittest.mock.patch.object(install, "_fetch_archive", fetch):
counts = install.install_data_directory(self._entry(), bios)
self.assertEqual(kept, [None])
self.assertEqual(counts["installed"], 1)
self.assertEqual(other.read_bytes(), b"another run is writing this")
self.assertTrue((root / ".dolphin-sys.archive.lock").exists())
def test_a_lone_run_keeps_its_archive_beside_the_tree(self):
work = scratch_dir(self)
archive = self._zip(work)
bios = work / "system"
kept: list = []
def fetch(url, directory, progress=None, keep=None):
kept.append(keep)
shutil.copy(archive, keep)
return keep
with unittest.mock.patch.object(install, "_fetch_archive", fetch):
install.install_data_directory(self._entry(), bios)
self.assertEqual(kept, [bios / "dolphin-emu" / ".dolphin-sys.archive.part"])
self.assertEqual(sorted(p.name for p in (bios / "dolphin-emu").iterdir()), ["Sys"])
def test_the_run_ends_on_the_archive_bytes_too(self):
from tests.test_install_selection import FakeRepository
member = os.urandom(300_000)
with FakeRepository() as repo:
with zipfile.ZipFile(repo.root / "tree.zip", "w") as zf:
zf.writestr("Sys/big.bin", member)
archive = (repo.root / "tree.zip").stat().st_size
listed = json.loads((repo.root / "install" / "retroarch.json").read_text())
listed["data_directories"] = [{
"ref": "tree", "dest": "tree", "type": "zip",
"url": f"http://127.0.0.1:{repo.httpd.server_address[1]}/tree.zip",
"systems": ["nintendo-gba"],
"files": {"Sys/big.bin": {"sha1": hashlib.sha1(member).hexdigest(),
"size": len(member)}}}]
(repo.root / "install" / "retroarch.json").write_text(json.dumps(listed))
proc = repo.run("--no-input", "--verbose")
bios = sum(len(payload) for payload, *_ in FakeRepository.PAYLOADS.values())
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
self.assertIn(f"{install.format_size(bios + archive)} downloaded in", proc.stdout)
def test_the_plan_announces_the_archive_not_the_tree(self):
"""yaps2's tarball is 21.2 MB for a 6.5 MB tree, Cemu's 21.4 MB for
9.9 KB: the plan announced the tree, and the run downloaded far more."""
from tests.test_install_selection import FakeRepository
member = b"profile"
archive_size = 21_360_579
with FakeRepository() as repo:
listed = json.loads((repo.root / "install" / "retroarch.json").read_text())
listed["data_directories"] = [{
"ref": "cemu-gameprofiles", "dest": "cemu", "type": "tarball",
"url": "https://example.invalid/Cemu.tar.gz", "source_path": "Cemu-1/bin",
"systems": ["nintendo-gba"], "archive_size": archive_size,
"files": {"gameProfiles/a.ini": {"sha1": hashlib.sha1(member).hexdigest(),
"size": len(member)}}}]
(repo.root / "install" / "retroarch.json").write_text(json.dumps(listed))
proc = repo.run("--no-input", "--dry-run")
bios = sum(len(payload) for payload, *_ in FakeRepository.PAYLOADS.values())
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
self.assertIn(f"({install.format_size(bios + archive_size)})", proc.stdout)
self.assertIn(f"(a {install.format_size(archive_size)} archive)", proc.stdout)
def test_a_pack_or_an_old_list_counts_the_files_themselves(self):
needed = {"a": {"size": 7}}
entry = {"archive_size": 21_360_579}
self.assertEqual(install.data_fetch_bytes(entry, needed), 21_360_579)
self.assertEqual(install.data_fetch_bytes({}, needed), 7)
self.assertEqual(install.data_fetch_bytes(entry, {}), 0)
with unittest.mock.patch.object(install.LOCAL, "pack", object()):
self.assertEqual(install.data_fetch_bytes(entry, needed), 7)
def test_verified_members_land_the_rest_are_said(self):
work = scratch_dir(self)
archive = self._zip(work)
bios = work / "system"
bios.mkdir()
def fetch(url, directory, progress=None, keep=None):
copy = Path(directory) / "a.zip"
shutil.copy(archive, copy)
return copy
with unittest.mock.patch.object(install, "_fetch_archive", fetch):
counts = install.install_data_directory(self._entry(), bios)
again = install.install_data_directory(self._entry(), bios)
self.assertEqual((counts["installed"], counts["changed"], counts["absent"]), (1, 1, 1))
self.assertEqual((bios / "dolphin-emu" / "Sys" / "GC" / "font.bin").read_bytes(), b"font")
self.assertFalse((bios / "dolphin-emu" / "Sys" / "GC" / "dsp.bin").exists())
self.assertFalse((bios / "dolphin-emu" / "Themes").exists())
self.assertEqual(again["present"], 1)
self.assertEqual(sorted(p.name for p in (bios / "dolphin-emu").iterdir()), ["Sys"])
def _served(self, archive: Path):
def fetch(url, directory, progress=None, keep=None):
copy = Path(directory) / "a.zip"
shutil.copy(archive, copy)
return copy
return fetch
def test_a_rolling_tree_lands_what_its_source_serves(self):
"""The buildbot rebuilt PPSSPP.zip and 49 of its 169 files were
refused on every install: a nightly source holds no other bytes.
RetroArch's own downloader writes what the buildbot serves."""
work = scratch_dir(self)
bios = work / "system"
bios.mkdir()
entry = {**self._entry(), "rolling": True}
with unittest.mock.patch.object(install, "_fetch_archive", self._served(self._zip(work))):
counts = install.install_data_directory(entry, bios)
self.assertEqual((counts["installed"], counts["updated"], counts["changed"],
counts["absent"]), (2, 1, 0, 1))
dsp = bios / "dolphin-emu" / "Sys" / "GC" / "dsp.bin"
self.assertEqual(dsp.read_bytes(), b"new bytes upstream")
self.assertIn({"dest": "dolphin-emu/Sys/GC/dsp.bin",
"sha1": hashlib.sha1(b"new bytes upstream").hexdigest(),
"size": len(b"new bytes upstream")}, counts["written"])
self.assertFalse((bios / "dolphin-emu" / "Themes").exists())
def test_a_rolling_file_an_earlier_run_wrote_is_in_place(self):
"""Its bytes are not the list's, they are what the source served and
the record names: the next run neither fetches nor rewrites it."""
work = scratch_dir(self)
bios = work / "system"
bios.mkdir()
entry = {**self._entry(), "rolling": True}
with unittest.mock.patch.object(install, "_fetch_archive", self._served(self._zip(work))):
counts = install.install_data_directory(entry, bios)
files = [install.record_file(item, "download", "data") for item in counts["written"]]
install.write_record(bios, "retroarch", {}, [], files)
_root, needed, present = install.data_directory_state(entry, bios)
self.assertEqual((sorted(needed), present), (["Sys/GC/gone.bin"], 2))
# Changed on the drive since: neither the list's bytes nor the run's.
(bios / "dolphin-emu" / "Sys" / "GC" / "dsp.bin").write_bytes(b"new bytes upstreaM")
self.assertIn("Sys/GC/dsp.bin", install.data_directory_state(entry, bios)[1])
# A tree listed at a commit takes only the list's bytes.
self.assertIn("Sys/GC/dsp.bin", install.data_directory_state(self._entry(), bios)[1])
def test_a_damaged_rolling_member_is_never_written(self):
"""No hash to hold a followed member to: its ZIP CRC-32 still is."""
import zipfile
work = scratch_dir(self)
archive = self._zip(work)
body = bytearray(archive.read_bytes())
at = body.index(b"new bytes upstream")
body[at] ^= 0x01
archive.write_bytes(bytes(body))
bios = work / "system"
bios.mkdir()
with unittest.mock.patch.object(install, "_fetch_archive", self._served(archive)):
counts = install.install_data_directory({**self._entry(), "rolling": True}, bios)
self.assertTrue(counts["error"])
self.assertFalse((bios / "dolphin-emu" / "Sys" / "GC" / "dsp.bin").exists())
self.assertEqual([p.name for p in (bios / "dolphin-emu" / "Sys" / "GC").iterdir()
if p.name.endswith(".part")], [])
self.assertTrue(zipfile.is_zipfile(archive))
def test_a_pack_rebuilds_a_rolling_tree_as_listed(self):
"""From a pack, the pack's copy is the tree: nothing newer exists."""
work = scratch_dir(self)
bios = work / "system"
bios.mkdir()
def tree(_dest):
yield "Sys/GC/dsp.bin", 18, lambda: io.BytesIO(b"new bytes upstream")
local = unittest.mock.Mock()
local.tree = tree
with unittest.mock.patch.object(install.LOCAL, "pack", local):
counts = install.install_data_directory({**self._entry(), "rolling": True}, bios)
self.assertEqual((counts["installed"], counts["updated"], counts["changed"]), (0, 0, 1))
def test_a_rolling_member_past_its_cap_is_not_written(self):
"""A followed member is read to the size its archive declares: a
source serving gigabytes where the list has 3 bytes fills no drive."""
work = scratch_dir(self)
entry = {**self._entry(), "rolling": True}
served: list[int] = []
def members(_archive, _entry):
for rel, size in (("Sys/GC/dsp.bin", 900 * 1024 * 1024),
("Sys/GC/font.bin", 4)):
yield rel, size, lambda size=size: served.append(size) or io.BytesIO(b"font")
with unittest.mock.patch.object(
install, "_fetch_archive",
lambda url, d, progress=None, keep=None: Path(d) / "a"), \
unittest.mock.patch.object(install, "_archive_members", members):
counts = install.install_data_directory(entry, work)
self.assertEqual((counts["changed"], counts["installed"]), (1, 1))
self.assertEqual(served, [4])
self.assertEqual(install._follow_cap(3), 16 * 1024 * 1024 + 6)
self.assertFalse(install._followable({"size": 3}, 40, [39]))
def test_a_rolling_flag_is_true_or_absent(self):
with self.assertRaisesRegex(ValueError, "rolling is invalid"):
install._check_data_directory({**self._entry(), "rolling": False}, "d")
install._check_data_directory({**self._entry(), "rolling": True}, "d")
def test_a_member_that_inflates_is_cut_at_its_size(self):
# A member whose header claims the recorded size and whose stream
# never ends: reading stops one byte past it.
class Endless(io.RawIOBase):
served = 0
def readable(self):
return True
def readinto(self, buffer):
Endless.served += len(buffer)
buffer[:] = b"x" * len(buffer)
return len(buffer)
work = scratch_dir(self)
entry = self._entry()
entry["files"] = {"Sys/GC/font.bin": {"sha1": "1" * 40, "size": 4}}
def members(_archive, _entry):
yield "Sys/GC/font.bin", 4, lambda: io.BufferedReader(Endless())
with unittest.mock.patch.object(
install, "_fetch_archive",
lambda url, d, progress=None, keep=None: Path(d) / "a"), \
unittest.mock.patch.object(install, "_archive_members", members):
counts = install.install_data_directory(entry, work)
self.assertEqual(counts["changed"], 1)
self.assertLess(Endless.served, 64 * 1024)
def test_a_damaged_member_is_a_reason_and_leaves_no_part(self):
"""zlib.error and EOFError from a damaged member ended the run on a
traceback, and a failed write left its .part beside the tree."""
import zlib
work = scratch_dir(self)
entry = self._entry()
entry["files"] = {"Sys/GC/font.bin": {"sha1": "1" * 40, "size": 4}}
for error in (zlib.error("invalid stored block lengths"), EOFError(),
OSError(28, "No space left on device")):
with self.subTest(error=type(error).__name__):
class Broken(io.RawIOBase):
def readable(self):
return True
def readinto(self, buffer, error=error):
raise error
def members(_archive, _entry):
yield "Sys/GC/font.bin", 4, lambda: io.BufferedReader(Broken())
with unittest.mock.patch.object(
install, "_fetch_archive",
lambda url, d, progress=None, keep=None: Path(d) / "a"), \
unittest.mock.patch.object(install, "_archive_members", members):
counts = install.install_data_directory(entry, work)
self.assertTrue(counts["error"])
self.assertEqual(counts["installed"], 0)
self.assertEqual([p.name for p in work.rglob("*.part")], [])
def test_offline_keeps_its_reason(self):
work = scratch_dir(self)
with unittest.mock.patch.dict(os.environ, {install.OFFLINE_VARIABLE: "1"}):
counts = install.install_data_directory(self._entry(), work)
self.assertIsInstance(counts["error"], install.OfflineReason)
def test_a_tarball_maps_through_source_path(self):
import io as _io
import tarfile
work = scratch_dir(self)
archive = work / "src.tar.gz"
with tarfile.open(archive, "w:gz") as tf:
data = b"index"
info = tarfile.TarInfo("pcsx2-v1/bin/resources/GameIndex.yaml")
info.size = len(data)
tf.addfile(info, _io.BytesIO(data))
entry = {"type": "tarball", "source_path": "pcsx2-v1/bin/resources"}
names = [rel for rel, _size, _open in install._archive_members(archive, entry)]
self.assertEqual(names, ["GameIndex.yaml"])
def test_the_trust_boundary(self):
entry = self._entry()
install._check_data_directory(entry, "d")
for bad in ({"url": "http://example.com/x.zip"}, {"dest": "../up"},
{"files": {"../x": {"sha1": "0" * 40, "size": 1}}},
{"type": "rar"}):
with self.subTest(bad=bad), self.assertRaises(ValueError):
install._check_data_directory({**entry, **bad}, "d")
def test_the_published_retroarch_list_carries_them(self):
manifest = json.loads((REPO_ROOT / "install" / "retroarch.json").read_text())
dirs = manifest.get("data_directories") or []
files = sum(len(d["files"]) for d in dirs)
from packs import archive as packs_archive
from packs import datatrees
written = packs_archive.PACK_DOCUMENTS + datatrees.pack_markers(
"retroarch", None, str(REPO_ROOT / "platforms"))
self.assertEqual(written, ("README.txt", "manifest.json", ".nomedia"))
self.assertEqual(
manifest["pack_files"] - manifest["total_files"] - files, len(written),
"the pack's two documents and its .nomedia are the only files the "
"installer lacks",
)
if __name__ == "__main__":
unittest.main()
+911
View File
@@ -0,0 +1,911 @@
"""install.py finds each platform where its own code puts it, on a desktop, under WSL or from the registry."""
from __future__ import annotations
import tests
from tests import scratch_dir
import contextlib
import io
import json
import os
import select
import subprocess
import sys
import tempfile
import unittest
from types import SimpleNamespace
import unittest.mock
from pathlib import Path
from tests.netguard import module_guard
from tests.installsupport import REPO_ROOT, install, reported
# No connection off the loopback and no card of the host reaches a test:
# the module fails at its end otherwise.
setUpModule, tearDownModule = module_guard("test_install_detection")
class DetectionFollowsTheRegistry(unittest.TestCase):
"""The registry declared RetroPie at ~/RetroPie/BIOS and BizHawk at
~/.config/BizHawk/Firmware, every manifest published it, and the
installer never read it: such a home detected nothing."""
def test_the_table_is_the_registry(self):
import yaml
registry = yaml.safe_load(
(Path(__file__).resolve().parents[1] / "platforms" / "_registry.yml").read_text()
)
rules = {
(rule["os"], name, rule["path"], rule.get("bios_path"))
for name, entry in registry["platforms"].items()
for rule in (entry.get("install") or {}).get("detect", [])
if rule["method"] == "path_exists"
}
self.assertEqual(set(install._PATH_RULES), rules)
def test_the_embedded_table_is_the_registry(self):
"""Only the path rules were compared: Knulli, muOS, ArkOS and the
Recalbox version file were detected by the installer and absent
from every manifest's `detect`, and ROCKNIX was declared another
way. A changed bios_path in the registry reached no installer."""
import yaml
registry = yaml.safe_load(
(Path(__file__).resolve().parents[1] / "platforms" / "_registry.yml").read_text()
)
rules = []
for name, entry in registry["platforms"].items():
for rule in (entry.get("install") or {}).get("detect", []):
if rule["method"] == "os_release":
values = tuple(rule["match"])
elif rule["method"] == "file_exists":
files = rule["file"]
values = tuple(files if isinstance(files, list) else [files])
elif rule["method"] == "file_matches":
values = (rule["file"], rule["pattern"])
else:
continue
rules.append(install._OsRule(
"" if rule.get("installs") is False else name, rule["name"],
rule["method"], values, rule.get("bios_path"),
tuple(rule.get("config", ())), rule.get("home", ""),
rule.get("derives_from", ""),
))
self.assertEqual(sorted(install._EMBEDDED_RULES, key=repr), sorted(rules, key=repr))
def test_every_config_file_read_is_published(self):
"""The registry published five RetroArch config files and the fixed
RetroDECK folder, but the installer also read Snap, the older macOS file,
C:\\RetroArch, Program Files, ES-DE, Steam and RetroDECK's own
config. A manifest reader saw a subset of what the installer does."""
import yaml
registry = yaml.safe_load(
(Path(__file__).resolve().parents[1] / "platforms" / "_registry.yml").read_text()
)
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
env = {
"HOME": str(root / "home"), "APPDATA": str(root / "appdata"),
"ProgramFiles": str(root / "pf"), "ProgramFiles(x86)": str(root / "pf86"),
# Not the host's: a WSL session can hold them.
"USERPROFILE": "", "LOCALAPPDATA": "", "ProgramData": "",
"SCOOP": "", "SCOOP_GLOBAL": "",
}
def expand(text: str) -> str:
text = text.replace("$HOME", env["HOME"])
for var in ("ProgramFiles(x86)", "ProgramFiles", "APPDATA"):
text = text.replace(f"%{var}%", env[var])
return str(Path(text.replace("\\", "/")))
published = {
(rule["os"], expand(rule["config"]))
for entry in registry["platforms"].values()
for rule in (entry.get("install") or {}).get("detect", [])
if rule["method"] == "config_file"
}
read: set[tuple[str, str]] = set()
def record(os_type: str):
def probe(path, *_args, **_kwargs):
read.add((os_type, str(Path(str(path).replace("\\", "/")))))
return
return probe
with unittest.mock.patch.dict(os.environ, env), \
unittest.mock.patch.object(install, "_detect_embedded", list), \
unittest.mock.patch.object(install, "_detect_android", list):
for os_type in ("linux", "darwin", "windows"):
with unittest.mock.patch.object(
install, "_parse_retroarch_system_dir", record(os_type)
):
install.detect_platforms(os_type)
with unittest.mock.patch.object(Path, "is_file", lambda _self: True):
for folder in install._windows_retroarch_dirs():
read.add(("windows", str(folder / "retroarch.cfg")))
retrodeck = root / "home/.var/app/net.retrodeck.retrodeck/config/retrodeck"
read |= {("linux", str(retrodeck / "retrodeck.json")),
("linux", str(retrodeck / "retrodeck.cfg"))}
# The default Steam library is the Program Files one, already read.
self.assertEqual(sorted(read - published), [])
def test_the_first_embedded_match_wins(self):
with unittest.mock.patch.object(install, "_parse_os_release", return_value={}), \
unittest.mock.patch.object(
Path, "exists",
lambda p: str(p) in ("/usr/share/knulli/knulli.version",
"/usr/share/batocera/batocera.version"),
):
self.assertEqual(install._detect_embedded(), [("knulli", Path("/userdata/bios"))])
# What ROCKNIX writes since 2025: OS_NAME and HW_DEVICE, no ID.
with unittest.mock.patch.object(install, "_parse_os_release",
return_value={"OS_NAME": "ROCKNIX", "HW_DEVICE": "RK3566"}):
self.assertEqual(
install._detect_embedded(), [("rocknix", Path("/storage/roms/bios"))]
)
def _home(self) -> Path:
home = scratch_dir(self)
patcher = unittest.mock.patch.dict(os.environ, {"HOME": str(home), "USERPROFILE": str(home)})
patcher.start()
self.addCleanup(patcher.stop)
return home
def test_a_home_layout_is_detected(self):
home = self._home()
(home / "RetroPie" / "BIOS").mkdir(parents=True)
found: list = []
install._detect_by_path("linux", found)
self.assertIn(("retropie", home / "RetroPie" / "BIOS"), found)
def test_bizhawk_is_found_by_its_executable(self):
"""~/.config/BizHawk/Firmware is a folder no build reads: the
archive extracted to ~/Games/BizHawk-2.11.1 reads the Firmware folder
beside EmuHawkMono.sh."""
home = self._home()
extracted = home / "Games" / "BizHawk-2.11.1"
extracted.mkdir(parents=True)
(extracted / "EmuHawkMono.sh").write_text("#!/bin/sh\n")
found: list = []
install._detect_by_executable("linux", found)
self.assertEqual(found, [("bizhawk", extracted / "Firmware")])
self.assertNotIn("bizhawk", install._os_default_dests("linux"))
self.assertNotIn("bizhawk", install._os_default_dests("windows"))
def test_a_portable_platform_is_not_guessed(self):
"""--platform bizhawk with no install found wrote 924 files into
~/.config/BizHawk/Firmware, then detected that folder next time."""
home = self._home()
with unittest.mock.patch.object(sys, "argv", ["install.py", "--platform", "bizhawk"]), \
unittest.mock.patch.object(install, "detect_os", return_value="linux"), \
unittest.mock.patch.object(install, "detect_platforms", return_value=[]), \
contextlib.redirect_stdout(io.StringIO()), \
contextlib.redirect_stderr(io.StringIO()) as err, \
self.assertRaises(SystemExit) as stop:
install.main()
self.assertEqual(stop.exception.code, 1)
self.assertIn("--dest", err.getvalue())
self.assertFalse((home / ".config" / "BizHawk").exists())
def test_the_executable_table_is_the_registry(self):
import yaml
registry = yaml.safe_load(
(Path(__file__).resolve().parents[1] / "platforms" / "_registry.yml").read_text()
)
rules = {
(rule["os"], name, tuple(rule["file"]), rule["bios_subdir"])
for name, entry in registry["platforms"].items()
for rule in (entry.get("install") or {}).get("detect", [])
if rule["method"] == "executable"
}
self.assertEqual(set(install._EXE_RULES), rules)
def test_a_windows_profile_path_is_detected(self):
home = self._home()
(home / "BizHawk" / "Firmware").mkdir(parents=True)
found: list = []
install._detect_by_path("windows", found)
self.assertEqual(found, [("bizhawk", home / "BizHawk" / "Firmware")])
def test_a_platform_already_found_is_not_added_again(self):
home = self._home()
(home / "Emulation" / "bios").mkdir(parents=True)
found = [("emudeck", home / "elsewhere" / "bios")]
install._detect_by_path("linux", found)
self.assertEqual(found, [("emudeck", home / "elsewhere" / "bios")])
def test_emudeck_on_macos_is_found_by_its_settings(self):
"""EmuDeck's macOS build reads ~/.config/EmuDeck/settings.sh as the
Linux one does (functions/all.sh:3-4,24, vars.sh:2): a Mac with
EmuDeck detected nothing and was offered RetroArch's folder."""
home = self._home()
settings = home / ".config" / "EmuDeck" / "settings.sh"
settings.parent.mkdir(parents=True)
settings.write_text(f"emulationPath={home / 'Games' / 'Emulation'}\n")
self.assertIn(("emudeck", home / "Games" / "Emulation" / "bios"),
install.detect_platforms("darwin"))
settings.unlink()
(home / "Emulation" / "bios").mkdir(parents=True)
self.assertIn(("emudeck", home / "Emulation" / "bios"),
install.detect_platforms("darwin"))
class TestDetectedPlatformPrompt(unittest.TestCase):
"""With two platforms detected the menu had no quit entry, and q or any
wrong answer brought back a bare prompt."""
PLATFORMS = [("emudeck", Path("/e")), ("retroarch", Path("/r"))]
def _answer(self, *answers: str):
replies = iter(answers)
out = io.StringIO()
with unittest.mock.patch("builtins.input", lambda _prompt="": next(replies)), \
contextlib.redirect_stdout(out):
try:
result = install._prompt_platform_choice(self.PLATFORMS)
except SystemExit as exc:
result = exc
return result, out.getvalue()
def test_q_quits_and_the_menu_says_so(self):
result, out = self._answer("q")
self.assertIsInstance(result, SystemExit)
self.assertEqual(result.code, install.QUIT_CODE)
self.assertIn("q) quit", out)
def test_a_wrong_answer_names_the_choices(self):
result, out = self._answer("x", "2")
self.assertEqual(result, [self.PLATFORMS[1]])
self.assertIn("Type a number from 1 to 2, or q to quit.", out)
def test_enter_takes_the_first_and_there_is_no_all(self):
"""The screen focuses the first and has no All."""
result, out = self._answer("")
self.assertEqual(result, [self.PLATFORMS[0]])
self.assertNotIn("All", out)
class TestManualPlatformPrompt(unittest.TestCase):
"""With nothing detected, an interactive run asks instead of exiting.
Every test runs on no drive and a home of its own: two read the host's
drives and home, and failed with a card in the reader."""
def setUp(self):
home = scratch_dir(self)
for patcher in (unittest.mock.patch.object(install, "_mounted_cards", return_value=[]),
unittest.mock.patch.object(install, "_mount_points",
return_value=frozenset()),
unittest.mock.patch.object(install.Path, "home", return_value=home),
unittest.mock.patch.dict(os.environ, {"HOME": str(home)}),
unittest.mock.patch("sys.stdout", io.StringIO())):
patcher.start()
self.addCleanup(patcher.stop)
def test_the_host_is_not_read(self):
self.assertEqual(install._mounted_cards(), [])
self.assertEqual(install._default_dest("linux", "retroarch"),
Path.home() / ".config" / "retroarch" / "system")
def test_number_picks_the_platform_and_default_directory(self):
answers = iter(["1", ""])
with unittest.mock.patch("builtins.input", lambda _prompt="": next(answers)):
chosen = install._prompt_manual_platform("linux")
self.assertEqual(
chosen, [("retroarch", Path.home() / ".config" / "retroarch" / "system")]
)
def test_enter_takes_retroarch_as_the_dest_menu_does(self):
"""The --dest menu offered `[Enter for RetroArch]` and this one, the
same list, had no default."""
answers = iter(["", ""])
out = io.StringIO()
with unittest.mock.patch("builtins.input", lambda _prompt="": next(answers)), \
contextlib.redirect_stdout(out):
chosen = install._prompt_manual_platform("linux")
self.assertEqual(chosen[0][0], "retroarch")
def test_name_and_explicit_directory(self):
answers = iter(["batocera", "~/my bios"])
with unittest.mock.patch("builtins.input", lambda _prompt="": next(answers)):
chosen = install._prompt_manual_platform("linux")
self.assertEqual(chosen, [("batocera", Path("~/my bios").expanduser())])
def test_default_directory_comes_from_the_table(self):
answers = iter(["retropie", ""])
with unittest.mock.patch("builtins.input", lambda _prompt="": next(answers)), \
unittest.mock.patch.object(install, "_mounted_cards", return_value=[]), \
contextlib.redirect_stdout(io.StringIO()):
chosen = install._prompt_manual_platform("linux")
self.assertEqual(chosen, [("retropie", install.DEFAULT_DESTS["retropie"])])
def test_lakka_on_a_computer_asks_for_its_card(self):
"""/storage/system, a folder only the Lakka machine has, was taken
on a computer: Lakka keeps its BIOS on its card's STORAGE part."""
card = scratch_dir(self)
(card / ".config" / "retroarch").mkdir(parents=True)
(card / ".config" / "retroarch" / "retroarch.cfg").write_text(
'system_directory = "/tmp/system"\n')
answers = iter(["lakka"])
out = io.StringIO()
with unittest.mock.patch("builtins.input", lambda _prompt="": next(answers)), \
unittest.mock.patch.object(install, "_mounted_cards", return_value=[card]), \
contextlib.redirect_stdout(out):
chosen = install._prompt_manual_platform("linux")
self.assertEqual(chosen, [("lakka", card / "system")])
self.assertIn("Lakka card found", out.getvalue())
def test_batocera_on_a_computer_gets_no_default(self):
"""/userdata/bios, a folder only the Batocera
machine has, was offered on a computer, and Enter took it."""
answers = iter(["batocera", "", "~/share/bios"])
out = io.StringIO()
with unittest.mock.patch("builtins.input", lambda _prompt="": next(answers)), \
unittest.mock.patch.object(install, "_mounted_cards", return_value=[]), \
contextlib.redirect_stdout(out):
chosen = install._prompt_manual_platform("linux")
self.assertEqual(chosen, [("batocera", Path("~/share/bios").expanduser())])
self.assertIn("SHARE drive", out.getvalue())
self.assertNotIn("/userdata/bios", out.getvalue())
def test_quit_stops_like_ctrl_c(self):
"""`install.sh && retroarch` went on to start RetroArch with no BIOS
after a q, which left with 0 where Ctrl+C leaves with 130."""
with unittest.mock.patch("builtins.input", lambda _prompt="": "q"), \
self.assertRaises(SystemExit) as raised:
install._prompt_manual_platform("linux")
self.assertEqual(raised.exception.code, install.QUIT_CODE)
def test_quotes_around_a_typed_folder_are_dropped(self):
"""Explorer's "Copy as path" quotes the folder: the files went into a
folder named `"` under the current directory, and the run said Done."""
root = scratch_dir(self)
for typed in (f'"{root}/My Card/bios"', f"'{root}/My Card/bios'",
f' "{root}/My Card/bios" ',
# A half-selected copy: line mode kept the quote.
f'"{root}/My Card/bios'):
answers = iter(["1", typed])
with self.subTest(typed=typed), \
unittest.mock.patch("builtins.input",
lambda _prompt="", answers=answers: next(answers)), \
contextlib.redirect_stdout(io.StringIO()):
chosen = install._prompt_manual_platform("linux")
self.assertEqual(chosen, [("retroarch", root / "My Card" / "bios")])
def test_home_and_variables_are_expanded_on_every_system(self):
for os_type in ("linux", "windows", "darwin"):
answers = iter(["1", '"~/$RB_TEST_DIR/bios"'])
with self.subTest(os_type=os_type), \
unittest.mock.patch.dict(os.environ, {"RB_TEST_DIR": "cards"}), \
unittest.mock.patch("builtins.input",
lambda _prompt="", answers=answers: next(answers)), \
contextlib.redirect_stdout(io.StringIO()):
chosen = install._prompt_manual_platform(os_type)
self.assertEqual(chosen[0][1], Path.home() / "cards" / "bios")
def test_a_default_folder_not_there_is_not_found(self):
"""With nothing installed, Enter printed "Found RetroArch at" a
folder that did not exist."""
root = scratch_dir(self)
answers = iter(["1", ""])
out = io.StringIO()
with unittest.mock.patch.object(install, "_default_dest",
return_value=root / "retroarch" / "system"), \
unittest.mock.patch("builtins.input", lambda _prompt="": next(answers)), \
contextlib.redirect_stdout(out):
install._prompt_manual_platform("linux")
self.assertIn("default BIOS folder", out.getvalue())
self.assertIn("will be created", out.getvalue())
self.assertNotIn("Found", out.getvalue())
def test_a_wrong_answer_says_what_to_type(self):
answers = iter(["99", "1", ""])
out = io.StringIO()
with unittest.mock.patch("builtins.input", lambda _prompt="": next(answers)), \
contextlib.redirect_stdout(out):
install._prompt_manual_platform("linux")
self.assertIn("or q to quit", out.getvalue())
def test_windows_hint_shows_the_one_liner_with_arguments(self):
with unittest.mock.patch.dict(os.environ, {"RETROBIOS_BOOTSTRAP": "ps1"}):
hint = "\n".join(install._manual_usage_hint("windows"))
self.assertIn("--platform retroarch --dest", hint)
self.assertIn("/i.ps1", hint)
class AnUnreadableRetroDeckConfigIsSaid(unittest.TestCase):
"""retrodeck.json present but unreadable: the installer wrote to
~/retrodeck without a word, where a home moved to an SD card is not."""
def test_the_fallback_is_announced(self):
import contextlib
home = scratch_dir(self)
conf = home / ".var" / "app" / "net.retrodeck.retrodeck" / "config" / "retrodeck"
conf.mkdir(parents=True)
(conf / "retrodeck.json").write_text("{truncated")
err = io.StringIO()
with unittest.mock.patch.object(install.Path, "home", return_value=home), \
unittest.mock.patch.object(install, "_detect_embedded", return_value=[]), \
contextlib.redirect_stderr(err):
found = install.detect_platforms("linux")
self.assertIn(("retrodeck", home / "retrodeck"), found)
self.assertIn("could not read paths.rd_home_path", err.getvalue())
class RetroDeckFoldersMovedOneByOne(unittest.TestCase):
"""RetroDECK moves its bios folder on its own and records the new place
in retrodeck.json paths.bios_path, but only rd_home_path was read: the
files went to <home>/bios, a folder RetroDECK no longer reads."""
def _detect(self, paths: dict) -> tuple[Path, list]:
home = scratch_dir(self)
conf = home / ".var/app/net.retrodeck.retrodeck/config/retrodeck"
conf.mkdir(parents=True)
(conf / "retrodeck.json").write_text(json.dumps({"paths": paths}))
with unittest.mock.patch.object(install.Path, "home", return_value=home), \
unittest.mock.patch.object(install, "_detect_embedded", return_value=[]), \
unittest.mock.patch.object(install.MOVED, "_by_root", {}), \
contextlib.redirect_stderr(io.StringIO()):
found = install.detect_platforms("linux")
dests = {name: install._destination_path(home / "retrodeck", f"{name}/a.bin")
for name in ("bios", "roms", "saves")}
label = install.found_label("retrodeck", home / "retrodeck")
return home, [found, dests, label]
def test_a_moved_bios_folder_is_followed(self):
home = scratch_dir(self)
sd = home / "sd"
root, (found, dests, label) = self._detect({
"rd_home_path": "", "bios_path": str(sd / "bios"), "roms_path": str(sd / "roms"),
})
rd_home = root / "retrodeck"
self.assertIn(("retrodeck", rd_home), found)
self.assertEqual(dests["bios"], (sd / "bios").resolve() / "a.bin")
self.assertEqual(dests["roms"], (sd / "roms").resolve() / "a.bin")
self.assertEqual(dests["saves"], rd_home.resolve() / "saves" / "a.bin")
self.assertIn(f"bios/ at {sd / 'bios'}", label)
def test_folders_in_place_change_nothing(self):
home = scratch_dir(self)
rd_home = home / "rd"
_root, (found, dests, label) = self._detect({
"rd_home_path": str(rd_home), "bios_path": str(rd_home / "bios"),
})
self.assertIn(("retrodeck", rd_home), found)
self.assertNotIn("bios/ at", label)
def test_a_run_that_never_detects_follows_the_move(self):
"""--platform retrodeck --dest D never detects, and the full-screen
interface passes --dest to every child run: the files went to
<home>/bios before a detection had filled the moved folders."""
home = scratch_dir(self)
sd = home / "sd"
conf = home / ".var/app/net.retrodeck.retrodeck/config/retrodeck"
conf.mkdir(parents=True)
(conf / "retrodeck.json").write_text(json.dumps({"paths": {
"rd_home_path": "", "bios_path": str(sd / "bios")}}))
with unittest.mock.patch.object(install.Path, "home", return_value=home), \
unittest.mock.patch.object(install.MOVED, "_by_root", {}):
dest = install._destination_path(home / "retrodeck", "bios/a.bin")
other = install._destination_path(home / "elsewhere", "bios/a.bin")
self.assertEqual(dest, (sd / "bios").resolve() / "a.bin")
self.assertEqual(other, (home / "elsewhere").resolve() / "bios" / "a.bin")
class TheInstallerPageNamesEveryEvidence(unittest.TestCase):
"""The page said BizHawk, RetroBat, RomM and RetroPie had no detection
and fell back to ~/bios, while install.py found each by its directory."""
def test_every_rule_is_on_the_page(self):
page = (REPO_ROOT / "wiki" / "installer.md").read_text(encoding="utf-8")
page = page.replace("\\\\", "\\")
expected = [path for rule in install._EMBEDDED_RULES
if rule.method == "file_exists" for path in rule.values]
expected += [rule.name for rule in install._EMBEDDED_RULES]
expected += [path.replace("$HOME", "~").rstrip("/")
for _os, _p, path, _b in install._PATH_RULES]
missing = [path for path in expected if path not in page]
self.assertEqual(missing, [])
self.assertNotIn("have no detection", page)
class BizHawkIsFoundWhereItReads(unittest.TestCase):
"""BizHawk reads <data dir>/Firmware: $BIZHAWK_DATA_HOME on Unix (the
Nix package sets $XDG_DATA_HOME/emuhawk-monort-<version>), else the
EmuHawk folder. Only two conventional folders were looked at, and a
forced --platform bizhawk wrote where EmuHawk never reads."""
def test_the_data_home_variable(self):
home = scratch_dir(self)
with unittest.mock.patch.dict(os.environ, {"BIZHAWK_DATA_HOME": str(home)}):
self.assertEqual(install._bizhawk_unix_firmware(), home / "Firmware")
def test_the_nix_data_home(self):
xdg = scratch_dir(self)
(xdg / "emuhawk-monort-2.11.1").mkdir()
env = {"XDG_DATA_HOME": str(xdg), "BIZHAWK_DATA_HOME": ""}
with unittest.mock.patch.dict(os.environ, env):
self.assertEqual(
install._bizhawk_unix_firmware(), xdg / "emuhawk-monort-2.11.1" / "Firmware"
)
def test_launchbox_names_emuhawk(self):
source = " ".join((REPO_ROOT / "install.py").read_text(encoding="utf-8").split())
self.assertIn('.get("emuhawk.exe")', source)
class QuitMeansNoAtTheLastQuestion(unittest.TestCase):
"""Every menu taught q as quit, yet at "Download 3.2 GB ...? [Y/n]" a q
started the download."""
def test_quit_words_refuse(self):
for answer, expected in (("q", False), ("quit", False), ("exit", False),
("n", False), ("No", False), ("", True), ("y", True)):
with self.subTest(answer=answer), \
unittest.mock.patch.object(install, "_ask", return_value=answer):
self.assertIs(install._confirm("? "), expected)
class WindowsSeenFromWsl(unittest.TestCase):
"""Under WSL, C:\\RetroBat\\bios was a relative path, %APPDATA% and
%USERPROFILE% were unset, and --platform retrobat wrote 7 753 files into
~/bios inside the distribution, where RetroBat reads nothing."""
def setUp(self):
self.root = scratch_dir(self)
patcher = unittest.mock.patch.dict(install._WSL, {"active": True, "root": str(self.root)})
patcher.start()
self.addCleanup(patcher.stop)
def test_a_drive_path_is_its_mount(self):
self.assertEqual(install._host_path("C:\\RetroBat\\bios"), self.root / "c" / "RetroBat" / "bios")
self.assertEqual(install._rule_path("C:\\RetroBat\\bios"), self.root / "c" / "RetroBat" / "bios")
self.assertEqual(install._default_dest("wsl", "retrobat"), self.root / "c" / "RetroBat" / "bios")
self.assertEqual(install._host_path("relative/path"), Path("relative/path"))
def test_retrobat_on_the_windows_side_is_detected(self):
(self.root / "c" / "RetroBat" / "bios").mkdir(parents=True)
found: list = []
install._detect_by_path("wsl", found)
self.assertIn(("retrobat", self.root / "c" / "RetroBat" / "bios"), found)
def test_retroarch_beside_its_exe_on_the_windows_side_is_detected(self):
"""The installer page said both sides are searched under WSL, and
RetroArch's install folders were looked in on Windows only: a
C:\\RetroArch-Win64 was never found from WSL."""
exe_dir = self.root / "c" / "RetroArch-Win64"
exe_dir.mkdir(parents=True)
(exe_dir / "retroarch.exe").write_bytes(b"MZ")
home = scratch_dir(self)
cleared = {name: "" for name in (*install._WINDOWS_VARIABLES, "SCOOP", "SCOOP_GLOBAL",
"ProgramData")}
with unittest.mock.patch.dict(os.environ, {**cleared, "HOME": str(home)}), \
unittest.mock.patch.object(Path, "home", return_value=home):
found = install.detect_platforms("wsl")
self.assertIn(("retroarch", exe_dir / "system"), found)
def test_the_windows_variables_are_read_through_cmd(self):
answers = {"USERPROFILE": "C:\\Users\\me", "APPDATA": "C:\\Users\\me\\AppData\\Roaming"}
def cmd(argv, **kwargs):
# cmd.exe must not read the terminal: it swallowed the answer
# typed at the first prompt.
self.assertIs(kwargs.get("stdin"), subprocess.DEVNULL)
name = argv[-1].split("%")[1]
return subprocess.CompletedProcess(argv, 0, answers.get(name, f"%{name}%") + "\r\n", "")
cleared = {name: "" for name in install._WINDOWS_VARIABLES}
with unittest.mock.patch.dict(os.environ, cleared), \
unittest.mock.patch.object(install.subprocess, "run", cmd):
install._enter_wsl()
self.assertEqual(os.environ["USERPROFILE"], str(self.root / "c" / "Users" / "me"))
self.assertEqual(os.environ["APPDATA"],
str(self.root / "c" / "Users" / "me" / "AppData" / "Roaming"))
# ProgramFiles among them: cmd answered %ProgramFiles% unexpanded.
for name in sorted(set(install._WINDOWS_VARIABLES) - answers.keys()):
with self.subTest(variable=name):
self.assertEqual(os.environ[name], "")
def test_a_steam_client_outside_program_files_is_found(self):
"""A Steam client in D:\\Steam hid every library: only Program Files
was searched, and Steam's RetroArch was not detected."""
empty = self.root / "c" / "Program Files (x86)"
empty.mkdir(parents=True)
common = self.root / "d" / "steam" / "steamapps" / "common" / "RetroArch"
common.mkdir(parents=True)
(common / "retroarch.exe").write_bytes(b"MZ")
(common / "retroarch.cfg").write_text('system_directory = "default"\n')
home = scratch_dir(self)
cleared = {name: "" for name in (*install._WINDOWS_VARIABLES, "SCOOP", "SCOOP_GLOBAL",
"ProgramData")}
asked: list = []
def registry(key, value):
asked.append((key, value))
return "d:/steam" if (key, value) == ("HKCU\\Software\\Valve\\Steam",
"SteamPath") else None
with unittest.mock.patch.dict(os.environ, {**cleared, "HOME": str(home),
"ProgramFiles(x86)": str(empty)}), \
unittest.mock.patch.object(Path, "home", return_value=home), \
unittest.mock.patch.object(install, "_read_registry", registry):
self.assertEqual(install._steam_libraries(), [self.root / "d" / "steam"])
found = install.detect_platforms("wsl")
self.assertIn(("retroarch", common / "system"), found)
def test_windows_not_answering_is_said_not_taken_for_nothing(self):
"""cmd.exe timing out on the first variable ended the import without
a word, and detection said no setup was installed."""
calls: list = []
def slow(argv, **kwargs):
calls.append(argv)
raise subprocess.TimeoutExpired(argv, kwargs.get("timeout", 10))
cleared = {name: "" for name in install._WINDOWS_VARIABLES}
home = scratch_dir(self)
err = io.StringIO()
with unittest.mock.patch.dict(os.environ, {**cleared, "HOME": str(home)}), \
unittest.mock.patch.object(install.subprocess, "run", slow), \
unittest.mock.patch.object(Path, "home", return_value=home), \
unittest.mock.patch.object(install, "_DETECTION_REFUSED", []), \
unittest.mock.patch.object(install.EVENTS, "emit") as emit, \
contextlib.redirect_stderr(err):
install._enter_wsl()
self.assertEqual(len(calls), 1)
install.detect_platforms("wsl")
install.detect_platforms("wsl")
said = err.getvalue()
self.assertEqual(said.count("on the Windows side"), 1, said)
self.assertIn("cmd.exe did not answer in 10 s", said)
kinds = [c.args[0] for c in emit.call_args_list]
self.assertEqual(kinds.count("unsearched"), 1)
self.assertEqual(emit.call_args_list[kinds.index("unsearched")].kwargs["folder"],
str(self.root))
class NoFallbackOutsideWhatAPlatformReads(unittest.TestCase):
"""A forced platform not found here fell back to ~/bios for RetroBat on
Linux and for BizHawk from the menu, and on Windows to /userdata/bios,
which resolves to the root of the current drive. Each run reported done."""
def test_the_fallback_is_none_where_nothing_is_read(self):
self.assertIsNone(install._default_dest("linux", "retrobat"))
self.assertIsNone(install._default_dest("linux", "bizhawk"))
self.assertIsNone(install._default_dest("windows", "batocera"))
self.assertIsNone(install._default_dest("windows", "retropie"))
self.assertEqual(install._default_dest("linux", "batocera"), Path("/userdata/bios"))
# emudeck-we functions/all.ps1:12-14, not the profile folder.
self.assertEqual(
install._default_dest("windows", "emudeck"), install._host_path("C:/Emulation/bios")
)
def test_a_forced_platform_without_a_folder_is_refused(self):
with unittest.mock.patch.object(install, "detect_platforms", return_value=[]), \
unittest.mock.patch.object(install, "detect_os", return_value="linux"), \
unittest.mock.patch("sys.argv", ["install.py", "--platform", "retrobat",
"--no-input"]), \
contextlib.redirect_stdout(io.StringIO()), \
contextlib.redirect_stderr(io.StringIO()) as err, \
self.assertRaises(SystemExit) as caught:
install.main()
self.assertEqual(caught.exception.code, 1)
self.assertIn("--dest", err.getvalue())
class AQuestionLeftWithCtrlDOrCtrlCIsAStop(unittest.TestCase):
"""Ctrl+C or Ctrl+D at a question left with 0, so `install.sh && retroarch`
carried on with no BIOS, while the page documents 130."""
def test_eof_raises_the_stop(self):
def closed(_prompt=""):
raise EOFError
with unittest.mock.patch("builtins.input", closed), \
contextlib.redirect_stdout(io.StringIO()), \
self.assertRaises(KeyboardInterrupt):
install._ask("> ")
@unittest.skipIf(os.name == "nt", "needs a pseudo-terminal")
def test_ctrl_d_at_the_platform_question_exits_130(self):
import pty
import time
home = scratch_dir(self)
master, slave = pty.openpty()
# Its output on the terminal too: piped, nothing is asked, and the
# test skipped on every machine.
proc = subprocess.Popen(
[sys.executable, "install.py", "--plain"], cwd=REPO_ROOT, stdin=slave,
stdout=slave, stderr=slave,
# No request leaves the machine: the standalone emulators' lists
# come from the network.
env={**os.environ, "HOME": str(home), "RETROBIOS_OS": "linux",
"RETROBIOS_INSTALL_OFFLINE": "1"},
)
os.close(slave)
self.addCleanup(os.close, master)
out = ""
deadline = time.monotonic() + 60
while "Which setup?" not in out and proc.poll() is None \
and time.monotonic() < deadline:
if select.select([master], [], [], 0.2)[0]:
out += os.read(master, 65536).decode("utf-8", "replace")
os.write(master, b"\x04")
proc.wait(timeout=60)
while select.select([master], [], [], 0.2)[0]:
try:
chunk = os.read(master, 65536)
except OSError:
break
if not chunk:
break
out += chunk.decode("utf-8", "replace")
if "Which setup?" not in out:
self.skipTest("a platform was detected on this machine")
self.assertEqual(proc.returncode, 130, out)
self.assertIn("Stopped", out)
class DestAloneFollowsWhatIsFoundHere(unittest.TestCase):
"""--dest without --platform always meant RetroArch's list, even on a
machine where Batocera was found, and nothing said so."""
def _pick(self, found, interactive=False, answer=""):
out = io.StringIO()
with unittest.mock.patch.object(install, "detect_platforms", return_value=found), \
unittest.mock.patch("builtins.input", lambda _prompt="": answer), \
contextlib.redirect_stdout(out), contextlib.redirect_stderr(out):
try:
plat, folder = reported(install._platform_for_dest, "linux",
Path("/media/card"), interactive)
self.assertEqual(folder, Path("/media/card"))
return plat, out.getvalue()
except SystemExit as exc:
return exc, out.getvalue()
def test_a_card_system_chosen_checks_the_folder(self):
"""The menu offered no card system, and one
chosen now reads --dest as --platform does."""
root = scratch_dir(self)
answers = iter(["Onion", "retroarch"])
out = io.StringIO()
with unittest.mock.patch.object(install, "detect_platforms", return_value=[]), \
unittest.mock.patch("builtins.input", lambda _prompt="": next(answers)), \
contextlib.redirect_stdout(out):
plat, folder = install._platform_for_dest("linux", root, True)
self.assertEqual((plat, folder), ("retroarch", root))
self.assertIn(f"{root} is not an Onion SD card", out.getvalue())
self.assertIn("Onion, SD card (Miyoo Mini, Miyoo Mini Plus)", out.getvalue())
def test_nothing_found_is_retroarch_said_aloud(self):
plat, out = self._pick([])
self.assertEqual(plat, "retroarch")
self.assertIn("File list: RetroArch (nothing was detected here)", out)
def test_the_one_platform_found_is_used(self):
plat, out = self._pick([("batocera", Path("/userdata/bios"))])
self.assertEqual(plat, "batocera")
self.assertIn("File list: Batocera", out)
def test_several_found_without_a_terminal_is_refused(self):
result, out = self._pick([("emudeck", Path("/e")), ("retroarch", Path("/r"))])
self.assertIsInstance(result, SystemExit)
self.assertEqual(result.code, 1)
self.assertIn("--platform", out)
def test_a_terminal_asks_and_enter_keeps_the_one_found(self):
plat, out = self._pick([("batocera", Path("/userdata/bios"))], interactive=True)
self.assertEqual(plat, "batocera")
self.assertIn("Which platform's files go to /media/card?", out)
class RetroBatIsFoundWhereItSaysItIs(unittest.TestCase):
"""RetroBat on D:\\RetroBat was not detected, and --platform retrobat
wrote 7.6 GB into C:\\RetroBat\\bios, a folder it does not read.
RetroBat.exe writes its folder to the registry on every start."""
def test_the_table_is_the_registry(self):
import yaml
registry = yaml.safe_load((REPO_ROOT / "platforms" / "_registry.yml").read_text())
rules = {
(rule["os"], name, rule["key"], rule["value"], rule["bios_subdir"])
for name, entry in registry["platforms"].items()
for rule in (entry.get("install") or {}).get("detect", [])
if rule["method"] == "registry"
}
self.assertEqual(set(install._REGISTRY_RULES), rules)
def test_the_registry_folder_wins_over_the_default(self):
folder = scratch_dir(self)
with unittest.mock.patch.object(install, "_read_registry",
return_value=str(folder) + "\\"):
found: list = []
install._detect_by_registry("windows", found)
self.assertEqual(found, [("retrobat", folder / "bios")])
def test_a_folder_since_removed_is_passed_over(self):
gone = scratch_dir(self) / "moved"
with unittest.mock.patch.object(install, "_read_registry", return_value=str(gone)):
found: list = []
install._detect_by_registry("windows", found)
self.assertEqual(found, [])
def test_wsl_reads_it_through_reg_exe(self):
output = ("\nHKEY_CURRENT_USER\\SOFTWARE\\RetroBat\n"
" LatestKnownInstallPath REG_SZ D:\\Games\\RetroBat\\\n\n")
with unittest.mock.patch.dict(install._WSL, {"active": True, "root": "/mnt"}), \
unittest.mock.patch.object(install.os, "name", "posix"), \
unittest.mock.patch.object(
install.subprocess, "run",
return_value=SimpleNamespace(stdout=output, returncode=0)) as run:
value = install._read_registry("HKCU\\SOFTWARE\\RetroBat", "LatestKnownInstallPath")
self.assertEqual(value, "D:\\Games\\RetroBat\\")
self.assertEqual(install._host_path(value.rstrip("\\")),
Path("/mnt/d/Games/RetroBat"))
self.assertEqual(run.call_args[0][0][:2], ["reg.exe", "query"])
class EmuDeckWindowsIsFoundAsItsScriptsFindIt(unittest.TestCase):
"""EmuDeck's Windows scripts read %APPDATA%\\EmuDeck\\settings.ps1, else
%USERPROFILE%\\EmuDeck\\settings.ps1, and take C:\\Emulation when neither
names a folder (functions/all.ps1:4-14). Only the first was read, and a
forced --platform emudeck wrote into %USERPROFILE%\\Emulation\\bios."""
def _env(self) -> tuple[Path, Path]:
root = scratch_dir(self)
appdata, profile = root / "appdata", root / "profile"
patcher = unittest.mock.patch.dict(
os.environ, {"APPDATA": str(appdata), "USERPROFILE": str(profile)})
patcher.start()
self.addCleanup(patcher.stop)
return appdata, profile
@staticmethod
def _settings(folder: Path, text: str, encoding: str = "utf-8-sig") -> None:
(folder / "EmuDeck").mkdir(parents=True, exist_ok=True)
(folder / "EmuDeck" / "settings.ps1").write_bytes(text.encode(encoding))
def test_the_profile_settings_are_read_when_appdata_has_none(self):
_appdata, profile = self._env()
self._settings(profile, '\n$emulationPath="/games/Emulation"\n')
self.assertEqual(install._emudeck_windows_bios(), Path("/games/Emulation/bios"))
def test_appdata_comes_first(self):
appdata, profile = self._env()
self._settings(profile, '$emulationPath="/b/Emulation"\n')
self._settings(appdata, '$emulationPath="/a/Emulation"\n', "utf-16")
self.assertEqual(install._emudeck_windows_bios(), Path("/a/Emulation/bios"))
def test_settings_naming_no_folder_mean_c_emulation(self):
appdata, _profile = self._env()
self._settings(appdata, '$device="Windows"\n')
self.assertEqual(install._emudeck_windows_bios(),
install._host_path("C:\\Emulation") / "bios")
def test_a_network_install_is_its_share(self):
appdata, _profile = self._env()
self._settings(appdata, '$networkInstallation="true"\n'
'$VolName="/nas/games"\n'
'$emulationPath=$testdrive.ToString() + ":\\Emulation"\n')
self.assertEqual(install._emudeck_windows_bios(),
Path("/nas/games\\Emulation") / "bios")
def test_no_settings_detects_nothing(self):
self._env()
self.assertIsNone(install._emudeck_windows_bios())
def test_a_forced_platform_falls_back_to_c_emulation(self):
self.assertEqual(install._default_dest("windows", "emudeck"),
install._host_path("C:/Emulation/bios"))
if __name__ == "__main__":
unittest.main()
File diff suppressed because it is too large. Load diff
+656
View File
@@ -0,0 +1,656 @@
"""install.py fetches lists and files over an unreliable network and says what failed in plain words."""
from __future__ import annotations
import tests
from tests import scratch_dir
import contextlib
import hashlib
import io
import json
import os
import socket
import subprocess
import sys
import time
import unittest
from types import SimpleNamespace
import unittest.mock
import urllib.error
from pathlib import Path
from tests.netguard import module_guard
from tests.installsupport import REPO_ROOT, install, reported, _FakeResponse
# No connection off the loopback and no card of the host reaches a test:
# the module fails at its end otherwise.
setUpModule, tearDownModule = module_guard("test_install_downloads")
class TestLocalCheckHashing(unittest.TestCase):
"""A file is read once, not once per declared digest."""
def test_both_digests_come_from_one_read(self):
tmp = scratch_dir(self)
payload = b"BIOS PAYLOAD"
(tmp / "boot.bin").write_bytes(payload)
entry = {
"dest": "boot.bin",
"size": len(payload),
"sha1": hashlib.sha1(payload).hexdigest(),
"sha256": hashlib.sha256(payload).hexdigest(),
}
calls = []
real_digest = install._digest_file
def recording(path, algorithms):
calls.append(tuple(sorted(algorithms)))
return real_digest(path, algorithms)
install._digest_file = recording
try:
to_download, up_to_date, mismatched = install.check_local([entry], tmp)
finally:
install._digest_file = real_digest
self.assertEqual((len(to_download), len(up_to_date), len(mismatched)), (0, 1, 0))
self.assertEqual(calls, [("sha1", "sha256")], "file must be read once")
def test_a_wrong_digest_still_lands_in_mismatched(self):
tmp = scratch_dir(self)
(tmp / "boot.bin").write_bytes(b"BIOS PAYLOAD")
entry = {
"dest": "boot.bin",
"size": 12,
"sha1": "0" * 40,
"sha256": hashlib.sha256(b"BIOS PAYLOAD").hexdigest(),
}
_, up_to_date, mismatched = install.check_local([entry], tmp)
self.assertEqual((len(up_to_date), len(mismatched)), (0, 1))
class FileListsAreRetried(unittest.TestCase):
"""The file list was fetched once with a 30 s timeout: one timeout on
flaky Wi-Fi ended the run, while each file got three attempts."""
def _fetch(self, outcomes):
calls = []
def urlopen(url, timeout=None):
calls.append(url)
outcome = outcomes.pop(0)
if isinstance(outcome, Exception):
raise outcome
return contextlib.nullcontext(io.BytesIO(outcome))
with unittest.mock.patch.object(install.urllib.request, "urlopen", urlopen), \
unittest.mock.patch.object(install.time, "sleep"), \
unittest.mock.patch.object(install, "_read_limited_json",
lambda resp, limit, label: json.loads(resp.read())):
try:
return install._fetch_json("https://x/list.json", 1000, "list"), calls
except OSError as exc:
return exc, calls
def test_a_timeout_then_an_answer_succeeds(self):
import urllib.error
result, calls = self._fetch([urllib.error.URLError("timed out"), b'{"a": 1}'])
self.assertEqual(result, {"a": 1})
self.assertEqual(len(calls), 2)
def test_a_404_is_not_retried(self):
import urllib.error
missing = urllib.error.HTTPError("https://x", 404, "Not Found", None, None)
result, calls = self._fetch([missing])
self.assertIs(result, missing)
self.assertEqual(len(calls), 1)
def test_a_sign_in_page_is_named(self):
"""A Wi-Fi sign-in page read as "invalid file list: Expecting value:
line 1 column 1 (char 0)", with no cause and no next step."""
page = io.BytesIO(b"<html>Sign in to Hotel WiFi</html>")
page.headers = {}
err = io.StringIO()
with unittest.mock.patch.object(
install.urllib.request, "urlopen",
lambda url, timeout=None: contextlib.nullcontext(page)), \
contextlib.redirect_stderr(err), self.assertRaises(SystemExit):
reported(install.fetch_manifest, "retroarch")
said = err.getvalue()
self.assertIn("a Wi-Fi sign-in page or a cut download looks like this", said)
self.assertIn("run the same command again", said)
self.assertNotIn("Expecting value", said)
def test_three_failures_end_with_advice(self):
import urllib.error
failures = [urllib.error.URLError("timed out")] * install.MAX_RETRIES
err = io.StringIO()
with unittest.mock.patch.object(install.urllib.request, "urlopen",
side_effect=failures), \
unittest.mock.patch.object(install.time, "sleep"), \
contextlib.redirect_stderr(err), self.assertRaises(SystemExit) as caught:
reported(install.fetch_manifest, "retroarch")
self.assertEqual(caught.exception.code, 1)
self.assertIn("HTTPS_PROXY", err.getvalue())
self.assertIn(f"after {install.MAX_RETRIES} attempts", err.getvalue())
class ASilentServerIsBounded(unittest.TestCase):
"""A server that takes the connection and never answers held line mode
93 s: three waits of 30 s for a first byte that never came."""
def test_two_short_waits_then_the_reason(self):
import socket
import threading
listener = socket.socket()
listener.bind(("127.0.0.1", 0))
listener.listen(8)
self.addCleanup(listener.close)
held = []
def accept():
while True:
try:
conn, _addr = listener.accept()
except OSError:
return
held.append(conn)
threading.Thread(target=accept, daemon=True).start()
self.addCleanup(lambda: [conn.close() for conn in held])
port = listener.getsockname()[1]
err = io.StringIO()
start = time.monotonic()
with unittest.mock.patch.object(install, "LIST_TIMEOUT", 0.5), \
unittest.mock.patch.object(install, "_retry_delay", return_value=0), \
unittest.mock.patch.dict(os.environ,
{"RETROBIOS_BASE_URL": f"http://127.0.0.1:{port}"}), \
contextlib.redirect_stderr(err), self.assertRaises(SystemExit):
reported(install.fetch_manifest, "retroarch")
elapsed = time.monotonic() - start
self.assertLess(elapsed, 5)
self.assertEqual(len(held), install.STALLED_ATTEMPTS)
self.assertIn(f"after {install.STALLED_ATTEMPTS} attempts", err.getvalue())
# The retry line counts the tries this failure gets: "(2/3)" promised
# a third that never came.
self.assertIn(f"(2/{install.STALLED_ATTEMPTS})", err.getvalue())
self.assertNotIn(f"/{install.MAX_RETRIES})", err.getvalue())
# A download keeps its own, longer wait between chunks.
self.assertGreater(install.LIST_TIMEOUT, 1)
class TargetListFailureIsNotAnAnswer(unittest.TestCase):
"""A timeout is not 'no targets': it sent users to the full pack."""
def test_unreachable_list_is_none(self):
import urllib.error
def boom(*args, **kwargs):
raise urllib.error.URLError("timed out")
with unittest.mock.patch.object(install.urllib.request, "urlopen", boom), \
contextlib.redirect_stderr(io.StringIO()):
self.assertIsNone(install.fetch_targets("retroarch"))
def test_absent_list_is_empty(self):
import urllib.error
def missing(url, *args, **kwargs):
raise urllib.error.HTTPError(url, 404, "Not Found", {}, None)
with unittest.mock.patch.object(install.urllib.request, "urlopen", missing):
self.assertEqual(install.fetch_targets("retroarch"), {})
class ACutFileListIsRetried(unittest.TestCase):
"""Wi-Fi dropped mid install/batocera.json: the short body parsed as an
invalid file list, no retry, no network advice."""
class _Response(io.BytesIO):
def __init__(self, body: bytes, length: int):
super().__init__(body)
self.headers = {"Content-Length": str(length)}
def __enter__(self):
return self
def __exit__(self, *exc):
return False
def test_the_second_attempt_succeeds(self):
full = json.dumps({"a": 1}).encode()
answers = iter([self._Response(full[:4], len(full)), self._Response(full, len(full))])
with unittest.mock.patch.object(install.urllib.request, "urlopen",
lambda *a, **k: next(answers)), \
unittest.mock.patch.object(install.time, "sleep"):
self.assertEqual(install._fetch_json("https://x/y.json", 1000, "manifest"), {"a": 1})
def test_a_retry_is_announced(self):
import contextlib
full = json.dumps({"a": 1}).encode()
answers = iter([self._Response(full[:4], len(full)), self._Response(full, len(full))])
err = io.StringIO()
with unittest.mock.patch.object(install.urllib.request, "urlopen",
lambda *a, **k: next(answers)), \
unittest.mock.patch.object(install.time, "sleep"), \
contextlib.redirect_stderr(err):
install._fetch_json("https://x/y.json", 1000, "file list")
self.assertIn("trying again (2/3)", err.getvalue())
def test_a_short_body_is_a_connection_error(self):
with self.assertRaises(ConnectionError):
install._read_limited_json(self._Response(b'{"a"', 10), 1000, "manifest")
class AChunkedCutIsADownloadFailure(unittest.TestCase):
"""Behind a proxy that re-encodes as chunked, a cut raised
http.client.IncompleteRead, which no handler caught: the run ended on a
traceback with no summary."""
def test_the_file_fails_and_the_run_goes_on(self):
import http.client
class Cut:
headers = {}
status = 200
def __enter__(self):
return self
def __exit__(self, *exc):
return False
def read(self, *_args):
raise http.client.IncompleteRead(b"abc", 10)
entry = {"dest": "a.bin", "repo_path": "bios/a.bin", "size": 10,
"sha1": "0" * 40, "sha256": "0" * 64}
with unittest.mock.patch.object(install.urllib.request, "urlopen",
lambda *a, **k: Cut()), \
unittest.mock.patch.object(install, "_retry_delay", return_value=0):
dest, ok, reason = install._download_one(entry, scratch_dir(self))
self.assertFalse(ok)
self.assertEqual(reason, "the connection was cut")
self.assertIsInstance(reason, install.NetworkReason)
class TheProgressLineFitsTheTerminal(unittest.TestCase):
"""At 80 columns the 94-100 column line wrapped, and every redraw left
a ghost line behind."""
def test_each_width(self):
import os as _os
for columns in (40, 60, 80, 120):
with self.subTest(columns=columns):
out = io.StringIO()
term = install.Terminal(out)
term.live = True
progress = install.Progress(5865, 2_300_000_000, term)
progress.bytes = 1_000_000_000
progress.files = 5000
with unittest.mock.patch.object(
install.shutil, "get_terminal_size",
return_value=_os.terminal_size((columns, 24)),
):
progress._draw()
line = out.getvalue().split("\033[K")[-1]
self.assertLess(len(line), columns)
self.assertIn("%", line)
class AResumedCopyIsNotSpeed(unittest.TestCase):
"""The live line divided every byte by the run's time, the 3 GB a
resumed partial copy already held included: 2.5 GB/s and 0:00 left on a
1 MB/s link."""
def test_speed_counts_only_what_this_run_fetched(self):
import os as _os
out = io.StringIO()
term = install.Terminal(out)
term.live = True
progress = install.Progress(1, 4_000_000_000, term)
# Ten seconds exactly: read from the clock, a slow runner added a few
# milliseconds and moved the last digit of the speed.
progress.elapsed = lambda: 10.0
progress.bytes = 3_000_000_000 + 10_000_000
progress.resumed = 3_000_000_000
with unittest.mock.patch.object(
install.shutil, "get_terminal_size",
return_value=_os.terminal_size((120, 24)),
):
progress._draw()
line = out.getvalue()
self.assertIn(f"{install.format_size(1_000_000)}/s", line)
self.assertNotIn("0:00 left", line)
class APartialIsClosedBeforeItIsDeleted(unittest.TestCase):
"""Windows refuses to delete an open file: a resumed partial copy whose
hash no longer matched was unlinked with its handle still open, and the
PermissionError ended the run."""
def test_the_source_closes_first(self):
source = (REPO_ROOT / "install.py").read_text(encoding="utf-8")
branch = source[source.index(" except _Corrupt:"):]
branch = branch[:branch.index("delay = 0.0")]
self.assertLess(branch.index("partial.close()"), branch.index("tmp_path.unlink()"))
class AMissingFileListNamesTheRef(unittest.TestCase):
"""A 404 on the manifest said three attempts were made and to check the
connection and proxy, but it was asked once, and the cause is the ref."""
def _run(self, ref: str) -> str:
error = install.urllib.error.HTTPError("u", 404, "Not Found", None, None)
with unittest.mock.patch.dict(os.environ, {"RETROBIOS_REF": ref,
"RETROBIOS_BASE_URL": ""}), \
unittest.mock.patch.object(install, "_fetch_json", side_effect=error), \
contextlib.redirect_stderr(io.StringIO()) as err, \
self.assertRaises(SystemExit):
reported(install.fetch_manifest, "retroarch")
return err.getvalue()
def test_a_pinned_tag_is_named(self):
text = self._run("v2020.01.01")
self.assertIn("RETROBIOS_REF=v2020.01.01", text)
self.assertNotIn("attempts", text)
self.assertNotIn("proxy", text)
def test_the_default_ref_is_not_blamed_on_the_network(self):
text = self._run(install.DEFAULT_RELEASE_REF)
self.assertIn("No file list for retroarch", text)
self.assertNotIn("proxy", text)
def _sharing_violation() -> PermissionError:
exc = PermissionError(13, "The process cannot access the file")
exc.winerror = 32
return exc
class ACompletePartialIsHandedOffOnWindows(unittest.TestCase):
"""Windows renames no open file, so the run that verified a large copy
released its lock before the rename. A second run claimed the copy in
that moment, found it complete and truncated it, and the first run's
rename failed with a sharing violation reported as "folder not
writable"."""
PAYLOAD = bytes(range(256)) * 200
ENTRY = {
"dest": "big.bin", "repo_path": "bios/big.bin", "size": len(PAYLOAD),
"sha256": hashlib.sha256(PAYLOAD).hexdigest(),
}
def _run(self, tmp: Path, seen: list[int]):
part = tmp / ".big.bin.part"
def urlopen(request, timeout=None):
seen.append(part.stat().st_size if part.exists() else -1)
return _FakeResponse(self.PAYLOAD)
with unittest.mock.patch.object(install, "RESUME_FROM_SIZE", 1024), \
unittest.mock.patch.object(install, "_NO_RENAME_WHILE_OPEN", True), \
unittest.mock.patch.object(install.urllib.request, "urlopen", urlopen):
return install._download_one(dict(self.ENTRY), tmp)
def test_the_other_runs_copy_is_left_whole(self):
tmp = scratch_dir(self)
(tmp / ".big.bin.part").write_bytes(self.PAYLOAD)
seen: list[int] = []
self.assertEqual(self._run(tmp, seen), ("big.bin", True, ""))
# The download ran while the complete copy was still whole.
self.assertEqual(seen, [len(self.PAYLOAD)])
self.assertEqual((tmp / "big.bin").read_bytes(), self.PAYLOAD)
# Nothing held it afterwards: it was a leftover, now removed.
self.assertFalse((tmp / ".big.bin.part").exists())
def test_the_rename_waits_for_the_other_run_to_let_go(self):
tmp = scratch_dir(self)
source = tmp / "copy"
source.write_bytes(b"x")
real_replace = os.replace
calls = []
def replace(src, dst):
calls.append(src)
if len(calls) < 3:
raise _sharing_violation()
return real_replace(src, dst)
with unittest.mock.patch.object(install.os, "replace", replace), \
unittest.mock.patch.object(install.time, "sleep"):
install._replace_released(source, tmp / "dest", {})
self.assertEqual(len(calls), 3)
self.assertEqual((tmp / "dest").read_bytes(), b"x")
def test_a_copy_the_other_run_placed_counts_once_verified(self):
tmp = scratch_dir(self)
dest = tmp / "dest"
dest.write_bytes(self.PAYLOAD)
expected = {"sha256": self.ENTRY["sha256"]}
install._replace_released(tmp / "gone", dest, expected)
dest.write_bytes(b"other bytes")
with self.assertRaises(FileNotFoundError):
install._replace_released(tmp / "gone", dest, expected)
def test_a_sharing_violation_is_no_refused_folder(self):
self.assertFalse(install._write_refused(_sharing_violation()))
self.assertTrue(install._write_refused(PermissionError(13, "denied")))
class ANetworkFailureNamesTheNetwork(unittest.TestCase):
"""Every transfer cut by the connection ended on "Location that was being
written to" and "once that is resolved", naming the folder and nothing
about the connection or the proxy."""
def _fail(self, error: BaseException) -> str:
def urlopen(*_args, **_kwargs):
raise error
entry = {"dest": "a.bin", "repo_path": "bios/a.bin", "size": 10,
"sha256": "0" * 64}
with unittest.mock.patch.object(install.urllib.request, "urlopen", urlopen), \
unittest.mock.patch.object(install, "_retry_delay", return_value=0):
_dest, ok, reason = install._download_one(entry, scratch_dir(self))
self.assertFalse(ok)
return reason
def test_cuts_and_refusals_are_network_failures(self):
import http.client
import socket
for error in (
urllib.error.URLError(socket.gaierror(-2, "Name or service not known")),
http.client.IncompleteRead(b"abc", 10),
ConnectionResetError(104, "Connection reset by peer"),
socket.timeout("timed out"),
urllib.error.HTTPError("u", 503, "Service Unavailable", None, None),
):
with self.subTest(error=error):
self.assertIsInstance(self._fail(error), install.NetworkReason)
def test_a_full_disk_and_a_missing_file_are_not(self):
import errno
for error in (OSError(errno.ENOSPC, "No space left on device"),
urllib.error.HTTPError("u", 404, "Not Found", None, None)):
with self.subTest(error=error):
self.assertNotIsInstance(self._fail(error), install.NetworkReason)
def test_a_bios_file_follows_the_shared_retry_rule(self):
"""A 404 on a BIOS file was asked three times, the file list once."""
cases = ((urllib.error.HTTPError("u", 404, "Not Found", None, None), 1),
(urllib.error.HTTPError("u", 503, "Service Unavailable", None, None),
install.MAX_RETRIES))
for error, expected in cases:
calls: list = []
def urlopen(*_args, _error=error, _calls=calls, **_kwargs):
_calls.append(1)
raise _error
entry = {"dest": "a.bin", "repo_path": "bios/a.bin", "size": 10,
"sha256": "0" * 64}
with self.subTest(code=error.code), \
unittest.mock.patch.object(install.urllib.request, "urlopen", urlopen), \
unittest.mock.patch.object(install, "_retry_delay", return_value=0):
_dest, ok, _reason = install._download_one(entry, scratch_dir(self))
self.assertFalse(ok)
self.assertEqual(len(calls), expected)
def test_the_summary_points_at_the_connection(self):
failures = [("a.bin", install.NetworkReason("timed out")),
("b.bin", install.NetworkReason("Connection reset by peer"))]
out = io.StringIO()
with contextlib.redirect_stdout(out), self.assertRaises(SystemExit) as raised:
install._report_outcome([Path("/bios")], 0, 0, 2, 0, False,
details=install.OutcomeDetails(failures=failures))
self.assertEqual(raised.exception.code, 2)
text = out.getvalue()
self.assertIn("HTTPS_PROXY", text)
self.assertIn("BIOS folder: /bios", text)
self.assertNotIn("being written to", text)
class AnHttpErrorNamesItsAddress(unittest.TestCase):
"""A file the server answered 404 for ended on "HTTP 404" alone: no
address, no cause, and a mirror chosen with RETROBIOS_BASE_URL went
unnamed, as it did behind a NETWORK failure of that mirror."""
def _said(self, failures: list, base_url: str = "") -> str:
out = io.StringIO()
env = {"RETROBIOS_BASE_URL": base_url} if base_url else {}
with unittest.mock.patch.dict(os.environ, env), contextlib.redirect_stdout(out), \
self.assertRaises(SystemExit) as raised:
install._report_outcome([Path("/bios")], 0, 0, len(failures), 0, False,
details=install.OutcomeDetails(failures=failures))
self.assertEqual(raised.exception.code, 2)
return " ".join(out.getvalue().split())
def _reason(self, kind: type, words: str, url: str) -> str:
reason = kind(words)
reason.url = url
return reason
def test_the_address_and_the_cause_are_said(self):
url = "https://raw.githubusercontent.com/Abdess/retrobios/main/bios/a.bin"
failures = [("a.bin", self._reason(install.HttpReason, "HTTP 404", url)),
("b.bin", self._reason(install.HttpReason, "HTTP 404", url + "x"))]
with unittest.mock.patch.dict(os.environ):
os.environ.pop("RETROBIOS_BASE_URL", None)
text = self._said(failures)
self.assertIn(f"The server answered HTTP 404 for {url} (and 1 other file).", text)
self.assertIn("running again changes nothing", text)
self.assertIn("BIOS folder: /bios", text)
self.assertNotIn("RETROBIOS_BASE_URL", text)
def test_a_mirror_named_in_the_environment_is_said(self):
mirror = "http://127.0.0.1:18766"
for kind, words in ((install.HttpReason, "HTTP 404"),
(install.NetworkReason, "the connection timed out")):
with self.subTest(kind=kind.__name__):
text = self._said([("a.bin", self._reason(kind, words, f"{mirror}/a.bin"))],
base_url=mirror)
self.assertIn("RETROBIOS_BASE_URL is set in this environment", text)
def test_the_download_records_the_address(self):
error = urllib.error.HTTPError("https://x/bios/a.bin", 404, "Not Found", None, None)
reason = ANetworkFailureNamesTheNetwork._fail(self, error)
self.assertIsInstance(reason, install.HttpReason)
self.assertTrue(reason.url.endswith("/bios/a.bin"), reason.url)
class NetworkFailuresInPlainWords(unittest.TestCase):
"""Offline, the run printed "[Errno -2] Name or service not known". A
beginner needs "no internet connection", and the raw text only under
--verbose."""
def _words(self, reason: BaseException, **kwargs) -> tuple[str, str]:
return install.network_words(urllib.error.URLError(reason), **kwargs)
def test_the_common_failures(self):
import ssl
cases = (
(socket.gaierror(-2, "Name or service not known"), "no internet connection"),
(socket.timeout("timed out"), "timed out"),
(ConnectionRefusedError(111, "Connection refused"), "refused the connection"),
(OSError(101, "Network is unreachable"), "no internet connection"),
(ssl.SSLCertVerificationError(1, "certificate verify failed"),
"certificate was rejected"),
)
with unittest.mock.patch.object(install, "_ca_bundle_found", return_value=True):
for reason, words in cases:
with self.subTest(reason=reason):
code, text = self._words(reason)
self.assertEqual(code, "network")
self.assertIn(words, text)
self.assertNotIn("Errno", text)
def test_verbose_keeps_the_raw_text(self):
_code, text = self._words(socket.gaierror(-2, "Name or service not known"),
verbose=True)
self.assertIn("Name or service not known", text)
def test_a_clock_in_the_past_is_named(self):
"""A handheld with no clock battery boots in 1980, and every
certificate then reads as not yet valid."""
import datetime
import ssl
error = ssl.SSLCertVerificationError(1, "certificate is not yet valid")
with unittest.mock.patch.object(install, "_ca_bundle_found", return_value=True):
code, text = self._words(error, today=datetime.date(1980, 1, 1))
self.assertEqual(code, "clock")
self.assertIn("The clock reads 1980-01-01", text)
self.assertIn("set the date", text)
code, _text = self._words(error, today=install.CLOCK_FLOOR)
self.assertEqual(code, "network")
def test_no_certificate_list_names_ssl_cert_file(self):
import ssl
error = ssl.SSLCertVerificationError(1, "unable to get local issuer certificate")
with unittest.mock.patch.object(install, "_ca_bundle_found", return_value=False):
code, text = self._words(error)
self.assertEqual(code, "no_ca")
self.assertIn("SSL_CERT_FILE", text)
def test_a_certificate_list_is_looked_for_where_openssl_and_systems_keep_it(self):
root = scratch_dir(self)
bundle = root / "cert.pem"
paths = SimpleNamespace(cafile=str(root / "none.pem"), capath=str(root / "none"),
openssl_cafile=str(root / "no.pem"),
openssl_capath=str(root / "no"))
with unittest.mock.patch("ssl.get_default_verify_paths", return_value=paths), \
unittest.mock.patch.object(install, "CA_BUNDLES", ()), \
unittest.mock.patch.dict(os.environ, {"SSL_CERT_FILE": "", "SSL_CERT_DIR": ""}):
self.assertFalse(install._ca_bundle_found())
with unittest.mock.patch.object(install, "CA_BUNDLES", (str(bundle),)):
self.assertFalse(install._ca_bundle_found())
bundle.write_text("x")
self.assertTrue(install._ca_bundle_found())
def test_a_file_list_out_of_reach_by_name_says_so(self):
env = {**os.environ, "RETROBIOS_BASE_URL": "https://retrobios-nohost.invalid",
"HOME": str(scratch_dir(self))}
proc = subprocess.run(
[sys.executable, str(REPO_ROOT / "install.py"), "--platform", "retroarch",
"--dest", str(scratch_dir(self)), "--check"],
env=env, capture_output=True, text=True, timeout=120, check=False,
stdin=subprocess.DEVNULL,
)
self.assertEqual(proc.returncode, 1)
self.assertIn("no internet connection", proc.stderr)
self.assertNotIn("Errno", proc.stderr)
if __name__ == "__main__":
unittest.main()
+370
View File
@@ -0,0 +1,370 @@
"""The --events stream, its schema and the installer page written from the code."""
from __future__ import annotations
import tests
from tests import scratch_dir
import argparse
import contextlib
import io
import json
import socket
import subprocess
import unittest
import unittest.mock
from pathlib import Path
from tests.netguard import module_guard
from tests.installsupport import REPO_ROOT, install, _install_run, _SyntheticTree, _events
# No connection off the loopback and no card of the host reaches a test:
# the module fails at its end otherwise.
setUpModule, tearDownModule = module_guard("test_install_events")
class SystemEventsFollowTheChoice(unittest.TestCase):
"""--system nintendo-gba wrote `system` events for nintendo-ds, gb and
gbc: SkyEmu's files carry four systems, and each was described by the
few of its files the choice kept."""
def test_only_the_chosen_systems_are_described(self):
files = [{"dest": "gba_bios.bin", "size": 1, "systems": ["nintendo-gba"]},
{"dest": "SkyEmu/dmg.bin", "size": 1,
"systems": ["nintendo-gb", "nintendo-gba"]}]
stream = io.StringIO()
install.EVENTS.open(stream)
self.addCleanup(setattr, install.EVENTS, "stream", None)
install._emit_check("retroarch", {}, Path("/b"), install.CheckTally(files, [], []),
chosen=["nintendo-gba"])
systems = [json.loads(line)["system"] for line in stream.getvalue().splitlines()
if json.loads(line)["event"] == "system"]
self.assertEqual(systems, ["nintendo-gba"])
stream.seek(0)
stream.truncate()
install._emit_check("retroarch", {}, Path("/b"), install.CheckTally(files, [], []))
systems = [json.loads(line)["system"] for line in stream.getvalue().splitlines()
if json.loads(line)["event"] == "system"]
self.assertEqual(systems, ["nintendo-gb", "nintendo-gba"])
class EventsDescribeTheRun(unittest.TestCase, _SyntheticTree):
"""--events: the handheld port reads the run as JSON lines on stdout and
never parses the text, which goes to stderr."""
def setUp(self):
self.env = self.serve_tree(self)
def _run(self, *argv: str) -> tuple[subprocess.CompletedProcess, list[dict]]:
proc = _install_run(self.env, "--platform", "retroarch", "--dest", str(self.bios),
"--events", "--jobs", "1", *argv)
self.assertNotIn("Traceback", proc.stderr)
return proc, _events(proc)
def _head(self, mode: str, installer: str) -> list[dict]:
return [
{"event": "start", "schema": install.EVENT_SCHEMA, "installer": installer,
"mode": mode},
{"event": "detect", "os": "linux", "os_name": install._os_name(),
"platforms": [{"platform": "retroarch", "name": "RetroArch",
"bios_dir": str(self.bios)}]},
# The systems Choose systems lists, before the files are read.
{"event": "choices", "platform": "retroarch",
"systems": ["sys-a", "sys-b", "other"]},
# Nothing set aside before the choice.
{"event": "set_aside", "platform": "retroarch", "dests": []},
# a.bin and b.bin are in place: the first read and the last.
{"event": "checking", "files_done": 1, "files_total": 2},
{"event": "checking", "files_done": 2, "files_total": 2},
{"event": "file", "platform": "retroarch", "dest": "a.bin",
"systems": ["sys-a"], "size": 100, "state": "present"},
{"event": "file", "platform": "retroarch", "dest": "b.bin",
"systems": ["sys-a"], "size": 150, "state": "wrong_hash"},
{"event": "file", "platform": "retroarch", "dest": "c.bin",
"systems": ["sys-b"], "size": 70, "state": "missing"},
{"event": "file", "platform": "retroarch", "dest": "d.bin",
"systems": [], "size": 25, "state": "missing"},
{"event": "system", "platform": "retroarch", "system": "other", "name": "other",
"files": 1, "present": 0, "missing": 1, "wrong_hash": 0, "bytes_needed": 25},
{"event": "system", "platform": "retroarch", "system": "sys-a", "name": "alpha",
"files": 2, "present": 1, "missing": 0, "wrong_hash": 1, "bytes_needed": 150},
{"event": "system", "platform": "retroarch", "system": "sys-b", "name": "sys-b",
"files": 1, "present": 0, "missing": 1, "wrong_hash": 0, "bytes_needed": 70},
{"event": "check", "platform": "retroarch", "name": "RetroArch",
"bios_dir": str(self.bios), "files": 4, "present": 1, "missing": 2,
"wrong_hash": 1, "bytes_needed": 245, "data_missing": 0,
"not_installable": 1},
# Every file of this list is the platform's own (cores: null).
{"event": "declared", "platform": "retroarch", "files": 4, "present": 1,
"missing": 2, "wrong_hash": 1, "bytes_needed": 245,
"dests": ["a.bin", "b.bin", "c.bin", "d.bin"]},
# No data tree in this list.
{"event": "data", "platform": "retroarch", "files": 0, "bytes": 0},
# The missing and the wrong copy, written: what the run holds
# against the free space.
{"event": "space", "platform": "retroarch", "bytes": 245},
# Each file not in place comes from the network: no copy here,
# no pack.
{"event": "fetch_plan", "platform": "retroarch", "files": 3, "bytes": 245,
"copied": [], "lacking": [], "data_files": 0, "data_bytes": 0},
]
SKIPPED = [{"reason": "not_found", "label": "not found", "files": 1, "required": 1}]
def test_a_check_run(self):
proc, events = self._run("--check", "--verbose")
self.assertEqual(proc.returncode, 3)
installer = events[0]["installer"]
self.assertEqual(events, self._head("check", installer) + [
{"event": "summary", "mode": "check", "installed": 0, "up_to_date": 1,
"pending": 3, "failed": [], "skipped": self.SKIPPED, "left_out": [], "kept": [],
"interrupted": False},
{"event": "end", "code": 3},
])
self.assertIn("1 in place, 2 missing, 1 wrong copy", proc.stderr)
self.assertFalse((self.bios / "c.bin").exists())
def test_an_install_run(self):
proc, events = self._run()
self.assertEqual(proc.returncode, 0, proc.stderr)
installer = events[0]["installer"]
kept = events[-2]["kept"]
self.assertEqual(len(kept), 1, kept)
self.assertEqual(Path(kept[0]["folder"]).parent, self.home / install.BACKUP_DIR)
progress = [event for event in events if event["event"] == "progress"]
for event in progress:
self.assertEqual(list(event), ["event", *install.EVENT_KEYS["progress"]])
done = [{"event": "downloaded", "dest": dest, "ok": True, "reason": "",
"size": size, "files_done": n, "files_total": 3, "bytes_done": total,
"bytes_total": 245}
for n, (dest, size, total) in enumerate(
(("c.bin", 70, 70), ("d.bin", 25, 95), ("b.bin", 150, 245)), 1)]
# bytes_done counts every byte arrived, as the progress events do:
# the worker may be into the next file before a finished one is
# reported, so it never runs back and covers at least the files done.
downloaded = [e for e in events if e["event"] == "downloaded"]
arrived = [e["bytes_done"] for e in downloaded]
self.assertEqual(arrived, sorted(arrived))
for event, expected in zip(downloaded, done):
self.assertGreaterEqual(event["bytes_done"], expected["bytes_done"])
self.assertLessEqual(event["bytes_done"], 245)
event["bytes_done"] = expected["bytes_done"]
# The last transfer: every byte came over the network.
self.assertEqual([e for e in events if e["event"] == "transfer"][-1],
{"event": "transfer", "bytes_local": 0, "bytes_network": 245})
record = next(e for e in events if e["event"] == "record")
# What the run placed, and its choice, the whole list: written
# right before the summary.
self.assertEqual({key: record[key] for key in ("folder", "status", "choice", "files")},
{"folder": str(self.bios), "status": "written",
"choice": ["--all-files"], "files": 3})
self.assertEqual(record["path"], str(self.home / install.BACKUP_DIR
/ f"{self.bios.name}.install.json"))
self.assertEqual([e for e in events if e["event"] not in ("progress", "transfer")],
self._head("install", installer) + [
{"event": "download", "platform": "retroarch", "files": 3, "bytes": 245},
*done,
record,
{"event": "summary", "mode": "install", "installed": 3, "up_to_date": 1,
"pending": 0, "failed": [], "skipped": self.SKIPPED, "left_out": [],
"kept": [{"folder": kept[0]["folder"], "files": 1, "bytes": 11}],
"interrupted": False},
{"event": "end", "code": 0},
])
self.assertEqual((self.bios / "b.bin").read_bytes(), self.FILES["bios/A/b.bin"])
self.assertIn("Done. 3 files installed", proc.stderr)
def test_every_event_has_its_schema_keys(self):
_proc, events = self._run()
for event in events:
self.assertEqual(list(event), ["event", *install.EVENT_KEYS[event["event"]]])
def test_a_file_list_out_of_reach_is_a_network_error(self):
# A port nothing listens on: the connection is refused at once.
with socket.socket() as probe:
probe.bind(("127.0.0.1", 0))
closed = probe.getsockname()[1]
env = {**self.env, "RETROBIOS_BASE_URL": f"http://127.0.0.1:{closed}"}
proc = _install_run(env, "--platform", "retroarch", "--dest", str(self.bios),
"--events", "--check")
events = _events(proc)
self.assertEqual([e["event"] for e in events], ["start", "detect", "error", "end"])
self.assertEqual(events[2]["reason"], "network")
self.assertEqual(events[2]["stage"], "manifest")
self.assertEqual(events[3], {"event": "end", "code": 1})
def test_nothing_detected_is_an_empty_platform_list(self):
proc = _install_run(self.env, "--events", "--check")
events = _events(proc)
self.assertEqual(proc.returncode, 1)
self.assertEqual(events[1]["platforms"], [])
self.assertEqual(events[-1], {"event": "end", "code": 1})
def test_the_schema_is_documented(self):
page = (REPO_ROOT / "wiki" / "installer.md").read_text(encoding="utf-8")
for kind, keys in install.EVENT_KEYS.items():
row = next((line for line in page.splitlines()
if line.startswith(f"| `{kind}` |")), None)
self.assertIsNotNone(row, kind)
for key in keys:
self.assertIn(f"`{key}`", row, f"{kind}.{key}")
def test_a_run_is_valid_against_the_published_schema(self):
try:
from jsonschema import Draft202012Validator
except ImportError:
self.skipTest("jsonschema is not installed")
schema = json.loads(EVENT_SCHEMA_PATH.read_text(encoding="utf-8"))
validator = Draft202012Validator(schema)
for argv in (("--check",), ()):
_proc, events = self._run(*argv)
for event in events:
errors = [error.message for error in validator.iter_errors(event)]
self.assertEqual(errors, [], event)
self.assertTrue(list(validator.iter_errors({"event": "file", "platform": "x"})))
self.assertTrue(list(validator.iter_errors(
{"event": "end", "code": 0, "extra": 1})))
EVENT_SCHEMA_PATH = REPO_ROOT / "schemas" / "install-events.schema.json"
class TheEventSchemaFileIsEventKeys(unittest.TestCase):
"""The table in the wiki was the only description of --events a reader
outside Python could check a stream against."""
def _generator(self):
from installer_build import eventschema
return eventschema
def test_the_committed_file_is_the_generated_one(self):
generator = self._generator()
self.assertEqual(EVENT_SCHEMA_PATH.read_text(encoding="utf-8"),
generator.render(generator.build_schema()),
"run python scripts/generate_event_schema.py")
def test_each_kind_has_exactly_its_keys(self):
schema = json.loads(EVENT_SCHEMA_PATH.read_text(encoding="utf-8"))
self.assertEqual(schema["properties"]["event"]["enum"], list(install.EVENT_KEYS))
for kind, keys in install.EVENT_KEYS.items():
shape = schema["$defs"][kind]
self.assertEqual(shape["required"], ["event", *keys], kind)
self.assertEqual(list(shape["properties"]), ["event", *keys], kind)
self.assertIs(shape["additionalProperties"], False, kind)
start = schema["$defs"]["start"]["properties"]["schema"]
self.assertEqual(start, {"const": install.EVENT_SCHEMA})
def test_a_key_without_a_type_stops_the_generator(self):
generator = self._generator()
with unittest.mock.patch.dict(install.EVENT_KEYS, {"new": ("untyped_key",)}), \
unittest.mock.patch.object(generator, "EVENT_KEYS", install.EVENT_KEYS), \
self.assertRaises(KeyError):
generator.build_schema()
def test_the_check_command_fails_on_a_stale_file(self):
import generate_event_schema as command
scratch = scratch_dir(self) / "events.schema.json"
scratch.write_text("{}\n", encoding="utf-8")
with contextlib.redirect_stdout(io.StringIO()), \
contextlib.redirect_stderr(io.StringIO()):
self.assertEqual(command.main(["--check", "--output", str(scratch)]), 1)
self.assertEqual(command.main(["--output", str(scratch)]), 0)
self.assertEqual(command.main(["--check", "--output", str(scratch)]), 0)
class TheInstallerPageIsWrittenFromTheCode(unittest.TestCase):
"""The options, events, exit codes and keys of wiki/installer.md were
written by hand beside the code that defines them, and every audit
found one of them behind it."""
PAGE = REPO_ROOT / "wiki" / "installer.md"
def setUp(self):
import generate_installer_doc
from installer_build import installerdoc
self.generator = generate_installer_doc
self.doc = installerdoc
self.scratch = scratch_dir(self) / "installer.md"
self.scratch.write_text(self.PAGE.read_text(encoding="utf-8"), encoding="utf-8")
def run_generator(self, *argv: str) -> tuple[int, str]:
err = io.StringIO()
with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(err):
code = self.generator.main([*argv, "--page", str(self.scratch)])
return code, err.getvalue()
def test_the_committed_page_is_the_generated_one(self):
with contextlib.redirect_stderr(io.StringIO()) as err:
self.assertEqual(self.generator.main(["--check"]), 0,
err.getvalue() + "run python scripts/generate_installer_doc.py")
def test_a_hand_edit_of_a_fact_fails_the_check(self):
page = self.scratch.read_text(encoding="utf-8")
for old, new in (("`--jobs N`", "`--jobs JOBS`"),
("| `end` | `code` |", "| `end` | `code`, `reason` |"),
("| Quit at once | none |", "| Quit at once | q |"),
# A code install.py never exits with.
("| `141` |", "| `142` | stopped twice |\n| `141` |")):
with self.subTest(edit=new):
self.assertIn(old, page)
self.scratch.write_text(page.replace(old, new, 1), encoding="utf-8")
self.assertEqual(self.run_generator("--check")[0], 1)
self.assertEqual(self.run_generator()[0], 0)
self.assertEqual(self.scratch.read_text(encoding="utf-8"), page)
def test_the_prose_is_kept(self):
page = self.scratch.read_text(encoding="utf-8")
old = "| Parallel downloads, 1 to 32, default 8 |"
self.assertIn(old, page)
edited = page.replace(old, "| Parallel downloads, from 1 to 32 |")
self.scratch.write_text(edited, encoding="utf-8")
self.assertEqual(self.run_generator("--check")[0], 0)
def test_what_the_code_gains_stops_it_until_the_page_says_it(self):
real = self.doc._install.build_parser
def gained() -> argparse.ArgumentParser:
parser = real()
parser.add_argument("--brand-new", action="store_true", help="new")
return parser
page = self.scratch.read_text(encoding="utf-8")
events = {**self.doc._install.EVENT_KEYS, "brand_new": ("thing",)}
for name, patch in (
("--brand-new", unittest.mock.patch.object(
self.doc._install, "build_parser", gained)),
("brand_new", unittest.mock.patch.object(
self.doc._install, "EVENT_KEYS", events))):
with self.subTest(gained=name), patch:
code, said = self.run_generator()
self.assertEqual(code, 1)
self.assertIn(name, said)
self.assertEqual(self.scratch.read_text(encoding="utf-8"), page)
self.assertEqual(self.run_generator("--check")[0], 1)
# The row added by hand, its first cell alone: the generator fills it.
close = "\n\n<!-- /generated: options -->"
self.scratch.write_text(
page.replace(close, "\n| `--brand-new` | Something new |" + close, 1),
encoding="utf-8")
with unittest.mock.patch.object(self.doc._install, "build_parser", gained):
self.assertEqual(self.run_generator()[0], 0)
self.assertIn('| <span id="option-brand-new"></span>`--brand-new` | Something new |',
self.scratch.read_text(encoding="utf-8"))
def test_the_exit_codes_are_the_ones_install_py_exits_with(self):
self.assertEqual(self.doc.exit_codes(), [0, 1, 2, 3, 4, 130, 141])
def test_a_missing_marker_stops_it(self):
page = self.scratch.read_text(encoding="utf-8")
self.scratch.write_text(page.replace("<!-- generated: route-keys -->\n", ""),
encoding="utf-8")
code, said = self.run_generator("--check")
self.assertEqual(code, 1)
self.assertIn("route-keys", said)
if __name__ == "__main__":
unittest.main()
+353
View File
@@ -0,0 +1,353 @@
"""A file the user had is kept: a differing BIOS is set aside and the run says where."""
from __future__ import annotations
import tests
from tests import scratch_dir
import contextlib
import hashlib
import io
import os
import stat
import shutil
import unittest
import unittest.mock
from pathlib import Path
from tests.netguard import module_guard
from tests.installsupport import install, _install_run, _FakeResponse, _SyntheticTree, _events
# No connection off the loopback and no card of the host reaches a test:
# the module fails at its end otherwise.
setUpModule, tearDownModule = module_guard("test_install_kept")
class AFileYouHadIsKept(unittest.TestCase):
"""A BIOS file whose contents differ was replaced and lost: a personal
dump or another revision could not be brought back by deleting what the
installer wrote. It is moved beside the BIOS folder first, never inside,
where a core that finds its BIOS by content could take it."""
NEW = b"the collection's copy"
OLD = b"a dump of my own"
def setUp(self):
install.KEPT.reset()
self.addCleanup(install.KEPT.reset)
self.home = scratch_dir(self)
self.bios = self.home / "system"
self.bios.mkdir()
def _entry(self, dest: str) -> dict:
return {"dest": dest, "repo_path": f"bios/{dest}", "size": len(self.NEW),
"sha1": hashlib.sha1(self.NEW).hexdigest(),
"sha256": hashlib.sha256(self.NEW).hexdigest()}
def _download(self, dest: str):
with unittest.mock.patch.object(install.urllib.request, "urlopen",
lambda *a, **k: _FakeResponse(self.NEW)):
return install._download_one(self._entry(dest), self.bios)
def _run_folder(self) -> Path:
runs = list((self.home / install.BACKUP_DIR).iterdir())
self.assertEqual(len(runs), 1, runs)
return runs[0]
def test_a_differing_file_is_kept_with_its_relative_path(self):
(self.bios / "sub").mkdir()
(self.bios / "sub" / "x.bin").write_bytes(self.OLD)
self.assertEqual(self._download("sub/x.bin"), ("sub/x.bin", True, ""))
self.assertEqual((self.bios / "sub" / "x.bin").read_bytes(), self.NEW)
run = self._run_folder()
self.assertEqual((run / "system" / "sub" / "x.bin").read_bytes(), self.OLD)
self.assertEqual(install.KEPT.summary(),
[{"folder": str(run), "files": 1, "bytes": len(self.OLD)}])
# Nothing new sits inside the BIOS folder, where a scan would read it.
self.assertEqual(sorted(p.name for p in self.bios.rglob("*")), ["sub", "x.bin"])
def test_an_identical_file_is_not_copied(self):
(self.bios / "x.bin").write_bytes(self.NEW)
self.assertEqual(self._download("x.bin"), ("x.bin", True, ""))
self.assertFalse((self.home / install.BACKUP_DIR).exists())
self.assertEqual(install.KEPT.summary(), [])
def test_a_missing_file_keeps_nothing(self):
self.assertEqual(self._download("x.bin"), ("x.bin", True, ""))
self.assertFalse((self.home / install.BACKUP_DIR).exists())
def test_a_second_replacement_keeps_the_first_file(self):
# Two lists in one run writing one path: what is kept is the user's.
(self.bios / "x.bin").write_bytes(self.OLD)
self._download("x.bin")
(self.bios / "x.bin").write_bytes(b"what this run wrote first")
self._download("x.bin")
self.assertEqual((self._run_folder() / "system" / "x.bin").read_bytes(), self.OLD)
def test_a_file_that_cannot_be_kept_stays(self):
(self.bios / "x.bin").write_bytes(self.OLD)
def refuse(*_args):
raise PermissionError(13, "Permission denied")
with unittest.mock.patch.object(install, "_move_aside", refuse):
_dest, ok, reason = self._download("x.bin")
self.assertFalse(ok)
self.assertIn(install.NOT_KEPT, reason)
self.assertEqual((self.bios / "x.bin").read_bytes(), self.OLD)
self.assertEqual(sorted(p.name for p in self.bios.iterdir()), ["x.bin"])
def test_a_symlinked_subfolder_keeps_working(self):
elsewhere = self.home / "shadPS4" / "sys_modules"
elsewhere.mkdir(parents=True)
(elsewhere / "lib.sprx").write_bytes(self.OLD)
(self.bios / "shadps4").mkdir()
(self.bios / "shadps4" / "sys_modules").symlink_to(elsewhere)
self.assertTrue(self._download("shadps4/sys_modules/lib.sprx")[1])
self.assertEqual((elsewhere / "lib.sprx").read_bytes(), self.NEW)
self.assertTrue((self.bios / "shadps4" / "sys_modules").is_symlink())
run = self._run_folder()
kept = run / "system" / "shadps4" / "sys_modules" / "lib.sprx"
self.assertEqual(kept.read_bytes(), self.OLD)
with contextlib.redirect_stdout(io.StringIO()):
self.assertEqual(install.restore_kept(run), 0)
self.assertEqual((elsewhere / "lib.sprx").read_bytes(), self.OLD)
self.assertTrue((self.bios / "shadps4" / "sys_modules").is_symlink())
def test_a_link_at_the_name_is_not_kept(self):
# The replace takes the link's place: the file it points to is untouched.
target = self.home / "mine.bin"
target.write_bytes(self.OLD)
(self.bios / "x.bin").symlink_to(target)
self.assertTrue(self._download("x.bin")[1])
self.assertEqual(target.read_bytes(), self.OLD)
self.assertFalse((self.home / install.BACKUP_DIR).exists())
@unittest.skipIf(hasattr(os, "geteuid") and os.geteuid() == 0, "root writes anywhere")
def test_without_room_beside_the_folder_it_goes_inside(self):
(self.bios / "x.bin").write_bytes(self.OLD)
self.home.chmod(0o555)
self.addCleanup(self.home.chmod, 0o755)
self.assertTrue(self._download("x.bin")[1])
runs = list((self.bios / install.BACKUP_DIR).iterdir())
self.assertEqual(len(runs), 1)
self.assertEqual((runs[0] / "x.bin").read_bytes(), self.OLD)
with contextlib.redirect_stdout(io.StringIO()):
self.assertEqual(install.restore_kept(runs[0]), 0)
self.assertEqual((self.bios / "x.bin").read_bytes(), self.OLD)
def test_a_data_tree_file_is_kept(self):
import zipfile
tree = self.bios / "dolphin-emu" / "Sys"
tree.mkdir(parents=True)
(tree / "a.ini").write_bytes(self.OLD)
archive = self.home / "src.zip"
with zipfile.ZipFile(archive, "w") as zf:
zf.writestr("Sys/a.ini", self.NEW)
entry = {"ref": "d", "dest": "dolphin-emu", "url": "https://example.invalid/d.zip",
"type": "zip", "files": {"Sys/a.ini": {
"sha1": hashlib.sha1(self.NEW).hexdigest(), "size": len(self.NEW)}}}
def fetch(url, directory, progress=None, keep=None):
copy = Path(directory) / "a.zip"
shutil.copy(archive, copy)
return copy
with unittest.mock.patch.object(install, "_fetch_archive", fetch):
counts = install.install_data_directory(entry, self.bios)
self.assertEqual(counts["installed"], 1)
self.assertEqual((tree / "a.ini").read_bytes(), self.NEW)
kept = self._run_folder() / "system" / "dolphin-emu" / "Sys" / "a.ini"
self.assertEqual(kept.read_bytes(), self.OLD)
def test_a_standalone_copy_keeps_the_emulators_file(self):
(self.bios / "ps2-0230a.bin").write_bytes(self.NEW)
pcsx2 = self.home / "PCSX2" / "bios"
pcsx2.mkdir(parents=True)
(pcsx2 / "ps2-0230a.bin").write_bytes(self.OLD)
manifest = {"files": [{"dest": "ps2-0230a.bin"}], "standalone_copies": [
{"pattern": "ps2-*.bin", "emulator": "pcsx2", "targets": {"linux": [str(pcsx2)]}}]}
self.assertEqual(install.do_standalone_copies(manifest, self.bios, "linux"), (1, 0, 0))
self.assertEqual((pcsx2 / "ps2-0230a.bin").read_bytes(), self.NEW)
runs = list((self.home / "PCSX2" / install.BACKUP_DIR).iterdir())
self.assertEqual((runs[0] / "bios" / "ps2-0230a.bin").read_bytes(), self.OLD)
# Copied again, the file is the same: nothing more is kept.
self.assertEqual(install.do_standalone_copies(manifest, self.bios, "linux"), (1, 0, 0))
self.assertEqual(install.KEPT.summary()[0]["files"], 1)
def test_a_kept_file_inside_the_tree_is_no_copy_source(self):
inside = self.bios / install.BACKUP_DIR / "20261022-153000"
inside.mkdir(parents=True)
(inside / "ps2-old.bin").write_bytes(self.OLD)
pcsx2 = self.home / "PCSX2" / "bios"
pcsx2.mkdir(parents=True)
manifest = {"files": [{"dest": "ps2-0230a.bin"}], "standalone_copies": [
{"pattern": "ps2-*.bin", "emulator": "pcsx2", "targets": {"linux": [str(pcsx2)]}}]}
self.assertEqual(install.do_standalone_copies(manifest, self.bios, "linux"), (0, 0, 0))
def test_restore_refuses_a_folder_that_is_no_backup(self):
with contextlib.redirect_stderr(io.StringIO()) as err:
self.assertEqual(install.restore_kept(self.bios), 2)
self.assertIn(install.BACKUP_DIR, err.getvalue())
def test_a_linked_backup_holder_is_not_written_through(self):
# Someone with write access to a shared parent folder placed a link
# where the holder goes: the user's file must not land at its target.
elsewhere = self.home / "elsewhere"
elsewhere.mkdir()
(self.home / install.BACKUP_DIR).symlink_to(elsewhere)
(self.bios / "x.bin").write_bytes(self.OLD)
self.assertTrue(self._download("x.bin")[1])
self.assertEqual(list(elsewhere.iterdir()), [])
inside = list((self.bios / install.BACKUP_DIR).iterdir())
self.assertEqual(len(inside), 1)
self.assertEqual((inside[0] / "x.bin").read_bytes(), self.OLD)
def test_a_holder_with_every_name_taken_is_not_reused(self):
# Folders planted under every dated name of this run: none of them
# may receive the user's file.
holder = self.home / install.BACKUP_DIR
stamp = "20260101-000000"
for n in range(1, 1000):
(holder / (stamp if n == 1 else f"{stamp}-{n}")).mkdir(parents=True)
(self.bios / "x.bin").write_bytes(self.OLD)
with unittest.mock.patch.object(install.time, "strftime", return_value=stamp):
self.assertTrue(self._download("x.bin")[1])
self.assertEqual(sum(1 for p in holder.rglob("x.bin")), 0)
inside = list((self.bios / install.BACKUP_DIR).iterdir())
self.assertEqual(len(inside), 1)
self.assertEqual((inside[0] / "x.bin").read_bytes(), self.OLD)
def test_a_holder_swapped_after_the_check_is_noticed(self):
holder = self.home / install.BACKUP_DIR
holder.mkdir()
checked = install._check_holder(holder)
self.assertTrue(install._same_folder(holder, checked))
holder.rename(self.home / "moved")
(self.home / "elsewhere").mkdir()
holder.symlink_to(self.home / "elsewhere")
self.assertFalse(install._same_folder(holder, checked))
with self.assertRaises(OSError):
install._check_holder(holder)
@unittest.skipUnless(hasattr(os, "getuid"), "user ids are POSIX")
def test_a_holder_another_user_owns_is_refused(self):
holder = self.home / install.BACKUP_DIR
holder.mkdir()
real = os.lstat
def planted(path, *args, **kwargs):
info = real(path, *args, **kwargs)
if Path(path) != holder:
return info
fields = list(info)
fields[stat.ST_UID] = info.st_uid + 2
return os.stat_result(fields)
with self.assertRaises(OSError), \
unittest.mock.patch.object(install.os, "lstat", side_effect=planted):
install._check_holder(holder)
install._check_holder(holder)
@unittest.skipUnless(hasattr(os, "getuid"), "user ids are POSIX")
def test_a_storage_with_one_owner_for_every_file_takes_the_holder(self):
# Android's shared storage, seen from Termux on Android 17: a folder
# the run has just made is shown as another user's, like every other
# file there. Refusing it left 25 files unreplaced on a real phone.
holder = self.home / install.BACKUP_DIR
holder.mkdir()
with unittest.mock.patch.object(install.os, "getuid", return_value=os.getuid() + 1):
install._check_holder(holder)
(self.bios / "x.bin").write_bytes(self.OLD)
self.assertTrue(self._download("x.bin")[1])
self.assertEqual((self._run_folder() / "system" / "x.bin").read_bytes(), self.OLD)
def test_restore_refuses_a_folder_the_installer_did_not_write(self):
planted = self.home / install.BACKUP_DIR / "20260101-000000"
(planted / "system").mkdir(parents=True)
(planted / "system" / "x.bin").write_bytes(b"not the user's")
(self.bios / "x.bin").write_bytes(self.OLD)
with contextlib.redirect_stderr(io.StringIO()):
self.assertEqual(install.restore_kept(planted), 2)
self.assertEqual((self.bios / "x.bin").read_bytes(), self.OLD)
def test_restore_does_not_follow_links_inside_the_backup(self):
(self.bios / "x.bin").write_bytes(self.OLD)
self.assertTrue(self._download("x.bin")[1])
run = self._run_folder()
outside = self.home / "outside"
(outside / "deep").mkdir(parents=True)
(outside / "deep" / "y.bin").write_bytes(b"foreign")
(run / "system" / "linked").symlink_to(outside)
with contextlib.redirect_stdout(io.StringIO()):
self.assertEqual(install.restore_kept(run), 0)
self.assertEqual((self.bios / "x.bin").read_bytes(), self.OLD)
self.assertFalse((self.bios / "linked").exists())
class KeptFilesEndToEnd(unittest.TestCase, _SyntheticTree):
"""The run says what it kept and where, --dry-run what it would keep
and writes nothing, and --restore puts the original bytes back."""
def setUp(self):
self.env = self.serve_tree(self)
def test_dry_run_says_what_would_be_kept_and_writes_nothing(self):
proc = _install_run(self.env, "--platform", "retroarch", "--dest", str(self.bios),
"--dry-run", "--no-input")
self.assertEqual(proc.returncode, 0, proc.stderr)
self.assertIn("Would keep 1 file already there (11 B)", proc.stdout)
self.assertIn(str(self.home / install.BACKUP_DIR), proc.stdout)
self.assertFalse((self.home / install.BACKUP_DIR).exists())
self.assertEqual((self.bios / "b.bin").read_bytes(), b"other bytes")
events = _events(_install_run(self.env, "--platform", "retroarch", "--dest",
str(self.bios), "--dry-run", "--events"))
kept = events[-2]["kept"]
self.assertEqual([(item["files"], item["bytes"]) for item in kept], [(1, 11)])
self.assertFalse((self.home / install.BACKUP_DIR).exists())
def test_install_then_restore(self):
proc = _install_run(self.env, "--platform", "retroarch", "--dest", str(self.bios),
"--no-input", "--jobs", "1", "--verbose")
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
self.assertEqual((self.bios / "b.bin").read_bytes(), self.FILES["bios/A/b.bin"])
# The install record sits beside the run folders, a file of its own.
held = list((self.home / install.BACKUP_DIR).iterdir())
runs = [path for path in held if path.is_dir()]
self.assertEqual(sorted(path.name for path in held if not path.is_dir()),
[f"{self.bios.name}.install.json"])
self.assertEqual(len(runs), 1)
self.assertIn("Kept 1 file already there (11 B), which the collection's copies "
f"replaced, in {runs[0]}", proc.stdout)
self.assertIn(f"--restore {runs[0]}", proc.stdout)
# a.bin held the expected bytes: it is not among the kept files.
self.assertEqual([p.name for p in (runs[0] / "bios").iterdir()], ["b.bin"])
refused = _install_run(self.env, "--restore", str(runs[0]), "--platform", "retroarch")
self.assertEqual(refused.returncode, 2)
self.assertIn("--restore", refused.stderr)
looked = _install_run(self.env, "--restore", str(runs[0]), "--dry-run")
self.assertEqual(looked.returncode, 0, looked.stderr)
self.assertIn(f"would put back {self.bios / 'b.bin'}", looked.stdout)
self.assertEqual((self.bios / "b.bin").read_bytes(), self.FILES["bios/A/b.bin"])
back = _install_run(self.env, "--restore", str(runs[0]))
self.assertEqual(back.returncode, 0, back.stdout + back.stderr)
self.assertEqual((self.bios / "b.bin").read_bytes(), b"other bytes")
self.assertEqual((self.bios / "a.bin").read_bytes(), self.FILES["bios/A/a.bin"])
self.assertIn("Put back 1 file", back.stdout)
# The backup stays until the user deletes it.
self.assertTrue((runs[0] / "bios" / "b.bin").is_file())
# A second install keeps the restored file in a new folder.
again = _install_run(self.env, "--platform", "retroarch", "--dest", str(self.bios),
"--no-input", "--jobs", "1")
self.assertEqual(again.returncode, 0, again.stderr)
self.assertEqual(len([path for path in (self.home / install.BACKUP_DIR).iterdir()
if path.is_dir()]), 2)
if __name__ == "__main__":
unittest.main()
+640
View File
@@ -0,0 +1,640 @@
"""The installer replays the pack builder: a narrowed install holds what the narrowed pack holds."""
from __future__ import annotations
import tests
import hashlib
import importlib.util
import json
import os
import tempfile
import unittest
from unittest import mock
from pathlib import Path
from packs import archive, coverage, platformpack
from packs import manifest as packs_manifest
REPO_ROOT = Path(__file__).resolve().parent.parent
_spec = importlib.util.spec_from_file_location("install", REPO_ROOT / "install.py")
install = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(install)
class InstallMatchesThePack(unittest.TestCase):
"""Narrowing the full manifest gives what the narrowed build carries."""
PLATFORM = "recalbox"
@classmethod
def setUpClass(cls):
db_path = REPO_ROOT / "database.json"
if not db_path.exists():
raise unittest.SkipTest("database.json is not built")
from model.profiles import load_emulator_profiles
cls.db = json.loads(db_path.read_text(encoding="utf-8"))
cls.profiles = load_emulator_profiles(str(REPO_ROOT / "emulators"))
cls.full = cls._manifest(None)
@classmethod
def _manifest(cls, regions):
return packs_manifest.generate_manifest(
cls.PLATFORM, str(REPO_ROOT / "platforms"), cls.db,
str(REPO_ROOT / "bios"), str(REPO_ROOT / "platforms" / "_registry.yml"),
emulators_dir=str(REPO_ROOT / "emulators"), emu_profiles=cls.profiles,
regions=regions, offline=True,
)
def _narrowed(self, systems, regions):
files, omitted = install.narrow(
self.full["files"], self.full["omitted_files"], systems, [], regions
)
return {f["dest"] for f in files}, {o["dest"] for o in omitted}
def test_regions(self):
for regions in (["north-america"], ["japan", "europe"]):
with self.subTest(regions=regions):
built = self._manifest(regions)
files, omitted = self._narrowed([], regions)
self.assertEqual(files, {f["dest"] for f in built["files"]})
self.assertEqual(omitted, {o["dest"] for o in built["omitted_files"]})
class InstallMatchesASystemPack(unittest.TestCase):
"""`install.py --system X --region R` against a real `generate_pack.py
--system X --region R` build. The cases are the ones a review reproduced:
ColecoVision's extras filed at destinations another system declares, and
PicoDrive's Mega CD variant group spread over two systems. The last pick
holds MAME device sets filed under several systems (systems:), under one,
and under none."""
PLATFORM = "retroarch"
CASES = (
(["coleco-colecovision"], []),
(["sega-mega-cd"], ["north-america"]),
(["sony-playstation"], ["japan"]),
(["elektronika-bk"], []),
)
@classmethod
def setUpClass(cls):
db_path = REPO_ROOT / "database.json"
if not db_path.exists():
raise unittest.SkipTest("database.json is not built")
from packs import manifest as packs_manifest
from model.profiles import load_emulator_profiles
from model.platforms import load_platform_config
cls.db = json.loads(db_path.read_text(encoding="utf-8"))
cls.profiles = load_emulator_profiles(str(REPO_ROOT / "emulators"))
cls.base = load_platform_config(
cls.PLATFORM, str(REPO_ROOT / "platforms")
).get("base_destination", "")
cls.full = packs_manifest.generate_manifest(
cls.PLATFORM, str(REPO_ROOT / "platforms"), cls.db,
str(REPO_ROOT / "bios"), str(REPO_ROOT / "platforms" / "_registry.yml"),
emulators_dir=str(REPO_ROOT / "emulators"), emu_profiles=cls.profiles,
offline=True,
)
def _packed(self, systems, regions) -> set[str]:
import zipfile
with tempfile.TemporaryDirectory() as out:
zip_path = platformpack.generate_pack(
self.PLATFORM, str(REPO_ROOT / "platforms"), self.db,
str(REPO_ROOT / "bios"), out,
emulators_dir=str(REPO_ROOT / "emulators"),
emu_profiles=self.profiles, system_filter=systems,
regions=regions or None, offline=True,
)
with zipfile.ZipFile(zip_path) as zf:
names = set(zf.namelist())
prefix = f"{self.base}/" if self.base else ""
return {n[len(prefix):] if prefix and n.startswith(prefix) else n for n in names}
def test_each_case(self):
known = {f["dest"] for f in self.full["files"]}
for systems, regions in self.CASES:
with self.subTest(systems=systems, regions=regions):
files, _omitted = install.narrow(
self.full["files"], self.full["omitted_files"],
systems, [], regions,
)
self.assertEqual(
{f["dest"] for f in files}, self._packed(systems, regions) & known
)
class DataTreesFollowTheSystemsTheyServe(InstallMatchesASystemPack):
"""The data trees a system choice keeps, installer against pack. GBA
alone took FBNeo's cheats and samples, 3,472 files, because the trees
listed every FBNeo system. The Mega Drive took no cheat: the tree said
sega-megadrive where RetroArch's list says sega-mega-drive."""
CASES = ((["nintendo-gba"], []), (["sega-mega-drive"], []), (["arcade"], []))
def _packed(self, systems, regions) -> set[str]:
import zipfile
from model.registry import load_data_dir_registry
# The trees come from the registry the manifest reads by itself.
with tempfile.TemporaryDirectory() as out:
zip_path = platformpack.generate_pack(
self.PLATFORM, str(REPO_ROOT / "platforms"), self.db,
str(REPO_ROOT / "bios"), out,
emulators_dir=str(REPO_ROOT / "emulators"),
emu_profiles=self.profiles, system_filter=systems,
regions=regions or None, offline=True,
data_registry=load_data_dir_registry(str(REPO_ROOT / "platforms")),
)
with zipfile.ZipFile(zip_path) as zf:
names = set(zf.namelist())
prefix = f"{self.base}/" if self.base else ""
return {n[len(prefix):] if prefix and n.startswith(prefix) else n for n in names}
def _members(self, trees) -> set[str]:
return {f"{tree['dest']}/{rel}" if tree["dest"] else rel
for tree in trees for rel in tree["files"]}
def test_each_case(self):
everything = self._members(self.full.get("data_directories") or [])
for systems, regions in self.CASES:
with self.subTest(systems=systems):
files, _omitted = install.narrow(
self.full["files"], self.full["omitted_files"], systems, [], regions)
trees = install.select_data_directories(self.full, files, True, systems=systems)
refs = {tree["ref"] for tree in trees}
if systems == ["nintendo-gba"]:
self.assertFalse({"fbneo-cheats", "fbneo-samples"} & refs)
if systems == ["sega-mega-drive"]:
self.assertIn("fbneo-cheats", refs)
self.assertNotIn("fbneo-samples", refs)
if systems == ["arcade"]:
self.assertTrue({"fbneo-cheats", "fbneo-samples"} <= refs)
self.assertEqual(self._members(trees),
self._packed(systems, regions) & everything)
class InstallMatchesATargetManifest(unittest.TestCase):
"""`install.py --target T` against the manifest the builder writes for T.
A target list names cores the way the buildbot does (`mednafen_psx`)
while a file names the profile that reads it (`beetle_psx`), and the
builder drops a platform system no available core reads. The installer
did neither: on RetroArch desktop it kept every system and lost the
extras of five cores. A destination two cores read was also filed under
the first alone, so a target without that core lost it, or got the
first core's copy where the other core wants different bytes
(megaplay.zip for mame and mame2003 on Switch).
"""
CASES = (
("retroarch", "desktop"),
("retroarch", "switch"),
("batocera", "x86_64"),
("batocera", "bcm2711"),
("retropie", "rpi4"),
("emudeck", "steamos"),
# A core only Batocera's x86_64 downloader brings, a missing file two
# cores read, a file another emulator installs itself, and a name a
# launcher opens that another core's copy used to fill.
("batocera", "jh7110"),
("knulli", "h700"),
("rocknix", "sdm845"),
("retropie", "x86"),
("retropie", "rpi1"),
)
@classmethod
def setUpClass(cls):
db_path = REPO_ROOT / "database.json"
if not db_path.exists():
raise unittest.SkipTest("database.json is not built")
from model.profiles import load_emulator_profiles
from model.targets import load_target_config
cls.load_target_config = staticmethod(load_target_config)
cls.db = json.loads(db_path.read_text(encoding="utf-8"))
cls.profiles = load_emulator_profiles(str(REPO_ROOT / "emulators"))
def _manifest(self, platform, target_cores=None, target=None):
return packs_manifest.generate_manifest(
platform, str(REPO_ROOT / "platforms"), self.db,
str(REPO_ROOT / "bios"), str(REPO_ROOT / "platforms" / "_registry.yml"),
emulators_dir=str(REPO_ROOT / "emulators"), emu_profiles=self.profiles,
target_cores=target_cores, target_name=target, offline=True,
)
def test_each_target(self):
for platform, target in self.CASES:
with self.subTest(platform=platform, target=target):
cores = sorted(self.load_target_config(
platform, target, str(REPO_ROOT / "platforms")
))
full = self._manifest(platform)
built = self._manifest(platform, set(cores), target)
files, omitted = install.narrow_to_cores(full, cores, target=target)
# A case-insensitive platform reads one file under both
# spellings (BlastEm's 32X_M_BIOS.bin, PicoDrive's .BIN).
from model.platforms import load_platform_config
config = load_platform_config(platform, str(REPO_ROOT / "platforms"))
fold = str.lower if config.get("case_insensitive_fs") else str
self.assertEqual(
{(fold(f["dest"]), f["sha1"]) for f in files},
{(fold(f["dest"]), f["sha1"]) for f in built["files"]},
)
self.assertEqual(
{o["dest"] for o in omitted},
{o["dest"] for o in built["omitted_files"]},
)
class AHeldFileTheInstallerCannotFetch(unittest.TestCase):
"""The builder holds a release asset this offline run has no copy of.
jp.bin is registered in .gitignore and indexed by the database, as every
file of the large-files release is, but neither the checkout nor
.cache/large/ holds it. The builder counts it held, so it displaces the
American fallback for --region japan, while the manifest can only list
it as omitted. Counted unheld there, the installer kept the fallback the
pack drops."""
def setUp(self):
from model import profiles
from model.platforms import _platform_config_cache
from model.profiles import emulator_profiles_cache
from collection.indexes import build_indexes
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
previous = os.getcwd()
os.chdir(tmp.name)
self.addCleanup(os.chdir, previous)
Path("bios/Sys").mkdir(parents=True)
Path("bios/Sys/us.bin").write_bytes(b"us")
files = {}
for name, payload in (("us.bin", b"us"), ("jp.bin", b"jp")):
sha1 = hashlib.sha1(payload).hexdigest()
files[sha1] = {"path": f"bios/Sys/{name}", "name": name,
"size": len(payload), "sha1": sha1,
"md5": hashlib.md5(payload).hexdigest(),
"sha256": hashlib.sha256(payload).hexdigest(),
"crc32": "00000001"}
# The registry of release assets: listed there, so a checkout has
# no copy, and this offline run fetches none.
Path(".gitignore").write_text("bios/Sys/jp.bin\n")
registered = mock.patch(
"model.largefiles.load_registered_paths", return_value=["bios/Sys/jp.bin"]
)
registered.start()
self.addCleanup(registered.stop)
self.db = {"files": files, "indexes": build_indexes(files, {})}
Path("platforms").mkdir()
Path("platforms/p.yml").write_text(json.dumps({
"platform": "P", "version": "2.1", "verification_mode": "existence", "cores": ["core"],
"systems": {"sys": {"files": []}},
}))
Path("platforms/_registry.yml").write_text(
json.dumps({"platforms": {"p": {"config": "p.yml"}}})
)
Path("emulators").mkdir()
Path("emulators/core.yml").write_text(json.dumps({
"emulator": "core", "type": "libretro", "systems": ["sys"], "files": [
{"name": "jp.bin", "system": "sys", "region": ["japan"],
"required": True},
{"name": "us.bin", "system": "sys", "region": ["north-america"],
"required": True},
]}))
_platform_config_cache.clear()
emulator_profiles_cache.clear()
self.addCleanup(_platform_config_cache.clear)
self.addCleanup(emulator_profiles_cache.clear)
self.profiles = profiles.load_emulator_profiles("emulators")
def _manifest(self, regions):
return packs_manifest.generate_manifest(
"p", "platforms", self.db, "bios", "platforms/_registry.yml",
emulators_dir="emulators", emu_profiles=self.profiles,
regions=regions, offline=True,
)
def test_the_installer_drops_the_fallback_the_pack_drops(self):
full = self._manifest(None)
omitted = {o["dest"]: o for o in full["omitted_files"]}
self.assertTrue(omitted["jp.bin"].get("held"))
built = self._manifest(["japan"])
self.assertNotIn("us.bin", {f["dest"] for f in built["files"]})
files, still = install.narrow(
full["files"], full["omitted_files"], [], [], ["japan"]
)
self.assertEqual({f["dest"] for f in files}, {f["dest"] for f in built["files"]})
self.assertEqual(
{o["dest"] for o in still}, {o["dest"] for o in built["omitted_files"]}
)
class AStandaloneLayoutBelowTheBiosFolder(unittest.TestCase):
"""DuckStation reads `bios/<name>` standalone, which a platform whose
base destination is `bios` places in that folder itself. The collector
deduplicated the path at `bios/bios/<name>` while the builder wrote it
at `bios/<name>`, so another core's claim on the same file took the
path under a second key: `--region north-america` withdrew one key and
the pack kept the European BIOS the installer drops."""
def setUp(self):
from model import profiles
from model.platforms import _platform_config_cache
from model.profiles import emulator_profiles_cache
from collection.indexes import build_indexes
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
previous = os.getcwd()
os.chdir(tmp.name)
self.addCleanup(os.chdir, previous)
Path("bios/Sys").mkdir(parents=True)
files = {}
for name in ("us.bin", "eu.bin", "jp.bin"):
payload = name.encode()
Path(f"bios/Sys/{name}").write_bytes(payload)
sha1 = hashlib.sha1(payload).hexdigest()
files[sha1] = {"path": f"bios/Sys/{name}", "name": name,
"size": len(payload), "sha1": sha1,
"md5": hashlib.md5(payload).hexdigest(),
"sha256": hashlib.sha256(payload).hexdigest(),
"crc32": "00000001"}
self.db = {"files": files, "indexes": build_indexes(files, {})}
Path("platforms").mkdir()
Path("platforms/p.yml").write_text(json.dumps({
"platform": "P", "version": "2.1", "verification_mode": "existence",
"base_destination": "bios", "cores": ["dsa", "other"],
"standalone_cores": ["dsa"], "systems": {"sys": {"files": []}},
}))
Path("platforms/_registry.yml").write_text(
json.dumps({"platforms": {"p": {"config": "p.yml"}}})
)
Path("emulators").mkdir()
Path("emulators/dsa.yml").write_text(json.dumps({
"emulator": "dsa", "type": "standalone + libretro", "systems": ["sys"],
"files": [
{"name": name, "standalone_path": f"bios/{name}", "system": "sys",
"region": [region_name], "required": False}
for name, region_name in (
("us.bin", "north-america"), ("eu.bin", "europe"),
("jp.bin", "japan"),
)
]}))
Path("emulators/other.yml").write_text(json.dumps({
"emulator": "other", "type": "libretro", "systems": ["sys"], "files": [
{"name": name, "system": "sys", "required": False}
for name in ("eu.bin", "jp.bin")
]}))
_platform_config_cache.clear()
emulator_profiles_cache.clear()
self.addCleanup(_platform_config_cache.clear)
self.addCleanup(emulator_profiles_cache.clear)
self.profiles = profiles.load_emulator_profiles("emulators")
def _manifest(self, regions):
return packs_manifest.generate_manifest(
"p", "platforms", self.db, "bios", "platforms/_registry.yml",
emulators_dir="emulators", emu_profiles=self.profiles,
regions=regions, offline=True,
)
def test_one_path_one_entry(self):
full = self._manifest(None)
dests = [f["dest"] for f in full["files"]]
self.assertEqual(sorted(dests), ["eu.bin", "jp.bin", "us.bin"])
def test_the_installer_keeps_what_the_pack_keeps(self):
full = self._manifest(None)
built = self._manifest(["north-america"])
self.assertEqual({f["dest"] for f in built["files"]}, {"us.bin"})
files, _omitted = install.narrow(
full["files"], full["omitted_files"], [], [], ["north-america"]
)
self.assertEqual({f["dest"] for f in files}, {f["dest"] for f in built["files"]})
class AnExtraBesideItsDeclarationBelowTheBiosFolder(unittest.TestCase):
"""RetroDECK declares bios/scph5501.bin and writes core files below bios/.
Beetle PSX's scph5501.bin was grouped bare, a key whose region lookup
another core leaves untagged, while the declaration's key resolved to
DuckStation's American tag: `--region japan` dropped the declaration and
the pack kept the file through the core's claim, while the installer,
which records the path once, dropped it."""
def setUp(self):
from model import profiles
from model.platforms import _platform_config_cache
from model.profiles import emulator_profiles_cache
from collection.indexes import build_indexes
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
previous = os.getcwd()
os.chdir(tmp.name)
self.addCleanup(os.chdir, previous)
Path("bios/Sys").mkdir(parents=True)
files = {}
for name in ("us.bin", "jp.bin"):
payload = name.encode()
Path(f"bios/Sys/{name}").write_bytes(payload)
sha1 = hashlib.sha1(payload).hexdigest()
files[sha1] = {"path": f"bios/Sys/{name}", "name": name,
"size": len(payload), "sha1": sha1,
"md5": hashlib.md5(payload).hexdigest(),
"sha256": hashlib.sha256(payload).hexdigest(),
"crc32": "00000001"}
self.db = {"files": files, "indexes": build_indexes(files, {})}
Path("platforms").mkdir()
Path("platforms/p.yml").write_text(json.dumps({
"platform": "P", "version": "2.1", "verification_mode": "existence",
"base_destination": "", "cores": ["beetle", "dsa", "other"],
"standalone_cores": ["dsa"], "systems": {"sys": {"files": [
{"name": name, "destination": f"bios/{name}", "required": False}
for name in ("us.bin", "jp.bin")
]}},
}))
Path("platforms/_registry.yml").write_text(
json.dumps({"platforms": {"p": {"config": "p.yml"}}})
)
Path("emulators").mkdir()
Path("emulators/dsa.yml").write_text(json.dumps({
"emulator": "dsa", "type": "standalone + libretro", "systems": ["sys"],
"files": [
{"name": name, "standalone_path": f"bios/{name}", "system": "sys",
"region": [region_name], "required": False}
for name, region_name in (("us.bin", "north-america"), ("jp.bin", "japan"))
]}))
# Read before dsa, as beetle_psx is before duckstation, and untagged
# by name through a second reader, as pcsx1 leaves scph5501.bin.
Path("emulators/beetle.yml").write_text(json.dumps({
"emulator": "beetle", "type": "libretro", "systems": ["sys"], "files": [
{"name": "us.bin", "system": "sys", "region": ["north-america"],
"required": False},
]}))
Path("emulators/other.yml").write_text(json.dumps({
"emulator": "other", "type": "libretro", "systems": ["sys"], "files": [
{"name": "us.bin", "path": "alt/us.bin", "system": "sys", "required": False},
]}))
_platform_config_cache.clear()
emulator_profiles_cache.clear()
self.addCleanup(_platform_config_cache.clear)
self.addCleanup(emulator_profiles_cache.clear)
self.profiles = profiles.load_emulator_profiles("emulators")
def _manifest(self, regions):
return packs_manifest.generate_manifest(
"p", "platforms", self.db, "bios", "platforms/_registry.yml",
emulators_dir="emulators", emu_profiles=self.profiles,
regions=regions, offline=True,
)
def _pack(self, regions):
record = archive.PackRecord()
platformpack.generate_pack(
"p", "platforms", self.db, "bios", "", emulators_dir="emulators",
emu_profiles=self.profiles, regions=regions, offline=True, record=record,
)
return {m for m in record.members if m.startswith("bios/")}
def test_the_slot_choices_are_computed_once(self):
"""pack_overrides recomputed the choices the manifest had just
computed with the same arguments: 8% of a manifest run."""
from packs import slots
real = slots.slot_choices
with mock.patch.object(slots, "slot_choices", side_effect=real) as counted:
self._manifest(None)
self.assertEqual(counted.call_count, 1)
def test_the_pack_and_the_installer_drop_the_same_files(self):
full = self._manifest(None)
files, _omitted = install.narrow(
full["files"], full["omitted_files"], [], [], ["japan"]
)
installed = {f["dest"] for f in files if f["dest"].startswith("bios/")}
self.assertIn("bios/jp.bin", installed)
self.assertEqual(self._pack(["japan"]), installed)
class ACoreWithNothingHeldIsAnOmission(unittest.TestCase):
"""DOSBox serves SC-55/WAVEROM3.BIN, DOSBox Pure wants it and nothing is
held for it. A pack built for a target without DOSBox lists it as left
out. Narrowed from the full manifest, it had vanished."""
MANIFEST = {"files": [{
"dest": "SC-55/WAVEROM3.BIN", "cores": ["dosbox"], "sha1": "1" * 40,
"absent_claims": [{"name": "WAVEROM3.BIN", "system": "dos", "required": False,
"reason": "not_found", "cores": ["dosbox_pure"]}],
}], "omitted_files": []}
def test_narrowing_away_the_holder_leaves_the_omission(self):
files, omitted = install.narrow_to_cores(self.MANIFEST, ["dosbox_pure"])
self.assertEqual(files, [])
self.assertEqual([o["dest"] for o in omitted], ["SC-55/WAVEROM3.BIN"])
files, omitted = install.narrow_to_cores(self.MANIFEST, ["dosbox", "dosbox_pure"])
self.assertEqual(len(files), 1)
self.assertEqual(omitted, [])
class ANativeNameSelectsOneSetEverywhere(unittest.TestCase):
"""`--system psx` was accepted by the installer and refused by
generate_pack and verify. The platform's own name now resolves through
common.resolve_system_arg in both tools, from the mapping the manifest's
system_names is built from, and selects what the system id selects:
the same pack, the same verify report, the same installed files."""
CASES = (
("batocera", "psx", "sony-playstation"),
("recalbox", "msx1,msx2", "microsoft-msx"),
("romm", "acpc", "amstrad-cpc"),
)
@classmethod
def setUpClass(cls):
db_path = REPO_ROOT / "database.json"
if not db_path.exists():
raise unittest.SkipTest("database.json is not built")
from model.profiles import load_emulator_profiles
cls.db = json.loads(db_path.read_text(encoding="utf-8"))
cls.profiles = load_emulator_profiles(str(REPO_ROOT / "emulators"))
def _packed(self, platform: str, systems: list[str]) -> tuple[set[str], set[str]]:
"""The pack's members and those the build took from data directories."""
import zipfile
from packs.packcounts import PackCounts
counts: list[PackCounts] = []
with tempfile.TemporaryDirectory() as out:
zip_path = platformpack.build_platform_pack(
platform, str(REPO_ROOT / "platforms"), self.db,
str(REPO_ROOT / "bios"), out,
emulators_dir=str(REPO_ROOT / "emulators"),
emu_profiles=self.profiles, system_filter=systems, offline=True,
counts=counts,
).zip_path
with zipfile.ZipFile(zip_path) as zf:
names = {i.filename for i in zf.infolist() if not i.is_dir()}
return names, set(counts[0].trees)
def test_pack_verify_and_installer_agree(self):
from model.platforms import load_platform_config
from model.systems import resolve_system_arg
from packs.archive import is_pack_note
for platform, native, system_id in self.CASES:
with self.subTest(platform=platform, name=native):
config = load_platform_config(platform, str(REPO_ROOT / "platforms"))
systems = list(config["systems"])
chosen = resolve_system_arg(native, systems, config)
self.assertEqual(chosen, resolve_system_arg(system_id, systems, config))
self.assertEqual(chosen, [system_id])
report = coverage.verify_platform(
config, self.db, str(REPO_ROOT / "emulators"), self.profiles,
system_filter=chosen,
)
self.assertEqual(report["systems"], [system_id])
credited = set(report["pack_destinations"])
members, trees = self._packed(platform, chosen)
written = {
name for name in members
if not is_pack_note(name) and name not in trees - credited
}
self.assertTrue(written)
self.assertEqual(credited, written)
manifest = packs_manifest.generate_manifest(
platform, str(REPO_ROOT / "platforms"), self.db,
str(REPO_ROOT / "bios"),
str(REPO_ROOT / "platforms" / "_registry.yml"),
emulators_dir=str(REPO_ROOT / "emulators"),
emu_profiles=self.profiles, offline=True,
)
picked = install.resolve_choices(
[native],
install.available_choices(
manifest["files"], manifest["omitted_files"]
)["systems"],
"system", install.system_aliases(manifest),
)
self.assertEqual(picked, [system_id])
files, _omitted = install.narrow(
manifest["files"], manifest["omitted_files"], picked, [], [],
)
base = config.get("base_destination", "")
prefix = f"{base}/" if base else ""
packed = {
n[len(prefix):] if prefix and n.startswith(prefix) else n
for n in members
}
known = {f["dest"] for f in manifest["files"]}
self.assertEqual({f["dest"] for f in files}, packed & known)
if __name__ == "__main__":
unittest.main()
+818
View File
@@ -0,0 +1,818 @@
"""install.py offers the platforms the manifests describe, names them and runs against the real lists."""
from __future__ import annotations
import tests
from tests import scratch_dir
import contextlib
import hashlib
import io
import json
import os
import shlex
import re
import shutil
import signal
import subprocess
import sys
import tempfile
import time
import unittest
import unittest.mock
from pathlib import Path
from tests.netguard import module_guard
from tests.installsupport import REPO_ROOT, install, reported, _LoopbackRun, _install_run
# No connection off the loopback and no card of the host reaches a test:
# the module fails at its end otherwise.
setUpModule, tearDownModule = module_guard("test_install_platforms")
# What a stale pin's failure says: the fix is one command.
PIN_ADVICE = "install.py changed: python scripts/pin_installer.py rewrites both pins"
class SaveFilesAListHoldsNoneOfAreNotPromised(unittest.TestCase):
"""RetroPie, ROCKNIX, AmberELEC, EmuELEC and RomM keep their save
directory in no fixed place: their lists leave those files out with no
hash or address. The run said "Name it with --save-dir", and said it
again with --save-dir given, which installs none of them."""
def test_the_reason_is_said_and_no_option_is_offered(self):
manifest = json.loads((REPO_ROOT / "install" / "retropie.json").read_text())
self.assertIsNone(manifest.get("save_directory"))
root = scratch_dir(self)
env = dict(os.environ, RETROBIOS_OS="linux")
for extra in ([], ["--save-dir", str(root / "saves")]):
with self.subTest(extra=extra):
proc = _install_run(
env, "--platform", "retropie", "--dest", str(root / "bios"),
"--manifest", "install/retropie.json", "--offline", "--no-input",
"--yes", "--emulator", "azahar", *extra)
out = proc.stdout + proc.stderr
self.assertIn("read from RetroPie's save directory left out: RetroPie "
"keeps it in no fixed place, and its file list holds none "
"of them.", out)
self.assertNotIn("Name it with --save-dir", out)
class TestNormalizePlatform(unittest.TestCase):
"""Manifest URLs are case sensitive: user input must be normalized."""
def test_lowercases_input(self):
self.assertEqual(install.normalize_platform("Batocera"), "batocera")
def test_strips_whitespace(self):
self.assertEqual(install.normalize_platform(" retroarch "), "retroarch")
def test_unknown_platform_exits(self):
with self.assertRaises(SystemExit):
install.normalize_platform("launchbox")
def test_all_available_platforms_accepted(self):
for plat in install.AVAILABLE_PLATFORMS:
self.assertEqual(install.normalize_platform(plat), plat)
class TestAvailablePlatforms(unittest.TestCase):
"""The platform list must match the generated manifests."""
def test_matches_install_manifests(self):
manifests = {p.stem for p in (REPO_ROOT / "install").glob("*.json")}
self.assertEqual(set(install.AVAILABLE_PLATFORMS), manifests)
def test_every_registered_platform_is_installable(self):
"""The installer serves every registered platform from its manifest."""
from model.registry import list_registered_platforms
registered = set(
list_registered_platforms(
str(REPO_ROOT / "platforms")
)
)
missing = registered - set(install.AVAILABLE_PLATFORMS)
self.assertEqual(missing, set(), f"no install manifest for {sorted(missing)}")
def test_powershell_wrapper_pins_installer_hash(self):
content = (REPO_ROOT / "install.ps1").read_text()
match = re.search(r'\$defaultInstallSha256 = "([0-9a-f]{64})"', content)
self.assertIsNotNone(match, "install.ps1 must embed the installer SHA256")
expected = hashlib.sha256((REPO_ROOT / "install.py").read_bytes()).hexdigest()
self.assertEqual(match.group(1), expected, PIN_ADVICE)
def test_powershell_wrapper_rejects_non_https_bootstrap(self):
content = (REPO_ROOT / "install.ps1").read_text()
self.assertIn('$uri.Scheme -ne "https"', content)
def test_shell_wrapper_pins_installer_hash(self):
content = (REPO_ROOT / "install.sh").read_text()
match = re.search(r'DEFAULT_INSTALL_SHA256="([0-9a-f]{64})"', content)
self.assertIsNotNone(match, "install.sh must embed the installer SHA256")
expected = hashlib.sha256((REPO_ROOT / "install.py").read_bytes()).hexdigest()
self.assertEqual(match.group(1), expected, PIN_ADVICE)
def test_pin_script_writes_both_pins(self):
"""Every edit to install.py meant a sha256sum pasted by hand into two
files with two syntaxes, and the failing test named no command."""
from installer_build import pins
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
for name in ("install.py", "install.sh", "install.ps1"):
shutil.copy2(REPO_ROOT / name, root / name)
(root / "install.py").write_text("# changed\n", encoding="utf-8")
sh_mode = (root / "install.sh").stat().st_mode
self.assertEqual(pins.stale_pins(root), ["install.sh", "install.ps1"])
self.assertEqual(pins.write_pins(root), ["install.sh", "install.ps1"])
self.assertEqual(pins.stale_pins(root), [])
digest = hashlib.sha256(b"# changed\n").hexdigest()
for name in ("install.sh", "install.ps1"):
before = (REPO_ROOT / name).read_text(encoding="utf-8").splitlines()
after = (root / name).read_text(encoding="utf-8").splitlines()
changed = [b for a, b in zip(before, after) if a != b]
self.assertEqual(len(changed), 1, name)
self.assertIn(digest, changed[0])
self.assertEqual((root / "install.sh").stat().st_mode, sh_mode)
def test_a_second_pin_line_is_refused(self):
"""sh keeps the last assignment: a second pin line after the right one
is the value the bootstrap runs with, so the check refuses it."""
from installer_build import pins
with tempfile.TemporaryDirectory(dir=REPO_ROOT / "tmp" / "tests") as directory:
root = Path(directory)
for name in ("install.py", "install.sh", "install.ps1"):
shutil.copy2(REPO_ROOT / name, root / name)
text = (root / "install.sh").read_text(encoding="utf-8")
pin = f'DEFAULT_INSTALL_SHA256="{pins.installer_sha256(root)}"\n'
self.assertIn(pin, text)
doubled = text.replace(pin, f'{pin}DEFAULT_INSTALL_SHA256="{"0" * 64}"\n')
(root / "install.sh").write_text(doubled, encoding="utf-8")
with self.assertRaisesRegex(ValueError, "2 installer pin lines"):
pins.stale_pins(root)
with self.assertRaisesRegex(ValueError, "2 installer pin lines"):
pins.pinned_sha256("install.sh", root)
def test_shell_one_liner_downloads_verifies_and_forwards_arguments(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
# A piped script must not trust a same-named file in the caller's
# working directory. Only a real local install.sh may use its peer.
(root / "install.py").write_text(
"raise SystemExit('untrusted cwd installer ran')\n",
encoding="utf-8",
)
fake_bin = root / "bin"
fake_bin.mkdir()
marker = root / "curl-called"
fake_curl = fake_bin / "curl"
fake_curl.write_text(
"#!/bin/sh\n"
"output=\n"
"while [ \"$#\" -gt 0 ]; do\n"
" if [ \"$1\" = --output ]; then output=$2; shift 2; else shift; fi\n"
"done\n"
"cp -- \"$FAKE_INSTALLER_SOURCE\" \"$output\"\n"
": > \"$FAKE_CURL_MARKER\"\n",
encoding="utf-8",
)
fake_curl.chmod(0o755)
env = os.environ.copy()
env.update(
PATH=f"{fake_bin}{os.pathsep}{env['PATH']}",
TMPDIR=str(root),
RETROBIOS_INSTALL_URL="https://example.invalid/install.py",
RETROBIOS_INSTALL_SHA256=hashlib.sha256(
(REPO_ROOT / "install.py").read_bytes()
).hexdigest(),
FAKE_INSTALLER_SOURCE=str(REPO_ROOT / "install.py"),
FAKE_CURL_MARKER=str(marker),
)
proc = subprocess.run(
["sh", "-s", "--", "--list-platforms"],
cwd=root,
env=env,
input=(REPO_ROOT / "install.sh").read_text(encoding="utf-8"),
capture_output=True,
text=True,
timeout=30,
check=False,
)
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
self.assertTrue(marker.is_file(), "piped bootstrap did not download install.py")
self.assertIn("retroarch", proc.stdout)
def test_busybox_wget_downloads_the_installer(self):
"""BusyBox wget, the only one in the RomM image, refuses
--https-only: with no curl the one-liner always failed and blamed
the network."""
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
fake_bin = root / "bin"
fake_bin.mkdir()
for tool in ("mktemp", "wc", "tr", "sha256sum", "awk", "rm", "rmdir",
"grep", "tail", "cp", "dirname", "cat", "python3"):
found = shutil.which(tool)
if found:
(fake_bin / tool).symlink_to(found)
(fake_bin / "wget").write_text(
"#!/bin/sh\n"
"output=\n"
"for arg in \"$@\"; do\n"
" case \"$arg\" in\n"
" --help) echo 'Usage: wget [-cqS] [-O FILE] [-T SEC] URL' >&2; exit 1 ;;\n"
" --https-only) echo \"wget: unrecognized option '$arg'\" >&2; exit 1 ;;\n"
" --output-document=*) output=${arg#--output-document=} ;;\n"
" esac\n"
"done\n"
"cp -- \"$FAKE_INSTALLER_SOURCE\" \"$output\"\n",
encoding="utf-8",
)
(fake_bin / "wget").chmod(0o755)
env = {
"PATH": str(fake_bin), "HOME": str(root), "TMPDIR": str(root),
"RETROBIOS_INSTALL_URL": "https://example.invalid/install.py",
"FAKE_INSTALLER_SOURCE": str(REPO_ROOT / "install.py"),
}
proc = subprocess.run(
["/bin/sh", "-s", "--", "--list-platforms"], cwd=root, env=env,
input=(REPO_ROOT / "install.sh").read_text(encoding="utf-8"),
capture_output=True, text=True, timeout=60, check=False,
)
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
self.assertIn("retroarch", proc.stdout)
@unittest.skipIf(os.name == "nt", "the sh bootstrap is POSIX")
def test_ctrl_c_during_the_download_ends_with_130(self):
"""Ctrl+C while install.py downloaded: the trap cleaned up and the
script went on to blame the network, with code 1."""
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
fake_bin = root / "bin"
fake_bin.mkdir()
started = root / "started"
(fake_bin / "curl").write_text(
f"#!/bin/sh\n: > '{started}'\nexec sleep 60\n", encoding="utf-8")
(fake_bin / "curl").chmod(0o755)
env = {**os.environ, "PATH": f"{fake_bin}{os.pathsep}{os.environ['PATH']}",
"TMPDIR": str(root),
"RETROBIOS_INSTALL_URL": "https://example.invalid/install.py"}
for shell in ("sh", "bash"):
if not shutil.which(shell):
continue
with self.subTest(shell=shell):
started.unlink(missing_ok=True)
proc = subprocess.Popen(
[shell, "-s", "--", "--check"], cwd=root, env=env,
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, text=True, start_new_session=True)
proc.stdin.write((REPO_ROOT / "install.sh").read_text(encoding="utf-8"))
proc.stdin.close()
proc.stdin = None
try:
deadline = time.monotonic() + 30
while not started.exists() and time.monotonic() < deadline:
time.sleep(0.05)
self.assertTrue(started.exists(), "the download never started")
os.killpg(proc.pid, signal.SIGINT)
_out, err = proc.communicate(timeout=30)
finally:
if proc.poll() is None:
os.killpg(proc.pid, signal.SIGKILL)
proc.communicate()
self.assertEqual(proc.returncode, 130, err)
self.assertNotIn("could not be downloaded", err)
self.assertEqual(sorted(p.name for p in root.iterdir()),
["bin", "started"])
@unittest.skipIf(os.name == "nt", "the sh bootstrap is POSIX")
def test_a_versioned_python_is_found_beside_an_old_python3(self):
"""Buster keeps python3 at 3.7: the user installs python3.8 as the
advice says, and the bootstrap, trying python3 and python alone,
gave the same advice again."""
root = scratch_dir(self)
shutil.copy(REPO_ROOT / "install.sh", root / "install.sh")
(root / "install.py").write_text("import sys\nprint('ran', sys.version_info >= (3, 8))\n")
fake_bin = root / "bin"
fake_bin.mkdir()
old = ("#!/bin/sh\ncase \"$*\" in *SystemExit*) exit 1 ;; esac\n"
"echo 3.7.3\n")
for name in ("python3", "python"):
(fake_bin / name).write_text(old)
(fake_bin / name).chmod(0o755)
tools = root / "tools"
tools.mkdir()
for tool in ("sh", "grep", "sort", "basename", "dirname", "uname", "sed", "tr"):
found = shutil.which(tool)
if found:
(tools / tool).symlink_to(found)
(fake_bin / "python3.11").symlink_to(sys.executable)
proc = subprocess.run(
[shutil.which("sh"), str(root / "install.sh")],
env={"PATH": f"{fake_bin}{os.pathsep}{tools}", "HOME": str(root)},
capture_output=True, text=True, timeout=60, stdin=subprocess.DEVNULL, check=False)
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
self.assertIn("ran True", proc.stdout)
def test_a_failed_download_says_so_in_one_message(self):
"""Offline, the one-liner printed curl's progress table and
"curl: (6) ...", then exited 6, a code the table does not list."""
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
fake_bin = root / "bin"
fake_bin.mkdir()
(fake_bin / "curl").write_text(
"#!/bin/sh\necho 'curl: (6) Could not resolve host: x' >&2\nexit 6\n",
encoding="utf-8",
)
(fake_bin / "curl").chmod(0o755)
env = os.environ.copy()
env.update(
PATH=f"{fake_bin}{os.pathsep}{env['PATH']}",
TMPDIR=str(root),
RETROBIOS_INSTALL_URL="https://example.invalid/install.py",
)
proc = subprocess.run(
["sh", "-s"], cwd=root, env=env,
input=(REPO_ROOT / "install.sh").read_text(encoding="utf-8"),
capture_output=True, text=True, timeout=30, check=False,
)
self.assertEqual(proc.returncode, 1, proc.stderr)
self.assertIn("could not be downloaded", proc.stderr)
self.assertIn("Nothing was run and nothing was written", proc.stderr)
self.assertEqual(proc.stderr.count("curl: (6)"), 1)
def test_powershell_turns_a_failure_into_one_line(self):
source = (REPO_ROOT / "install.ps1").read_text(encoding="utf-8")
self.assertIn('[Console]::Error.WriteLine("Error: "', source)
self.assertIn("winget install Python", source)
@unittest.skipUnless(shutil.which("script"), "script(1) provides the terminal")
def test_piped_one_liner_hands_the_terminal_to_the_installer(self):
"""curl | sh gives the installer the script as stdin, so isatty()
was false and every question it asks was skipped on Linux and Mac."""
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
probe = root / "probe.py"
probe.write_text(
"import sys\nprint('installer stdin is a terminal:', sys.stdin.isatty())\n",
encoding="utf-8",
)
fake_bin = root / "bin"
fake_bin.mkdir()
(fake_bin / "curl").write_text(
"#!/bin/sh\n"
"output=\n"
"while [ \"$#\" -gt 0 ]; do\n"
" if [ \"$1\" = --output ]; then output=$2; shift 2; else shift; fi\n"
"done\n"
f"cp -- {shlex.quote(str(probe))} \"$output\"\n",
encoding="utf-8",
)
(fake_bin / "curl").chmod(0o755)
env = os.environ.copy()
env.update(
PATH=f"{fake_bin}{os.pathsep}{env['PATH']}",
TMPDIR=str(root),
RETROBIOS_INSTALL_URL="https://example.invalid/install.py",
RETROBIOS_INSTALL_SHA256=hashlib.sha256(probe.read_bytes()).hexdigest(),
)
piped = f"sh -s < {shlex.quote(str(REPO_ROOT / 'install.sh'))}"
proc = subprocess.run(
["script", "-qec", piped, "/dev/null"],
cwd=root, env=env, capture_output=True, text=True, timeout=30,
check=False,
)
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
self.assertIn("installer stdin is a terminal: True", proc.stdout)
class CaseVariants(unittest.TestCase):
"""The RetroArch list is folded for Windows and macOS: a Linux user got
BIOS.col and np2kai/2608_BD.WAV only, while Mesen-CE opens bios.col and
np2kai np2kai/2608_bd.wav. The manifest carries those as case_variants,
installed where the directory keeps case."""
def _manifest(self, variants):
entry = {
"dest": "BIOS.col", "sha1": "a" * 40, "sha256": "b" * 64,
"size": 8192, "repo_path": "bios/Coleco/BIOS.col", "cores": None,
}
return {
"manifest_version": 2, "platform": "retroarch", "files": [entry],
"total_files": 1, "total_size": 8192, "case_variants": variants,
}
def test_a_kept_case_directory_is_detected(self):
directory = scratch_dir(self) / "not" / "yet"
self.assertTrue(install.keeps_case(directory))
self.assertEqual(list(Path(directory.parents[1]).iterdir()), [])
def test_variants_cross_the_trust_boundary(self):
variant = {
"dest": "bios.col", "sha1": "a" * 40, "sha256": "b" * 64,
"size": 8192, "repo_path": "bios/Coleco/BIOS.col", "cores": ["mesence"],
}
install._validate_manifest(self._manifest([variant]), "retroarch")
for bad in ({**variant, "dest": "../x"}, {**variant, "dest": "BIOS.col"}):
with self.subTest(dest=bad["dest"]), self.assertRaises(ValueError):
install._validate_manifest(self._manifest([bad]), "retroarch")
def test_main_adds_them_only_where_case_is_kept(self):
source = (REPO_ROOT / "install.py").read_text(encoding="utf-8")
compact = " ".join(source.split())
self.assertIn(
'variants = manifest.get("case_variants", []) '
"case_kept = keeps_case(bios_path) if variants and case_kept:", compact,
)
def test_the_published_retroarch_list_carries_them(self):
manifest = json.loads((REPO_ROOT / "install" / "retroarch.json").read_text())
variants = {e["dest"] for e in manifest.get("case_variants", [])}
self.assertIn("bios.col", variants)
lakka = json.loads((REPO_ROOT / "install" / "lakka.json").read_text())
folded = {e["dest"] for e in manifest["files"]}
# A case-keeping RetroArch install is the Lakka list, less what
# Lakka's own core packages install (installed_on: lakka).
from model.profiles import load_emulator_profiles
installed = {
entry.get("path") or entry.get("name")
for profile in load_emulator_profiles(str(REPO_ROOT / "emulators")).values()
for entry in profile.get("files") or []
if isinstance(entry, dict) and "lakka" in (entry.get("installed_on") or [])
}
lakka_dests = {e["dest"] for e in lakka["files"]}
self.assertEqual(lakka_dests - (folded | variants), set())
self.assertEqual((folded | variants) - lakka_dests - installed, set(),
"a case-keeping RetroArch install equals the Lakka list")
class CaseTwinsOnAFoldingVolume(unittest.TestCase):
"""Batocera and Recalbox list np2kai/bios.rom and np2kai/BIOS.ROM, other
bytes each. Installed to a share opened from Windows or an exFAT card,
the second overwrote the first and --check never converged."""
def test_one_file_per_name_is_kept(self):
files = [{"dest": "np2kai/bios.rom"}, {"dest": "np2kai/BIOS.ROM"}, {"dest": "x.bin"}]
kept, folded = install.fold_case_twins(files)
self.assertEqual([f["dest"] for f in kept], ["np2kai/bios.rom", "x.bin"])
self.assertEqual(folded, ["np2kai/BIOS.ROM"])
def test_main_folds_only_where_case_is_not_kept(self):
source = " ".join((REPO_ROOT / "install.py").read_text(encoding="utf-8").split())
self.assertIn("if not case_kept: files, folded = fold_case_twins(files)", source)
class AListingAppliesItsOptionsOrRefusesThem(unittest.TestCase):
"""--list-systems --list-cores printed the systems alone, and
--list-platforms, --list-targets and --list-emulators printed the same
list with --system, --region or --core as without, code 0."""
def test_two_listings_or_an_option_a_listing_ignores_are_refused(self):
env = dict(os.environ, RETROBIOS_OS="linux")
given = ["--platform", "retroarch", "--offline", "--no-input",
"--manifest", "install/retroarch.json"]
for argv, words in (
([*given, "--list-systems", "--list-cores"],
"--list-systems and --list-cores each print a list"),
([*given, "--list-cores", "--list-emulators"],
"--list-cores and --list-emulators each print a list"),
(["--list-platforms", "--list-systems", "--platform", "retroarch"],
"--list-platforms and --list-systems each print a list"),
(["--list-platforms", "--region", "us"],
"--list-platforms prints the whole list, which --region does not narrow"),
(["--list-platforms", "--platform", "retroarch"],
"--list-platforms prints the whole list, which --platform does not narrow"),
([*given, "--list-targets", "--system", "sony-playstation"],
"--list-targets prints the whole list, which --system does not narrow"),
([*given, "--list-targets", "--target", "switch"],
"--list-targets prints the whole list, which --target does not narrow"),
([*given, "--list-emulators", "--system", "sony-playstation"],
"--list-emulators prints the whole list, which --system does not narrow"),
([*given, "--list-emulators", "--core", "beetle_psx"],
"--list-emulators prints the whole list, which --core does not narrow"),
):
with self.subTest(argv=argv):
proc = _install_run(env, *argv)
self.assertEqual(proc.returncode, 2, proc.stdout + proc.stderr)
self.assertIn(words, proc.stderr)
def test_the_options_a_listing_reads_still_apply(self):
env = dict(os.environ, RETROBIOS_OS="linux")
given = ["--platform", "retroarch", "--offline", "--no-input",
"--manifest", "install/retroarch.json"]
whole = _install_run(env, *given, "--list-cores")
narrowed = _install_run(env, *given, "--list-cores", "--system", "nintendo-gba")
self.assertEqual((whole.returncode, narrowed.returncode), (0, 0), narrowed.stderr)
self.assertLess(len(narrowed.stdout), len(whole.stdout))
class AMissingPackNamesTheOptionAndThePacksBeside(unittest.TestCase):
"""A typo in --from-pack said "Error: X does not exist": not the option,
not the pack sitting next to the name typed."""
def test_the_pack_beside_is_named(self):
folder = scratch_dir(self)
(folder / "RetroArch_BIOS_Pack.zip").write_bytes(b"")
with self.assertRaises(ValueError) as raised:
install.PackSource._resolve([folder / "RetroArch_BIOS_Pak.zip"])
said = str(raised.exception)
self.assertIn("--from-pack names a pack ZIP", said)
self.assertIn(f"The packs in {folder}: RetroArch_BIOS_Pack.zip.", said)
class AnUnknownNameIsAnEvent(unittest.TestCase):
"""--events carried start then end with code 1 for an unknown platform,
target or system: the reason was on stderr only, which readers ignore."""
def _events(self, *argv: str) -> tuple[int, list[dict]]:
env = {key: value for key, value in os.environ.items()
if key != "RETROBIOS_BASE_URL"}
env[install.OFFLINE_VARIABLE] = "1"
with tempfile.TemporaryDirectory() as dest:
proc = subprocess.run(
[sys.executable, str(REPO_ROOT / "install.py"), "--dest", dest,
"--check", "--events", "--no-input", *argv],
env=env, capture_output=True, text=True, timeout=120,
stdin=subprocess.DEVNULL, check=False)
return proc.returncode, [json.loads(line) for line in proc.stdout.splitlines()]
def test_each_refusal_says_why(self):
manifest = str(REPO_ROOT / "install" / "retroarch.json")
for argv, words in (
(["--platform", "nope"], "Unknown platform 'nope'"),
(["--platform", "retroarch", "--manifest", manifest, "--system", "nope"],
"unknown system: nope"),
(["--platform", "retroarch", "--manifest", manifest, "--core", "nope"],
"unknown core: nope"),
(["--platform", "retroarch", "--manifest", manifest, "--emulator", "nope"],
"unknown emulator: nope")):
with self.subTest(argv=argv):
code, events = self._events(*argv)
self.assertEqual(code, 2)
errors = [event for event in events if event["event"] == "error"]
self.assertEqual(len(errors), 1, events)
self.assertEqual((errors[0]["stage"], errors[0]["reason"]),
("option", "unknown"))
self.assertIn(words, errors[0]["message"])
self.assertFalse(errors[0]["message"].startswith("Error"))
self.assertEqual(events[-1]["event"], "end")
class ATypoGetsTheClosestName(unittest.TestCase):
"""--platform batocra, --system playstation or --core swanstaton were
answered with every name the platform has, up to 400 on one line, and
MiSTer FPGA as the site writes it was refused."""
def test_the_names_the_site_writes_are_platforms(self):
self.assertEqual(install.normalize_platform("MiSTer FPGA"), "misterfpga")
self.assertEqual(install.normalize_platform("RetroArch "), "retroarch")
self.assertEqual(install.normalize_platform("RetroDECK"), "retrodeck")
def test_a_system_name_stands_for_the_list_it_gets(self):
"""--list-platforms printed `muOS: retroarch`, and --platform muos
answered "Unknown platform"."""
for typed, plat in (("muos", "retroarch"), ("muOS", "retroarch"),
("ArkOS", "retroarch"), ("PAN4ELEC", "amberelec"),
("JELOS", "retroarch")):
out = io.StringIO()
with self.subTest(typed=typed), contextlib.redirect_stdout(out):
self.assertEqual(install.normalize_platform(typed), plat)
self.assertIn(f"--platform {plat}", out.getvalue())
def test_a_system_that_gets_no_list_is_still_refused(self):
"""TrimUI, which --list-platforms prints, was answered "Unknown
platform" with no way forward."""
err = io.StringIO()
with contextlib.redirect_stderr(err), self.assertRaises(SystemExit):
reported(install.normalize_platform, "TrimUI")
self.assertNotIn("Unknown platform", err.getvalue())
self.assertIn("--platform retroarch --dest FOLDER", err.getvalue())
def test_a_frontend_without_a_bios_folder_says_what_to_do(self):
"""ES-DE, LaunchBox and Android have no BIOS folder: each emulator
reads its own, and --platform es-de was "Unknown platform"."""
for typed, said in (("es-de", "ES-DE"), ("esde", "ES-DE"),
("launchbox", "LaunchBox"), ("android", "Android")):
err = io.StringIO()
with self.subTest(typed=typed), contextlib.redirect_stderr(err), \
self.assertRaises(SystemExit) as raised:
reported(install.normalize_platform, typed)
self.assertEqual(raised.exception.code, 2)
self.assertIn(f"{said} has no BIOS folder of its own: run again without "
"--platform", err.getvalue())
def test_a_system_name_is_suggested(self):
err = io.StringIO()
with contextlib.redirect_stderr(err), self.assertRaises(SystemExit):
reported(install.normalize_platform, "spruce")
self.assertIn("Close matches: spruceOS.", err.getvalue())
# The distinct names --list-platforms prints: Batocera is both a
# platform and a system it recognises.
names = [*install.AVAILABLE_PLATFORMS,
*(line.split(":")[0] for line in install._system_lines()),
*install._NO_BIOS_FOLDER]
listed = len({install._name_key(name) for name in names})
self.assertIn(f"lists all {listed}.", err.getvalue())
def test_a_platform_typo_is_answered_with_one_name(self):
err = io.StringIO()
with contextlib.redirect_stderr(err), self.assertRaises(SystemExit) as raised:
reported(install.normalize_platform, "batocra")
self.assertEqual(raised.exception.code, 2)
self.assertIn("Close matches: batocera.", err.getvalue())
self.assertNotIn("recalbox", err.getvalue())
def test_a_system_is_found_inside_its_full_id(self):
known = ["3do", "nintendo-gba", "sega-saturn", "sony-playstation", "sony-playstation-2"]
with self.assertRaises(ValueError) as raised:
install.resolve_choices(["playstation"], known, "system")
text = str(raised.exception)
self.assertIn("unknown system: playstation", text)
self.assertIn("sony-playstation", text)
self.assertIn("--list-systems", text)
self.assertNotIn("3do", text)
def test_a_core_typo_points_at_the_listing(self):
known = [f"core{i}" for i in range(200)] + ["swanstation"]
with self.assertRaises(ValueError) as raised:
install.resolve_choices(["swanstaton"], known, "core")
text = str(raised.exception)
self.assertIn("Close matches: swanstation.", text)
self.assertIn("--list-cores lists all 201", text)
self.assertNotIn("core17", text)
def test_a_name_spelled_as_the_site_writes_it(self):
known = ["sony-playstation", "sony-playstation-2"]
self.assertEqual(
install.resolve_choices(["Sony PlayStation"], known, "system"),
["sony-playstation"],
)
def test_a_target_typo(self):
err = io.StringIO()
with contextlib.redirect_stderr(err), self.assertRaises(SystemExit):
reported(install._unknown_target, "swich", "retroarch",
{"switch": {}, "desktop": {}})
self.assertIn("Close matches: switch.", err.getvalue())
self.assertIn("--list-targets", err.getvalue())
class PlatformsAreNamedAsTheyNameThemselves(unittest.TestCase):
"""str.capitalize() printed Retroarch, Retrodeck and Misterfpga on the
first screen a user sees."""
def test_the_table_is_the_manifests(self):
for plat in install.AVAILABLE_PLATFORMS:
manifest = json.loads((REPO_ROOT / "install" / f"{plat}.json").read_text())
with self.subTest(plat=plat):
self.assertEqual(install.PLATFORM_NAMES[plat], manifest["display_name"])
def test_the_menus_use_them(self):
out = io.StringIO()
with unittest.mock.patch("builtins.input", lambda _prompt="": "1"), \
contextlib.redirect_stdout(out):
install._prompt_platform_choice(
[("misterfpga", Path("/m")), ("retrodeck", Path("/r"))])
self.assertIn("MiSTer FPGA: /m", out.getvalue())
self.assertIn("RetroDECK: /r", out.getvalue())
self.assertEqual(install.display_name("romm", {"display_name": "RomM"}), "RomM")
self.assertEqual(install.display_name("romm", {"display_name": "\x1b[2J"}), "RomM")
class RunsAgainstTheRealLists(unittest.TestCase, _LoopbackRun):
"""Behaviour read end to end over loopback."""
def setUp(self):
self.env = self.serve(self)
def test_a_wrong_hash_is_listed_first(self):
dest = self.home / "system"
dest.mkdir()
(dest / "scph5501.bin").write_bytes(b"junk")
proc = _install_run(self.env, "--platform", "retroarch", "--dest", str(dest),
"--check", "--no-input", "--verbose")
self.assertEqual(proc.returncode, 3, proc.stderr)
listing = proc.stdout.split("to download.\n", 1)[1].splitlines()
self.assertEqual(listing[0].strip(), "wrong copy: scph5501.bin")
def test_a_dry_run_counts_the_data_trees_and_ends_on_its_last_line(self):
proc = _install_run(self.env, "--platform", "retroarch", "--dest",
str(self.home / "s"), "--dry-run", "--no-input")
self.assertEqual(proc.returncode, 0, proc.stderr)
lines = [line for line in proc.stdout.splitlines() if line.strip()]
self.assertIn("Nothing was written", lines[-1])
at = next(index for index, line in enumerate(lines) if line.startswith("Would download"))
# One total on the header, the data files among the kinds under it.
self.assertNotIn("data files", lines[at])
self.assertIn("data files", lines[at + 1])
def test_dest_alone_says_whose_list_it_is(self):
proc = _install_run(self.env, "--dest", str(self.home / "card"), "--dry-run",
"--no-input", "--system", "atari-lynx")
self.assertEqual(proc.returncode, 0, proc.stderr)
self.assertIn("File list: RetroArch", proc.stdout)
self.assertIn("--platform", proc.stdout)
def test_check_says_in_place_only_when_the_data_files_are_too(self):
"""The PSP BIOS in place and PPSSPP's assets absent: --check said
"Everything is in place." then "169 to fetch", and exited 1."""
dest = self.home / "psp"
(dest / "PPSSPP").mkdir(parents=True)
for repo_path in ("bios/Sony/PlayStation Portable/ppge_atlas.zim",
"bios/Other/ppsspp/PPSSPP/ppge_atlas.meta"):
shutil.copy(REPO_ROOT / repo_path, dest / "PPSSPP")
proc = _install_run(self.env, "--platform", "retroarch", "--dest", str(dest),
"--system", "sony-psp", "--check", "--no-input", "--verbose")
self.assertEqual(proc.returncode, 3, proc.stdout + proc.stderr)
self.assertNotIn("Every file on the list is in place", proc.stdout)
found = re.search(r"(\d+) in place, (\d+) missing", proc.stdout)
self.assertIsNotNone(found, proc.stdout)
self.assertIn(f"\n {found.group(2)} data files to fetch\n", proc.stdout)
def test_check_gives_one_total_offline(self):
"""Offline on an empty folder: "10805 missing", then "8164 files not in
place" and "8164 files need the internet", the data files left out."""
dest = self.home / "empty"
dest.mkdir()
proc = _install_run({**self.env, "RETROBIOS_INSTALL_OFFLINE": "1"}, "--platform",
"retroarch", "--dest", str(dest), "--check", "--no-input",
"--verbose",
"--manifest", str(REPO_ROOT / "install" / "retroarch.json"))
self.assertEqual(proc.returncode, 3, proc.stdout + proc.stderr)
missing = re.search(r"0 in place, (\d+) missing", proc.stdout).group(1)
self.assertIn(f"\n {missing} files not in place.\n", proc.stdout)
self.assertIn(f" {missing} files need the internet", proc.stdout)
def test_a_cores_folder_can_be_named(self):
cores = self.home / "cores"
cores.mkdir()
(cores / "handy_libretro.so").write_bytes(b"")
proc = _install_run(self.env, "--platform", "retroarch", "--dest",
str(self.home / "s"), "--cores-dir", str(cores),
"--dry-run", "--no-input", "--verbose")
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
self.assertIn("to the installed cores", proc.stdout)
def test_a_cores_folder_that_cannot_be_listed_says_why(self):
"""A typo (core/ for cores/) or a closed folder read as an empty
one: the run said "none in" and asked for the --cores-dir given."""
missing = self.home / "core"
a_file = self.home / "cores.txt"
a_file.write_text("")
for folder, why in ((missing, "does not exist"), (a_file, "is not a folder")):
with self.subTest(why=why):
proc = _install_run(self.env, "--platform", "retroarch", "--dest",
str(self.home / "s"), "--cores-dir", str(folder),
"--dry-run", "--no-input")
self.assertEqual(proc.returncode, 1)
self.assertIn(f"--cores-dir {folder} {why}", proc.stderr)
self.assertNotIn("none in", proc.stderr)
@unittest.skipIf(os.name == "nt" or os.geteuid() == 0,
"a folder without read permission is POSIX")
def test_a_closed_cores_folder_says_so(self):
cores = self.home / "closed"
cores.mkdir()
(cores / "handy_libretro.so").write_bytes(b"")
os.chmod(cores, 0o000)
try:
proc = _install_run(self.env, "--platform", "retroarch", "--dest",
str(self.home / "s"), "--cores-dir", str(cores),
"--dry-run", "--no-input")
finally:
os.chmod(cores, 0o755)
self.assertEqual(proc.returncode, 1)
self.assertIn(f"--cores-dir {cores} cannot be opened by this user", proc.stderr)
def test_no_core_found_says_how_to_go_on(self):
proc = _install_run(self.env, "--platform", "retroarch", "--dest",
str(self.home / "s"), "--installed-cores", "--dry-run",
"--no-input")
self.assertEqual(proc.returncode, 1)
self.assertIn("--cores-dir <cores folder>", proc.stderr)
self.assertIn("without --installed-cores", proc.stderr)
def test_a_refusal_reaches_the_event_stream(self):
"""--events read start, detect, then end with code 1: the reason
went to stderr alone."""
for options, reason in ((["--cores-dir", str(self.home / "core")], "not_found"),
(["--installed-cores"], "none")):
with self.subTest(reason=reason):
proc = _install_run(self.env, "--platform", "retroarch", "--dest",
str(self.home / "s"), *options, "--dry-run", "--events")
self.assertEqual(proc.returncode, 1, proc.stderr)
events = [json.loads(line) for line in proc.stdout.splitlines()]
self.assertEqual([event["event"] for event in events][-2:], ["error", "end"])
error = events[-2]
self.assertEqual((error["stage"], error["reason"], error["platform"]),
("cores", reason, "retroarch"))
self.assertIn(error["message"].split(". ")[0], proc.stderr)
if __name__ == "__main__":
unittest.main()
+960
View File
@@ -0,0 +1,960 @@
"""A run's outcome: free space, destinations, exit codes, the messages and hints it leaves."""
from __future__ import annotations
import tests
from tests import loopback
from tests import scratch_dir
import contextlib
import hashlib
import errno
import io
import json
import os
import shlex
import re
import shutil
import subprocess
import sys
import tempfile
import unittest
from types import SimpleNamespace
import unittest.mock
from pathlib import Path
from tests.netguard import module_guard
from tests.installsupport import REPO_ROOT, install, reported, _install_run
# No connection off the loopback and no card of the host reaches a test:
# the module fails at its end otherwise.
setUpModule, tearDownModule = module_guard("test_install_runs")
class TestRunOutcome(unittest.TestCase):
"""A run says what happened and leaves with a status that matches."""
def _report(self, **kw):
defaults = dict(
paths=[Path("/userdata/bios")],
downloaded=0,
up_to_date=0,
errors=0,
omitted=0,
interrupted=False,
)
details = {key: kw.pop(key) for key in list(kw) if key not in defaults}
defaults.update(kw)
buf = io.StringIO()
code = 0
with contextlib.redirect_stdout(buf):
try:
install._report_outcome(**defaults, details=install.OutcomeDetails(**details))
except SystemExit as exc:
code = exc.code
return buf.getvalue(), code
def test_success_names_the_location_and_the_way_to_check(self):
install.OUTPUT.verbose = install.OUTPUT.where_said = False
out, code = self._report(downloaded=12, up_to_date=3)
self.assertEqual(code, 0)
self.assertIn("12 files installed", out)
self.assertIn("/userdata/bios", out)
self.assertIn("Next: ", out)
self.assertNotIn("--check", out)
self.addCleanup(setattr, install.OUTPUT, "verbose", False)
install.OUTPUT.verbose = True
self.assertIn("--check", self._report(downloaded=12, up_to_date=3)[0])
def test_total_failure_does_not_say_done_and_exits_two(self):
out, code = self._report(errors=7)
self.assertEqual(code, 2)
self.assertNotIn("Done.", out)
self.assertIn("Nothing was installed", out)
def test_partial_failure_exits_one(self):
out, code = self._report(downloaded=5, errors=2)
self.assertEqual(code, 1)
self.assertIn("5 files installed", out)
self.assertIn("2 files failed", out)
def test_interruption_is_resumable_and_exits_130(self):
out, code = self._report(downloaded=431, interrupted=True)
self.assertEqual(code, 130)
self.assertIn("carry on", out)
self.assertIn("Nothing is lost", out)
def test_one_file_reads_as_one_file(self):
out, _ = self._report(downloaded=1)
self.assertIn("1 file installed", out)
self.assertNotIn("1 files", out)
def test_every_destination_is_named(self):
out, _ = self._report(
paths=[Path("/a/bios"), Path("/b/bios")], downloaded=2
)
self.assertIn("/a/bios", out)
self.assertIn("/b/bios", out)
class TestFreeSpaceGuard(unittest.TestCase):
"""The run refuses before writing when the volume cannot hold it."""
def setUp(self):
self.root = scratch_dir(self)
self.real = install.shutil.disk_usage
def tearDown(self):
install.shutil.disk_usage = self.real
shutil.rmtree(self.root, ignore_errors=True)
def _run(self, free: int, needed: int, path: Path | None = None):
install.shutil.disk_usage = lambda _p: SimpleNamespace(free=free)
buf = io.StringIO()
code = 0
with contextlib.redirect_stderr(buf):
try:
reported(install._check_free_space, path or self.root, needed)
except SystemExit as exc:
code = exc.code
return buf.getvalue(), code
def test_enough_space_says_nothing(self):
out, code = self._run(free=10_000, needed=1_000)
self.assertEqual((out, code), ("", 0))
def test_short_of_space_names_both_numbers_and_stops(self):
"""Nothing was installed: code 2, as the exit table says, not the 1
of a partial install."""
out, code = self._run(free=1_000, needed=5_000)
self.assertEqual(code, 2)
self.assertIn("Needed", out)
self.assertIn("Free", out)
self.assertIn("--dest", out)
def test_short_of_space_is_an_event_with_both_numbers(self):
"""A --events reader got only end code 2, like any other stop."""
install.shutil.disk_usage = lambda _p: SimpleNamespace(free=1_000)
events = io.StringIO()
with unittest.mock.patch.object(install.EVENTS, "stream", events), \
contextlib.redirect_stderr(io.StringIO()), self.assertRaises(SystemExit):
reported(install._check_free_space, self.root, 5_000, "batocera")
said = [json.loads(line) for line in events.getvalue().splitlines()]
self.assertEqual(said[0], {"event": "no_space", "platform": "batocera",
"folder": str(self.root), "bytes_needed": 5_000,
"bytes_free": 1_000})
self.assertEqual((said[1]["event"], said[1]["stage"], said[1]["reason"]),
("error", "space", "drive_full"))
def test_probe_climbs_to_an_existing_ancestor(self):
missing = self.root / "not" / "created" / "yet"
out, code = self._run(free=1, needed=2, path=missing)
self.assertEqual(code, 2)
def test_the_space_check_comes_before_the_folder(self):
source = (REPO_ROOT / "install.py").read_text(encoding="utf-8")
check = source.index("_check_free_space(bios_path, written")
self.assertLess(check, source.index("bios_path.mkdir(parents=True, exist_ok=True)"))
class ThePlansCountWhatTheRunRefusesOn(unittest.TestCase):
"""The line plan counted the absent files alone: a file in place with
the wrong bytes, to be replaced, gave no warning at 100 B free, and the
run stopped for space right after Enter."""
def setUp(self):
self.root = scratch_dir(self)
self.addCleanup(shutil.rmtree, self.root, True)
# The plan's reads are kept for the check after it.
install.head_reads_done()
self.addCleanup(install.head_reads_done)
wrong = b"w" * 1000
(self.root / "a.bin").write_bytes(wrong)
right = b"r" * 1000
self.files = [{"dest": "a.bin", "size": 1000,
"sha1": hashlib.sha1(right).hexdigest()}]
def short(self, free: int, trees: list[dict] | None = None) -> tuple[int, int] | None:
with unittest.mock.patch.object(install.shutil, "disk_usage",
lambda _p: SimpleNamespace(free=free)):
return install._space_short(self.root, self.files, trees or [])
def test_a_file_to_replace_is_counted(self):
self.assertEqual(self.short(100), (1000, 100))
self.assertEqual(self.short(100), (install.bytes_written(self.files, []), 100))
def test_a_file_in_place_is_not(self):
(self.root / "a.bin").write_bytes(b"r" * 1000)
self.assertIsNone(self.short(100))
def test_the_data_trees_count(self):
(self.root / "a.bin").write_bytes(b"r" * 1000)
tree = {"ref": "t", "dest": "tree", "systems": [], "files": {
"x.bin": {"sha1": hashlib.sha1(b"x").hexdigest(), "size": 500}}}
with unittest.mock.patch.object(install, "_offline_only", return_value=False):
# The tree's file, and its source beside it until it is read:
# a list that records no archive size gives the files' sizes.
self.assertEqual(self.short(100, [tree]), (1000, 100))
def test_what_fits_whole_reads_nothing(self):
with unittest.mock.patch.object(install, "read_in_place") as read:
self.assertIsNone(self.short(10_000))
read.assert_not_called()
def test_a_check_says_the_space_it_needs(self):
self.assertIn("space", install.EVENT_KEYS)
source = (REPO_ROOT / "install.py").read_text(encoding="utf-8")
self.assertIn('EVENTS.emit("space", platform=plat, bytes=tally.space_bytes)', source)
class TheDataTreesCountInTheFreeSpace(unittest.TestCase):
"""The space check held only the BIOS files: 18.8 KB passed on a card
with 20 MB free, then 55.4 MB of archives came and 81.8 MB were written,
against the page's promise to refuse before writing anything."""
def _run(self, *frees_after_bios: int) -> list[tuple[int, str, list]]:
"""One run per free size, in one folder: the second finds the BIOS
files the first installed."""
from types import SimpleNamespace
from tests.test_install_selection import FakeRepository
member = os.urandom(4096)
results = []
# The run is a Linux one whatever the host: under WSL main() reads
# the Windows side and keeps that in the module, where the Windows
# detection tests run after it found D:\ turned into /mnt/d.
with FakeRepository() as repo, unittest.mock.patch.dict(os.environ, RETROBIOS_OS="linux"), \
unittest.mock.patch.dict(install._WSL):
listed = json.loads((repo.root / "install" / "retroarch.json").read_text())
listed["data_directories"] = [{
"ref": "tree", "dest": "tree", "type": "zip", "archive_size": 50_000_000,
"url": "https://example.invalid/tree.zip", "systems": ["nintendo-gba"],
"files": {"Sys/a.bin": {"sha1": hashlib.sha1(member).hexdigest(),
"size": len(member)}}}]
(repo.root / "install" / "retroarch.json").write_text(json.dumps(listed))
bios = sum(len(payload) for payload, *_ in FakeRepository.PAYLOADS.values())
base = repo.env()["RETROBIOS_BASE_URL"]
for after in frees_after_bios:
out = io.StringIO()
fetched: list = []
free = bios + after
with unittest.mock.patch.object(
sys, "argv", ["install.py", "--platform", "retroarch", "--dest",
str(repo.dest), "--no-input"]), \
unittest.mock.patch.dict(os.environ, {"RETROBIOS_BASE_URL": base}), \
unittest.mock.patch.object(install.shutil, "disk_usage",
lambda _p, free=free: SimpleNamespace(free=free)), \
unittest.mock.patch.object(
install, "install_data_directory",
lambda entry, *a, fetched=fetched, **k: fetched.append(entry["ref"]) or {
"present": 0, "installed": 1, "updated": 0, "changed": 0, "absent": 0,
"error": "", "not_kept": []}), \
contextlib.redirect_stdout(out), contextlib.redirect_stderr(out):
try:
install.main()
code = 0
except SystemExit as exc:
code = exc.code
results.append((code, out.getvalue(), repo.installed() + fetched))
return results
def test_a_card_short_for_the_trees_is_refused_before_any_write(self):
((code, said, written),) = self._run(20_000_000)
self.assertEqual(code, 2, said)
self.assertIn("Not enough space", said)
self.assertEqual(written, [])
def test_room_for_the_trees_goes_on(self):
((code, said, written),) = self._run(60_000_000)
self.assertEqual(code, 0, said)
self.assertIn("tree", written)
def test_the_trees_alone_are_checked_too(self):
"""Every BIOS file in place: the trees were written unchecked."""
_first, (code, said, written) = self._run(60_000_000, -1)
self.assertEqual(code, 2, said)
self.assertIn("Not enough space", said)
self.assertNotIn("tree", written)
def test_the_bytes_counted(self):
state = (Path("x"), {"a": {"size": 10}, "b": {"size": 5}}, 0)
entry = {"archive_size": 1000}
with unittest.mock.patch.object(install.LOCAL, "pack", None), \
unittest.mock.patch.object(install, "_offline_only", return_value=False):
self.assertEqual(install.data_space_bytes([(entry, state), (entry, state)]), 1030)
with unittest.mock.patch.object(install.LOCAL, "pack", object()):
self.assertEqual(install.data_space_bytes([(entry, state)]), 15)
class AMissingDestIsSaid(unittest.TestCase):
"""--dest ~/.config/retroarh/system (a typo) was created with every
missing parent and reported done, and --check on it said only
"0/7 present"."""
def _call(self, dest: Path, ask: bool, answer: str = "") -> tuple[str, object]:
out = io.StringIO()
code = None
with contextlib.redirect_stdout(out), \
unittest.mock.patch("builtins.input", return_value=answer):
try:
install._missing_dest(dest, ask)
except SystemExit as exc:
code = exc.code
return out.getvalue(), code
def test_an_existing_folder_says_nothing(self):
root = scratch_dir(self)
self.assertEqual(self._call(root, True), ("", None))
def test_a_deep_missing_path_is_asked_and_no_is_the_default(self):
root = scratch_dir(self)
dest = root / "retroarh" / "system"
out, code = self._call(dest, True, "")
self.assertEqual(code, 2)
self.assertIn(f"The nearest folder that does: {root}", out)
self.assertFalse((root / "retroarh").exists())
self.assertEqual(self._call(dest, True, "y")[1], None)
def test_only_y_or_yes_creates_the_folders(self):
# A default-no question read "yeah" as yes, as no other one does.
root = scratch_dir(self)
self.assertEqual(self._call(root / "a" / "b", True, "yeah")[1], 2)
self.assertFalse((root / "a").exists())
def test_one_missing_level_or_no_terminal_is_said_not_asked(self):
root = scratch_dir(self)
# A "no" that was asked would stop the run with 2. Said with
# --verbose: the folder is made, and the run names it.
install.OUTPUT.verbose = False
self.assertEqual(self._call(root / "bios", True, "n"), ("", None))
self.addCleanup(setattr, install.OUTPUT, "verbose", False)
install.OUTPUT.verbose = True
out, code = self._call(root / "bios", True, "n")
self.assertIsNone(code)
self.assertIn("does not exist", out)
out, code = self._call(root / "a" / "b", False, "n")
self.assertIsNone(code)
self.assertIn("does not exist", out)
class ADestThatIsAFileIsNamed(unittest.TestCase):
"""--dest naming a file, or a path under one, failed at mkdir with
EEXIST or ENOTDIR, and the run said this user cannot write there and
offered a sudo command that fails the same way."""
def _run(self, dest: Path) -> subprocess.CompletedProcess:
env = dict(os.environ, RETROBIOS_OS="linux")
return subprocess.run(
[sys.executable, str(REPO_ROOT / "install.py"), "--platform", "retroarch",
"--dest", str(dest), "--manifest", str(REPO_ROOT / "install" / "retroarch.json"),
"--system", "nintendo-gba", "--yes", "--events", "--offline"],
env=env, capture_output=True, text=True, timeout=120, check=False,
stdin=subprocess.DEVNULL)
def test_the_file_is_named_and_sudo_is_not_offered(self):
root = scratch_dir(self)
(root / "scph5501.bin").write_bytes(b"x")
for dest, words in (
(root / "scph5501.bin", f"{root / 'scph5501.bin'} is a file, not a folder"),
(root / "scph5501.bin" / "sub",
f"cannot be a folder: {root / 'scph5501.bin'} is a file"),
):
with self.subTest(dest=dest):
proc = self._run(dest)
self.assertEqual(proc.returncode, 1, proc.stderr)
self.assertIn(words, proc.stderr)
self.assertNotIn("sudo", proc.stderr)
errors = [json.loads(line) for line in proc.stdout.splitlines()
if line.startswith("{") and '"error"' in line]
self.assertEqual([(e["stage"], e["reason"]) for e in errors],
[("dest", "not_a_folder")])
self.assertEqual((root / "scph5501.bin").read_bytes(), b"x")
class AFolderThatRefusesWritesIsNamed(unittest.TestCase):
"""A read-only BIOS folder ended the run on a traceback (a subfolder to
create), or on three retries per file and "once that is resolved", and a
platform named but not detected was told to use sudo, which on another
machine creates /userdata/bios on the wrong disk."""
ENTRY = {"dest": "mame2015/neogeo.zip", "repo_path": "bios/x", "size": 4}
def test_a_subfolder_that_cannot_be_made_is_a_failure_not_a_crash(self):
with unittest.mock.patch.object(Path, "mkdir", side_effect=PermissionError(13, "denied")):
result = install._download_one(self.ENTRY, Path("/userdata/bios"))
self.assertEqual(result, ("mame2015/neogeo.zip", False, install.NOT_WRITABLE))
def test_a_refused_write_is_not_retried(self):
class _Body(io.BytesIO):
status, headers = 200, {"Content-Length": "4"}
with tempfile.TemporaryDirectory() as tmp, \
unittest.mock.patch.object(install.urllib.request, "urlopen",
return_value=_Body(b"data")) as opened, \
unittest.mock.patch.object(install.tempfile, "NamedTemporaryFile",
side_effect=PermissionError(13, "denied")):
result = install._download_one(self.ENTRY, Path(tmp))
self.assertEqual(result[1:], (False, install.NOT_WRITABLE))
self.assertEqual(opened.call_count, 1)
def _summary_for(self, assumed):
buf = io.StringIO()
with contextlib.redirect_stdout(buf), self.assertRaises(SystemExit):
install._report_outcome(paths=[Path("/userdata/bios")], downloaded=0, up_to_date=0,
errors=1, omitted=0, interrupted=False,
details=install.OutcomeDetails(failures=[("neogeo.zip", install.NOT_WRITABLE)],
assumed=assumed))
return buf.getvalue()
def test_the_summary_says_sudo_on_the_console_and_dest_elsewhere(self):
here = self._summary_for(set())
self.assertIn("sudo", here)
guessed = self._summary_for({Path("/userdata/bios")})
self.assertIn("--dest", guessed)
self.assertNotIn("sudo", guessed)
class TestStopFlag(unittest.TestCase):
"""Ctrl+C ends the run instead of letting the queue drain."""
def tearDown(self):
install._stop.clear()
def test_worker_returns_immediately_once_stopped(self):
install._stop.set()
dest, ok, reason = install._download_one({"dest": "x.bin"}, Path("/nowhere"))
self.assertEqual((dest, ok, reason), ("x.bin", False, "stopped"))
def test_download_files_reports_no_interruption_normally(self):
failed, interrupted = install.download_files([], Path("/nowhere"))
self.assertEqual((failed, interrupted), ([], False))
@unittest.skipIf(os.name == "nt" or os.geteuid() == 0, "needs a folder this user cannot write")
class AnUnwritableFolderIsCodeTwo(unittest.TestCase):
"""A BIOS folder that could not be created ended with 1, while the same
folder existing read-only ended with the documented 2."""
def test_both_shapes_exit_two(self):
base = loopback.serve(self, REPO_ROOT)
home = Path(tempfile.mkdtemp())
locked = home / "locked"
locked.mkdir()
locked.chmod(0o555)
self.addCleanup(shutil.rmtree, home, True)
self.addCleanup(locked.chmod, 0o755)
env = {
**os.environ,
"HOME": str(home),
"RETROBIOS_OS": "linux",
"RETROBIOS_BASE_URL": base,
}
for dest in (locked / "sub", locked):
with self.subTest(dest=dest.name):
proc = subprocess.run(
[sys.executable, "install.py", "--platform", "retroarch",
"--dest", str(dest), "--system", "atari-lynx", "--no-input"],
cwd=str(REPO_ROOT), env=env, capture_output=True, text=True,
timeout=300, check=False,
)
self.assertEqual(proc.returncode, 2, proc.stdout + proc.stderr)
class SudoAdviceNamesTheFolder(unittest.TestCase):
""""Run the same command with sudo" re-ran detection under root's HOME,
found nothing, installed under /root and reported success."""
def test_the_advice_carries_platform_and_folder(self):
advice = install._write_advice(Path("/media/card/Emulation/bios"), False, "emudeck")
self.assertIn("--platform emudeck", advice)
self.assertIn("--dest /media/card/Emulation/bios", advice)
def test_the_closing_report_passes_the_platform(self):
source = (REPO_ROOT / "install.py").read_text(encoding="utf-8")
self.assertIn("platform_of={path: plat for plat, path in platforms}", source)
@unittest.skipUnless(hasattr(os, "statvfs"), "statvfs reads mount flags")
def test_a_read_only_mount_is_not_sent_to_sudo(self):
"""A card Linux mounted read-only after a dirty unplug was answered
with 'run it again with sudo', which fails the same way."""
real = os.statvfs(".")
flags = SimpleNamespace(f_flag=real.f_flag | os.ST_RDONLY)
for plat in ("retroarch", ""):
with self.subTest(platform=plat), \
unittest.mock.patch.object(install.os, "statvfs", return_value=flags):
advice = install._write_advice(Path("/media/card/bios"), False, plat)
self.assertIn("mounted read-only", advice)
self.assertNotIn("Run it again with sudo", advice)
self.assertNotIn("sudo python3", advice)
writable = SimpleNamespace(f_flag=real.f_flag & ~os.ST_RDONLY)
with unittest.mock.patch.object(install.os, "statvfs", return_value=writable):
self.assertIn("sudo", install._write_advice(Path("/media/card/bios"), False, "retroarch"))
class ACheckNamesWhatTheCollectionLacks(unittest.TestCase):
"""--check counted the required files the manifest leaves out and named
none, then said all files were up to date: on Recalbox, c3.555 stayed
unnamed for a user asking why Caesar III would not start."""
def test_recalbox_caesar3(self):
import shutil
import subprocess
manifest = json.loads((REPO_ROOT / "install" / "recalbox.json").read_text())
lacking = sorted(
e["dest"] for e in manifest.get("omitted_files", [])
if e.get("required") and e.get("system") == "caesar3"
)
if not lacking:
self.skipTest("the Recalbox manifest omits no required caesar3 file")
base = loopback.serve(self, REPO_ROOT)
home = Path(tempfile.mkdtemp())
self.addCleanup(shutil.rmtree, home, True)
env = {
**os.environ, "HOME": str(home), "RETROBIOS_OS": "linux",
"RETROBIOS_BASE_URL": base,
}
proc = subprocess.run(
[sys.executable, "-I", "install.py", "--platform", "recalbox",
"--no-input", "--dest", str(home / "bios"), "--check", "--verbose"],
cwd=REPO_ROOT, env=env, capture_output=True, text=True, timeout=300,
check=False,
)
self.assertNotIn("Traceback", proc.stderr)
for dest in lacking:
self.assertIn(dest, proc.stdout)
self.assertNotIn("unsafe", proc.stdout)
class AFullDriveIsSaidAsOne(unittest.TestCase):
"""A card filling up during a tree rebuild read `fbneo-cheats: could not
read https://...: [Errno 28] No space left on device`, putting the cause
on the source, and the run ended on `Run the same command again`."""
def test_the_tree_reports_the_drive(self):
work = scratch_dir(self)
entry = {"ref": "fbneo-cheats", "dest": "fbneo/cheats", "type": "zip",
"url": "https://example.invalid/c.zip", "systems": ["arcade"],
"files": {"1941r1.ini": {"sha1": "1" * 40, "size": 4}}}
class Full(io.RawIOBase):
def readable(self):
return True
def readinto(self, buffer):
return 0
def members(_archive, _entry):
yield "1941r1.ini", 4, lambda: io.BufferedReader(Full())
def full(*_args, **_kwargs):
raise OSError(errno.ENOSPC, "No space left on device")
with unittest.mock.patch.object(
install, "_fetch_archive",
lambda url, d, progress=None, keep=None: Path(d) / "a"), \
unittest.mock.patch.object(install, "_archive_members", members), \
unittest.mock.patch.object(install, "_install_data_member", full):
counts = install.install_data_directory(entry, work)
self.assertEqual(counts["error"], install.DRIVE_FULL)
def test_the_end_names_space_not_a_retry(self):
bios = scratch_dir(self)
out = io.StringIO()
with contextlib.redirect_stdout(out), self.assertRaises(SystemExit) as ended:
install._report_outcome([bios], 7, 0, 1, 0, False,
details=install.OutcomeDetails(failures=[("fbneo-cheats", install.DRIVE_FULL)]))
text = out.getvalue()
self.assertEqual(ended.exception.code, 1)
self.assertIn("is full: free up space, or install to another drive with --dest", text)
self.assertNotIn("retry only those", text)
def test_a_bios_file_on_a_full_drive_is_not_retried(self):
bios = scratch_dir(self)
attempts: list = []
def urlopen(*_args, **_kwargs):
attempts.append(1)
raise OSError(errno.ENOSPC, "No space left on device")
entry = {"dest": "a.bin", "repo_path": "bios/a.bin", "size": 4,
"sha1": "1" * 40, "sha256": "2" * 64}
with unittest.mock.patch.object(install.urllib.request, "urlopen", urlopen), \
unittest.mock.patch.object(install, "_network_allowed", lambda url: None), \
unittest.mock.patch.object(install, "_retry_delay", lambda *a: 0):
result = install._download_one(entry, bios)
self.assertEqual(result[1:], (False, install.DRIVE_FULL))
self.assertEqual(len(attempts), 1)
class ACheckThatFindsFilesMissingIsNoFailure(unittest.TestCase):
"""--check ended on 1 for files missing as for a list refused or a
--from folder that does not exist: a scheduled check could not tell a
BIOS folder to complete from a tool that never ran."""
def test_missing_is_3_and_a_failure_stays_1(self):
from tests.test_install_selection import FakeRepository
with FakeRepository() as repo:
missing = repo.run("--no-input", "--check")
nowhere = repo.run("--no-input", "--check", "--from", str(repo.root / "nope"))
repo.run("--no-input")
complete = repo.run("--no-input", "--check")
self.assertEqual(missing.returncode, 3, missing.stdout + missing.stderr)
self.assertEqual(nowhere.returncode, 1, nowhere.stdout + nowhere.stderr)
self.assertEqual(complete.returncode, 0, complete.stdout + complete.stderr)
class TheInstallerSaysWhatItFound(unittest.TestCase):
"""--check gave a count and no name, a read-only folder printed a line
per file, a piped log put errors before their step, and Ctrl+C while
the file list was fetched ended on a socket traceback."""
def setUp(self):
import shutil
base = loopback.serve(self, REPO_ROOT)
self.home = Path(tempfile.mkdtemp())
self.addCleanup(shutil.rmtree, self.home, True)
self.env = {
**os.environ, "HOME": str(self.home), "RETROBIOS_OS": "linux",
"RETROBIOS_BASE_URL": base,
}
def _run(self, *extra: str) -> subprocess.CompletedProcess:
return subprocess.run(
[sys.executable, "install.py", "--platform", "retroarch", "--no-input", *extra],
cwd=REPO_ROOT, env=self.env, capture_output=True, text=True, timeout=300,
check=False,
)
def test_check_names_the_files(self):
dest = self.home / "system"
self.assertEqual(self._run("--dest", str(dest), "--system", "atari-lynx", "--yes").returncode, 0)
(dest / "lynxboot.img").write_bytes(b"corrupt")
proc = self._run("--dest", str(dest), "--system", "atari-lynx", "--check")
self.assertEqual(proc.returncode, 3)
# A wrong copy is named in every mode: it is what the user acts on.
self.assertIn("1 file to download.", proc.stdout)
self.assertIn("wrong copy: lynxboot.img", proc.stdout)
self.assertIn("without --check", proc.stdout)
def test_a_read_only_folder_is_said_once(self):
if os.geteuid() == 0:
self.skipTest("root writes anywhere")
dest = self.home / "system"
dest.mkdir()
dest.chmod(0o500)
self.addCleanup(dest.chmod, 0o700)
proc = self._run("--dest", str(dest), "--system", "atari-lynx", "--yes")
output = proc.stdout + proc.stderr
self.assertNotEqual(proc.returncode, 0)
self.assertLessEqual(output.count("FAILED"), 1, output)
self.assertIn("failed: 2 files (folder not writable)", output)
def test_a_piped_log_keeps_its_order(self):
proc = subprocess.run(
[sys.executable, "install.py", "--platform", "retroarch", "--no-input",
"--verbose", "--target", "no-such-target", "--dest", str(self.home / "s")],
cwd=REPO_ROOT, env=self.env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True, timeout=300, check=False,
)
banner = proc.stdout.find("RetroBIOS")
error = proc.stdout.lower().find("unknown target")
self.assertGreaterEqual(banner, 0, proc.stdout)
self.assertGreater(error, banner, proc.stdout)
def test_ctrl_c_before_the_downloads_is_a_stop(self):
import signal
import socket
silent = socket.socket()
silent.bind(("127.0.0.1", 0))
silent.listen(1)
self.addCleanup(silent.close)
env = {**self.env, "RETROBIOS_BASE_URL": f"http://127.0.0.1:{silent.getsockname()[1]}"}
proc = subprocess.Popen(
[sys.executable, "install.py", "--platform", "retroarch", "--no-input",
"--dest", str(self.home / "s")],
cwd=REPO_ROOT, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,
)
import time
time.sleep(3)
proc.send_signal(signal.SIGINT)
out, err = proc.communicate(timeout=60)
self.assertEqual(proc.returncode, 130, out + err)
self.assertIn("Stopped", out)
self.assertNotIn("Traceback", err)
class HintsGiveTheUsersOwnCommandBack(unittest.TestCase):
"""A one-liner user was told to run python3 install.py, a file they do
not have, and the write advice dropped --system, turning a retry of 2
files into 8069, with sudo on Windows."""
def setUp(self):
patcher = unittest.mock.patch.object(install, "_INVOCATION", install.Invocation())
patcher.start()
self.addCleanup(patcher.stop)
def _record(self, *argv: str) -> None:
args = SimpleNamespace(
system=None, core=None, emulator=None, region=None, target=None,
cores_dir=None, every_board=False, installed_cores=False,
standalone_copies=False, check=False, dry_run=False, no_game_data=False,
declared_only=False,
)
flags = list(argv)
while flags:
flag = flags.pop(0)
attr = flag[2:].replace("-", "_")
if attr in ("system", "core", "emulator", "region"):
setattr(args, attr, [*(getattr(args, attr) or []), flags.pop(0)])
else:
setattr(args, attr, True)
install._INVOCATION.record(args)
def test_the_shell_one_liner_is_rebuilt(self):
with unittest.mock.patch.dict(os.environ, {"RETROBIOS_BOOTSTRAP": "sh"}):
hint = "\n".join(install._manual_usage_hint("linux"))
self.assertIn("/i | sh -s -- --platform retroarch", hint)
self.assertNotIn("python3 install.py", hint)
self.assertIn(install.INSTALLER_PAGE, hint)
def test_a_clone_keeps_python(self):
with unittest.mock.patch.dict(os.environ, {"RETROBIOS_BOOTSTRAP": ""}):
hint = "\n".join(install._manual_usage_hint("linux"))
self.assertIn("python3 install.py --platform retroarch", hint)
def test_a_listing_hint_keeps_the_listing(self):
"""--list-systems with nothing detected was answered with a full
install command: the listing flag gone, a --dest added."""
home = scratch_dir(self)
env = {**os.environ, "HOME": str(home), "RETROBIOS_OS": "linux",
"RETROBIOS_BOOTSTRAP": ""}
for flag in ("--list-systems", "--list-cores", "--list-emulators", "--list-targets"):
with self.subTest(flag=flag):
proc = _install_run(env, flag, "--no-input")
self.assertEqual(proc.returncode, 1, proc.stdout + proc.stderr)
self.assertIn(f"python3 install.py --platform retroarch {flag}", proc.stdout)
self.assertNotIn("--dest", proc.stdout)
def test_the_write_advice_keeps_the_narrowing(self):
self._record("--system", "atari-lynx", "--region", "us")
install._INVOCATION.os_type = "linux"
with unittest.mock.patch.dict(os.environ, {"RETROBIOS_BOOTSTRAP": "sh"}):
advice = install._write_advice(Path("/media/ro"), False, "retroarch")
self.assertIn("| sudo sh -s -- --platform retroarch --dest /media/ro "
"--system atari-lynx --region us", advice)
def test_the_platforms_own_list_is_kept_in_the_hint(self):
self._record("--declared-only", "--system", "atari-lynx")
install._INVOCATION.os_type = "linux"
with unittest.mock.patch.dict(os.environ, {"RETROBIOS_BOOTSTRAP": ""}):
command = install._INVOCATION.command("retroarch", None)
self.assertIn("--system atari-lynx --declared-only", command)
def test_windows_has_no_sudo(self):
self._record("--system", "atari-lynx")
install._INVOCATION.os_type = "windows"
with unittest.mock.patch.dict(os.environ, {"RETROBIOS_BOOTSTRAP": "ps1"}):
advice = install._write_advice(Path("C:/Program Files/RetroArch/system"),
False, "retroarch")
self.assertNotIn("sudo", advice)
self.assertIn("administrator", advice)
self.assertIn("/i.ps1", advice)
self.assertIn("--system atari-lynx", advice)
def test_a_full_drive_keeps_the_platform(self):
install._INVOCATION.os_type = "linux"
err = io.StringIO()
with unittest.mock.patch.object(install.shutil, "disk_usage",
return_value=SimpleNamespace(free=1)), \
contextlib.redirect_stderr(err), self.assertRaises(SystemExit):
reported(install._check_free_space, scratch_dir(self), 10, "batocera")
self.assertIn("--platform batocera --dest <folder on another drive>", err.getvalue())
def test_the_one_liner_tells_the_installer_how_it_was_started(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
probe = root / "probe.py"
probe.write_text("import os\nprint('bootstrap:', os.environ.get('RETROBIOS_BOOTSTRAP'))\n")
fake_bin = root / "bin"
fake_bin.mkdir()
(fake_bin / "curl").write_text(
"#!/bin/sh\noutput=\n"
"while [ \"$#\" -gt 0 ]; do\n"
" if [ \"$1\" = --output ]; then output=$2; shift 2; else shift; fi\n"
"done\n"
f"cp -- {shlex.quote(str(probe))} \"$output\"\n")
(fake_bin / "curl").chmod(0o755)
env = {**os.environ, "PATH": f"{fake_bin}{os.pathsep}{os.environ['PATH']}",
"TMPDIR": str(root),
"RETROBIOS_INSTALL_URL": "https://example.invalid/install.py",
"RETROBIOS_INSTALL_SHA256": hashlib.sha256(probe.read_bytes()).hexdigest()}
proc = subprocess.run(["sh", "-s"], cwd=root, env=env, capture_output=True,
input=(REPO_ROOT / "install.sh").read_text(),
text=True, timeout=30, check=False)
self.assertIn("bootstrap: sh", proc.stdout, proc.stderr)
def test_the_help_has_examples_and_the_page(self):
env = {**os.environ, "RETROBIOS_BOOTSTRAP": "sh"}
proc = _install_run(env, "--help")
self.assertEqual(proc.returncode, 0)
self.assertIn("examples:", proc.stdout)
self.assertIn("/i | sh -s -- --platform batocera --dest", proc.stdout)
self.assertIn(install.INSTALLER_PAGE, proc.stdout)
class APlanGroupFitsItsLine(unittest.TestCase):
"""Batocera's group of systems sharing MAME's device ROMs was one plan
line of 732 characters: counted as one row, it wrapped to ten and the
total scrolled out of sight."""
def _plan(self, files: list[dict], size: tuple[int, int], verbose: bool = False) -> str:
out = io.StringIO()
with unittest.mock.patch.object(install.shutil, "get_terminal_size",
return_value=os.terminal_size(size)), \
contextlib.redirect_stdout(out):
install._print_plan(files, Path("dest"), install.Terminal(io.StringIO()),
verbose=verbose)
return out.getvalue()
def test_a_wide_group_is_named_by_its_first_two(self):
shared = [f"system-number-{n:02d}" for n in range(40)]
text = self._plan([{"dest": "dev.rom", "size": 1, "systems": shared}], (80, 40))
self.assertIn("system-number-00, system-number-01 and 38 more systems", text)
self.assertLess(max(map(len, text.splitlines())), 80)
self.assertIn("system-number-39", self._plan(
[{"dest": "dev.rom", "size": 1, "systems": shared}], (80, 40), verbose=True))
def test_wrapped_rows_count_against_the_screen(self):
# Six lines that each wrap to three rows on a 20-column terminal of
# 20 rows: 18 rows of listing, past what the screen leaves it.
files = [{"dest": f"{'x' * 50}{n}.bin", "size": 1, "systems": [f"s{n}"]}
for n in range(3)]
text = self._plan(files, (20, 20))
self.assertIn("the largest first", text)
class ReadersAreThePlatformsCores(unittest.TestCase):
"""--check said bbcb.zip is "read by clk, mame, mame0258, mame0278" on
Batocera: system_cores reaches every profile, Recalbox's builds too."""
def test_an_off_platform_core_is_not_named(self):
manifest = {
"system_cores": {"bbc": ["clk", "mame", "mame0258"]},
"files": [{"dest": "bbcb.zip", "systems": ["bbc"], "cores": None},
{"dest": "other.rom", "systems": ["x"], "cores": ["clk", "mame"]}],
}
self.assertEqual(install.readers(manifest["files"][0], manifest), ["clk", "mame"])
def test_on_the_real_lists(self):
for plat, dest, absent in (("batocera", "bbcb.zip", "mame0258"),
("retroarch", "prboom.wad", "bizhawk")):
manifest = json.loads((REPO_ROOT / "install" / f"{plat}.json").read_text())
[entry] = [e for e in manifest["files"] + manifest["omitted_files"]
if e["dest"] == dest]
with self.subTest(platform=plat):
self.assertNotIn(absent, install.readers(entry, manifest))
class WhatTheCollectionLacksSaysForWhom(unittest.TestCase):
"""Required files the installer cannot supply were listed by bare name:
DARK.GOB, Data.rsdk, ... and 69 more, with no system and no emulator."""
def test_grouped_by_system_with_its_emulators(self):
entries = [
{"dest": "DARK.GOB", "system": "dark-forces", "cores": ["theforceengine"]},
{"dest": "SOUNDS.GOB", "system": "dark-forces", "cores": ["theforceengine"]},
{"dest": "Data.rsdk", "system": "sonic-mania", "cores": ["sonicmania"]},
]
lines = install._needed_lines(entries, "Needed to start an emulator")
self.assertIn(" dark-forces (theforceengine): DARK.GOB, SOUNDS.GOB", lines)
self.assertIn(" sonic-mania (sonicmania): Data.rsdk", lines)
self.assertIn("(3 files)", lines[0])
# The page of each emulator under its group: the index alone left a
# search over several hundred pages.
self.assertEqual(lines[lines.index(" dark-forces (theforceengine): DARK.GOB, "
"SOUNDS.GOB") + 1],
f" {install.SITE_URL}emulators/theforceengine/")
self.assertIn(f" {install.SITE_URL}emulators/sonicmania/", lines)
self.assertNotIn(f"{install.SITE_URL}emulators/\n", "\n".join(lines) + "\n")
def test_a_group_with_no_emulator_keeps_the_index(self):
lines = install._needed_lines([{"dest": "x.bin", "system": "x"}], "Needed")
self.assertIn(f"{install.SITE_URL}emulators/", lines[-1])
class AnErrorIsOneShortSentence(unittest.TestCase):
"""A message on stderr says what to do in a few words: one of 35 words
asked a beginner to read a paragraph to find the option to give."""
BUDGET = 25
def test_every_stderr_message_is_short(self):
import ast
tree = ast.parse((REPO_ROOT / "install.py").read_text(encoding="utf-8"))
long = []
for node in ast.walk(tree):
if not isinstance(node, ast.Call):
continue
name = getattr(node.func, "id", getattr(node.func, "attr", ""))
to_err = any(k.arg == "file" and "stderr" in ast.unparse(k.value)
for k in node.keywords)
if not (to_err or name == "error"):
continue
text = " ".join(ast.unparse(a) for a in node.args)
if ".command(" in text:
continue
prose = re.sub(r"\{[^}]*\}", "X", text)
words = len(re.findall(r"[A-Za-z']+", prose))
if words > self.BUDGET:
long.append((node.lineno, words))
self.assertEqual(long, [])
class TheNextStepFitsWhereTheInstallerRuns(unittest.TestCase):
"""Run on a KNULLI device, the last line said "Next: start KNULLI and
play.": the system it runs on is already started."""
def test_on_the_device_detection_found(self):
folder = Path("/userdata/bios")
with unittest.mock.patch.dict(install._DETECTED_OS, {("knulli", folder): "KNULLI"},
clear=True), \
unittest.mock.patch.object(install, "_mounted_cards", return_value=[]):
self.assertEqual(install.next_step_words({folder: "knulli"}, [folder]),
"Next: start a game.")
def test_on_a_computer(self):
folder = Path("/home/me/.config/retroarch/system")
with unittest.mock.patch.dict(install._DETECTED_OS, clear=True), \
unittest.mock.patch.object(install, "_mounted_cards", return_value=[]):
self.assertEqual(install.next_step_words({folder: "retroarch"}, [folder]),
"Next: start RetroArch and play.")
def test_a_card_mounted_on_a_computer(self):
card = Path("/media/me/KNULLI")
with unittest.mock.patch.dict(install._DETECTED_OS, clear=True), \
unittest.mock.patch.object(install, "on_its_card", return_value=True):
self.assertEqual(install.next_step_words({card / "bios": "knulli"}, [card / "bios"]),
install.NEXT_ON_CARD)
class EachRunSaysErrorsFromItsOwnOptions(unittest.TestCase):
"""--verbose set the raw-cause flag and nothing cleared it: a later run
in the same process ended its clock message with the raw SSL text."""
def test_a_run_without_verbose_clears_it(self):
self.addCleanup(install._RAW_ERRORS.clear)
install.say_raw_errors(True)
self.assertTrue(install._RAW_ERRORS.is_set())
install.say_raw_errors(False)
self.assertFalse(install._RAW_ERRORS.is_set())
if __name__ == "__main__":
unittest.main()
-618
View File
@@ -39,8 +39,6 @@ from model import region
# that floor.
if sys.version_info >= (3, 9):
from resolve import regions as resolve_regions
from packs import archive, coverage, platformpack
from packs import manifest as packs_manifest
REPO_ROOT = Path(__file__).resolve().parent.parent
@@ -805,524 +803,6 @@ class TheOneLinerNarrows(unittest.TestCase):
self.assertEqual(len(repo.installed()), 5)
class InstallMatchesThePack(unittest.TestCase):
"""Narrowing the full manifest gives what the narrowed build carries."""
PLATFORM = "recalbox"
@classmethod
def setUpClass(cls):
db_path = REPO_ROOT / "database.json"
if not db_path.exists():
raise unittest.SkipTest("database.json is not built")
from model.profiles import load_emulator_profiles
cls.db = json.loads(db_path.read_text(encoding="utf-8"))
cls.profiles = load_emulator_profiles(str(REPO_ROOT / "emulators"))
cls.full = cls._manifest(None)
@classmethod
def _manifest(cls, regions):
return packs_manifest.generate_manifest(
cls.PLATFORM, str(REPO_ROOT / "platforms"), cls.db,
str(REPO_ROOT / "bios"), str(REPO_ROOT / "platforms" / "_registry.yml"),
emulators_dir=str(REPO_ROOT / "emulators"), emu_profiles=cls.profiles,
regions=regions, offline=True,
)
def _narrowed(self, systems, regions):
files, omitted = install.narrow(
self.full["files"], self.full["omitted_files"], systems, [], regions
)
return {f["dest"] for f in files}, {o["dest"] for o in omitted}
def test_regions(self):
for regions in (["north-america"], ["japan", "europe"]):
with self.subTest(regions=regions):
built = self._manifest(regions)
files, omitted = self._narrowed([], regions)
self.assertEqual(files, {f["dest"] for f in built["files"]})
self.assertEqual(omitted, {o["dest"] for o in built["omitted_files"]})
class InstallMatchesASystemPack(unittest.TestCase):
"""`install.py --system X --region R` against a real `generate_pack.py
--system X --region R` build. The cases are the ones a review reproduced:
ColecoVision's extras filed at destinations another system declares, and
PicoDrive's Mega CD variant group spread over two systems. The last pick
holds MAME device sets filed under several systems (systems:), under one,
and under none."""
PLATFORM = "retroarch"
CASES = (
(["coleco-colecovision"], []),
(["sega-mega-cd"], ["north-america"]),
(["sony-playstation"], ["japan"]),
(["elektronika-bk"], []),
)
@classmethod
def setUpClass(cls):
db_path = REPO_ROOT / "database.json"
if not db_path.exists():
raise unittest.SkipTest("database.json is not built")
from packs import manifest as packs_manifest
from model.profiles import load_emulator_profiles
from model.platforms import load_platform_config
cls.db = json.loads(db_path.read_text(encoding="utf-8"))
cls.profiles = load_emulator_profiles(str(REPO_ROOT / "emulators"))
cls.base = load_platform_config(
cls.PLATFORM, str(REPO_ROOT / "platforms")
).get("base_destination", "")
cls.full = packs_manifest.generate_manifest(
cls.PLATFORM, str(REPO_ROOT / "platforms"), cls.db,
str(REPO_ROOT / "bios"), str(REPO_ROOT / "platforms" / "_registry.yml"),
emulators_dir=str(REPO_ROOT / "emulators"), emu_profiles=cls.profiles,
offline=True,
)
def _packed(self, systems, regions) -> set[str]:
import zipfile
with tempfile.TemporaryDirectory() as out:
zip_path = platformpack.generate_pack(
self.PLATFORM, str(REPO_ROOT / "platforms"), self.db,
str(REPO_ROOT / "bios"), out,
emulators_dir=str(REPO_ROOT / "emulators"),
emu_profiles=self.profiles, system_filter=systems,
regions=regions or None, offline=True,
)
with zipfile.ZipFile(zip_path) as zf:
names = set(zf.namelist())
prefix = f"{self.base}/" if self.base else ""
return {n[len(prefix):] if prefix and n.startswith(prefix) else n for n in names}
def test_each_case(self):
known = {f["dest"] for f in self.full["files"]}
for systems, regions in self.CASES:
with self.subTest(systems=systems, regions=regions):
files, _omitted = install.narrow(
self.full["files"], self.full["omitted_files"],
systems, [], regions,
)
self.assertEqual(
{f["dest"] for f in files}, self._packed(systems, regions) & known
)
class DataTreesFollowTheSystemsTheyServe(InstallMatchesASystemPack):
"""The data trees a system choice keeps, installer against pack. GBA
alone took FBNeo's cheats and samples, 3,472 files, because the trees
listed every FBNeo system. The Mega Drive took no cheat: the tree said
sega-megadrive where RetroArch's list says sega-mega-drive."""
CASES = ((["nintendo-gba"], []), (["sega-mega-drive"], []), (["arcade"], []))
def _packed(self, systems, regions) -> set[str]:
import zipfile
from model.registry import load_data_dir_registry
# The trees come from the registry the manifest reads by itself.
with tempfile.TemporaryDirectory() as out:
zip_path = platformpack.generate_pack(
self.PLATFORM, str(REPO_ROOT / "platforms"), self.db,
str(REPO_ROOT / "bios"), out,
emulators_dir=str(REPO_ROOT / "emulators"),
emu_profiles=self.profiles, system_filter=systems,
regions=regions or None, offline=True,
data_registry=load_data_dir_registry(str(REPO_ROOT / "platforms")),
)
with zipfile.ZipFile(zip_path) as zf:
names = set(zf.namelist())
prefix = f"{self.base}/" if self.base else ""
return {n[len(prefix):] if prefix and n.startswith(prefix) else n for n in names}
def _members(self, trees) -> set[str]:
return {f"{tree['dest']}/{rel}" if tree["dest"] else rel
for tree in trees for rel in tree["files"]}
def test_each_case(self):
everything = self._members(self.full.get("data_directories") or [])
for systems, regions in self.CASES:
with self.subTest(systems=systems):
files, _omitted = install.narrow(
self.full["files"], self.full["omitted_files"], systems, [], regions)
trees = install.select_data_directories(self.full, files, True, systems=systems)
refs = {tree["ref"] for tree in trees}
if systems == ["nintendo-gba"]:
self.assertFalse({"fbneo-cheats", "fbneo-samples"} & refs)
if systems == ["sega-mega-drive"]:
self.assertIn("fbneo-cheats", refs)
self.assertNotIn("fbneo-samples", refs)
if systems == ["arcade"]:
self.assertTrue({"fbneo-cheats", "fbneo-samples"} <= refs)
self.assertEqual(self._members(trees),
self._packed(systems, regions) & everything)
class InstallMatchesATargetManifest(unittest.TestCase):
"""`install.py --target T` against the manifest the builder writes for T.
A target list names cores the way the buildbot does (`mednafen_psx`)
while a file names the profile that reads it (`beetle_psx`), and the
builder drops a platform system no available core reads. The installer
did neither: on RetroArch desktop it kept every system and lost the
extras of five cores. A destination two cores read was also filed under
the first alone, so a target without that core lost it, or got the
first core's copy where the other core wants different bytes
(megaplay.zip for mame and mame2003 on Switch).
"""
CASES = (
("retroarch", "desktop"),
("retroarch", "switch"),
("batocera", "x86_64"),
("batocera", "bcm2711"),
("retropie", "rpi4"),
("emudeck", "steamos"),
# A core only Batocera's x86_64 downloader brings, a missing file two
# cores read, a file another emulator installs itself, and a name a
# launcher opens that another core's copy used to fill.
("batocera", "jh7110"),
("knulli", "h700"),
("rocknix", "sdm845"),
("retropie", "x86"),
("retropie", "rpi1"),
)
@classmethod
def setUpClass(cls):
db_path = REPO_ROOT / "database.json"
if not db_path.exists():
raise unittest.SkipTest("database.json is not built")
from model.profiles import load_emulator_profiles
from model.targets import load_target_config
cls.load_target_config = staticmethod(load_target_config)
cls.db = json.loads(db_path.read_text(encoding="utf-8"))
cls.profiles = load_emulator_profiles(str(REPO_ROOT / "emulators"))
def _manifest(self, platform, target_cores=None, target=None):
return packs_manifest.generate_manifest(
platform, str(REPO_ROOT / "platforms"), self.db,
str(REPO_ROOT / "bios"), str(REPO_ROOT / "platforms" / "_registry.yml"),
emulators_dir=str(REPO_ROOT / "emulators"), emu_profiles=self.profiles,
target_cores=target_cores, target_name=target, offline=True,
)
def test_each_target(self):
for platform, target in self.CASES:
with self.subTest(platform=platform, target=target):
cores = sorted(self.load_target_config(
platform, target, str(REPO_ROOT / "platforms")
))
full = self._manifest(platform)
built = self._manifest(platform, set(cores), target)
files, omitted = install.narrow_to_cores(full, cores, target=target)
# A case-insensitive platform reads one file under both
# spellings (BlastEm's 32X_M_BIOS.bin, PicoDrive's .BIN).
from model.platforms import load_platform_config
config = load_platform_config(platform, str(REPO_ROOT / "platforms"))
fold = str.lower if config.get("case_insensitive_fs") else str
self.assertEqual(
{(fold(f["dest"]), f["sha1"]) for f in files},
{(fold(f["dest"]), f["sha1"]) for f in built["files"]},
)
self.assertEqual(
{o["dest"] for o in omitted},
{o["dest"] for o in built["omitted_files"]},
)
class AHeldFileTheInstallerCannotFetch(unittest.TestCase):
"""The builder holds a release asset this offline run has no copy of.
jp.bin is registered in .gitignore and indexed by the database, as every
file of the large-files release is, but neither the checkout nor
.cache/large/ holds it. The builder counts it held, so it displaces the
American fallback for --region japan, while the manifest can only list
it as omitted. Counted unheld there, the installer kept the fallback the
pack drops."""
def setUp(self):
from model import profiles
from model.platforms import _platform_config_cache
from model.profiles import emulator_profiles_cache
from collection.indexes import build_indexes
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
previous = os.getcwd()
os.chdir(tmp.name)
self.addCleanup(os.chdir, previous)
Path("bios/Sys").mkdir(parents=True)
Path("bios/Sys/us.bin").write_bytes(b"us")
files = {}
for name, payload in (("us.bin", b"us"), ("jp.bin", b"jp")):
sha1 = hashlib.sha1(payload).hexdigest()
files[sha1] = {"path": f"bios/Sys/{name}", "name": name,
"size": len(payload), "sha1": sha1,
"md5": hashlib.md5(payload).hexdigest(),
"sha256": hashlib.sha256(payload).hexdigest(),
"crc32": "00000001"}
# The registry of release assets: listed there, so a checkout has
# no copy, and this offline run fetches none.
Path(".gitignore").write_text("bios/Sys/jp.bin\n")
registered = mock.patch(
"model.largefiles.load_registered_paths", return_value=["bios/Sys/jp.bin"]
)
registered.start()
self.addCleanup(registered.stop)
self.db = {"files": files, "indexes": build_indexes(files, {})}
Path("platforms").mkdir()
Path("platforms/p.yml").write_text(json.dumps({
"platform": "P", "version": "2.1", "verification_mode": "existence", "cores": ["core"],
"systems": {"sys": {"files": []}},
}))
Path("platforms/_registry.yml").write_text(
json.dumps({"platforms": {"p": {"config": "p.yml"}}})
)
Path("emulators").mkdir()
Path("emulators/core.yml").write_text(json.dumps({
"emulator": "core", "type": "libretro", "systems": ["sys"], "files": [
{"name": "jp.bin", "system": "sys", "region": ["japan"],
"required": True},
{"name": "us.bin", "system": "sys", "region": ["north-america"],
"required": True},
]}))
_platform_config_cache.clear()
emulator_profiles_cache.clear()
self.addCleanup(_platform_config_cache.clear)
self.addCleanup(emulator_profiles_cache.clear)
self.profiles = profiles.load_emulator_profiles("emulators")
def _manifest(self, regions):
return packs_manifest.generate_manifest(
"p", "platforms", self.db, "bios", "platforms/_registry.yml",
emulators_dir="emulators", emu_profiles=self.profiles,
regions=regions, offline=True,
)
def test_the_installer_drops_the_fallback_the_pack_drops(self):
full = self._manifest(None)
omitted = {o["dest"]: o for o in full["omitted_files"]}
self.assertTrue(omitted["jp.bin"].get("held"))
built = self._manifest(["japan"])
self.assertNotIn("us.bin", {f["dest"] for f in built["files"]})
files, still = install.narrow(
full["files"], full["omitted_files"], [], [], ["japan"]
)
self.assertEqual({f["dest"] for f in files}, {f["dest"] for f in built["files"]})
self.assertEqual(
{o["dest"] for o in still}, {o["dest"] for o in built["omitted_files"]}
)
class AStandaloneLayoutBelowTheBiosFolder(unittest.TestCase):
"""DuckStation reads `bios/<name>` standalone, which a platform whose
base destination is `bios` places in that folder itself. The collector
deduplicated the path at `bios/bios/<name>` while the builder wrote it
at `bios/<name>`, so another core's claim on the same file took the
path under a second key: `--region north-america` withdrew one key and
the pack kept the European BIOS the installer drops."""
def setUp(self):
from model import profiles
from model.platforms import _platform_config_cache
from model.profiles import emulator_profiles_cache
from collection.indexes import build_indexes
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
previous = os.getcwd()
os.chdir(tmp.name)
self.addCleanup(os.chdir, previous)
Path("bios/Sys").mkdir(parents=True)
files = {}
for name in ("us.bin", "eu.bin", "jp.bin"):
payload = name.encode()
Path(f"bios/Sys/{name}").write_bytes(payload)
sha1 = hashlib.sha1(payload).hexdigest()
files[sha1] = {"path": f"bios/Sys/{name}", "name": name,
"size": len(payload), "sha1": sha1,
"md5": hashlib.md5(payload).hexdigest(),
"sha256": hashlib.sha256(payload).hexdigest(),
"crc32": "00000001"}
self.db = {"files": files, "indexes": build_indexes(files, {})}
Path("platforms").mkdir()
Path("platforms/p.yml").write_text(json.dumps({
"platform": "P", "version": "2.1", "verification_mode": "existence",
"base_destination": "bios", "cores": ["dsa", "other"],
"standalone_cores": ["dsa"], "systems": {"sys": {"files": []}},
}))
Path("platforms/_registry.yml").write_text(
json.dumps({"platforms": {"p": {"config": "p.yml"}}})
)
Path("emulators").mkdir()
Path("emulators/dsa.yml").write_text(json.dumps({
"emulator": "dsa", "type": "standalone + libretro", "systems": ["sys"],
"files": [
{"name": name, "standalone_path": f"bios/{name}", "system": "sys",
"region": [region_name], "required": False}
for name, region_name in (
("us.bin", "north-america"), ("eu.bin", "europe"),
("jp.bin", "japan"),
)
]}))
Path("emulators/other.yml").write_text(json.dumps({
"emulator": "other", "type": "libretro", "systems": ["sys"], "files": [
{"name": name, "system": "sys", "required": False}
for name in ("eu.bin", "jp.bin")
]}))
_platform_config_cache.clear()
emulator_profiles_cache.clear()
self.addCleanup(_platform_config_cache.clear)
self.addCleanup(emulator_profiles_cache.clear)
self.profiles = profiles.load_emulator_profiles("emulators")
def _manifest(self, regions):
return packs_manifest.generate_manifest(
"p", "platforms", self.db, "bios", "platforms/_registry.yml",
emulators_dir="emulators", emu_profiles=self.profiles,
regions=regions, offline=True,
)
def test_one_path_one_entry(self):
full = self._manifest(None)
dests = [f["dest"] for f in full["files"]]
self.assertEqual(sorted(dests), ["eu.bin", "jp.bin", "us.bin"])
def test_the_installer_keeps_what_the_pack_keeps(self):
full = self._manifest(None)
built = self._manifest(["north-america"])
self.assertEqual({f["dest"] for f in built["files"]}, {"us.bin"})
files, _omitted = install.narrow(
full["files"], full["omitted_files"], [], [], ["north-america"]
)
self.assertEqual({f["dest"] for f in files}, {f["dest"] for f in built["files"]})
class AnExtraBesideItsDeclarationBelowTheBiosFolder(unittest.TestCase):
"""RetroDECK declares bios/scph5501.bin and writes core files below bios/.
Beetle PSX's scph5501.bin was grouped bare, a key whose region lookup
another core leaves untagged, while the declaration's key resolved to
DuckStation's American tag: `--region japan` dropped the declaration and
the pack kept the file through the core's claim, while the installer,
which records the path once, dropped it."""
def setUp(self):
from model import profiles
from model.platforms import _platform_config_cache
from model.profiles import emulator_profiles_cache
from collection.indexes import build_indexes
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
previous = os.getcwd()
os.chdir(tmp.name)
self.addCleanup(os.chdir, previous)
Path("bios/Sys").mkdir(parents=True)
files = {}
for name in ("us.bin", "jp.bin"):
payload = name.encode()
Path(f"bios/Sys/{name}").write_bytes(payload)
sha1 = hashlib.sha1(payload).hexdigest()
files[sha1] = {"path": f"bios/Sys/{name}", "name": name,
"size": len(payload), "sha1": sha1,
"md5": hashlib.md5(payload).hexdigest(),
"sha256": hashlib.sha256(payload).hexdigest(),
"crc32": "00000001"}
self.db = {"files": files, "indexes": build_indexes(files, {})}
Path("platforms").mkdir()
Path("platforms/p.yml").write_text(json.dumps({
"platform": "P", "version": "2.1", "verification_mode": "existence",
"base_destination": "", "cores": ["beetle", "dsa", "other"],
"standalone_cores": ["dsa"], "systems": {"sys": {"files": [
{"name": name, "destination": f"bios/{name}", "required": False}
for name in ("us.bin", "jp.bin")
]}},
}))
Path("platforms/_registry.yml").write_text(
json.dumps({"platforms": {"p": {"config": "p.yml"}}})
)
Path("emulators").mkdir()
Path("emulators/dsa.yml").write_text(json.dumps({
"emulator": "dsa", "type": "standalone + libretro", "systems": ["sys"],
"files": [
{"name": name, "standalone_path": f"bios/{name}", "system": "sys",
"region": [region_name], "required": False}
for name, region_name in (("us.bin", "north-america"), ("jp.bin", "japan"))
]}))
# Read before dsa, as beetle_psx is before duckstation, and untagged
# by name through a second reader, as pcsx1 leaves scph5501.bin.
Path("emulators/beetle.yml").write_text(json.dumps({
"emulator": "beetle", "type": "libretro", "systems": ["sys"], "files": [
{"name": "us.bin", "system": "sys", "region": ["north-america"],
"required": False},
]}))
Path("emulators/other.yml").write_text(json.dumps({
"emulator": "other", "type": "libretro", "systems": ["sys"], "files": [
{"name": "us.bin", "path": "alt/us.bin", "system": "sys", "required": False},
]}))
_platform_config_cache.clear()
emulator_profiles_cache.clear()
self.addCleanup(_platform_config_cache.clear)
self.addCleanup(emulator_profiles_cache.clear)
self.profiles = profiles.load_emulator_profiles("emulators")
def _manifest(self, regions):
return packs_manifest.generate_manifest(
"p", "platforms", self.db, "bios", "platforms/_registry.yml",
emulators_dir="emulators", emu_profiles=self.profiles,
regions=regions, offline=True,
)
def _pack(self, regions):
record = archive.PackRecord()
platformpack.generate_pack(
"p", "platforms", self.db, "bios", "", emulators_dir="emulators",
emu_profiles=self.profiles, regions=regions, offline=True, record=record,
)
return {m for m in record.members if m.startswith("bios/")}
def test_the_slot_choices_are_computed_once(self):
"""pack_overrides recomputed the choices the manifest had just
computed with the same arguments: 8% of a manifest run."""
from packs import slots
real = slots.slot_choices
with mock.patch.object(slots, "slot_choices", side_effect=real) as counted:
self._manifest(None)
self.assertEqual(counted.call_count, 1)
def test_the_pack_and_the_installer_drop_the_same_files(self):
full = self._manifest(None)
files, _omitted = install.narrow(
full["files"], full["omitted_files"], [], [], ["japan"]
)
installed = {f["dest"] for f in files if f["dest"].startswith("bios/")}
self.assertIn("bios/jp.bin", installed)
self.assertEqual(self._pack(["japan"]), installed)
class ACoreWithNothingHeldIsAnOmission(unittest.TestCase):
"""DOSBox serves SC-55/WAVEROM3.BIN, DOSBox Pure wants it and nothing is
held for it. A pack built for a target without DOSBox lists it as left
out. Narrowed from the full manifest, it had vanished."""
MANIFEST = {"files": [{
"dest": "SC-55/WAVEROM3.BIN", "cores": ["dosbox"], "sha1": "1" * 40,
"absent_claims": [{"name": "WAVEROM3.BIN", "system": "dos", "required": False,
"reason": "not_found", "cores": ["dosbox_pure"]}],
}], "omitted_files": []}
def test_narrowing_away_the_holder_leaves_the_omission(self):
files, omitted = install.narrow_to_cores(self.MANIFEST, ["dosbox_pure"])
self.assertEqual(files, [])
self.assertEqual([o["dest"] for o in omitted], ["SC-55/WAVEROM3.BIN"])
files, omitted = install.narrow_to_cores(self.MANIFEST, ["dosbox", "dosbox_pure"])
self.assertEqual(len(files), 1)
self.assertEqual(omitted, [])
class AListingNeedsNoInstall(unittest.TestCase):
"""--list-systems with --platform bizhawk stopped on "not found, name it
with --dest": a question asked before downloading BizHawk was refused."""
@@ -1418,104 +898,6 @@ class ThePlatformsOwnSystemNamesAreAccepted(unittest.TestCase):
self.assertIn("Close matches: sony-playstation", typo.stderr)
class ANativeNameSelectsOneSetEverywhere(unittest.TestCase):
"""`--system psx` was accepted by the installer and refused by
generate_pack and verify. The platform's own name now resolves through
common.resolve_system_arg in both tools, from the mapping the manifest's
system_names is built from, and selects what the system id selects:
the same pack, the same verify report, the same installed files."""
CASES = (
("batocera", "psx", "sony-playstation"),
("recalbox", "msx1,msx2", "microsoft-msx"),
("romm", "acpc", "amstrad-cpc"),
)
@classmethod
def setUpClass(cls):
db_path = REPO_ROOT / "database.json"
if not db_path.exists():
raise unittest.SkipTest("database.json is not built")
from model.profiles import load_emulator_profiles
cls.db = json.loads(db_path.read_text(encoding="utf-8"))
cls.profiles = load_emulator_profiles(str(REPO_ROOT / "emulators"))
def _packed(self, platform: str, systems: list[str]) -> tuple[set[str], set[str]]:
"""The pack's members and those the build took from data directories."""
import zipfile
from packs.packcounts import PackCounts
counts: list[PackCounts] = []
with tempfile.TemporaryDirectory() as out:
zip_path = platformpack.build_platform_pack(
platform, str(REPO_ROOT / "platforms"), self.db,
str(REPO_ROOT / "bios"), out,
emulators_dir=str(REPO_ROOT / "emulators"),
emu_profiles=self.profiles, system_filter=systems, offline=True,
counts=counts,
).zip_path
with zipfile.ZipFile(zip_path) as zf:
names = {i.filename for i in zf.infolist() if not i.is_dir()}
return names, set(counts[0].trees)
def test_pack_verify_and_installer_agree(self):
from model.platforms import load_platform_config
from model.systems import resolve_system_arg
from packs.archive import is_pack_note
for platform, native, system_id in self.CASES:
with self.subTest(platform=platform, name=native):
config = load_platform_config(platform, str(REPO_ROOT / "platforms"))
systems = list(config["systems"])
chosen = resolve_system_arg(native, systems, config)
self.assertEqual(chosen, resolve_system_arg(system_id, systems, config))
self.assertEqual(chosen, [system_id])
report = coverage.verify_platform(
config, self.db, str(REPO_ROOT / "emulators"), self.profiles,
system_filter=chosen,
)
self.assertEqual(report["systems"], [system_id])
credited = set(report["pack_destinations"])
members, trees = self._packed(platform, chosen)
written = {
name for name in members
if not is_pack_note(name) and name not in trees - credited
}
self.assertTrue(written)
self.assertEqual(credited, written)
manifest = packs_manifest.generate_manifest(
platform, str(REPO_ROOT / "platforms"), self.db,
str(REPO_ROOT / "bios"),
str(REPO_ROOT / "platforms" / "_registry.yml"),
emulators_dir=str(REPO_ROOT / "emulators"),
emu_profiles=self.profiles, offline=True,
)
picked = install.resolve_choices(
[native],
install.available_choices(
manifest["files"], manifest["omitted_files"]
)["systems"],
"system", install.system_aliases(manifest),
)
self.assertEqual(picked, [system_id])
files, _omitted = install.narrow(
manifest["files"], manifest["omitted_files"], picked, [], [],
)
base = config.get("base_destination", "")
prefix = f"{base}/" if base else ""
packed = {
n[len(prefix):] if prefix and n.startswith(prefix) else n
for n in members
}
known = {f["dest"] for f in manifest["files"]}
self.assertEqual({f["dest"] for f in files}, packed & known)
class SystemNamesResolveOneWay(unittest.TestCase):
"""common.resolve_system_arg, the one reading of --system."""
+747
View File
@@ -0,0 +1,747 @@
"""install.py copies the files standalone emulators read into their own folders, and stays inside them."""
from __future__ import annotations
import tests
from tests import scratch_dir
import contextlib
import io
import json
import os
import re
import shutil
import subprocess
import tempfile
import unittest
import unittest.mock
from pathlib import Path, PurePosixPath
from tests.netguard import module_guard
from tests.installsupport import REPO_ROOT, install, _LoopbackRun, _install_run
# No connection off the loopback and no card of the host reaches a test:
# the module fails at its end otherwise.
setUpModule, tearDownModule = module_guard("test_install_standalone")
class TestStandaloneCopiesExtraDirs(unittest.TestCase):
"""Setups that keep emulators outside the default locations."""
def _manifest(self) -> dict:
return {
"files": [{"dest": "ps2-0230a.bin"}, {"dest": "GC/USA/IPL.bin"}],
"standalone_copies": [
{
"pattern": "ps2-*.bin",
"emulator": "pcsx2",
"targets": {"windows": []},
},
{
"file": "GC/USA/IPL.bin",
"emulator": "dolphin",
"targets": {"windows": []},
},
]
}
def test_pattern_entry_copies_into_extra_dir(self):
bios = scratch_dir(self)
(bios / "ps2-0230a.bin").write_text("rom")
extra = scratch_dir(self) / "bios"
extra.mkdir()
copied, _, _ = install.do_standalone_copies(
self._manifest(), bios, "windows", {"pcsx2": extra}
)
self.assertEqual(copied, 1)
self.assertTrue((extra / "ps2-0230a.bin").exists())
def test_file_entry_keeps_its_subdirectory(self):
bios = scratch_dir(self)
(bios / "GC" / "USA").mkdir(parents=True)
(bios / "GC" / "USA" / "IPL.bin").write_text("ipl")
user = scratch_dir(self) / "User"
(user / "GC" / "USA").mkdir(parents=True)
copied, _, _ = install.do_standalone_copies(
self._manifest(), bios, "windows", {"dolphin": user}
)
self.assertEqual(copied, 1)
self.assertTrue((user / "GC" / "USA" / "IPL.bin").exists())
def test_no_extra_dirs_is_unchanged(self):
bios = scratch_dir(self)
(bios / "ps2-0230a.bin").write_text("rom")
self.assertEqual(
install.do_standalone_copies(self._manifest(), bios, "windows"), (0, 0, 0)
)
@unittest.skipIf(os.name == "nt" or (hasattr(os, "geteuid") and os.geteuid() == 0),
"a closed folder needs POSIX permissions and a non-root user")
class StandaloneFolderClosedToThisUser(unittest.TestCase):
"""Termux on Android 11 and later cannot look into another app's
Android/data folder: is_dir() on Dolphin's GC/USA raised PermissionError
after the install, and the run stopped with code 1."""
def setUp(self) -> None:
self.bios = scratch_dir(self)
(self.bios / "IPL.bin").write_text("ipl")
self.app = scratch_dir(self) / "org.dolphinemu.dolphinemu"
(self.app / "files" / "GC" / "USA").mkdir(parents=True)
self.app.chmod(0)
self.addCleanup(self.app.chmod, 0o755)
self.target = self.app / "files" / "GC" / "USA"
self.manifest = {"files": [{"dest": "IPL.bin"}], "standalone_copies": [
{"file": "IPL.bin", "targets": {"android": [str(self.target)]}},
{"note": "Dolphin reads GC/", "detect": {"android": [str(self.target)]}},
]}
def test_listing_the_folders_here_skips_it(self):
self.assertEqual(install.standalone_folders_here(self.manifest, "android"), [])
def test_planning_skips_it(self):
self.assertEqual(install.standalone_plan([self.manifest], "android"), [])
def test_copying_names_it_and_goes_on(self):
"""Skipped, not failed: Android answers the same refusal for an app
that is not installed, and a run without Dolphin is no failure."""
failures: list[tuple[str, str]] = []
with contextlib.redirect_stdout(io.StringIO()) as out:
result = install.do_standalone_copies(
self.manifest, self.bios, "android", failures=failures)
self.assertEqual(result, (0, 1, 0))
self.assertEqual(failures, [])
self.assertIn(f"{self.target}: could not be read", out.getvalue())
class StandaloneCopiesReadWhereThePackPutTheFile(unittest.TestCase):
"""The registry names Citra/sysdata/aes_keys.txt for every platform,
but EmuDeck's manifest installs it at the root. The installer looked under
the registry's path, found nothing and copied nothing to Azahar."""
def _copy(self, entry: dict) -> tuple[Path, tuple[int, int, int]]:
bios = scratch_dir(self)
(bios / "aes_keys.txt").write_text("keys")
sysdata = scratch_dir(self) / "sysdata"
sysdata.mkdir()
manifest = {"files": [{"dest": "aes_keys.txt"}], "standalone_copies": [
{**entry, "emulator": "azahar", "targets": {"linux": [str(sysdata)]}},
]}
return sysdata, install.do_standalone_copies(manifest, bios, "linux")
def test_the_source_the_manifest_names_is_copied(self):
sysdata, (copied, _, _) = self._copy(
{"file": "Citra/sysdata/aes_keys.txt", "source": "aes_keys.txt"}
)
self.assertEqual(copied, 1)
self.assertEqual((sysdata / "aes_keys.txt").read_text(), "keys")
def test_without_a_source_the_file_path_is_read(self):
_, (copied, _, _) = self._copy({"file": "Citra/sysdata/aes_keys.txt"})
self.assertEqual(copied, 0)
def test_an_unsafe_source_is_refused_at_load(self):
manifest = json.loads(
(Path(__file__).resolve().parents[1] / "install" / "emudeck.json").read_text()
)
manifest["standalone_copies"] = [
{"file": "aes_keys.txt", "source": "../../etc/passwd",
"targets": {"linux": ["/tmp"]}},
]
with self.assertRaisesRegex(ValueError, r"standalone_copies\[0\]\.source"):
install._validate_manifest(manifest, "emudeck")
class StandaloneCopiesFollowTheList(unittest.TestCase):
"""The platform pass scanned the BIOS folder for each pattern: a user's
own bios/old/scph9999.bin went to DuckStation, while the plan of a run
with no platform named scph5501.bin alone."""
def setUp(self) -> None:
self.bios = scratch_dir(self)
(self.bios / "scph5501.bin").write_bytes(b"rom")
(self.bios / "old").mkdir()
(self.bios / "old" / "scph9999.bin").write_bytes(b"other")
self.emu = scratch_dir(self) / "duckstation"
self.emu.mkdir()
self.manifest = {"files": [{"dest": "scph5501.bin"}], "standalone_copies": [
{"pattern": "scph*.bin", "emulator": "duckstation",
"targets": {"linux": [str(self.emu)]}}]}
def test_the_copy_and_the_plan_name_the_same_files(self):
plan = install.standalone_plan([self.manifest], "linux")
self.assertEqual([(folder, [entry["dest"] for entry in entries])
for folder, entries in plan], [(self.emu, ["scph5501.bin"])])
self.assertEqual(install.do_standalone_copies(self.manifest, self.bios, "linux"),
(1, 0, 0))
self.assertEqual(sorted(path.name for path in self.emu.iterdir()), ["scph5501.bin"])
def test_only_a_file_the_run_verified_is_copied(self):
self.assertEqual(install.do_standalone_copies(self.manifest, self.bios, "linux",
verified=[]), (0, 0, 0))
self.assertEqual(list(self.emu.iterdir()), [])
class OuterRootsAreUntrusted(unittest.TestCase):
"""roms_directory and emulators_directory come from a downloaded list: each
path in them meets the rules a BIOS destination meets, and each folder
they resolve to stays where the platform's layout puts it."""
ROMS = {"bios_folder": "bios", "from_bios": ".."}
EMULATORS = {
"bios_folder": "bios", "searched": ["frontend/{key}", "emulators/{key}"],
"default": "emulators/{key}", "config": "frontend/launcher.cfg",
}
UNSAFE = ("..\\x", "C:", "C:/x", "..//x", "../", "../x\x01", "x" * 1025)
def test_unsafe_from_bios_is_refused(self):
for value in (*self.UNSAFE, "../../../x", "../x/..", "x", "./x", "/x", "..x", None):
with self.subTest(from_bios=value), self.assertRaises(ValueError):
install._check_relative_directories({"roms_directory": {**self.ROMS, "from_bios": value}})
def test_the_relations_the_rom_folders_files_use_are_accepted(self):
bios = Path("/card/system/bios")
expected = {
".": bios, "..": Path("/card/system"), "../roms": Path("/card/system/roms"),
"../Roms": Path("/card/system/Roms"), "../../Roms": Path("/card/Roms"),
"../../ROMS": Path("/card/ROMS"),
}
for value, root in expected.items():
with self.subTest(from_bios=value):
install._check_relative_directories({"roms_directory": {**self.ROMS, "from_bios": value}})
self.assertEqual(install._relative_root(bios, value), root)
self.assertEqual(install.MAX_ROMS_UPS, 2)
def test_a_list_without_a_roms_root_leaves_the_files_out(self):
files = [{"dest": "pico-8/pico8_64", "root": "roms", "cores": ["x"]}]
kept, left = install.place_relative_entries(files, Path("/card/roms/bios"), {})
self.assertEqual((kept, [o["reason"] for o in left]), ([], ["no_roms_directory"]))
def test_roms_left_out_are_counted_after_the_choice(self):
# --system panasonic-3do said 1918 EmuDeck roms files were left out, 6 of them its.
files = [{"dest": f"{system}/x{n}.bin", "root": "roms", "systems": [system]}
for system, count in (("panasonic-3do", 1), ("sony-psx", 3)) for n in range(count)]
bios = Path("/card/notbios")
with contextlib.redirect_stdout(io.StringIO()) as out:
kept, left = install.place_relative_entries(files, bios, {})
self.assertEqual((kept, len(left), out.getvalue()), ([], 4, ""))
chosen = install.narrow([], left, ["panasonic-3do"], [], [])[1]
with contextlib.redirect_stdout(io.StringIO()) as out:
install.say_unplaced(chosen, "EmuDeck")
self.assertEqual(out.getvalue(), " 1 file read from EmuDeck's roms folder left out: "
f"{bios} is not its BIOS folder.\n")
def test_unsafe_emulator_folders_are_refused(self):
for field in ("searched", "default", "config"):
for value in self.UNSAFE + ("../{key}", "/abs/{key}"):
layout = dict(self.EMULATORS)
if field == "searched":
layout["searched"] = [value if "{key}" in value else value + "/{key}"]
layout["default"] = layout["searched"][0]
elif field == "default":
layout["default"] = value
else:
layout["config"] = value
with self.subTest(field=field, value=value), self.assertRaises(ValueError):
install._check_emulators_directory({"emulators_directory": layout})
def test_a_drive_or_colon_segment_is_refused_on_either_host(self):
from pathlib import PureWindowsPath
bios = PureWindowsPath("C:/RetroBat/bios")
for host in ("posix", "nt"):
with unittest.mock.patch.object(install.os, "name", host):
for value in ("../C:x", "..\\x", "../x:", "../Roms:x"):
with self.subTest(host=host, from_bios=value), self.assertRaises(ValueError):
install._check_relative_directories(
{"roms_directory": {**self.ROMS, "from_bios": value}})
for value in ("C:x/{key}", "x:/{key}", "emu:x/{key}"):
layout = {**self.EMULATORS, "searched": [value], "default": value}
with self.subTest(host=host, searched=value), self.assertRaises(ValueError):
install._check_emulators_directory({"emulators_directory": layout})
for dest in ("bios/C:x.bin", "bios/x:", "a:b/c.bin"):
with self.subTest(host=host, dest=dest), self.assertRaises(ValueError):
install._safe_relative_path(dest, "dest")
with self.subTest(host=host), self.assertRaises(ValueError):
install._safe_relative_path("pico8/x:y", "dest", colons=False)
# Each of these leaves the folder on Windows once joined.
self.assertEqual(PureWindowsPath(bios, "..", "D:x"), PureWindowsPath("D:x"))
self.assertEqual(PureWindowsPath("C:x").drive, "C:")
# RetroDECK's ares names are kept where the host is not Windows.
with unittest.mock.patch.object(install.os, "name", "posix"):
install._safe_relative_path("bios/snes:st018.data.rom", "dest")
with unittest.mock.patch.object(install.os, "name", "nt"), self.assertRaises(ValueError):
install._safe_relative_path("bios/snes:st018.data.rom", "dest")
def test_an_emulator_folder_stays_below_the_platform_folder(self):
with tempfile.TemporaryDirectory() as tmp:
home = Path(tmp)
(home / "bios").mkdir()
(home / "frontend").mkdir()
(home / "frontend" / "launcher.cfg").write_text("")
manifest = {"emulators_directory": self.EMULATORS}
for dest in ("../x/prog", "C:/prog", "prog\\..\\..\\x/a"):
with self.subTest(dest=dest), self.assertRaises(ValueError):
install.place_emulator_entries(
[{"dest": dest, "root": "emulators"}], home / "bios", manifest)
kept, left = install.place_emulator_entries(
[{"dest": "prog/prog.exe", "root": "emulators"}], home / "bios", manifest)
self.assertEqual((len(kept), left), (1, []))
folder, rest = install._entry_place(home / "bios", kept[0])
self.assertEqual((folder, rest), (home / "emulators" / "prog", "prog.exe"))
class ARefusedCopyIsAnError(unittest.TestCase):
"""A PermissionError on a standalone copy was counted as "dir not found"
and the run ended on Done with exit code 0."""
def test_a_refused_copy_is_counted_failed_not_skipped(self):
with tempfile.TemporaryDirectory() as tmp:
bios = Path(tmp, "bios")
bios.mkdir()
(bios / "prod.keys").write_bytes(b"k")
target = Path(tmp, "keys")
target.mkdir()
manifest = {"files": [{"dest": "prod.keys"}], "standalone_copies": [
{"file": "prod.keys", "targets": {"linux": [str(target)]}}
]}
with unittest.mock.patch("shutil.copy2", side_effect=PermissionError(13, "denied")):
result = install.do_standalone_copies(manifest, bios, "linux")
self.assertEqual(result, (0, 0, 1))
class TestStandaloneCopyTargetsAreUntrusted(unittest.TestCase):
"""The manifest names these directories, so it does not get to escape them."""
def _manifest(self, target: str) -> dict:
return {
"files": [{"dest": "boot.bin"}],
"standalone_copies": [
{"file": "boot.bin", "targets": {"linux": [target]}}
]
}
def test_traversal_in_a_target_is_refused_by_validation(self):
manifest = {
"manifest_version": 2,
"platform": "retroarch",
"files": [],
"standalone_copies": [
{"file": "boot.bin", "targets": {"linux": ["~/emu/../../../etc"]}}
],
}
with self.assertRaises(ValueError):
install._validate_manifest(manifest, "retroarch")
def test_nul_in_a_target_is_refused_by_validation(self):
manifest = {
"manifest_version": 2,
"platform": "retroarch",
"files": [],
"standalone_copies": [
{"file": "boot.bin", "targets": {"linux": ["/tmp/a\x00b"]}}
],
}
with self.assertRaises(ValueError):
install._validate_manifest(manifest, "retroarch")
def test_entries_the_copy_step_cannot_read_are_refused(self):
"""Each passed validation and crashed do_standalone_copies after the
download: a list for detect, no file nor pattern, a list for emulator."""
for entry in (
{"note": "hi", "detect": ["/x"]},
{"emulator": "pcsx2", "targets": {"linux": ["/tmp"]}},
{"file": "bios/x.bin", "emulator": ["a"], "targets": {"linux": ["/tmp"]}},
):
manifest = {"manifest_version": 2, "platform": "retroarch", "files": [],
"standalone_copies": [entry]}
with self.subTest(entry=entry), self.assertRaises(ValueError):
install._validate_manifest(manifest, "retroarch")
def test_plain_absolute_target_still_works(self):
manifest = {
"manifest_version": 2,
"platform": "retroarch",
"files": [],
"standalone_copies": [
{"file": "boot.bin", "targets": {"linux": ["~/.config/emu/bios"]}}
],
}
self.assertIsInstance(install._validate_manifest(manifest, "retroarch"), dict)
def test_symlinked_destination_is_not_written_through(self):
"""A link at the name was skipped in silence and counted as "dir
not found" while the folder existed: the emulator kept whatever the
link pointed at. The link now gives way to the file."""
bios = scratch_dir(self)
(bios / "boot.bin").write_text("rom")
target = scratch_dir(self)
outside = scratch_dir(self) / "secret"
outside.write_text("untouched")
(target / "boot.bin").symlink_to(outside)
out = io.StringIO()
with contextlib.redirect_stdout(out):
copied, skipped, failed = install.do_standalone_copies(
self._manifest(str(target)), bios, "linux"
)
self.assertEqual((copied, skipped, failed), (1, 0, 0))
self.assertEqual(outside.read_text(), "untouched")
self.assertFalse((target / "boot.bin").is_symlink())
self.assertEqual((target / "boot.bin").read_text(), "rom")
self.assertIn("link replaced", out.getvalue())
self.assertEqual(sorted(p.name for p in target.iterdir()), ["boot.bin"])
@unittest.skipUnless(shutil.which("pwsh"), "pwsh not available")
class TestDestinationFollowsUserLinks(unittest.TestCase):
"""EmuDeck links bios/shadps4/sys_modules into shadPS4's own directory.
The manifest still cannot climb out of the BIOS root, but a link the
user's frontend placed under it is where the emulator reads the file.
"""
def test_symlinked_directory_under_root_is_followed(self):
bios = scratch_dir(self)
elsewhere = scratch_dir(self) / "sys_modules"
elsewhere.mkdir()
(bios / "shadps4").mkdir()
(bios / "shadps4" / "sys_modules").symlink_to(elsewhere)
dest = install._destination_path(bios, "shadps4/sys_modules/libSceLibcInternal.sprx")
self.assertEqual(dest, bios.resolve() / "shadps4" / "sys_modules" / "libSceLibcInternal.sprx")
self.assertEqual(dest.resolve().parent, elsewhere.resolve())
def test_check_local_survives_a_linked_directory(self):
bios = scratch_dir(self)
elsewhere = scratch_dir(self) / "sys_modules"
elsewhere.mkdir()
(bios / "shadps4").mkdir()
(bios / "shadps4" / "sys_modules").symlink_to(elsewhere)
entry = {"dest": "shadps4/sys_modules/x.sprx", "size": 1, "sha1": "0" * 40}
to_download, up_to_date, mismatched = install.check_local([entry], bios)
self.assertEqual([e["dest"] for e in to_download], ["shadps4/sys_modules/x.sprx"])
self.assertEqual((up_to_date, mismatched), ([], []))
def test_climbing_is_still_refused(self):
bios = scratch_dir(self)
with self.assertRaises(ValueError):
install._destination_path(bios, "../outside.bin")
with self.assertRaises(ValueError):
install._destination_path(bios, "/etc/passwd")
class StandaloneCopiesReachFlatpakInstalls(unittest.TestCase):
"""Copies went only to native and AppImage directories: Dolphin, PPSSPP
and Azahar from Flathub received nothing. Azahar's native Linux
directory was wrong too: XDG_DATA_HOME/azahar-emu (common_paths.h
EMU_DATA_DIR), not azahar."""
def test_flatpak_and_native_directories(self):
import yaml
registry = yaml.safe_load((REPO_ROOT / "platforms" / "_registry.yml").read_text())
linux = {
target
for entry in registry["_standalone_copies"]
for target in (entry.get("targets") or {}).get("linux", [])
}
for expected in (
"$HOME/.var/app/org.DolphinEmu.dolphin-emu/data/dolphin-emu/GC/USA",
"$HOME/.var/app/org.ppsspp.PPSSPP/config/ppsspp/PSP/SYSTEM",
"$HOME/.var/app/org.azahar_emu.Azahar/data/azahar-emu/sysdata",
"$HOME/.local/share/azahar-emu/sysdata",
):
with self.subTest(target=expected):
self.assertIn(expected, linux)
self.assertNotIn("$HOME/.local/share/azahar/sysdata", linux)
class ARefusedStandaloneCopyIsNamed(unittest.TestCase):
"""A standalone copy refused by a read-only emulator folder counted as
an error with no failures entry: the summary named no file, pointed at
the BIOS folder and gave no advice."""
def test_the_file_and_its_folder_reach_the_summary(self):
import contextlib
if os.name == "nt" or os.geteuid() == 0:
self.skipTest("a read-only folder needs POSIX modes and a non-root user")
bios = scratch_dir(self)
(bios / "scph5501.bin").write_bytes(b"x")
target = scratch_dir(self) / "pcsx2-bios"
target.mkdir()
target.chmod(0o500)
self.addCleanup(target.chmod, 0o700)
manifest = {"files": [{"dest": "scph5501.bin"}], "standalone_copies": [
{"file": "scph5501.bin", "emulator": "pcsx2",
"targets": {"linux": [str(target)]}},
]}
failures: list = []
with contextlib.redirect_stdout(io.StringIO()):
_copied, _skipped, failed = install.do_standalone_copies(
manifest, bios, "linux", None, failures)
self.assertEqual(failed, 1)
self.assertEqual(failures, [(str(target / "scph5501.bin"), install.NOT_WRITABLE)])
out = io.StringIO()
with contextlib.redirect_stdout(out), self.assertRaises(SystemExit):
install._report_outcome([bios], 0, 1, failed, 0, False,
details=install.OutcomeDetails(failures=failures))
text = out.getvalue()
self.assertIn(str(target / "scph5501.bin"), text)
self.assertIn(str(target), text.split("BIOS folder")[-1])
self.assertIn("cannot write there", text)
class OneEmulatorsFilesInOneCommand(unittest.TestCase):
"""An emulator page offers one command for that emulator's files.
--core kept the platform's whole list, and narrowing to cores kept the
platform files of every system no core is mapped to."""
MANIFEST = {
"system_cores": {"ibm-pc": ["pcem"], "nintendo-nes": ["fceumm"]},
"core_names": {"pcem": ["pcem"], "fceumm": ["fceumm"]},
"files": [],
}
def test_only_its_files_and_its_systems(self):
import contextlib
files = [
{"dest": "pcem/ibmpc.bin", "cores": ["pcem"], "systems": ["ibm-pc"]},
{"dest": "disksys.rom", "cores": None, "systems": ["nintendo-nes"]},
{"dest": "ep128emu/exos.rom", "cores": None, "systems": ["enterprise"]},
{"dest": "ibm.rom", "cores": None, "systems": ["ibm-pc"]},
]
with contextlib.redirect_stdout(io.StringIO()):
_chosen, kept, _omitted = install._narrow_to_emulators(
["PCem"], self.MANIFEST, "retroarch", files, [])
self.assertEqual(sorted(e["dest"] for e in kept), ["ibm.rom", "pcem/ibmpc.bin"])
def test_an_unknown_name_is_refused(self):
import contextlib
with contextlib.redirect_stderr(io.StringIO()), self.assertRaises(SystemExit):
install._narrow_to_emulators(["nope"], self.MANIFEST, "retroarch", [], [])
class StandaloneCopiesUnderTheirOwnName(unittest.TestCase):
"""melonDS reads dsisd.bin and xemu eeprom.bin, names the platforms
give dsi_sd_card.bin and xbox_eeprom.bin: a copy says the name the
emulator reads."""
def test_the_copy_takes_the_emulator_name(self):
bios = scratch_dir(self)
(bios / "xbox_eeprom.bin").write_bytes(b"eeprom")
folder = scratch_dir(self)
manifest = {"files": [{"dest": "xbox_eeprom.bin"}], "standalone_copies": [
{"file": "xbox_eeprom.bin", "as": "eeprom.bin", "targets": {"linux": [str(folder)]}}]}
install._validate_manifest(
{**self._published(), "standalone_copies": manifest["standalone_copies"]}, "emudeck")
self.assertEqual(install.do_standalone_copies(manifest, bios, "linux"), (1, 0, 0))
self.assertEqual((folder / "eeprom.bin").read_bytes(), b"eeprom")
self.assertFalse((folder / "xbox_eeprom.bin").exists())
plan = install.standalone_plan(
[{"files": [{"dest": "xbox_eeprom.bin", "sha1": "0" * 40, "size": 6}],
**manifest}], "linux")
self.assertEqual([entry["dest"] for entry in plan[0][1]], ["eeprom.bin"])
def _published(self) -> dict:
return json.loads((REPO_ROOT / "install" / "emudeck.json").read_text())
def test_a_name_that_climbs_is_refused(self):
for name in ("../eeprom.bin", "a/eeprom.bin", "", 3):
with self.subTest(name=name), \
self.assertRaisesRegex(ValueError, "standalone copy name|\\.as"):
install._validate_manifest({**self._published(), "standalone_copies": [
{"file": "x.bin", "as": name, "targets": {}}]}, "emudeck")
def test_cemu_alone_is_found_from_the_published_list(self):
home = scratch_dir(self)
(home / ".local" / "share" / "Cemu").mkdir(parents=True)
manifest = json.loads((REPO_ROOT / "install" / "retroarch.json").read_text())
with unittest.mock.patch.dict(os.environ, {"HOME": str(home)}):
plan = dict(install.standalone_plan([manifest], "linux"))
names = {entry["dest"] for entry in plan[home / ".local" / "share" / "Cemu"]}
self.assertLessEqual({"keys.txt", "otp.bin", "seeprom.bin"}, names)
class EveryStandaloneCopyNamesItsMacFolder(unittest.TestCase):
"""--standalone-copies copied Azahar's keys and Flycast's boot ROM on
Linux and Windows and silently skipped them on macOS, where both
emulators read a fixed Application Support folder."""
def test_each_copy_has_a_darwin_target(self):
import yaml
registry = yaml.safe_load(
(Path(__file__).resolve().parents[1] / "platforms" / "_registry.yml").read_text()
)
missing = [
entry.get("file") or entry.get("pattern")
for entry in registry["_standalone_copies"]
if entry.get("targets") and "darwin" not in entry["targets"]
]
self.assertEqual(missing, [])
class StandaloneFoldersAreNamedWhenThereAreAny(unittest.TestCase, _LoopbackRun):
"""'Standalone copies skipped' closed every install, on a machine with
no standalone emulator as on one whose DuckStation lacked its BIOS."""
def setUp(self):
self.env = self.serve(self)
def _run(self) -> str:
proc = _install_run(self.env, "--platform", "retroarch", "--dest",
str(self.home / "system"), "--system", "atari-lynx",
"--no-input", "--verbose")
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
return proc.stdout
def test_nothing_is_said_without_an_emulator_folder(self):
out = self._run()
self.assertNotIn("skipped", out)
self.assertNotIn("--standalone-copies", out)
def test_a_found_folder_is_named(self):
duckstation = self.home / ".local" / "share" / "duckstation" / "bios"
duckstation.mkdir(parents=True)
out = self._run()
self.assertIn("Standalone emulator folders found here:", out)
self.assertIn(str(duckstation), out)
self.assertIn("--standalone-copies", out)
class StandaloneCopiesAreChecked(unittest.TestCase, _LoopbackRun):
"""--check and --dry-run with --standalone-copies said nothing of the
emulator folders: the flag was taken and never applied."""
def setUp(self) -> None:
self.env = self.serve(self)
self.duckstation = self.home / ".local" / "share" / "duckstation" / "bios"
self.duckstation.mkdir(parents=True)
def _run(self, *argv: str) -> subprocess.CompletedProcess:
return _install_run(self.env, "--platform", "retroarch", "--dest",
str(self.home / "system"), "--system", "sony-playstation",
"--standalone-copies", "--no-input", *argv)
def test_check_names_what_the_folder_lacks(self):
proc = self._run("--check")
self.assertEqual(proc.returncode, 3, proc.stdout + proc.stderr)
self.assertIn("Standalone emulators:", proc.stdout)
self.assertIn(f"{self.duckstation}:", proc.stdout)
self.assertIn("missing: scph1001.bin", proc.stdout)
def test_dry_run_plans_the_copies(self):
proc = self._run("--dry-run")
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
self.assertIn(f"{self.duckstation}:", proc.stdout)
self.assertEqual(list(self.duckstation.iterdir()), [])
class StandaloneEmulatorsWithoutAPlatformAreServed(unittest.TestCase, _LoopbackRun):
"""ES-DE with DuckStation and PCSX2 and no RetroArch: the run said 'No
supported platform detected' and offered RetroArch's folder, which no
emulator on the machine reads."""
def setUp(self):
self.env = self.serve(self)
(self.home / "ES-DE").mkdir()
self.duckstation = self.home / ".local" / "share" / "duckstation" / "bios"
self.duckstation.mkdir(parents=True)
def test_the_emulator_folder_is_filled(self):
proc = _install_run(self.env, "--no-input", "--dry-run")
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
self.assertIn("Found standalone emulator folders:", proc.stdout)
self.assertIn(f"to {self.duckstation}:", proc.stdout)
self.assertNotIn("--platform retroarch --dest", proc.stdout)
proc = _install_run(self.env, "--no-input")
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
names = sorted(path.name for path in self.duckstation.iterdir())
self.assertIn("scph5501.bin", names)
# Every image the DuckStation profile places in its bios folder,
# its PS2 and OpenBIOS images included, and nothing else.
import yaml
profile = yaml.safe_load((REPO_ROOT / "emulators" / "duckstation.yml").read_text())
named = {PurePosixPath(entry["standalone_path"]).name
for entry in profile["files"] if entry.get("standalone_path")}
self.assertEqual([name for name in names
if name not in named and not name.startswith("scph")], [])
proc = _install_run(self.env, "--no-input", "--check")
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
self.assertIn("Every file on the list is in place.", proc.stdout)
def test_the_run_ends_on_its_summary(self):
"""--check --events gave code 3 and only start, detect and end: no
summary, so no pending, installed or failed to read."""
proc = _install_run(self.env, "--no-input", "--check", "--events")
self.assertEqual(proc.returncode, 3, proc.stdout + proc.stderr)
events = [json.loads(line) for line in proc.stdout.splitlines()
if line.startswith("{")]
summary = [event for event in events if event["event"] == "summary"]
self.assertEqual(len(summary), 1, events)
self.assertEqual(summary[0]["mode"], "check")
self.assertGreater(summary[0]["pending"], 0)
self.assertEqual(events[-1], {"event": "end", "code": 3})
proc = _install_run(self.env, "--no-input", "--events")
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
events = [json.loads(line) for line in proc.stdout.splitlines()
if line.startswith("{")]
summary = [event for event in events if event["event"] == "summary"]
self.assertEqual(len(summary), 1, events)
self.assertGreater(summary[0]["installed"], 0)
self.assertEqual(summary[0]["pending"], 0)
def test_no_emulator_folder_keeps_the_hint(self):
self.duckstation.rmdir()
proc = _install_run(self.env, "--no-input")
self.assertEqual(proc.returncode, 1, proc.stdout + proc.stderr)
self.assertIn("--platform retroarch --dest", proc.stdout)
class TheEmulatorNamesHaveTheirOwnListing(unittest.TestCase, _LoopbackRun):
"""--emulator nope said "--list-cores lists all 441" and --list-cores
printed 189: the buildbot names --emulator accepts were listed nowhere."""
def setUp(self):
self.env = self.serve(self)
def test_the_hint_counts_what_the_listing_prints(self):
proc = _install_run(self.env, "--platform", "retroarch", "--no-input",
"--emulator", "nope")
self.assertEqual(proc.returncode, 2, proc.stdout)
found = re.search(r"--list-emulators lists all (\d+)\.", proc.stderr)
self.assertIsNotNone(found, proc.stderr)
listing = _install_run(self.env, "--platform", "retroarch", "--no-input",
"--list-emulators")
self.assertEqual(listing.returncode, 0, listing.stderr)
names = [re.split(r"\s{2,}", line.strip())[0]
for line in listing.stdout.splitlines() if line.startswith(" ")]
manifest = json.loads((REPO_ROOT / "install" / "retroarch.json").read_text())
accepted = install.emulator_names(
manifest, manifest["files"], manifest.get("omitted_files", []))
self.assertEqual(int(found.group(1)), len(accepted))
self.assertTrue(set(accepted) <= set(names), set(accepted) - set(names))
self.assertIn("mednafen_psx", accepted)
self.assertRegex(listing.stdout, r"mednafen_psx\s+same as beetle_psx")
def test_every_listed_name_is_accepted(self):
manifest = {"core_names": {"beetle_psx": ["mednafen_psx"]}}
files = [{"dest": "a", "cores": ["pcsx_rearmed"]}]
names = install.emulator_names(manifest, files, [])
self.assertEqual(names, {"beetle_psx": "beetle_psx", "mednafen_psx": "beetle_psx",
"pcsx_rearmed": "pcsx_rearmed"})
self.assertEqual(install.resolve_choices(["MEDNAFEN_PSX"], sorted(names), "emulator"),
["mednafen_psx"])
if __name__ == "__main__":
unittest.main()
+2 -1
View File
@@ -4,7 +4,8 @@ The screens are driven here with PortMasterDialog's functions replaced by
stubs that record each command and answer from a script, and with a fake
install.py that writes canned --events streams. What a device adds (pugwash
drawing, the pad, mount, the network) is not exercised. The full-screen
route of the port, and the other launchers, are in test_handheld_launchers.
route of the port is in test_handheld_portmaster, the other launchers in
test_handheld_frontends.
"""
from __future__ import annotations
+1 -1
View File
@@ -747,7 +747,7 @@ cores the pack ships.
The `install.detect` rules in `_registry.yml` say how `install.py` finds the
platform on the user's machine and where its BIOS folder is. The installer
runs before it fetches any manifest, so it carries the same rules in its own
tables, and `tests/test_install.py` holds the two equal.
tables, and `tests/test_install_detection.py` holds the two equal.
| Method | Finds the platform when | Fields |
|--------|-------------------------|--------|
+2 -2
View File
@@ -517,7 +517,7 @@ subset:
|------|----------|
| `test_e2e_*.py` | resolution, verification, packs, inheritance, targets, truth and exporters |
| `test_profile_sync.py`, `test_upstream.py` | source anchoring, forge parsing, revisions, cache and guarded writes |
| `test_install.py`, `test_audit_*.py` | automatic wrappers, manifest boundaries, pipeline exits, strong hashes and safe archives |
| `test_install_bootstrap.py`, `test_audit_*.py` | automatic wrappers, manifest boundaries, pipeline exits, strong hashes and safe archives |
| `test_region.py` | region vocabulary, ranking, per-system grouping and repository conformance |
| `test_site_exports.py`, `test_site_validation.py` | versioned exports, SQLite, metadata, accessibility structure and complete local-link graph |
| parser/provenance modules | MAME, FBNeo, DAT import, merge and provenance joins |
@@ -528,7 +528,7 @@ python -m unittest discover -s tests -t . -v # full suite
python -m unittest tests.test_e2e_region -v # single module
```
`test_e2e_*.py`, `test_install.py`, `test_provenance.py`, the parser tests,
`test_e2e_*.py`, `test_install_detection.py`, `test_provenance.py`, the parser tests,
`test_profile_sync.py` and `test_upstream.py` run on synthetic fixtures with no
network and no real BIOS files. `test_pack_integrity.py`, `test_torrentzip.py` and
`test_no_case_collisions.py` read the working tree and skip when the data they
+14 -11
View File
@@ -36,7 +36,7 @@ Run a single module, or several:
```bash
python -m unittest tests.test_e2e_resolution -v
python -m unittest tests.test_install tests.test_install_cores -v
python -m unittest tests.test_install_detection tests.test_install_cores -v
```
The table below says which module covers what.
@@ -69,8 +69,8 @@ Every module in `tests/`, by the area it guards.
| Packs and manifests | synthetic + real packs | `test_artifact_lock`, `test_atomic_artifacts`, `test_batocera_cores`, `test_core_data_trees`, `test_deterministic_zip`, `test_emulator_pack_counts`, `test_finalize_pack_names`, `test_large_file_cache`, `test_libretro_layout`, `test_manifest_entry`, `test_manifest_narrowings`, `test_minui_platform`, `test_no_game_data`, `test_pack_conformance_dirs`, `test_pack_core_extras`, `test_pack_counts`, `test_pack_destinations`, `test_pack_failures`, `test_pack_file_names`, `test_pack_grouping`, `test_pack_integrity`, `test_pack_member_identity`, `test_pack_readme`, `test_platform_inheritance`, `test_platform_keys`, `test_release_packs`, `test_required_only_archives`, `test_required_only_twins`, `test_save_root`, `test_split_extras`, `test_split_names`, `test_split_pack`, `test_system_pack_extras`, `test_system_pack_parity`, `test_torrentzip`, `test_which_pack` |
| Slots and regions | synthetic + profiles | `test_region`, `test_region_agnostic_scan`, `test_region_groups`, `test_region_held`, `test_region_platform_index`, `test_region_required_only`, `test_slot`, `test_slots`, `test_slots_shipped_file` |
| Hardware targets and systems | synthetic + target files | `test_amberelec_targets`, `test_emuelec_targets`, `test_extract_targets`, `test_lakka_targets`, `test_libretro_slugs`, `test_system_ids`, `test_system_selection`, `test_target_names`, `test_target_scrapers`, `test_target_systems`, `test_target_verify` |
| Installer and downloads | loopback server | `test_build_installer`, `test_check_release`, `test_download`, `test_install`, `test_install_cores`, `test_install_experience`, `test_install_expert`, `test_install_filesystems`, `test_install_app`, `test_install_from`, `test_install_games`, `test_install_journeys`, `test_install_offline`, `test_install_pack`, `test_install_removal`, `test_install_selection`, `test_install_tui`, `test_pypi_package` |
| Handheld launchers | a pty as the console, fake installer and firmware tools, stubbed PortMaster dialogs | `test_handheld_launchers`, `test_port`, `test_site_handheld` |
| Installer and downloads | loopback server | `test_build_installer`, `test_check_release`, `test_download`, `test_install_app`, `test_install_bootstrap`, `test_install_config`, `test_install_cores`, `test_install_data_trees`, `test_install_detection`, `test_install_devices`, `test_install_downloads`, `test_install_events`, `test_install_experience`, `test_install_expert`, `test_install_filesystems`, `test_install_from`, `test_install_games`, `test_install_journeys`, `test_install_kept`, `test_install_offline`, `test_install_pack`, `test_install_pack_parity`, `test_install_platforms`, `test_install_removal`, `test_install_runs`, `test_install_selection`, `test_install_standalone`, `test_install_tui`, `test_pypi_package` |
| Handheld launchers | a pty as the console, fake installer and firmware tools, stubbed PortMaster dialogs | `test_handheld_frontends`, `test_handheld_portmaster`, `test_handheld_scripts`, `test_port`, `test_site_handheld` |
| Running cores | parsers on fixed inputs, a libretro core built from C in the test when a compiler exists | `test_core_probe` |
| Measurement records | synthetic record files, the committed `measurements/` against HEAD, the **Proof** line naming the BIOS a core ran, and that no page shows a Tested block, a region outcome or a time per frame | `test_measurements` |
| Real-condition campaign | synthetic profiles, records and content folders, a fake host for load, disk, clock and commands, and whole passes against a libretro core built from C when a compiler and strace exist | `test_campaign` |
@@ -186,10 +186,12 @@ DATs: the Logiqx XML parser, the Redump scraper's parsing path, the DAT pack
importer, the SHA1-then-MD5+size join performed by `generate_db.py`, and the
coverage report's covered/uncovered DAT accounting.
**test_install.** Covers `install.py` without touching the network: OS
detection, each registry detection method (`config_file`, `path_exists`,
`file_exists`), config-file key parsing, manifest loading, target filtering,
and destination resolution.
**test_install_detection** and its siblings. Cover `install.py` without
touching the network: OS detection and each registry detection method
(`config_file`, `path_exists`, `file_exists`) in `test_install_detection`,
config-file key parsing in `test_install_config`, handhelds, boards and target
filtering in `test_install_devices`, manifest loading in
`test_install_platforms`, and destination resolution in `test_install_runs`.
**test_profile_sync.** Covers ref anchoring end to end: the six statuses,
widening a one-line anchor until it is unique, refusing to rebase an ambiguous
@@ -307,7 +309,7 @@ python scripts/generate_pack.py --all --verify-packs --output-dir dist/
The pipeline runs the same check as step 6, after the consistency check and
before the README. It needs packs in `dist/`, and `--skip-packs` skips it.
### test_handheld_launchers.py
### test_handheld_scripts.py, test_handheld_portmaster.py, test_handheld_frontends.py
The launchers in `ports/`: the PortMaster port, the Batocera and Knulli
port, the muOS app and the Tools entry, all built on `ports/shared/tui.txt`.
@@ -315,9 +317,10 @@ port, the muOS app and the Tools entry, all built on `ports/shared/tui.txt`.
installed), the gptokeyb and evmapy mappings decoded by the interface's own
key decoder, and the four archives `scripts/build_port.py` writes (layout,
reproducibility, the bundled `install.py` equal to the one `install.sh`
pins).
pins), in `test_handheld_scripts`.
Each launcher then runs from its extracted archive on a simulated device: a
Each launcher then runs from its extracted archive on a simulated device (`test_handheld_portmaster`
for the PortMaster port, `test_handheld_frontends` for the others): a
pseudo-terminal stands for the console, and fakes stand for `install.py`,
gptokeyb and gptokeyb2 (each killing the way its source does), setfont,
chvt, curl, sudo, foot, xterm, muxterm, PortMaster's `control.txt` and
@@ -331,7 +334,7 @@ read-only card. None of these may leave a console waiting for a button.
What reaches the screen and the pad on a real device is not exercised.
```bash
python -m unittest tests.test_handheld_launchers -v
python -m unittest tests.test_handheld_scripts tests.test_handheld_portmaster tests.test_handheld_frontends -v
```
### test_port.py