import builtins import hashlib import pytest from hasher import hash_file def test_sha256_matches_hashlib(tmp_path): p = tmp_path / "f.bin" # Larger than the 64KB read chunk so the streaming loop iterates. data = b"hello world\n" * 10000 p.write_bytes(data) assert hash_file(p, "sha256") == hashlib.sha256(data).hexdigest() def test_default_algorithm_is_sha256(tmp_path): p = tmp_path / "f.bin" p.write_bytes(b"abc") assert hash_file(p) == hashlib.sha256(b"abc").hexdigest() def test_blake3_produces_expected_digest(tmp_path): blake3 = pytest.importorskip("blake3") p = tmp_path / "f.bin" p.write_bytes(b"abc") assert hash_file(p, "blake3") == blake3.blake3(b"abc").hexdigest() def test_blake3_missing_raises_importerror(tmp_path, monkeypatch): p = tmp_path / "f.bin" p.write_bytes(b"abc") real_import = builtins.__import__ def fake_import(name, *args, **kwargs): if name == "blake3": raise ImportError("simulated missing blake3") return real_import(name, *args, **kwargs) monkeypatch.setattr(builtins, "__import__", fake_import) with pytest.raises(ImportError): hash_file(p, "blake3")