mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-22 17:47:48 -06:00
refactor: share the loopback server of the tests
Each installer test module rebuilt the same loopback fixture: a silent SimpleHTTPRequestHandler, a ThreadingHTTPServer on a daemon thread with its two cleanups, closed_port(), noise() and the CRC-32 text. tests/loopback.py holds them once; handlers with their own behaviour (Slow, Range, Held) pass in as handler=. The test_install servers that set log_message on a functools.partial, which never reached the handler, now use the quiet one. The journeys comment says why their root stays in tmp/.
This commit is contained in:
1 parent
13db912e52
commit
98775b7e4d
15 files changed
+144
-285
No files matched your search
@@ -0,0 +1,50 @@
|
||||
"""A folder served over loopback HTTP, for the installer tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import functools
|
||||
import hashlib
|
||||
import http.server
|
||||
import socket
|
||||
import threading
|
||||
import unittest
|
||||
import zlib
|
||||
from pathlib import Path
|
||||
from typing import Callable
|
||||
|
||||
|
||||
class Quiet(http.server.SimpleHTTPRequestHandler):
|
||||
"""Serves files without a log line per request."""
|
||||
|
||||
def log_message(self, format, *args):
|
||||
return
|
||||
|
||||
|
||||
def serve(test: unittest.TestCase, directory: Path | None = None,
|
||||
handler: Callable[..., http.server.BaseHTTPRequestHandler] = Quiet) -> str:
|
||||
"""The base URL of a server for the test's length, serving directory
|
||||
(or answering with handler alone when none is given)."""
|
||||
factory = functools.partial(handler, directory=str(directory)) if directory else handler
|
||||
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), factory)
|
||||
threading.Thread(target=server.serve_forever, daemon=True).start()
|
||||
test.addCleanup(server.server_close)
|
||||
test.addCleanup(server.shutdown)
|
||||
return f"http://127.0.0.1:{server.server_address[1]}"
|
||||
|
||||
|
||||
def closed_port() -> int:
|
||||
"""A loopback port nothing listens on."""
|
||||
with socket.socket() as sock:
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
return sock.getsockname()[1]
|
||||
|
||||
|
||||
def noise(seed: str, size: int) -> bytes:
|
||||
"""Bytes deflate cannot shrink."""
|
||||
return b"".join(hashlib.sha256(f"{seed}{n}".encode()).digest()
|
||||
for n in range(size // 32))
|
||||
|
||||
|
||||
def crc(data: bytes) -> str:
|
||||
"""The CRC-32 as the manifests write it."""
|
||||
return f"{zlib.crc32(data) & 0xFFFFFFFF:08x}"
|
||||
@@ -7,6 +7,7 @@ plus recipes/archives.json, without a copy of that archive from elsewhere.
|
||||
from __future__ import annotations
|
||||
|
||||
import tests
|
||||
from tests import loopback
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
@@ -177,8 +178,8 @@ class Capture(Fixture):
|
||||
def test_dat_agreement_and_pins_are_recorded(self):
|
||||
key = romrecipes_capture.member_key(
|
||||
[{"name": n, "crc32": c} for n, c in (
|
||||
("a.rom", self._crc(ROM_A)), ("b.rom", self._crc(ROM_B)),
|
||||
("c.rom", self._crc(ROM_C)),
|
||||
("a.rom", loopback.crc(ROM_A)), ("b.rom", loopback.crc(ROM_B)),
|
||||
("c.rom", loopback.crc(ROM_C)),
|
||||
)]
|
||||
)
|
||||
md5 = hashlib.md5(self.exact_path.read_bytes()).hexdigest()
|
||||
@@ -232,12 +233,6 @@ class Capture(Fixture):
|
||||
pins = romrecipes_capture.container_pins(str(platforms), str(emulators))
|
||||
self.assertEqual(pins, {"a" * 32: {"platform:plain"}, "d" * 40: {"profile:core"}})
|
||||
|
||||
@staticmethod
|
||||
def _crc(data: bytes) -> str:
|
||||
import zlib
|
||||
|
||||
return f"{zlib.crc32(data) & 0xFFFFFFFF:08x}"
|
||||
|
||||
|
||||
class Build(Fixture):
|
||||
def setUp(self):
|
||||
|
||||
+3
-15
@@ -10,6 +10,7 @@ checksum of each layout and the staging location.
|
||||
from __future__ import annotations
|
||||
|
||||
import tests
|
||||
from tests import loopback
|
||||
from tests import scratch_dir
|
||||
|
||||
import contextlib
|
||||
@@ -21,7 +22,6 @@ import io
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
@@ -61,18 +61,6 @@ RELEASE_ASSETS = [
|
||||
]
|
||||
|
||||
|
||||
def _closed_port() -> int:
|
||||
"""A loopback port nothing listens on, so connecting is refused at once."""
|
||||
with socket.socket() as sock:
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
return sock.getsockname()[1]
|
||||
|
||||
|
||||
class _QuietHandler(http.server.SimpleHTTPRequestHandler):
|
||||
def log_message(self, *args): # keep the test output pristine
|
||||
pass
|
||||
|
||||
|
||||
def _release(names, base="https://example.invalid", size=10):
|
||||
return {
|
||||
"assets": [
|
||||
@@ -361,7 +349,7 @@ class ReleaseServer:
|
||||
|
||||
(self.root / "assets" / "SHA256SUMS.txt").write_text(sums)
|
||||
|
||||
handler = functools.partial(_QuietHandler, directory=str(self.root))
|
||||
handler = functools.partial(loopback.Quiet, directory=str(self.root))
|
||||
self.httpd = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
self.base = f"http://127.0.0.1:{self.httpd.server_address[1]}"
|
||||
|
||||
@@ -521,7 +509,7 @@ class TestDownloadPython(DownloaderCase):
|
||||
|
||||
def test_an_unreachable_release_endpoint_fails(self):
|
||||
env = self.env()
|
||||
env["RETROBIOS_API"] = f"http://127.0.0.1:{_closed_port()}"
|
||||
env["RETROBIOS_API"] = f"http://127.0.0.1:{loopback.closed_port()}"
|
||||
proc = subprocess.run(
|
||||
["python3", str(REPO_ROOT / "scripts" / "download.py"), "--list"],
|
||||
env=env,
|
||||
|
||||
+14
-58
@@ -2,6 +2,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import tests
|
||||
from tests import loopback
|
||||
from tests import scratch_dir
|
||||
|
||||
import argparse
|
||||
@@ -1659,21 +1660,14 @@ class InstallerStaysOnPython38AndTheStdlib(unittest.TestCase):
|
||||
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")
|
||||
handler = functools.partial(
|
||||
http.server.SimpleHTTPRequestHandler, directory=str(REPO_ROOT)
|
||||
)
|
||||
handler.log_message = lambda *args: None
|
||||
httpd = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=httpd.serve_forever, daemon=True).start()
|
||||
self.addCleanup(httpd.server_close)
|
||||
self.addCleanup(httpd.shutdown)
|
||||
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": f"http://127.0.0.1:{httpd.server_address[1]}",
|
||||
"RETROBIOS_BASE_URL": base,
|
||||
}
|
||||
dest = home / "system"
|
||||
base = [python38, "-I", "install.py", "--platform", "retroarch", "--no-input"]
|
||||
@@ -2304,7 +2298,7 @@ class TestLaunchboxDetectionPowershell(unittest.TestCase):
|
||||
env = dict(os.environ)
|
||||
env.update(
|
||||
USERPROFILE=str(userprofile),
|
||||
APPDATA=str(str(scratch_dir(self))),
|
||||
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",
|
||||
@@ -3299,14 +3293,7 @@ class AnUnwritableFolderIsCodeTwo(unittest.TestCase):
|
||||
folder existing read-only ended with the documented 2."""
|
||||
|
||||
def test_both_shapes_exit_two(self):
|
||||
handler = functools.partial(
|
||||
http.server.SimpleHTTPRequestHandler, directory=str(REPO_ROOT)
|
||||
)
|
||||
handler.log_message = lambda *args: None
|
||||
httpd = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=httpd.serve_forever, daemon=True).start()
|
||||
self.addCleanup(httpd.server_close)
|
||||
self.addCleanup(httpd.shutdown)
|
||||
base = loopback.serve(self, REPO_ROOT)
|
||||
home = Path(tempfile.mkdtemp())
|
||||
locked = home / "locked"
|
||||
locked.mkdir()
|
||||
@@ -3317,7 +3304,7 @@ class AnUnwritableFolderIsCodeTwo(unittest.TestCase):
|
||||
**os.environ,
|
||||
"HOME": str(home),
|
||||
"RETROBIOS_OS": "linux",
|
||||
"RETROBIOS_BASE_URL": f"http://127.0.0.1:{httpd.server_address[1]}",
|
||||
"RETROBIOS_BASE_URL": base,
|
||||
}
|
||||
for dest in (locked / "sub", locked):
|
||||
with self.subTest(dest=dest.name):
|
||||
@@ -4698,19 +4685,12 @@ class ACheckNamesWhatTheCollectionLacks(unittest.TestCase):
|
||||
)
|
||||
if not lacking:
|
||||
self.skipTest("the Recalbox manifest omits no required caesar3 file")
|
||||
handler = functools.partial(
|
||||
http.server.SimpleHTTPRequestHandler, directory=str(REPO_ROOT)
|
||||
)
|
||||
handler.log_message = lambda *args: None
|
||||
httpd = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=httpd.serve_forever, daemon=True).start()
|
||||
self.addCleanup(httpd.server_close)
|
||||
self.addCleanup(httpd.shutdown)
|
||||
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": f"http://127.0.0.1:{httpd.server_address[1]}",
|
||||
"RETROBIOS_BASE_URL": base,
|
||||
}
|
||||
proc = subprocess.run(
|
||||
[sys.executable, "-I", "install.py", "--platform", "recalbox",
|
||||
@@ -5207,23 +5187,14 @@ class TheInstallerSaysWhatItFound(unittest.TestCase):
|
||||
the file list was fetched ended on a socket traceback."""
|
||||
|
||||
def setUp(self):
|
||||
import functools
|
||||
import http.server
|
||||
import shutil
|
||||
|
||||
handler = functools.partial(
|
||||
http.server.SimpleHTTPRequestHandler, directory=str(REPO_ROOT)
|
||||
)
|
||||
handler.log_message = lambda *args: None
|
||||
httpd = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=httpd.serve_forever, daemon=True).start()
|
||||
self.addCleanup(httpd.server_close)
|
||||
self.addCleanup(httpd.shutdown)
|
||||
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": f"http://127.0.0.1:{httpd.server_address[1]}",
|
||||
"RETROBIOS_BASE_URL": base,
|
||||
}
|
||||
|
||||
def _run(self, *extra: str) -> subprocess.CompletedProcess:
|
||||
@@ -5346,18 +5317,11 @@ class _LoopbackRun:
|
||||
"""install.py against the repository's own manifests over loopback."""
|
||||
|
||||
def serve(self, test: unittest.TestCase) -> dict:
|
||||
handler = functools.partial(
|
||||
http.server.SimpleHTTPRequestHandler, directory=str(REPO_ROOT)
|
||||
)
|
||||
handler.log_message = lambda *args: None
|
||||
httpd = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=httpd.serve_forever, daemon=True).start()
|
||||
test.addCleanup(httpd.server_close)
|
||||
test.addCleanup(httpd.shutdown)
|
||||
base = loopback.serve(test, REPO_ROOT)
|
||||
self.home = scratch_dir(test)
|
||||
return {
|
||||
**os.environ, "HOME": str(self.home), "RETROBIOS_OS": "linux",
|
||||
"RETROBIOS_BASE_URL": f"http://127.0.0.1:{httpd.server_address[1]}",
|
||||
"RETROBIOS_BASE_URL": base,
|
||||
}
|
||||
|
||||
|
||||
@@ -6973,15 +6937,7 @@ class _SyntheticTree:
|
||||
(root / "install").mkdir()
|
||||
(root / "install" / "retroarch.json").write_text(json.dumps(manifest))
|
||||
|
||||
class Quiet(http.server.SimpleHTTPRequestHandler):
|
||||
def log_message(self, *args) -> None:
|
||||
return
|
||||
|
||||
handler = functools.partial(Quiet, directory=str(root))
|
||||
httpd = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=httpd.serve_forever, daemon=True).start()
|
||||
test.addCleanup(httpd.server_close)
|
||||
test.addCleanup(httpd.shutdown)
|
||||
base = loopback.serve(test, root)
|
||||
self.home = scratch_dir(test)
|
||||
self.bios = self.home / "bios"
|
||||
self.bios.mkdir()
|
||||
@@ -6989,7 +6945,7 @@ class _SyntheticTree:
|
||||
(self.bios / "b.bin").write_bytes(b"other bytes")
|
||||
return {
|
||||
**os.environ, "HOME": str(self.home), "RETROBIOS_OS": "linux",
|
||||
"RETROBIOS_BASE_URL": f"http://127.0.0.1:{httpd.server_address[1]}",
|
||||
"RETROBIOS_BASE_URL": base,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ platform and compare each (dest, sha1) with what the installer keeps.
|
||||
from __future__ import annotations
|
||||
|
||||
import tests
|
||||
from tests import loopback
|
||||
|
||||
import contextlib
|
||||
import copy
|
||||
@@ -628,24 +629,9 @@ class ExpertRuns(Lists):
|
||||
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
|
||||
|
||||
def test_a_layout_file_missing_from_the_server(self):
|
||||
import http.server
|
||||
import threading
|
||||
|
||||
served = self.root / "served"
|
||||
(served / "install").mkdir(parents=True)
|
||||
|
||||
class Quiet(http.server.SimpleHTTPRequestHandler):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, directory=str(served), **kwargs)
|
||||
|
||||
def log_message(self, *_args):
|
||||
return
|
||||
|
||||
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Quiet)
|
||||
threading.Thread(target=server.serve_forever, daemon=True).start()
|
||||
self.addCleanup(server.server_close)
|
||||
self.addCleanup(server.shutdown)
|
||||
base = f"http://127.0.0.1:{server.server_address[1]}"
|
||||
base = loopback.serve(self, served)
|
||||
proc = self.run_install(LIST, "--dry-run", "--emulator", "duckstation", "--standalone",
|
||||
layouts={}, env={"RETROBIOS_BASE_URL": base})
|
||||
self.assertEqual(proc.returncode, 1, proc.stdout + proc.stderr)
|
||||
|
||||
@@ -9,12 +9,11 @@ index, published by scripts/generate_games_index.py.
|
||||
from __future__ import annotations
|
||||
|
||||
import tests
|
||||
from tests import loopback
|
||||
|
||||
import contextlib
|
||||
import functools
|
||||
import gzip
|
||||
import hashlib
|
||||
import http.server
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
@@ -22,11 +21,9 @@ import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import unittest
|
||||
import urllib.error
|
||||
import zipfile
|
||||
import zlib
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
@@ -36,11 +33,6 @@ import install
|
||||
REPO_ROOT = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
|
||||
def _crc(data: bytes) -> str:
|
||||
return f"{zlib.crc32(data) & 0xFFFFFFFF:08x}"
|
||||
|
||||
|
||||
MD_EU = b"md europe game " * 64
|
||||
MD_JP = b"md japan game " * 64
|
||||
MD_ZIPPED = b"md zipped usa game " * 64
|
||||
@@ -120,9 +112,9 @@ SYSTEMS = {
|
||||
"sega-megadrive": {
|
||||
"identify": {"sources": [{"source": "no-intro", "dat": "MD"}],
|
||||
"regions": [["europe"], ["japan"], ["north-america"]],
|
||||
"games": sorted([[_crc(MD_EU), len(MD_EU), 0, "Game (Europe)"],
|
||||
[_crc(MD_JP), len(MD_JP), 1, "Game (Japan)"],
|
||||
[_crc(MD_ZIPPED), len(MD_ZIPPED), 2, "Zipped (USA)"]]),
|
||||
"games": sorted([[loopback.crc(MD_EU), len(MD_EU), 0, "Game (Europe)"],
|
||||
[loopback.crc(MD_JP), len(MD_JP), 1, "Game (Japan)"],
|
||||
[loopback.crc(MD_ZIPPED), len(MD_ZIPPED), 2, "Zipped (USA)"]]),
|
||||
"serials": {}},
|
||||
"headers": {"readers": {"md-country": {
|
||||
"location": {"offset": 0x1F0, "length": 3, "encoding": "ascii"},
|
||||
@@ -478,7 +470,7 @@ class RegionMatrix(GamesTestCase):
|
||||
# The catalog matches the whole file: write the header into it first.
|
||||
path = self.card.roms / "MD" / "jp.md"
|
||||
data = path.read_bytes()
|
||||
rows = [[_crc(data), len(data), 1, "Game (Japan)"]]
|
||||
rows = [[loopback.crc(data), len(data), 1, "Game (Japan)"]]
|
||||
systems = json.loads(json.dumps(SYSTEMS))
|
||||
systems["sega-megadrive"]["identify"]["games"] = rows
|
||||
write_index(self.card.lists / "games", systems=systems)
|
||||
@@ -898,12 +890,7 @@ class CommandLine(unittest.TestCase):
|
||||
payload = entry["dest"].encode().ljust(entry["size"], b".")[:entry["size"]]
|
||||
(root / entry["repo_path"]).write_bytes(payload)
|
||||
(root / "install" / "retroarch.json").write_text(json.dumps(listed))
|
||||
handler = functools.partial(_Quiet, directory=str(root))
|
||||
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=server.serve_forever, daemon=True).start()
|
||||
self.addCleanup(server.server_close)
|
||||
self.addCleanup(server.shutdown)
|
||||
base = f"http://127.0.0.1:{server.server_address[1]}"
|
||||
base = loopback.serve(self, root)
|
||||
proc = run_cli(self.card, "--for-games", "--required-only", "--yes", offline=False,
|
||||
base=base)
|
||||
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
|
||||
@@ -963,13 +950,7 @@ class ARunThatAsksNothing(unittest.TestCase):
|
||||
payload = entry["dest"].encode().ljust(entry["size"], b".")[:entry["size"]]
|
||||
(root / entry["repo_path"]).write_bytes(payload)
|
||||
(root / "install" / "retroarch.json").write_text(json.dumps(listed))
|
||||
handler = functools.partial(_Quiet, directory=str(root))
|
||||
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=server.serve_forever, daemon=True).start()
|
||||
self.addCleanup(server.server_close)
|
||||
self.addCleanup(server.shutdown)
|
||||
proc = run_cli(card, *options, offline=False,
|
||||
base=f"http://127.0.0.1:{server.server_address[1]}")
|
||||
proc = run_cli(card, *options, offline=False, base=loopback.serve(self, root))
|
||||
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
|
||||
return sorted(p.relative_to(card.bios).as_posix()
|
||||
for p in card.bios.rglob("*") if p.is_file()), proc.stdout
|
||||
@@ -1018,11 +999,6 @@ class DataTreesFollowTheGamesCores(unittest.TestCase):
|
||||
[fbneo])
|
||||
|
||||
|
||||
class _Quiet(http.server.SimpleHTTPRequestHandler):
|
||||
def log_message(self, format, *args):
|
||||
return
|
||||
|
||||
|
||||
class LineMenu(GamesTestCase):
|
||||
def menu(self, answers: list[str], given: object = None) -> tuple[object, str]:
|
||||
self.card.game("SEGACD", "sonic.iso", mcd_iso(100))
|
||||
|
||||
@@ -13,10 +13,9 @@ one that lengthens it has to say so here too.
|
||||
from __future__ import annotations
|
||||
|
||||
import tests
|
||||
from tests import loopback
|
||||
|
||||
import functools
|
||||
import hashlib
|
||||
import http.server
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
@@ -24,7 +23,6 @@ import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
@@ -120,20 +118,15 @@ _WITH_CARDS = (
|
||||
)
|
||||
|
||||
|
||||
class _Quiet(http.server.SimpleHTTPRequestHandler):
|
||||
def log_message(self, format, *args):
|
||||
return
|
||||
|
||||
|
||||
class Repository:
|
||||
"""The games fixture of test_install_games served on loopback: a
|
||||
RetroArch list, the same list for Batocera, the games index with a
|
||||
muOS setup, and every payload."""
|
||||
|
||||
def __init__(self, test: unittest.TestCase) -> None:
|
||||
# The screens print the card's path and the word budgets count it:
|
||||
# the per-module scratch folder of run_tests.py is long enough to
|
||||
# wrap it onto more lines.
|
||||
# The screens print the card's path and the word budgets count the
|
||||
# lines it wraps onto: the root stays where the budgets were set,
|
||||
# not in the per-module scratch folder of run_tests.py.
|
||||
(REPO_ROOT / "tmp").mkdir(exist_ok=True)
|
||||
self.root = Path(tempfile.mkdtemp(dir=REPO_ROOT / "tmp"))
|
||||
test.addCleanup(shutil.rmtree, self.root, True)
|
||||
@@ -147,16 +140,12 @@ class Repository:
|
||||
retroarch = dict(games.SETUP, roms_root={"path": None, "from_bios": None})
|
||||
games.write_index(self.site / "install" / "games", setup=retroarch,
|
||||
more_setups={"muos": muos})
|
||||
handler = functools.partial(_Quiet, directory=str(self.site))
|
||||
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=server.serve_forever, daemon=True).start()
|
||||
test.addCleanup(server.server_close)
|
||||
test.addCleanup(server.shutdown)
|
||||
base = loopback.serve(test, self.site)
|
||||
self.home = self.root / "home"
|
||||
self.home.mkdir()
|
||||
self.env = {**os.environ, "TERM": "xterm", "HOME": str(self.home),
|
||||
"RETROBIOS_OS": "linux", "XDG_CONFIG_HOME": str(self.home / ".config"),
|
||||
"RETROBIOS_BASE_URL": f"http://127.0.0.1:{server.server_address[1]}",
|
||||
"RETROBIOS_BASE_URL": base,
|
||||
"RETROBIOS_CACHE": str(self.root / "cache")}
|
||||
for name in ("DISPLAY", "WAYLAND_DISPLAY", "SSH_CONNECTION", "SSH_TTY", "NO_COLOR",
|
||||
install.TUI_PAD_ENV, install.OFFLINE_VARIABLE, *install._STEAM_MARKERS):
|
||||
|
||||
@@ -10,6 +10,7 @@ installs again from disk, --from-pack, every file checked by its hash.
|
||||
from __future__ import annotations
|
||||
|
||||
import tests
|
||||
from tests import loopback
|
||||
|
||||
import contextlib
|
||||
import hashlib
|
||||
@@ -17,7 +18,6 @@ import io
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
@@ -77,17 +77,11 @@ PAYLOAD = {"scph5501.bin": US, "scph5500.bin": JP, "gba_bios.bin": GBA,
|
||||
"pcsx/extra.bin": EXTRA}
|
||||
|
||||
|
||||
def _closed_port() -> int:
|
||||
with socket.socket() as sock:
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
return sock.getsockname()[1]
|
||||
|
||||
|
||||
def _offline_env() -> dict:
|
||||
"""An environment where any request the installer makes is refused."""
|
||||
env = dict(os.environ)
|
||||
env["RETROBIOS_BASE_URL"] = f"http://127.0.0.1:{_closed_port()}"
|
||||
env["HTTPS_PROXY"] = env["HTTP_PROXY"] = f"http://127.0.0.1:{_closed_port()}"
|
||||
env["RETROBIOS_BASE_URL"] = f"http://127.0.0.1:{loopback.closed_port()}"
|
||||
env["HTTPS_PROXY"] = env["HTTP_PROXY"] = f"http://127.0.0.1:{loopback.closed_port()}"
|
||||
return env
|
||||
|
||||
|
||||
@@ -326,13 +320,7 @@ def _pack(directory: Path, name: str, members: dict[str, bytes]) -> Path:
|
||||
return path
|
||||
|
||||
|
||||
def _noise(seed: str, size: int) -> bytes:
|
||||
"""Bytes deflate cannot shrink."""
|
||||
return b"".join(hashlib.sha256(f"{seed}{n}".encode()).digest()
|
||||
for n in range(size // 32))
|
||||
|
||||
|
||||
BIG = {f"dir{n}/file{n}.bin": _noise(str(n), 2048) for n in range(4)}
|
||||
BIG = {f"dir{n}/file{n}.bin": loopback.noise(str(n), 2048) for n in range(4)}
|
||||
|
||||
|
||||
class SplitParts(Scratch):
|
||||
@@ -720,11 +708,9 @@ class OfflineMakesNoRequest(Scratch):
|
||||
learn its platform, since nothing said "make no request"."""
|
||||
|
||||
def _served(self, *options: str, env: dict | None = None):
|
||||
from tests import test_install_selection
|
||||
|
||||
hits: list[str] = []
|
||||
with FakeRepository() as repo, unittest.mock.patch.object(
|
||||
test_install_selection._QuietHandler, "log_message",
|
||||
loopback.Quiet, "log_message",
|
||||
lambda handler, *args: hits.append(handler.path)):
|
||||
started = time.monotonic()
|
||||
proc = subprocess.run(
|
||||
@@ -814,7 +800,6 @@ class ANetworkThatDroppedFailsFast(unittest.TestCase):
|
||||
|
||||
def test_a_slow_answer_on_an_open_connection_still_arrives(self):
|
||||
import http.server
|
||||
import threading
|
||||
|
||||
delay = install.CONNECT_TIMEOUT + 1
|
||||
|
||||
@@ -830,13 +815,10 @@ class ANetworkThatDroppedFailsFast(unittest.TestCase):
|
||||
def log_message(self, *args):
|
||||
return
|
||||
|
||||
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Slow)
|
||||
threading.Thread(target=server.serve_forever, daemon=True).start()
|
||||
self.addCleanup(server.server_close)
|
||||
self.addCleanup(server.shutdown)
|
||||
base = loopback.serve(self, handler=Slow)
|
||||
install.install_bounded_opener()
|
||||
self.addCleanup(install.urllib.request.install_opener, None)
|
||||
url = f"http://127.0.0.1:{server.server_address[1]}/list.json"
|
||||
url = f"{base}/list.json"
|
||||
started = time.monotonic()
|
||||
self.assertEqual(install._fetch_json(url, 1024, "file list", attempts=1), {"ok": True})
|
||||
self.assertGreaterEqual(time.monotonic() - started, delay - 0.5)
|
||||
|
||||
@@ -9,21 +9,18 @@ and a failure leaves no file behind.
|
||||
from __future__ import annotations
|
||||
|
||||
import tests
|
||||
from tests import loopback
|
||||
|
||||
import argparse
|
||||
import functools
|
||||
import hashlib
|
||||
import http.server
|
||||
import importlib.util
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import unittest
|
||||
import zipfile
|
||||
import zlib
|
||||
@@ -41,12 +38,6 @@ def _sha1(data: bytes) -> str:
|
||||
return hashlib.sha1(data).hexdigest()
|
||||
|
||||
|
||||
def _noise(seed: str, size: int) -> bytes:
|
||||
"""Bytes deflate cannot shrink."""
|
||||
return b"".join(hashlib.sha256(f"{seed}{n}".encode()).digest()
|
||||
for n in range(size // 32))
|
||||
|
||||
|
||||
# dest: (bytes, systems, regions, cores). Two dests share one file: the
|
||||
# pack stores it once.
|
||||
PAYLOADS = {
|
||||
@@ -58,21 +49,10 @@ PAYLOADS = {
|
||||
}
|
||||
TREE = {"Sys/GC/font.bin": b"font bytes", "Sys/Wii/setting.txt": b"setting"}
|
||||
# Files too large for one part of MIN_PART_BYTES together.
|
||||
BIG = {f"arcade/set{n}.zip": (_noise(str(n), 24 * 1024), ["arcade"], None, None)
|
||||
BIG = {f"arcade/set{n}.zip": (loopback.noise(str(n), 24 * 1024), ["arcade"], None, None)
|
||||
for n in range(5)}
|
||||
|
||||
|
||||
class _Quiet(http.server.SimpleHTTPRequestHandler):
|
||||
def log_message(self, format, *args):
|
||||
return
|
||||
|
||||
|
||||
def _closed_port() -> int:
|
||||
with socket.socket() as sock:
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
return sock.getsockname()[1]
|
||||
|
||||
|
||||
class Repository(unittest.TestCase):
|
||||
"""A loopback copy of the repository: a RetroArch list, its files and a
|
||||
data tree's archive."""
|
||||
@@ -86,12 +66,7 @@ class Repository(unittest.TestCase):
|
||||
self.served = self.root / "served"
|
||||
(self.served / "install").mkdir(parents=True)
|
||||
(self.served / "bios").mkdir()
|
||||
handler = functools.partial(_Quiet, directory=str(self.served))
|
||||
self.httpd = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=self.httpd.serve_forever, daemon=True).start()
|
||||
self.addCleanup(self.httpd.server_close)
|
||||
self.addCleanup(self.httpd.shutdown)
|
||||
self.base = f"http://127.0.0.1:{self.httpd.server_address[1]}"
|
||||
self.base = loopback.serve(self, self.served)
|
||||
self.out = self.root / "out"
|
||||
self.out.mkdir()
|
||||
self.write_list()
|
||||
@@ -125,8 +100,8 @@ class Repository(unittest.TestCase):
|
||||
env.pop(install.OFFLINE_VARIABLE, None)
|
||||
env["RETROBIOS_BASE_URL"] = self.base
|
||||
if offline:
|
||||
env["RETROBIOS_BASE_URL"] = f"http://127.0.0.1:{_closed_port()}"
|
||||
env["HTTPS_PROXY"] = env["HTTP_PROXY"] = f"http://127.0.0.1:{_closed_port()}"
|
||||
env["RETROBIOS_BASE_URL"] = f"http://127.0.0.1:{loopback.closed_port()}"
|
||||
env["HTTPS_PROXY"] = env["HTTP_PROXY"] = f"http://127.0.0.1:{loopback.closed_port()}"
|
||||
env[install.OFFLINE_VARIABLE] = "1"
|
||||
return env
|
||||
|
||||
@@ -292,7 +267,7 @@ class Parts(Repository):
|
||||
self.assertTrue(all(".part" in path.name for path in self.packs()))
|
||||
|
||||
def test_a_file_larger_than_a_part_is_refused_before_any_download(self):
|
||||
self.payloads = {"big.bin": (_noise("x", 80 * 1024), ["arcade"], None, None)}
|
||||
self.payloads = {"big.bin": (loopback.noise("x", 80 * 1024), ["arcade"], None, None)}
|
||||
self.write_list()
|
||||
proc = self.make("--split", "64K")
|
||||
self.assertEqual(proc.returncode, 2, proc.stdout + proc.stderr)
|
||||
@@ -444,7 +419,7 @@ def _members(folder: Path, sizes: dict[str, int]) -> list:
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
members = []
|
||||
for name, size in sorted(sizes.items()):
|
||||
data = _noise(name, size)
|
||||
data = loopback.noise(name, size)
|
||||
source = folder / name.replace("/", "_")
|
||||
source.write_bytes(data)
|
||||
members.append(install.PackMember(name, _sha1(data), len(data), source))
|
||||
|
||||
@@ -10,6 +10,7 @@ to one answer.
|
||||
from __future__ import annotations
|
||||
|
||||
import tests
|
||||
from tests import loopback
|
||||
from tests.sources import script_source
|
||||
|
||||
import contextlib
|
||||
@@ -520,11 +521,6 @@ class TheBoundaryChecksTheNewFields(unittest.TestCase):
|
||||
install._validate_manifest(manifest, "retroarch")
|
||||
|
||||
|
||||
class _QuietHandler(http.server.SimpleHTTPRequestHandler):
|
||||
def log_message(self, format, *args):
|
||||
return
|
||||
|
||||
|
||||
class FakeRepository:
|
||||
"""A loopback copy of the repository: one manifest and its payloads."""
|
||||
|
||||
@@ -562,7 +558,7 @@ class FakeRepository:
|
||||
files.append(entry)
|
||||
manifest = {"manifest_version": 2, "platform": "retroarch", "files": files}
|
||||
(self.root / "install" / "retroarch.json").write_text(json.dumps(manifest))
|
||||
handler = functools.partial(_QuietHandler, directory=str(self.root))
|
||||
handler = functools.partial(loopback.Quiet, directory=str(self.root))
|
||||
self.httpd = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=self.httpd.serve_forever, daemon=True).start()
|
||||
self.dest = self.root / "dest"
|
||||
|
||||
@@ -10,6 +10,7 @@ installs it).
|
||||
from __future__ import annotations
|
||||
|
||||
import tests
|
||||
from tests import loopback
|
||||
from tests.sources import script_paths, script_source
|
||||
|
||||
import argparse
|
||||
@@ -43,7 +44,7 @@ from unittest import mock
|
||||
|
||||
import install
|
||||
from tests.test_install_app import make_card
|
||||
from tests.test_install_selection import FakeRepository, _QuietHandler
|
||||
from tests.test_install_selection import FakeRepository
|
||||
from tests.netguard import module_guard
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parent.parent
|
||||
@@ -5023,7 +5024,7 @@ class Pty:
|
||||
return self.raw.split(b"\x1b[?1049l")[-1].decode("utf-8", "replace")
|
||||
|
||||
|
||||
class _SlowHandler(_QuietHandler):
|
||||
class _SlowHandler(loopback.Quiet):
|
||||
"""Serves each file after a pause, so a test can stop a download midway."""
|
||||
|
||||
def do_GET(self) -> None:
|
||||
@@ -15302,13 +15303,9 @@ class GamesEndToEnd(unittest.TestCase):
|
||||
payload = entry["dest"].encode().ljust(entry["size"], b".")[:entry["size"]]
|
||||
(root / entry["repo_path"]).write_bytes(payload)
|
||||
(root / "install" / "retroarch.json").write_text(json.dumps(listed))
|
||||
handler = functools.partial(_QuietHandler, directory=str(root))
|
||||
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=server.serve_forever, daemon=True).start()
|
||||
self.addCleanup(server.server_close)
|
||||
self.addCleanup(server.shutdown)
|
||||
base = loopback.serve(self, root)
|
||||
env = {**os.environ, "TERM": "xterm", "HOME": str(card.root), "RETROBIOS_OS": "linux",
|
||||
"RETROBIOS_BASE_URL": f"http://127.0.0.1:{server.server_address[1]}",
|
||||
"RETROBIOS_BASE_URL": base,
|
||||
"RETROBIOS_CACHE": str(card.cache)}
|
||||
for name in ("DISPLAY", "WAYLAND_DISPLAY", "SSH_CONNECTION", "SSH_TTY", "NO_COLOR",
|
||||
install.TUI_PAD_ENV, install.OFFLINE_VARIABLE, *install._STEAM_MARKERS):
|
||||
|
||||
@@ -12,6 +12,7 @@ profiles, placed below that folder.
|
||||
from __future__ import annotations
|
||||
|
||||
import tests
|
||||
from tests import loopback
|
||||
from tests.sources import script_source
|
||||
|
||||
import contextlib
|
||||
@@ -23,7 +24,6 @@ import shutil
|
||||
import tempfile
|
||||
import unittest
|
||||
import zipfile
|
||||
import zlib
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
@@ -160,10 +160,6 @@ class ScraperReadsThePaks(unittest.TestCase):
|
||||
)
|
||||
|
||||
|
||||
def _crc(data: bytes) -> str:
|
||||
return format(zlib.crc32(data) & 0xFFFFFFFF, "08x")
|
||||
|
||||
|
||||
class PackPlacesCoreFilesUnderTheTag(unittest.TestCase):
|
||||
"""The pack, the report and the manifest put a core's files in each
|
||||
folder the platform hands it, and nowhere else."""
|
||||
@@ -219,7 +215,7 @@ class PackPlacesCoreFilesUnderTheTag(unittest.TestCase):
|
||||
"cores": ["arcade_lib"], "archive_prefix": "arcadecore",
|
||||
"files": [
|
||||
{"name": "rom_a.bin", "archive": "arc.zip", "required": True,
|
||||
"crc32": _crc(self.member)},
|
||||
"crc32": loopback.crc(self.member)},
|
||||
],
|
||||
},
|
||||
"unlisted": {
|
||||
@@ -242,7 +238,7 @@ class PackPlacesCoreFilesUnderTheTag(unittest.TestCase):
|
||||
"name": os.path.basename(relative),
|
||||
"sha1": hashlib.sha1(data).hexdigest(),
|
||||
"md5": hashlib.md5(data).hexdigest(),
|
||||
"crc32": _crc(data),
|
||||
"crc32": loopback.crc(data),
|
||||
"size": len(data),
|
||||
}
|
||||
|
||||
|
||||
@@ -10,19 +10,17 @@ full manifest.
|
||||
from __future__ import annotations
|
||||
|
||||
import tests
|
||||
from tests import loopback
|
||||
from tests import scratch_dir
|
||||
|
||||
import contextlib
|
||||
import functools
|
||||
import hashlib
|
||||
import http.server
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import unittest
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
@@ -227,11 +225,6 @@ class PackNarrowing(_Fixture):
|
||||
self.assertEqual(held, FIRMWARE)
|
||||
|
||||
|
||||
class _Quiet(http.server.SimpleHTTPRequestHandler):
|
||||
def log_message(self, *args) -> None:
|
||||
return
|
||||
|
||||
|
||||
class InstallerLeavesGameDataOut(unittest.TestCase):
|
||||
"""The installer cut, in each mode, from a manifest served over loopback."""
|
||||
|
||||
@@ -263,16 +256,12 @@ class InstallerLeavesGameDataOut(unittest.TestCase):
|
||||
manifest["categorized"] = True
|
||||
(root / "install").mkdir()
|
||||
(root / "install" / "retroarch.json").write_text(json.dumps(manifest))
|
||||
handler = functools.partial(_Quiet, directory=str(root))
|
||||
httpd = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=httpd.serve_forever, daemon=True).start()
|
||||
self.addCleanup(httpd.server_close)
|
||||
self.addCleanup(httpd.shutdown)
|
||||
base = loopback.serve(self, root)
|
||||
home = scratch_dir(self)
|
||||
self.bios = home / "bios"
|
||||
self.bios.mkdir()
|
||||
self.env = {**os.environ, "HOME": str(home), "RETROBIOS_OS": "linux",
|
||||
"RETROBIOS_BASE_URL": f"http://127.0.0.1:{httpd.server_address[1]}"}
|
||||
"RETROBIOS_BASE_URL": base}
|
||||
|
||||
def run_installer(self, *argv: str) -> subprocess.CompletedProcess:
|
||||
return subprocess.run(
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import tests
|
||||
from tests import loopback
|
||||
|
||||
import contextlib
|
||||
import hashlib
|
||||
@@ -42,12 +43,6 @@ ROM_A = b"first rom payload"
|
||||
ROM_B = b"second rom payload"
|
||||
|
||||
|
||||
def _crc(data: bytes) -> str:
|
||||
import zlib
|
||||
|
||||
return f"{zlib.crc32(data) & 0xFFFFFFFF:08x}"
|
||||
|
||||
|
||||
class RecipeCollection(unittest.TestCase):
|
||||
def test_only_recipes_with_every_crc_are_usable(self):
|
||||
profiles = {
|
||||
@@ -95,14 +90,14 @@ class PoolAndBuild(unittest.TestCase):
|
||||
self.temp.cleanup()
|
||||
|
||||
def test_atoms_are_addressed_by_crc32(self):
|
||||
self.assertIn(_crc(ROM_A), self.pool)
|
||||
self.assertEqual(self.pool.read(_crc(ROM_A)), ROM_A)
|
||||
self.assertIn(loopback.crc(ROM_A), self.pool)
|
||||
self.assertEqual(self.pool.read(loopback.crc(ROM_A)), ROM_A)
|
||||
|
||||
def test_a_recipe_missing_an_atom_builds_nothing(self):
|
||||
self.assertIsNone(self.pool.build([("x.rom", "deadbeef")]))
|
||||
|
||||
def test_build_is_torrentzip_and_reproducible(self):
|
||||
recipe = [("a.rom", _crc(ROM_A)), ("b.rom", _crc(ROM_B))]
|
||||
recipe = [("a.rom", loopback.crc(ROM_A)), ("b.rom", loopback.crc(ROM_B))]
|
||||
first = self.pool.build(recipe)
|
||||
self.assertEqual(first, build_torrentzip([("a.rom", ROM_A), ("b.rom", ROM_B)]))
|
||||
target = self.bios / "built.zip"
|
||||
@@ -110,12 +105,12 @@ class PoolAndBuild(unittest.TestCase):
|
||||
self.assertTrue(is_torrentzip(target))
|
||||
|
||||
def test_identify_names_the_recipe_that_rebuilds_the_archive(self):
|
||||
recipe = [("a.rom", _crc(ROM_A)), ("b.rom", _crc(ROM_B))]
|
||||
recipe = [("a.rom", loopback.crc(ROM_A)), ("b.rom", loopback.crc(ROM_B))]
|
||||
built = self.bios / "built.zip"
|
||||
built.write_bytes(self.pool.build(recipe))
|
||||
label = identify_archive(
|
||||
built,
|
||||
{"v2": recipe, "v1": [("a.rom", _crc(ROM_A))]},
|
||||
{"v2": recipe, "v1": [("a.rom", loopback.crc(ROM_A))]},
|
||||
self.pool,
|
||||
)
|
||||
self.assertEqual(label, "v2")
|
||||
@@ -124,11 +119,11 @@ class PoolAndBuild(unittest.TestCase):
|
||||
other = self.bios / "other.zip"
|
||||
other.write_bytes(build_torrentzip([("z.rom", b"unrelated")]))
|
||||
self.assertIsNone(
|
||||
identify_archive(other, {"v1": [("a.rom", _crc(ROM_A))]}, self.pool)
|
||||
identify_archive(other, {"v1": [("a.rom", loopback.crc(ROM_A))]}, self.pool)
|
||||
)
|
||||
|
||||
def test_reconstruct_finds_the_recipe_behind_a_pinned_md5(self):
|
||||
recipe = [("a.rom", _crc(ROM_A))]
|
||||
recipe = [("a.rom", loopback.crc(ROM_A))]
|
||||
target = hashlib.md5(self.pool.build(recipe)).hexdigest()
|
||||
found = reconstruct(target, {"v1": recipe}, self.pool)
|
||||
self.assertIsNotNone(found)
|
||||
@@ -136,7 +131,7 @@ class PoolAndBuild(unittest.TestCase):
|
||||
self.assertEqual(hashlib.md5(found[1]).hexdigest(), target)
|
||||
|
||||
def test_reconstruct_selects_the_recipe_with_every_member(self):
|
||||
crc_a, crc_b = _crc(ROM_A), _crc(ROM_B)
|
||||
crc_a, crc_b = loopback.crc(ROM_A), loopback.crc(ROM_B)
|
||||
wanted = hashlib.md5(
|
||||
build_torrentzip([("a.rom", ROM_A), ("b.rom", ROM_B)])
|
||||
).hexdigest()
|
||||
@@ -180,7 +175,7 @@ class ReportsOverAFixture(unittest.TestCase):
|
||||
archive.writestr("a.rom", ROM_A)
|
||||
archive.writestr("b.rom", ROM_B)
|
||||
self.pool = AtomPool(self.bios)
|
||||
self.recipe = [("a.rom", _crc(ROM_A)), ("b.rom", _crc(ROM_B))]
|
||||
self.recipe = [("a.rom", loopback.crc(ROM_A)), ("b.rom", loopback.crc(ROM_B))]
|
||||
self.set_path = self.bios / "set.zip"
|
||||
self.set_path.write_bytes(self.pool.build(self.recipe))
|
||||
# The pool must see the archive it will be asked to identify.
|
||||
@@ -314,7 +309,7 @@ class DatRecipes(unittest.TestCase):
|
||||
"""A DAT states, per set, the members one emulator version expects."""
|
||||
|
||||
def _dat(self) -> str:
|
||||
return DAT.format(crc_a=_crc(ROM_A), crc_b=_crc(ROM_B))
|
||||
return DAT.format(crc_a=loopback.crc(ROM_A), crc_b=loopback.crc(ROM_B))
|
||||
|
||||
def test_a_set_becomes_one_recipe_named_after_its_archive(self):
|
||||
entries = recipe_entries(parse_logiqx(self._dat()))
|
||||
@@ -405,7 +400,7 @@ class ListXmlRecipes(unittest.TestCase):
|
||||
return lambda: io.BytesIO(text.encode("utf-8"))
|
||||
|
||||
def _document(self) -> str:
|
||||
return LISTXML.format(crc_a=_crc(ROM_A), crc_b=_crc(ROM_B))
|
||||
return LISTXML.format(crc_a=loopback.crc(ROM_A), crc_b=loopback.crc(ROM_B))
|
||||
|
||||
def test_a_child_inherits_its_parent_members(self):
|
||||
"""A non-merged archive carries the parent set's ROMs as well."""
|
||||
@@ -463,8 +458,8 @@ class ListXmlRecipes(unittest.TestCase):
|
||||
from scripts.scraper.romset_dat_importer import member_list
|
||||
|
||||
child = [
|
||||
{"name": "own.rom", "crc32": _crc(ROM_B)},
|
||||
{"name": "shared.rom", "crc32": _crc(ROM_A)},
|
||||
{"name": "own.rom", "crc32": loopback.crc(ROM_B)},
|
||||
{"name": "shared.rom", "crc32": loopback.crc(ROM_A)},
|
||||
]
|
||||
entries = listxml_entries(
|
||||
self._opener(self._document()), "MAME 0.289", {"other.zip"},
|
||||
@@ -503,11 +498,11 @@ class ListXmlRecipes(unittest.TestCase):
|
||||
from scripts.scraper.romset_dat_importer import member_list
|
||||
|
||||
members = [
|
||||
{"name": "01", "crc32": _crc(ROM_A)},
|
||||
{"name": "23", "crc32": _crc(ROM_B)},
|
||||
{"name": "01", "crc32": loopback.crc(ROM_A)},
|
||||
{"name": "23", "crc32": loopback.crc(ROM_B)},
|
||||
]
|
||||
entries = recipe_entries(
|
||||
parse_logiqx(DAT.format(crc_a=_crc(ROM_A), crc_b=_crc(ROM_B))),
|
||||
parse_logiqx(DAT.format(crc_a=loopback.crc(ROM_A), crc_b=loopback.crc(ROM_B))),
|
||||
keep=set(), by_members={member_list(members): {"salora.zip"}},
|
||||
)
|
||||
self.assertEqual(
|
||||
@@ -535,7 +530,7 @@ class ListXmlRecipes(unittest.TestCase):
|
||||
self.assertEqual(names, {"set.zip", "other.zip"})
|
||||
self.assertEqual(
|
||||
by_members,
|
||||
{member_list([{"name": "a.rom", "crc32": _crc(ROM_A)}]): {"set.zip", "other.zip"}},
|
||||
{member_list([{"name": "a.rom", "crc32": loopback.crc(ROM_A)}]): {"set.zip", "other.zip"}},
|
||||
)
|
||||
|
||||
def test_a_missing_database_stops_both_importers(self):
|
||||
@@ -555,16 +550,16 @@ class ListXmlRecipes(unittest.TestCase):
|
||||
def test_a_set_holding_a_missing_catalog_dump_is_kept(self):
|
||||
"""fm7740sx carries three No-Intro dumps the collection lacks. No
|
||||
profile names it, so the missing report named no place to find them."""
|
||||
targets = {(_crc(ROM_B), len(ROM_B)): {""}}
|
||||
targets = {(loopback.crc(ROM_B), len(ROM_B)): {""}}
|
||||
entries = listxml_entries(
|
||||
self._opener(self._document()), "MAME 0.289", {"other.zip"}, targets
|
||||
)
|
||||
self.assertEqual([entry["name"] for entry in entries], ["childset.zip"])
|
||||
|
||||
def test_a_catalog_sha1_must_agree(self):
|
||||
member = {"crc32": _crc(ROM_B), "size": len(ROM_B), "sha1": "a" * 40}
|
||||
self.assertFalse(holds_target([member], {(_crc(ROM_B), len(ROM_B)): {"b" * 40}}))
|
||||
self.assertTrue(holds_target([member], {(_crc(ROM_B), len(ROM_B)): {"a" * 40}}))
|
||||
member = {"crc32": loopback.crc(ROM_B), "size": len(ROM_B), "sha1": "a" * 40}
|
||||
self.assertFalse(holds_target([member], {(loopback.crc(ROM_B), len(ROM_B)): {"b" * 40}}))
|
||||
self.assertTrue(holds_target([member], {(loopback.crc(ROM_B), len(ROM_B)): {"a" * 40}}))
|
||||
|
||||
def test_catalog_targets_leave_out_what_the_collection_holds(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
@@ -811,7 +806,7 @@ class BuildDigestsOutliveTheRun(unittest.TestCase):
|
||||
with zipfile.ZipFile(bios / "source.zip", "w") as archive:
|
||||
archive.writestr("a.rom", ROM_A)
|
||||
cache = Path(tmp) / "digests.json"
|
||||
recipe = [("a.rom", _crc(ROM_A))]
|
||||
recipe = [("a.rom", loopback.crc(ROM_A))]
|
||||
first = AtomPool(bios, cache)
|
||||
digest = first.digest(recipe)
|
||||
blob = first.build(recipe)
|
||||
@@ -890,7 +885,7 @@ FBNEO_DAT = (
|
||||
b'<?xml version="1.0"?>\n<datafile><header><name>FinalBurn Neo - Demo</name>'
|
||||
b"<version>1.0.0.03</version></header>"
|
||||
b'<game name="demobios"><description>Demo BIOS</description>'
|
||||
b'<rom name="a.bin" size="17" crc="' + _crc(ROM_A).encode() + b'"/></game>'
|
||||
b'<rom name="a.bin" size="17" crc="' + loopback.crc(ROM_A).encode() + b'"/></game>'
|
||||
b"</datafile>\n"
|
||||
)
|
||||
|
||||
@@ -981,12 +976,12 @@ class LooseRomsAreAtoms(unittest.TestCase):
|
||||
bios.mkdir()
|
||||
loose = bios / "nmk004.bin"
|
||||
loose.write_bytes(ROM_A)
|
||||
db = {"files": {"x": {"path": str(loose), "crc32": _crc(ROM_A)}}}
|
||||
self.assertNotIn(_crc(ROM_A), AtomPool(bios))
|
||||
db = {"files": {"x": {"path": str(loose), "crc32": loopback.crc(ROM_A)}}}
|
||||
self.assertNotIn(loopback.crc(ROM_A), AtomPool(bios))
|
||||
pool = AtomPool(bios, None, db)
|
||||
self.assertIn(_crc(ROM_A), pool)
|
||||
self.assertEqual(pool.read(_crc(ROM_A)), ROM_A)
|
||||
self.assertIsNotNone(pool.build([("nmk004.bin", _crc(ROM_A))]))
|
||||
self.assertIn(loopback.crc(ROM_A), pool)
|
||||
self.assertEqual(pool.read(loopback.crc(ROM_A)), ROM_A)
|
||||
self.assertIsNotNone(pool.build([("nmk004.bin", loopback.crc(ROM_A))]))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
+3
-14
@@ -12,10 +12,9 @@ directory the frontend's config names.
|
||||
from __future__ import annotations
|
||||
|
||||
import tests
|
||||
from tests import loopback
|
||||
|
||||
import functools
|
||||
import hashlib
|
||||
import http.server
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
@@ -23,7 +22,6 @@ import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import unittest
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
@@ -698,15 +696,11 @@ class _Repository:
|
||||
"base_destination": "system", "files": entries,
|
||||
"save_directory": {"folder": "saves", "by_core": True}}
|
||||
(self.root / "install" / "retroarch.json").write_text(json.dumps(manifest))
|
||||
handler = functools.partial(_Quiet, directory=str(self.root))
|
||||
self.httpd = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=self.httpd.serve_forever, daemon=True).start()
|
||||
test.addCleanup(self.httpd.server_close)
|
||||
test.addCleanup(self.httpd.shutdown)
|
||||
self.base = loopback.serve(test, self.root)
|
||||
|
||||
def run(self, *options: str) -> subprocess.CompletedProcess:
|
||||
env = dict(os.environ)
|
||||
env["RETROBIOS_BASE_URL"] = f"http://127.0.0.1:{self.httpd.server_address[1]}"
|
||||
env["RETROBIOS_BASE_URL"] = self.base
|
||||
return subprocess.run(
|
||||
[sys.executable, str(REPO_ROOT / "install.py"), "--platform", "retroarch",
|
||||
"--yes", *options],
|
||||
@@ -715,11 +709,6 @@ class _Repository:
|
||||
)
|
||||
|
||||
|
||||
class _Quiet(http.server.SimpleHTTPRequestHandler):
|
||||
def log_message(self, *args) -> None:
|
||||
pass
|
||||
|
||||
|
||||
class TheInstallerWritesThem(unittest.TestCase):
|
||||
FILES = {
|
||||
"sys.bin": (b"system file", {}),
|
||||
|
||||
Reference in new issue
Block a user