|
| 1 | +"""Tests for copy_template hardlink optimization.""" |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from tests.support.state import copy_template |
| 6 | + |
| 7 | + |
| 8 | +def test_copy_template_hardlinks_blobs(tmp_path: Path) -> None: |
| 9 | + """Blobs should be hardlinked (not copied) for I/O efficiency.""" |
| 10 | + template = tmp_path / "template" |
| 11 | + template.mkdir() |
| 12 | + blob = template / "blobs" / "sha256" / "00" / "abc123" |
| 13 | + blob.parent.mkdir(parents=True) |
| 14 | + blob.write_bytes(b"blob content") |
| 15 | + (template / "metadata.sqlite3").write_bytes(b"database") |
| 16 | + |
| 17 | + dest = tmp_path / "destination" |
| 18 | + copy_template(template, dest) |
| 19 | + |
| 20 | + assert (dest / "blobs" / "sha256" / "00" / "abc123").read_bytes() == b"blob content" |
| 21 | + template_inode = os.stat(blob).st_ino |
| 22 | + dest_inode = os.stat(dest / "blobs" / "sha256" / "00" / "abc123").st_ino |
| 23 | + assert template_inode == dest_inode, "blob should be hardlinked" |
| 24 | + |
| 25 | + template_meta = os.stat(template / "metadata.sqlite3").st_ino |
| 26 | + dest_meta = os.stat(dest / "metadata.sqlite3").st_ino |
| 27 | + assert template_meta != dest_meta, "metadata should be copied, not hardlinked" |
| 28 | + |
| 29 | + |
| 30 | +def test_copy_template_preserves_all_files(tmp_path: Path) -> None: |
| 31 | + """All non-blob files should be present in the destination.""" |
| 32 | + template = tmp_path / "template" |
| 33 | + template.mkdir() |
| 34 | + (template / "blobs").mkdir() |
| 35 | + blob = template / "blobs" / "sha256" / "00" / "def456" |
| 36 | + blob.parent.mkdir(parents=True) |
| 37 | + blob.write_bytes(b"blob") |
| 38 | + (template / "metadata.sqlite3").write_bytes(b"database") |
| 39 | + (template / "metadata.sqlite3-shm").write_bytes(b"shm") |
| 40 | + (template / "metadata.sqlite3-wal").write_bytes(b"wal") |
| 41 | + |
| 42 | + dest = tmp_path / "destination" |
| 43 | + copy_template(template, dest) |
| 44 | + |
| 45 | + for name in ("metadata.sqlite3", "metadata.sqlite3-shm", "metadata.sqlite3-wal"): |
| 46 | + assert (dest / name).exists(), f"{name} should be copied" |
| 47 | + |
| 48 | + |
| 49 | +def test_copy_template_raises_on_existing_destination(tmp_path: Path) -> None: |
| 50 | + """copy_template should refuse to overwrite an existing destination.""" |
| 51 | + template = tmp_path / "template" |
| 52 | + template.mkdir() |
| 53 | + (template / "blobs").mkdir() |
| 54 | + (template / "blobs" / "sha256" / "00" / "abc").parent.mkdir(parents=True) |
| 55 | + (template / "blobs" / "sha256" / "00" / "abc").write_bytes(b"blob") |
| 56 | + |
| 57 | + dest = tmp_path / "destination" |
| 58 | + dest.mkdir() |
| 59 | + import pytest |
| 60 | + |
| 61 | + with pytest.raises(FileExistsError): |
| 62 | + copy_template(template, dest) |
0 commit comments