Files
libretro/tests/test_hashing.py
T
Abdessamad Derraz c784aef883 refactor: move leaf modules into packages
core: hashing, artifacts, ziptools, deterministic_zip, romdir, sect233r1,
crypto_verify, paths (REPO_ROOT). model: safeparse (needs yaml),
dumpcatalog, largefiles, alternatives. resolve: nativemode, validation,
launchers, slot, truth, gamedata, and exporter/baseline.py and
exporter/candidates.py as resolve/reconcile.py and resolve/categories.py,
which truth and gamedata read.

Every importer follows. Modules one level deeper read the repository
root from core.paths instead of __file__.
2026-11-18 10:50:45 +01:00

46 lines
1.4 KiB
Python

"""compute_hashes reads a file once per process and change."""
from __future__ import annotations
import tests
import shutil
import tempfile
import unittest
from pathlib import Path
from unittest import mock
from core import hashing
REPO_ROOT = Path(__file__).resolve().parents[1]
class HashesAreReadOncePerContent(unittest.TestCase):
"""verify --all hashed GB_dmg.bin 13 times and scph5501.bin 12, once
per platform and entry, in one process."""
def setUp(self):
self.dir = Path(tempfile.mkdtemp(dir=REPO_ROOT / "tmp"))
self.addCleanup(shutil.rmtree, self.dir, True)
self.file = self.dir / "rom.bin"
self.file.write_bytes(b"a" * 64)
def test_a_second_ask_reads_nothing(self):
first = hashing.compute_hashes(self.file)
with mock.patch.object(hashing, "_compute_hashes", side_effect=AssertionError):
self.assertEqual(hashing.compute_hashes(self.file), first)
self.assertEqual(
hashing.compute_hashes(self.file, frozenset({"md5"})),
{"md5": first["md5"]},
)
def test_a_rewrite_in_place_is_read_again(self):
first = hashing.compute_hashes(self.file)
with open(self.file, "r+b") as handle:
handle.write(b"b" * 64)
self.assertNotEqual(hashing.compute_hashes(self.file)["sha1"], first["sha1"])
if __name__ == "__main__":
unittest.main()