Skip to content

Commit 0b761c2

Browse files
committed
cleaner
1 parent 4ed6e69 commit 0b761c2

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

src/huggingface_hub/utils/_verification.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,12 @@ def compute_file_hash(path: Path, algorithm: HashAlgo) -> str:
8181
Compute the checksum of a local file using the requested algorithm.
8282
"""
8383

84-
if algorithm == "sha256":
85-
with path.open("rb") as stream:
84+
with path.open("rb") as stream:
85+
if algorithm == "sha256":
8686
return sha_fileobj(stream).hex()
87-
88-
if algorithm == "git-sha1":
89-
with path.open("rb") as stream:
87+
if algorithm == "git-sha1":
9088
return git_hash(stream.read())
91-
92-
raise ValueError(f"Unsupported hash algorithm: {algorithm}")
89+
raise ValueError(f"Unsupported hash algorithm: {algorithm}")
9390

9491

9592
def verify_maps(

tests/test_cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ def test_verify_reports_mismatch(self, runner: CliRunner) -> None:
253253
mismatches=[{"path": "pytorch_model.bin", "expected": "dead", "actual": "beef", "algorithm": "sha256"}],
254254
missing_paths=[],
255255
extra_paths=[],
256+
verified_path=Path("/tmp/cache/user/model"),
256257
)
257258

258259
with patch("huggingface_hub.cli.cache.get_hf_api") as get_api_mock:

tests/test_verification.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,11 @@ def test_compute_file_hash_algorithms(tmp_path: Path, algorithm: HashAlgo, data:
4444
fp = tmp_path / "x.bin"
4545
_write(fp, data)
4646

47-
cache: dict[Path, str] = {}
48-
actual = compute_file_hash(fp, algorithm, git_hash_cache=cache)
47+
actual = compute_file_hash(fp, algorithm)
4948
assert actual == expected_fn(data)
5049

5150

52-
def test_compute_file_hash_git_sha1_uses_cache(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
51+
def test_compute_file_hash_git_sha1_computes_hash(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
5352
fp = tmp_path / "x.txt"
5453
data = b"cached!"
5554
_write(fp, data)
@@ -62,12 +61,12 @@ def fake_git_hash(b: bytes) -> str:
6261

6362
monkeypatch.setattr(verification_module, "git_hash", fake_git_hash, raising=False)
6463

65-
cache: dict[Path, str] = {}
66-
h1 = compute_file_hash(fp, "git-sha1", git_hash_cache=cache)
67-
h2 = compute_file_hash(fp, "git-sha1", git_hash_cache=cache)
64+
h1 = compute_file_hash(fp, "git-sha1")
65+
h2 = compute_file_hash(fp, "git-sha1")
6866

6967
assert h1 == h2 == git_hash(data)
70-
assert calls["count"] == 1
68+
# Each call computes the hash independently (no cache)
69+
assert calls["count"] == 2
7170

7271

7372
def test_resolve_local_root_cache_single_snapshot(tmp_path: Path) -> None:
@@ -104,11 +103,17 @@ def test_verify_maps_success_local_dir(tmp_path: Path) -> None:
104103
lfs={"sha256": hashlib.sha256(b"bb").hexdigest()},
105104
),
106105
}
107-
res = verify_maps(remote_by_path=remote_by_path, local_by_path=local_by_path, revision="abc")
106+
res = verify_maps(
107+
remote_by_path=remote_by_path,
108+
local_by_path=local_by_path,
109+
revision="abc",
110+
verified_path=loc,
111+
)
108112
assert res.checked_count == 2
109113
assert res.mismatches == []
110114
assert res.missing_paths == []
111115
assert res.extra_paths == []
116+
assert res.verified_path == loc
112117

113118

114119
def test_verify_maps_reports_mismatch(tmp_path: Path) -> None:
@@ -117,10 +122,16 @@ def test_verify_maps_reports_mismatch(tmp_path: Path) -> None:
117122
_write(loc / "a.txt", b"wrong")
118123
local_by_path = collect_local_files(loc)
119124
remote_by_path = {"a.txt": SimpleNamespace(path="a.txt", blob_id=git_hash(b"right"), lfs=None)}
120-
res = verify_maps(remote_by_path=remote_by_path, local_by_path=local_by_path, revision="r")
125+
res = verify_maps(
126+
remote_by_path=remote_by_path,
127+
local_by_path=local_by_path,
128+
revision="r",
129+
verified_path=loc,
130+
)
121131
assert len(res.mismatches) == 1
122132
m = res.mismatches[0]
123133
assert m["path"] == "a.txt" and m["algorithm"] == "git-sha1"
134+
assert res.verified_path == loc
124135

125136

126137
def test_api_verify_repo_checksums_cache_mode(tmp_path: Path) -> None:

0 commit comments

Comments
 (0)