|
| 1 | +"""Tests for merge_zip_into: preserve richest out_files.zip across reruns.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import tempfile |
| 5 | +import zipfile |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from qtaim_gen.source.utils.io import merge_zip_into |
| 10 | + |
| 11 | + |
| 12 | +def _make_zip(path: str, entries: dict) -> None: |
| 13 | + with zipfile.ZipFile(path, "w", zipfile.ZIP_DEFLATED) as zf: |
| 14 | + for name, data in entries.items(): |
| 15 | + zf.writestr(name, data) |
| 16 | + |
| 17 | + |
| 18 | +def _read_zip(path: str) -> dict: |
| 19 | + out = {} |
| 20 | + with zipfile.ZipFile(path, "r") as zf: |
| 21 | + for name in zf.namelist(): |
| 22 | + out[name] = zf.read(name) |
| 23 | + return out |
| 24 | + |
| 25 | + |
| 26 | +def test_move_when_dest_missing(): |
| 27 | + with tempfile.TemporaryDirectory() as tmp: |
| 28 | + src = os.path.join(tmp, "src.zip") |
| 29 | + dest = os.path.join(tmp, "sub", "dest.zip") |
| 30 | + _make_zip(src, {"a.out": b"hello"}) |
| 31 | + |
| 32 | + merge_zip_into(src, dest) |
| 33 | + |
| 34 | + assert not os.path.exists(src) |
| 35 | + assert os.path.exists(dest) |
| 36 | + assert _read_zip(dest) == {"a.out": b"hello"} |
| 37 | + |
| 38 | + |
| 39 | +def test_merge_adds_missing_entries(): |
| 40 | + with tempfile.TemporaryDirectory() as tmp: |
| 41 | + src = os.path.join(tmp, "src.zip") |
| 42 | + dest = os.path.join(tmp, "dest.zip") |
| 43 | + _make_zip(dest, {"a.out": b"AAA"}) |
| 44 | + _make_zip(src, {"b.out": b"BBB", "c.out": b"CCC"}) |
| 45 | + |
| 46 | + merge_zip_into(src, dest) |
| 47 | + |
| 48 | + assert not os.path.exists(src) |
| 49 | + assert _read_zip(dest) == { |
| 50 | + "a.out": b"AAA", |
| 51 | + "b.out": b"BBB", |
| 52 | + "c.out": b"CCC", |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | +def test_collision_prefers_larger(): |
| 57 | + with tempfile.TemporaryDirectory() as tmp: |
| 58 | + src = os.path.join(tmp, "src.zip") |
| 59 | + dest = os.path.join(tmp, "dest.zip") |
| 60 | + _make_zip(dest, {"adch.out": b"short", "cm5.out": b"rich cm5 content"}) |
| 61 | + _make_zip(src, {"adch.out": b"richer adch content here", "cm5.out": b"x"}) |
| 62 | + |
| 63 | + merge_zip_into(src, dest) |
| 64 | + |
| 65 | + merged = _read_zip(dest) |
| 66 | + assert merged["adch.out"] == b"richer adch content here" |
| 67 | + assert merged["cm5.out"] == b"rich cm5 content" |
| 68 | + |
| 69 | + |
| 70 | +def test_collision_equal_size_keeps_existing(): |
| 71 | + with tempfile.TemporaryDirectory() as tmp: |
| 72 | + src = os.path.join(tmp, "src.zip") |
| 73 | + dest = os.path.join(tmp, "dest.zip") |
| 74 | + _make_zip(dest, {"a.out": b"XXXXX"}) |
| 75 | + _make_zip(src, {"a.out": b"YYYYY"}) |
| 76 | + |
| 77 | + merge_zip_into(src, dest) |
| 78 | + |
| 79 | + assert _read_zip(dest) == {"a.out": b"XXXXX"} |
| 80 | + |
| 81 | + |
| 82 | +def test_missing_src_is_noop(): |
| 83 | + with tempfile.TemporaryDirectory() as tmp: |
| 84 | + src = os.path.join(tmp, "missing.zip") |
| 85 | + dest = os.path.join(tmp, "dest.zip") |
| 86 | + _make_zip(dest, {"a.out": b"AAA"}) |
| 87 | + |
| 88 | + merge_zip_into(src, dest) |
| 89 | + |
| 90 | + assert _read_zip(dest) == {"a.out": b"AAA"} |
| 91 | + |
| 92 | + |
| 93 | +def test_dest_corrupt_raises_and_preserves_dest(): |
| 94 | + with tempfile.TemporaryDirectory() as tmp: |
| 95 | + src = os.path.join(tmp, "src.zip") |
| 96 | + dest = os.path.join(tmp, "dest.zip") |
| 97 | + _make_zip(src, {"a.out": b"AAA"}) |
| 98 | + with open(dest, "wb") as f: |
| 99 | + f.write(b"not a zip file") |
| 100 | + |
| 101 | + with pytest.raises(zipfile.BadZipFile): |
| 102 | + merge_zip_into(src, dest) |
| 103 | + |
| 104 | + assert os.path.exists(src) |
| 105 | + with open(dest, "rb") as f: |
| 106 | + assert f.read() == b"not a zip file" |
| 107 | + assert not os.path.exists(dest + ".merge.tmp") |
| 108 | + |
| 109 | + |
| 110 | +def test_simulated_rerun_preserves_original_entries(): |
| 111 | + # Simulate first full run producing many .out files, then a parse_only |
| 112 | + # rerun producing only a subset - merged zip should retain everything. |
| 113 | + with tempfile.TemporaryDirectory() as tmp: |
| 114 | + dest = os.path.join(tmp, "out_files.zip") |
| 115 | + first_run = { |
| 116 | + "adch.out": b"first adch full output", |
| 117 | + "cm5.out": b"first cm5 full output", |
| 118 | + "hirshfeld.out": b"first hirshfeld full output", |
| 119 | + "fuzzy_full.out": b"first fuzzy full output", |
| 120 | + } |
| 121 | + _make_zip(dest, first_run) |
| 122 | + |
| 123 | + src = os.path.join(tmp, "out_files.zip.new") |
| 124 | + rerun = {"adch.out": b"short rerun"} |
| 125 | + _make_zip(src, rerun) |
| 126 | + |
| 127 | + merge_zip_into(src, dest) |
| 128 | + |
| 129 | + merged = _read_zip(dest) |
| 130 | + assert set(merged) == set(first_run) |
| 131 | + for k, v in first_run.items(): |
| 132 | + assert merged[k] == v |
0 commit comments