@@ -18,11 +18,6 @@ def _graph(cache_dir: Path, name: str, root: Path | None) -> Path:
1818 return graph_dir
1919
2020
21- def test_default_cache_dir_is_under_the_user_cache (tmp_path : Path , monkeypatch ) -> None :
22- monkeypatch .setattr (Path , "home" , classmethod (lambda _cls : tmp_path ))
23- assert cache .default_cache_dir () == tmp_path / ".cache" / "cortex" / "code-graphs"
24-
25-
2621def test_marker_records_the_resolved_root (tmp_path : Path ) -> None :
2722 root = tmp_path / "proj"
2823 root .mkdir ()
@@ -105,7 +100,9 @@ def test_missing_cache_dir_is_empty_report(tmp_path: Path) -> None:
105100 assert report == cache .PruneReport ((), (), ())
106101
107102
108- def test_marker_altered_by_redaction_is_removed (tmp_path : Path , monkeypatch ) -> None :
103+ def test_marker_altered_by_redaction_is_removed (
104+ tmp_path : Path , monkeypatch , caplog
105+ ) -> None :
109106 """A redacted root would name a missing path and get the graph pruned."""
110107 root = tmp_path / "proj"
111108 root .mkdir ()
@@ -117,9 +114,119 @@ def _redacting_write(path, data, **kwargs):
117114 return real_write (path , {"project_root" : "REDACTED" }, ** kwargs )
118115
119116 monkeypatch .setattr (cache .gated_files , "write_json" , _redacting_write )
120- cache .write_root_marker (graph_dir , root )
117+ with caplog .at_level (logging .WARNING , logger = cache .__name__ ):
118+ cache .write_root_marker (graph_dir , root )
119+
120+ assert caplog .messages == [
121+ f"root marker for { graph_dir } was altered by redaction; removed"
122+ ]
121123
122124 assert not (graph_dir / cache .ROOT_MARKER ).exists ()
123125 assert cache .prune_orphan_graphs (graph_dir .parent ).unattributed == (
124126 "proj-6666aaaa" ,
125127 )
128+
129+
130+ def test_tilde_paths_expand_to_home (tmp_path : Path , monkeypatch ) -> None :
131+ monkeypatch .setenv ("HOME" , str (tmp_path ))
132+ (tmp_path / "proj" ).mkdir ()
133+ cache .write_root_marker ("~/cache/proj-7777aaaa" , "~/proj" )
134+ graph_dir = tmp_path / "cache" / "proj-7777aaaa"
135+ assert cache .read_root_marker (graph_dir ) == str ((tmp_path / "proj" ).resolve ())
136+ (tmp_path / "proj" ).rmdir ()
137+ assert cache .prune_orphan_graphs ("~/cache" ).removed == ("proj-7777aaaa" ,)
138+
139+
140+ def test_prune_logs_removed_and_unattributed (tmp_path : Path , caplog ) -> None :
141+ gone = tmp_path / "gone"
142+ gone .mkdir ()
143+ cache_dir = tmp_path / "cache"
144+ _graph (cache_dir , "gone-8888aaaa" , gone )
145+ _graph (cache_dir , "legacy-8888bbbb" , None )
146+ gone .rmdir ()
147+
148+ with caplog .at_level (logging .INFO , logger = cache .__name__ ):
149+ cache .prune_orphan_graphs (cache_dir )
150+
151+ assert caplog .messages == [
152+ "pruned 1 orphan code graph(s): ['gone-8888aaaa']" ,
153+ f"1 code graph(s) under { cache_dir } carry no cortex-root.json and cannot "
154+ "be tied to a project root; kept, remove by hand if unused: "
155+ "['legacy-8888bbbb']" ,
156+ ]
157+
158+
159+ def test_failed_removal_is_reported (tmp_path : Path , monkeypatch , caplog ) -> None :
160+ gone = tmp_path / "gone"
161+ gone .mkdir ()
162+ cache_dir = tmp_path / "cache"
163+ graph_dir = _graph (cache_dir , "gone-9999aaaa" , gone )
164+ gone .rmdir ()
165+
166+ def _refuse (path ):
167+ raise OSError ("busy" )
168+
169+ monkeypatch .setattr (cache .shutil , "rmtree" , _refuse )
170+ with caplog .at_level (logging .WARNING , logger = cache .__name__ ):
171+ report = cache .prune_orphan_graphs (cache_dir )
172+
173+ assert report == cache .PruneReport ((), (), ("gone-9999aaaa" ,))
174+ assert caplog .messages == [f"could not prune orphan graph { graph_dir } : busy" ]
175+
176+
177+ def test_plain_file_in_cache_is_skipped (tmp_path : Path ) -> None :
178+ gone = tmp_path / "gone"
179+ gone .mkdir ()
180+ cache_dir = tmp_path / "cache"
181+ _graph (cache_dir , "b-gone" , gone )
182+ (cache_dir / "a-stray.txt" ).write_text ("x" , encoding = "utf-8" )
183+ gone .rmdir ()
184+ assert cache .prune_orphan_graphs (cache_dir ) == cache .PruneReport (
185+ ("b-gone" ,), (), ()
186+ )
187+
188+
189+ def test_marker_without_a_root_string_is_unattributed (tmp_path : Path ) -> None :
190+ cache_dir = tmp_path / "cache"
191+ for name , payload in (
192+ ("list-1" , "[]" ),
193+ ("empty-2" , '{"project_root": ""}' ),
194+ ("number-3" , '{"project_root": 3}' ),
195+ ("missing-4" , '{"other": "/x"}' ),
196+ ):
197+ graph_dir = _graph (cache_dir , name , None )
198+ (graph_dir / cache .ROOT_MARKER ).write_text (payload , encoding = "utf-8" )
199+ report = cache .prune_orphan_graphs (cache_dir )
200+ assert report .unattributed == ("empty-2" , "list-1" , "missing-4" , "number-3" )
201+
202+
203+ def test_record_root_writes_the_marker_when_the_dir_exists (tmp_path : Path ) -> None :
204+ (tmp_path / "out" ).mkdir ()
205+ cache .record_root (tmp_path / "out" , tmp_path )
206+ assert cache .read_root_marker (tmp_path / "out" ) == str (tmp_path .resolve ())
207+
208+
209+ def test_record_root_expands_a_tilde_dir (tmp_path : Path , monkeypatch ) -> None :
210+ monkeypatch .setenv ("HOME" , str (tmp_path ))
211+ (tmp_path / "out" ).mkdir ()
212+ cache .record_root ("~/out" , tmp_path )
213+ assert cache .read_root_marker (tmp_path / "out" ) == str (tmp_path .resolve ())
214+
215+
216+ def test_record_root_creates_nothing_for_an_absent_dir (tmp_path : Path ) -> None :
217+ cache .record_root (tmp_path / "never-built" , tmp_path )
218+ assert not (tmp_path / "never-built" ).exists ()
219+
220+
221+ def test_record_root_logs_a_failed_write (tmp_path : Path , monkeypatch , caplog ) -> None :
222+ (tmp_path / "out" ).mkdir ()
223+
224+ def _refuse (graph_dir , project_root ):
225+ raise OSError ("read-only" )
226+
227+ monkeypatch .setattr (cache , "write_root_marker" , _refuse )
228+ with caplog .at_level (logging .WARNING , logger = cache .__name__ ):
229+ cache .record_root (tmp_path / "out" , tmp_path )
230+ assert caplog .messages == [
231+ f"could not write root marker in { tmp_path / 'out' } : read-only"
232+ ]
0 commit comments