From 0a5e15789f0faba500db963ae3587ccfd2ea8025 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Fri, 16 Oct 2026 22:09:57 +0200 Subject: [PATCH] fix: exit non-zero when torrentzip check fails --- scripts/torrentzip.py | 8 ++++---- tests/test_torrentzip.py | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/scripts/torrentzip.py b/scripts/torrentzip.py index a04ee621..914513f5 100644 --- a/scripts/torrentzip.py +++ b/scripts/torrentzip.py @@ -114,8 +114,8 @@ def identify_romset( return None -def main() -> None: - """Entry point.""" +def main() -> int: + """Entry point: 1 when a checked archive is not TorrentZip.""" parser = argparse.ArgumentParser(description="TorrentZip builder and checker") parser.add_argument("--check", metavar="ZIP", help="report TorrentZip conformance") parser.add_argument("--rebuild", metavar="ZIP", help="normalize an archive") @@ -130,14 +130,14 @@ def main() -> None: print(f" md5: {hashlib.md5(data).hexdigest()}") print(f" rebuilt: {hashlib.md5(rebuilt).hexdigest()}") print(f" conform: {data == rebuilt}") - return + return 0 if data == rebuilt else 1 if args.rebuild: data = rebuild_torrentzip(args.rebuild) dest = args.output or args.rebuild Path(dest).write_bytes(data) print(f"{dest}: {len(data)} bytes, md5 {hashlib.md5(data).hexdigest()}") - return + return 0 parser.error("nothing to do: pass --check or --rebuild") diff --git a/tests/test_torrentzip.py b/tests/test_torrentzip.py index c864690a..e8e3e61d 100644 --- a/tests/test_torrentzip.py +++ b/tests/test_torrentzip.py @@ -153,5 +153,29 @@ def _as_file(test: unittest.TestCase, data: bytes) -> str: return path +class CheckModeExitCode(unittest.TestCase): + """--check printed `conform: False` and exited 0, so no script could use it.""" + + def test_the_exit_code_follows_conformance(self): + import subprocess + + with tempfile.TemporaryDirectory(dir=os.path.join(REPO_ROOT, "tmp")) as directory: + plain = os.path.join(directory, "plain.zip") + with zipfile.ZipFile(plain, "w") as zf: + zf.writestr("a.bin", b"rom") + torrent = os.path.join(directory, "torrent.zip") + with open(torrent, "wb") as handle: + handle.write(build_torrentzip([("a.bin", b"rom")])) + script = os.path.join(REPO_ROOT, "scripts", "torrentzip.py") + codes = [ + subprocess.run( + [sys.executable, script, "--check", path], + capture_output=True, check=False, timeout=60, + ).returncode + for path in (plain, torrent) + ] + self.assertEqual(codes, [1, 0]) + + if __name__ == "__main__": unittest.main()