refactor: one refusal path to stderr and events

Library functions printed their refusal and exited, each deciding on its
own whether --events got an error event. --cores-dir, --installed-cores
with no core, a list without system_cores or standalone_builds, a pack
that cannot be used, a board list a run needs and --dest alone beside
two setups ended on code 1 with no reason in the stream.

They now raise Refusal (installer/refusal.py), carrying the stage, the
reason, the platform, the lines and the code. The install, an action
framed by events and main report it once: the error event, then the
same lines on stderr, so the text and exit codes stay as they were.
This commit is contained in:
Abdessamad Derraz committed 2026-11-21 19:50:46 +01:00
1 parent ced68a9135
commit eba627aa89
20 files changed
+542 -402

No files matched your search

+33 -7
View File
@@ -49,6 +49,16 @@ PIN_ADVICE = "install.py changed: python scripts/pin_installer.py rewrites both
setUpModule, tearDownModule = module_guard("test_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
def _parenthesized_withs(tokens: list) -> list[str]:
import tokenize
@@ -2896,7 +2906,7 @@ class FileListsAreRetried(unittest.TestCase):
install.urllib.request, "urlopen",
lambda url, timeout=None: contextlib.nullcontext(page)), \
contextlib.redirect_stderr(err), self.assertRaises(SystemExit):
install.fetch_manifest("retroarch")
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)
@@ -2911,7 +2921,7 @@ class FileListsAreRetried(unittest.TestCase):
side_effect=failures), \
unittest.mock.patch.object(install.time, "sleep"), \
contextlib.redirect_stderr(err), self.assertRaises(SystemExit) as caught:
install.fetch_manifest("retroarch")
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())
@@ -2949,7 +2959,7 @@ class ASilentServerIsBounded(unittest.TestCase):
unittest.mock.patch.dict(os.environ,
{"RETROBIOS_BASE_URL": f"http://127.0.0.1:{port}"}), \
contextlib.redirect_stderr(err), self.assertRaises(SystemExit):
install.fetch_manifest("retroarch")
reported(install.fetch_manifest, "retroarch")
elapsed = time.monotonic() - start
self.assertLess(elapsed, 5)
self.assertEqual(len(held), install.STALLED_ATTEMPTS)
@@ -5235,7 +5245,7 @@ class AMissingFileListNamesTheRef(unittest.TestCase):
unittest.mock.patch.object(install, "_fetch_json", side_effect=error), \
contextlib.redirect_stderr(io.StringIO()) as err, \
self.assertRaises(SystemExit):
install.fetch_manifest("retroarch")
reported(install.fetch_manifest, "retroarch")
return err.getvalue()
def test_a_pinned_tag_is_named(self):
@@ -5760,7 +5770,7 @@ class AnOverrideIsNamedWhenItIsTheCause(unittest.TestCase):
unittest.mock.patch.object(install, "_fetch_json", side_effect=error), \
contextlib.redirect_stderr(io.StringIO()) as err, \
self.assertRaises(SystemExit):
install.fetch_manifest("retroarch")
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)
@@ -6056,6 +6066,22 @@ class RunsAgainstTheRealLists(unittest.TestCase, _LoopbackRun):
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)
class DestAloneFollowsWhatIsFoundHere(unittest.TestCase):
"""--dest without --platform always meant RetroArch's list, even on a
@@ -6067,8 +6093,8 @@ class DestAloneFollowsWhatIsFoundHere(unittest.TestCase):
unittest.mock.patch("builtins.input", lambda _prompt="": answer), \
contextlib.redirect_stdout(out), contextlib.redirect_stderr(out):
try:
plat, folder = install._platform_for_dest("linux", Path("/media/card"),
interactive)
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:
+1 -1
View File
@@ -1390,7 +1390,7 @@ class ADetectedClosedFolderSaysWhereItCameFrom(unittest.TestCase):
def test_an_assumed_default_reads_plainly(self):
self.assertIn("used by default, since {display_name(name)} was not found",
_source_of(install._install))
_source_of(install._install_run))
class OfflineWithNothingFound(unittest.TestCase):
+2 -3
View File
@@ -1155,11 +1155,10 @@ class LocalFilesSayWhyTheyCannotBeUsed(unittest.TestCase):
self.addCleanup(patcher.stop)
def _said(self, call) -> str:
with contextlib.redirect_stderr(io.StringIO()) as err, \
self.assertRaises(SystemExit) as stop:
with self.assertRaises(install.Refusal) as stop:
call()
self.assertEqual(stop.exception.code, 1)
return err.getvalue()
return "\n".join(stop.exception.lines)
def test_an_oversized_standalone_layout(self):
(self.root / "standalone").mkdir()
+17 -7
View File
@@ -57,6 +57,16 @@ BUILDER_FLOOR = (3, 9)
# card in the reader. The module fails at its end when one is tried.
setUpModule, tearDownModule = module_guard("test_install_tui")
def reported(call, *args, **kwargs):
"""call(...), a refusal said as the command says it: a library
function raises it, and the command prints it."""
try:
return call(*args, **kwargs)
except install.Refusal as refusal:
refusal.report()
raise
try:
import pyte
except ImportError:
@@ -2867,7 +2877,7 @@ class CardsSpaceAndConfirm(unittest.TestCase):
stray = root / "NOTMUOS" / "bios"
with self.assertRaises(SystemExit), \
contextlib.redirect_stderr(io.StringIO()) as err:
install.card_dest("muos", "retroarch", stray)
reported(install.card_dest, "muos", "retroarch", stray)
self.assertIn("does not exist", err.getvalue())
card = self.card({"MUOS/bios/": ""})
self.assertEqual(install.card_dest("muos", "retroarch", card / "muos" / "BIOS"),
@@ -6637,7 +6647,7 @@ class EveryCardSetupGoesToItsCard(unittest.TestCase):
with mock.patch.object(install, "_mounted_cards", return_value=[]), \
contextlib.redirect_stderr(io.StringIO()) as err, \
self.assertRaises(SystemExit) as stop:
install.card_install_dest(setup.name, plat, interactive=False)
reported(install.card_install_dest, setup.name, plat, interactive=False)
self.assertEqual(stop.exception.code, 1)
self.assertIn(f"{setup.name} keeps its BIOS files on its SD card",
err.getvalue())
@@ -7311,7 +7321,7 @@ class OneCardReading(unittest.TestCase):
with mock.patch.object(install, "_mounted_cards", return_value=[]), \
contextlib.redirect_stderr(io.StringIO()) as err, \
self.assertRaises(SystemExit):
install.card_install_dest("Anbernic stock firmware", "retroarch", False)
reported(install.card_install_dest, "Anbernic stock firmware", "retroarch", False)
self.assertIn("--platform anbernicstockfirmware --dest", err.getvalue())
@@ -9429,8 +9439,8 @@ class PacksWordingAndDocs(unittest.TestCase):
err = io.StringIO()
install.LOCAL.pack = install.PackSource([old])
with contextlib.redirect_stderr(err), self.assertRaises(SystemExit):
install._requested_narrowing(_args(system=["nintendo-gba"]), "retroarch",
install.LOCAL.pack.manifest("retroarch")["files"], [])
reported(install._requested_narrowing, _args(system=["nintendo-gba"]), "retroarch",
install.LOCAL.pack.manifest("retroarch")["files"], [])
self.assertIn("Add --manifest FILE, a copy of install/retroarch.json", err.getvalue())
self.assertNotIn("older release", err.getvalue())
@@ -9809,7 +9819,7 @@ class CardFoldersAndNarrowBars(unittest.TestCase):
with self.subTest(named=named):
with contextlib.redirect_stderr(io.StringIO()) as err, \
self.assertRaises(SystemExit):
install.card_dest(named, plat, dest)
reported(install.card_dest, named, plat, dest)
self.assertIn(words, err.getvalue())
self.assertNotIn(never, err.getvalue())
minui = self.root / "MINUI"
@@ -9830,7 +9840,7 @@ class CardFoldersAndNarrowBars(unittest.TestCase):
mock.patch.object(install, "_mount_points", return_value=frozenset()):
with contextlib.redirect_stderr(io.StringIO()) as err, \
self.assertRaises(SystemExit):
install.card_dest("onion", "retroarch", mnt / "f")
reported(install.card_dest, "onion", "retroarch", mnt / "f")
self.assertIn("F:\\ is a muOS card", err.getvalue())
note = install.card_choice("knulli", "knulli", mnt / "g").note
self.assertIn("G:\\bios", note)