|
| 1 | +"""#3989 — a process that touches more than one cache root must not have the |
| 2 | +first root's stat-index.json poisoned with entries from every root that |
| 3 | +followed it. |
| 4 | +
|
| 5 | +``_ensure_stat_index`` used to bind to the FIRST root/cache_root it ever saw |
| 6 | +and return immediately for every later call, for the life of the process. |
| 7 | +Reads for a later root silently came from the first root's (irrelevant) file, |
| 8 | +and at exit the accumulated in-memory index — now holding entries for every |
| 9 | +root ever touched — was written back into the FIRST root's file alone. |
| 10 | +""" |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import json |
| 14 | +import warnings |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from graphify import cache |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(autouse=True) |
| 23 | +def _fresh_index(): |
| 24 | + def reset(): |
| 25 | + cache._stat_index_root = None |
| 26 | + cache._stat_index_anchor = None |
| 27 | + cache._stat_index = {} |
| 28 | + cache._stat_index_dirty = False |
| 29 | + cache._stat_index_path = None |
| 30 | + reset() |
| 31 | + yield |
| 32 | + reset() |
| 33 | + |
| 34 | + |
| 35 | +def _make_project(tmp_path: Path, name: str) -> tuple[Path, Path]: |
| 36 | + """Return (source root, absolute cache dir) for a fake project ``name`` — |
| 37 | + the exact shape of the issue's own reproduction (an absolute |
| 38 | + ``_GRAPHIFY_OUT`` set fresh before each root's calls).""" |
| 39 | + src = tmp_path / name / "src" |
| 40 | + src.mkdir(parents=True) |
| 41 | + (src / "doc.md").write_text(f"content of {name}", encoding="utf-8") |
| 42 | + out = tmp_path / name / "out" |
| 43 | + out.mkdir(parents=True) |
| 44 | + return src, out |
| 45 | + |
| 46 | + |
| 47 | +def _stat_index_file(cache_dir: Path) -> Path: |
| 48 | + return cache_dir / "cache" / "stat-index.json" |
| 49 | + |
| 50 | + |
| 51 | +def test_each_root_gets_its_own_stat_index_file_not_the_first_roots(monkeypatch, tmp_path): |
| 52 | + """Two different projects, each with their own absolute cache dir, must |
| 53 | + each end up with their OWN stat-index.json — not a shared/poisoned one.""" |
| 54 | + src_a, out_a = _make_project(tmp_path, "a") |
| 55 | + src_b, out_b = _make_project(tmp_path, "b") |
| 56 | + |
| 57 | + with warnings.catch_warnings(): |
| 58 | + warnings.simplefilter("ignore") |
| 59 | + monkeypatch.setattr(cache, "_GRAPHIFY_OUT", str(out_a)) |
| 60 | + cache.file_hash(src_a / "doc.md", root=src_a) |
| 61 | + monkeypatch.setattr(cache, "_GRAPHIFY_OUT", str(out_b)) |
| 62 | + cache.file_hash(src_b / "doc.md", root=src_b) |
| 63 | + cache._flush_stat_index() |
| 64 | + |
| 65 | + assert _stat_index_file(out_a).exists() |
| 66 | + assert _stat_index_file(out_b).exists() |
| 67 | + index_a = json.loads(_stat_index_file(out_a).read_text(encoding="utf-8")) |
| 68 | + index_b = json.loads(_stat_index_file(out_b).read_text(encoding="utf-8")) |
| 69 | + # Project A's file must hold only A's own (relativized) key, never a key |
| 70 | + # belonging to project B, and vice versa. |
| 71 | + assert any("doc.md" in k for k in index_a) |
| 72 | + assert any("doc.md" in k for k in index_b) |
| 73 | + assert not any(str(src_b) in k for k in index_a) |
| 74 | + assert not any(str(src_a) in k for k in index_b) |
| 75 | + |
| 76 | + |
| 77 | +def test_switching_cache_root_mid_process_warns(monkeypatch, tmp_path): |
| 78 | + """Crossing from one bound root to another in the same process must be |
| 79 | + visible, not silent (#3989 minimum-fix requirement).""" |
| 80 | + src_a, out_a = _make_project(tmp_path, "a") |
| 81 | + src_b, out_b = _make_project(tmp_path, "b") |
| 82 | + |
| 83 | + monkeypatch.setattr(cache, "_GRAPHIFY_OUT", str(out_a)) |
| 84 | + cache.file_hash(src_a / "doc.md", root=src_a) |
| 85 | + monkeypatch.setattr(cache, "_GRAPHIFY_OUT", str(out_b)) |
| 86 | + with pytest.warns(RuntimeWarning, match="stat index switched"): |
| 87 | + cache.file_hash(src_b / "doc.md", root=src_b) |
| 88 | + |
| 89 | + |
| 90 | +def test_revisiting_the_first_root_reloads_its_own_entries(monkeypatch, tmp_path): |
| 91 | + """After switching away and back, the first root's file must still carry |
| 92 | + its own entry — the switch-back must reload it, not keep serving/writing |
| 93 | + whatever root happened to be bound last.""" |
| 94 | + src_a, out_a = _make_project(tmp_path, "a") |
| 95 | + src_b, out_b = _make_project(tmp_path, "b") |
| 96 | + |
| 97 | + with warnings.catch_warnings(): |
| 98 | + warnings.simplefilter("ignore") |
| 99 | + monkeypatch.setattr(cache, "_GRAPHIFY_OUT", str(out_a)) |
| 100 | + digest_a_first = cache.file_hash(src_a / "doc.md", root=src_a) |
| 101 | + monkeypatch.setattr(cache, "_GRAPHIFY_OUT", str(out_b)) |
| 102 | + cache.file_hash(src_b / "doc.md", root=src_b) |
| 103 | + monkeypatch.setattr(cache, "_GRAPHIFY_OUT", str(out_a)) |
| 104 | + digest_a_second = cache.file_hash(src_a / "doc.md", root=src_a) |
| 105 | + cache._flush_stat_index() |
| 106 | + |
| 107 | + assert digest_a_first == digest_a_second |
| 108 | + index_a = json.loads(_stat_index_file(out_a).read_text(encoding="utf-8")) |
| 109 | + assert any("doc.md" in k for k in index_a) |
| 110 | + assert not any(str(src_b) in k for k in index_a) |
0 commit comments