Files
libretro/tests/test_hashing.py
T
Abdessamad Derraz 5f18caec31 refactor: put scripts/ on the test path once
tests/__init__ now puts the repository root and scripts/ on sys.path,
so no test module inserts them and every import sits in the import
block. ruff accepts the bare `import tests` the modules keep for its
side effects. The last suppressions go: helper test modules import as
tests.<name>, files open in with blocks, lambdas become functions.
2026-11-17 16:11:23 +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
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()