|
1 | 1 | """Unit tests for dfetch.vcs.archive and dfetch.project.archivesubproject.""" |
2 | 2 |
|
| 3 | +import hashlib |
3 | 4 | import io |
4 | 5 | import os |
5 | 6 | import tarfile |
6 | 7 | import tempfile |
7 | 8 | import zipfile |
| 9 | +from unittest.mock import patch |
8 | 10 |
|
9 | 11 | import pytest |
10 | 12 |
|
11 | | -from dfetch.project.archivesubproject import _suffix_for_url |
| 13 | +from dfetch.manifest.project import ProjectEntry |
| 14 | +from dfetch.manifest.version import Version |
| 15 | +from dfetch.project.archivesubproject import ArchiveSubProject, _suffix_for_url |
12 | 16 | from dfetch.vcs.archive import ( |
13 | 17 | ARCHIVE_EXTENSIONS, |
14 | 18 | ArchiveLocalRepo, |
@@ -249,3 +253,115 @@ def test_all_archive_extensions_covered(): |
249 | 253 | assert len(ARCHIVE_EXTENSIONS) > 0 |
250 | 254 | for ext in ARCHIVE_EXTENSIONS: |
251 | 255 | assert ext.startswith(".") |
| 256 | + |
| 257 | + |
| 258 | +# --------------------------------------------------------------------------- |
| 259 | +# Helpers shared by ArchiveSubProject tests |
| 260 | +# --------------------------------------------------------------------------- |
| 261 | + |
| 262 | + |
| 263 | +def _make_tar_gz(path: str, content: bytes = b"hello") -> None: |
| 264 | + """Write a minimal .tar.gz archive containing one file to *path*.""" |
| 265 | + with tarfile.open(path, "w:gz") as tf: |
| 266 | + info = tarfile.TarInfo(name="pkg/README.md") |
| 267 | + info.size = len(content) |
| 268 | + tf.addfile(info, io.BytesIO(content)) |
| 269 | + |
| 270 | + |
| 271 | +def _sha256_file(path: str) -> str: |
| 272 | + h = hashlib.sha256() |
| 273 | + with open(path, "rb") as f: |
| 274 | + for chunk in iter(lambda: f.read(8192), b""): |
| 275 | + h.update(chunk) |
| 276 | + return h.hexdigest() |
| 277 | + |
| 278 | + |
| 279 | +def _file_url(path: str) -> str: |
| 280 | + return "file:///" + path.lstrip("/") |
| 281 | + |
| 282 | + |
| 283 | +def _make_subproject(url: str) -> ArchiveSubProject: |
| 284 | + return ArchiveSubProject( |
| 285 | + ProjectEntry({"name": "pkg", "url": url, "vcs": "archive"}) |
| 286 | + ) |
| 287 | + |
| 288 | + |
| 289 | +# --------------------------------------------------------------------------- |
| 290 | +# ArchiveSubProject._download_and_compute_hash – explicit url parameter |
| 291 | +# --------------------------------------------------------------------------- |
| 292 | + |
| 293 | + |
| 294 | +def test_download_and_compute_hash_default_uses_remote_repo(): |
| 295 | + """Without an explicit url the hash is computed from self._remote_repo.""" |
| 296 | + with tempfile.TemporaryDirectory() as tmp: |
| 297 | + archive = os.path.join(tmp, "pkg.tar.gz") |
| 298 | + _make_tar_gz(archive) |
| 299 | + url = _file_url(archive) |
| 300 | + sp = _make_subproject(url) |
| 301 | + |
| 302 | + result = sp._download_and_compute_hash("sha256") |
| 303 | + |
| 304 | + assert result.algorithm == "sha256" |
| 305 | + assert result.hex_digest == _sha256_file(archive) |
| 306 | + |
| 307 | + |
| 308 | +def test_download_and_compute_hash_explicit_url_overrides_remote_repo(): |
| 309 | + """When *url* is supplied a fresh ArchiveRemote for that URL is used. |
| 310 | +
|
| 311 | + This is the regression guard for the fix: if the manifest URL was changed |
| 312 | + after fetching, freeze must still hash the *original* archive (the one |
| 313 | + recorded in the on-disk revision), not the current manifest URL. |
| 314 | + """ |
| 315 | + with tempfile.TemporaryDirectory() as tmp: |
| 316 | + archive_a = os.path.join(tmp, "pkg_a.tar.gz") |
| 317 | + archive_b = os.path.join(tmp, "pkg_b.tar.gz") |
| 318 | + _make_tar_gz(archive_a, content=b"version A") |
| 319 | + _make_tar_gz(archive_b, content=b"version B") |
| 320 | + url_a = _file_url(archive_a) |
| 321 | + url_b = _file_url(archive_b) |
| 322 | + |
| 323 | + # SubProject points to archive_b (current manifest URL). |
| 324 | + sp = _make_subproject(url_b) |
| 325 | + |
| 326 | + # Passing url=url_a must use archive_a's content. |
| 327 | + result = sp._download_and_compute_hash("sha256", url=url_a) |
| 328 | + |
| 329 | + assert result.hex_digest == _sha256_file(archive_a) |
| 330 | + assert result.hex_digest != _sha256_file(archive_b) |
| 331 | + |
| 332 | + |
| 333 | +# --------------------------------------------------------------------------- |
| 334 | +# ArchiveSubProject.freeze_project – uses on-disk revision URL |
| 335 | +# --------------------------------------------------------------------------- |
| 336 | + |
| 337 | + |
| 338 | +def test_freeze_project_uses_on_disk_url_not_manifest_url(): |
| 339 | + """freeze_project must hash the archive at the on-disk revision URL. |
| 340 | +
|
| 341 | + Scenario: the manifest URL was updated after the last fetch. Without the |
| 342 | + fix, freeze would download from the new (current) manifest URL and produce |
| 343 | + a hash that doesn't match the fetched archive. With the fix it uses the |
| 344 | + URL stored in the on-disk revision. |
| 345 | + """ |
| 346 | + with tempfile.TemporaryDirectory() as tmp: |
| 347 | + archive_a = os.path.join(tmp, "pkg_a.tar.gz") |
| 348 | + archive_b = os.path.join(tmp, "pkg_b.tar.gz") |
| 349 | + _make_tar_gz(archive_a, content=b"original fetch") |
| 350 | + _make_tar_gz(archive_b, content=b"updated manifest url") |
| 351 | + url_a = _file_url(archive_a) |
| 352 | + url_b = _file_url(archive_b) |
| 353 | + |
| 354 | + # SubProject now points to archive_b (manifest was updated after fetch). |
| 355 | + sp = _make_subproject(url_b) |
| 356 | + |
| 357 | + # Simulate on-disk state: was fetched from url_a (no hash-pin at the time). |
| 358 | + on_disk = Version(revision=url_a) |
| 359 | + with patch.object(sp, "on_disk_version", return_value=on_disk): |
| 360 | + project_entry = ProjectEntry( |
| 361 | + {"name": "pkg", "url": url_b, "vcs": "archive"} |
| 362 | + ) |
| 363 | + sp.freeze_project(project_entry) |
| 364 | + |
| 365 | + expected_hash = f"sha256:{_sha256_file(archive_a)}" |
| 366 | + assert project_entry.hash == expected_hash |
| 367 | + assert _sha256_file(archive_b) not in project_entry.hash |
0 commit comments