Skip to content

Commit bcacf39

Browse files
committed
test: define persistent cache behavior
1 parent 388276d commit bcacf39

2 files changed

Lines changed: 242 additions & 1 deletion

File tree

‎tests/test_architecture.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
}
4343

4444
# Top-level modules that wire the pipeline together and may import any layer.
45-
ROOT_MODULES = {"__init__", "__main__", "cli", "engine"}
45+
ROOT_MODULES = {"__init__", "__main__", "cache", "cli", "engine"}
4646

4747

4848
def package_files() -> Iterator[tuple[Path, str, str]]:

‎tests/test_cache.py‎

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
"""Acceptance tests for the persistent cache (``docs/architecture.md`` §11, §38 Phase 10).
2+
3+
``ProjectCache`` stores, per module, its function summaries, its call sites and its
4+
findings under a key derived from the source text, the engine, schema and plugin
5+
versions, the security models, the advisories, the dependency graph and the keys of the
6+
modules it imports transitively. On a later run a module whose key is unchanged is
7+
served from the cache and never lowered again; its summaries seed the project index, so
8+
changing one file re-analyses that file and its importers only.
9+
10+
Expected to remain red until ``coretrace_python.cache`` and the ``--cache`` option exist.
11+
"""
12+
13+
from __future__ import annotations
14+
15+
import json
16+
from pathlib import Path
17+
18+
import pytest
19+
20+
from coretrace_python import engine
21+
from coretrace_python.cli import main
22+
from coretrace_python.findings import Confidence, Finding, Severity
23+
from coretrace_python.source import SourceId, SourceSpan
24+
25+
try:
26+
from coretrace_python.cache import CachedModule, ProjectCache, decode, encode
27+
except ImportError as error: # pragma: no cover - red until the cache lands
28+
MISSING = error
29+
else:
30+
MISSING = None
31+
32+
33+
@pytest.fixture(autouse=True)
34+
def require_cache() -> None:
35+
if MISSING is not None:
36+
pytest.fail(f"persistent cache is not implemented yet: {MISSING}")
37+
38+
39+
REPO = Path(__file__).resolve().parent.parent
40+
PLUGINS = REPO / "plugins"
41+
42+
HELPERS = "import os\n\ndef execute(command):\n os.system(command)\n"
43+
MAIN = "from app.helpers import execute\n\ndef run():\n execute(input())\n"
44+
CLEAN = "def ok():\n return 1\n"
45+
46+
47+
def project(root: Path, files: dict[str, str]) -> Path:
48+
for relative, text in files.items():
49+
path = root / relative
50+
path.parent.mkdir(parents=True, exist_ok=True)
51+
path.write_text(text, encoding="utf-8")
52+
return root
53+
54+
55+
def standard(root: Path) -> Path:
56+
return project(
57+
root / "src",
58+
{"app/__init__.py": "", "app/helpers.py": HELPERS, "app/main.py": MAIN, "app/clean.py": CLEAN},
59+
)
60+
61+
62+
def rules(findings: tuple[Finding, ...]) -> list[tuple[str, str, int]]:
63+
return sorted((Path(str(f.span.source_id)).name, f.rule_id, f.span.start_line) for f in findings)
64+
65+
66+
# --------------------------------------------------------------------------- codec
67+
68+
69+
def test_findings_and_summaries_round_trip_through_json() -> None:
70+
from coretrace_python.interprocedural import ExternalCall, FunctionSummary
71+
from coretrace_python.semantic.symbols import SymbolId
72+
73+
span = SourceSpan(SourceId("/p/a.py"), 3, 5, 3, 9)
74+
finding = Finding("r", "m", Severity.HIGH, Confidence.MEDIUM, span, "f", {"k": "v"})
75+
summary = FunctionSummary(
76+
"f",
77+
2,
78+
frozenset({0}),
79+
(ExternalCall(SymbolId("python.os.system"), (frozenset({0}), frozenset()), frozenset({1}), span, None),),
80+
False,
81+
frozenset({SymbolId("python.builtins.input")}),
82+
)
83+
84+
text = json.dumps(encode(CachedModule(("f",), {"f": summary}, (), (finding,))))
85+
restored = decode(json.loads(text))
86+
87+
assert restored.findings == (finding,)
88+
assert restored.summaries["f"] == summary
89+
assert restored.functions == ("f",)
90+
91+
92+
# --------------------------------------------------------------------------- keys
93+
94+
95+
def test_keys_depend_on_source_configuration_and_imports(tmp_path: Path) -> None:
96+
root = standard(tmp_path)
97+
first = engine.analyze_project(root, [PLUGINS]).keys
98+
(root / "app" / "clean.py").write_text("def ok():\n return 2\n", encoding="utf-8")
99+
changed_clean = engine.analyze_project(root, [PLUGINS]).keys
100+
(root / "app" / "helpers.py").write_text(HELPERS + "\n", encoding="utf-8")
101+
changed_helpers = engine.analyze_project(root, [PLUGINS]).keys
102+
without_plugins = engine.analyze_project(root).keys
103+
104+
assert set(first) == {"app", "app.helpers", "app.main", "app.clean"}
105+
assert changed_clean["app.clean"] != first["app.clean"]
106+
assert changed_clean["app.main"] == first["app.main"]
107+
assert changed_helpers["app.helpers"] != changed_clean["app.helpers"]
108+
assert changed_helpers["app.main"] != changed_clean["app.main"]
109+
assert changed_helpers["app.clean"] == changed_clean["app.clean"]
110+
assert without_plugins["app.clean"] != changed_helpers["app.clean"]
111+
112+
113+
def test_keys_depend_on_dependency_files(tmp_path: Path) -> None:
114+
root = standard(tmp_path)
115+
before = engine.analyze_project(root, [PLUGINS]).keys
116+
(root / "requirements.txt").write_text("pyyaml==5.3.1\n", encoding="utf-8")
117+
after = engine.analyze_project(root, [PLUGINS]).keys
118+
119+
assert all(after[name] != before[name] for name in before)
120+
121+
122+
# --------------------------------------------------------------------------- reuse
123+
124+
125+
def test_second_run_reuses_every_module_and_reports_the_same_findings(tmp_path: Path) -> None:
126+
root = standard(tmp_path)
127+
cache = ProjectCache(tmp_path / "cache")
128+
129+
first = engine.analyze_project(root, [PLUGINS], cache=cache)
130+
second = engine.analyze_project(root, [PLUGINS], cache=cache)
131+
132+
assert first.reused == ()
133+
assert set(second.reused) == {"app", "app.helpers", "app.main", "app.clean"}
134+
assert second.findings == first.findings
135+
assert rules(second.findings) == [("main.py", "command-injection", 4)]
136+
assert second.index == first.index
137+
assert list((tmp_path / "cache").glob("*.json"))
138+
139+
140+
def test_changing_a_file_reanalyses_it_and_its_importers_only(tmp_path: Path) -> None:
141+
root = standard(tmp_path)
142+
cache = ProjectCache(tmp_path / "cache")
143+
engine.analyze_project(root, [PLUGINS], cache=cache)
144+
145+
(root / "app" / "helpers.py").write_text("import os\n\ndef execute(command):\n os.system('ls')\n", encoding="utf-8")
146+
result = engine.analyze_project(root, [PLUGINS], cache=cache)
147+
148+
assert set(result.reused) == {"app", "app.clean"}
149+
assert result.findings == ()
150+
151+
152+
def test_changing_an_importer_leaves_the_callee_cached(tmp_path: Path) -> None:
153+
root = standard(tmp_path)
154+
cache = ProjectCache(tmp_path / "cache")
155+
engine.analyze_project(root, [PLUGINS], cache=cache)
156+
157+
(root / "app" / "main.py").write_text("from app.helpers import execute\n\ndef run():\n execute('ls')\n", encoding="utf-8")
158+
result = engine.analyze_project(root, [PLUGINS], cache=cache)
159+
160+
assert set(result.reused) == {"app", "app.helpers", "app.clean"}
161+
assert result.findings == ()
162+
163+
164+
def test_cached_modules_still_serve_project_plugins_and_correlation(tmp_path: Path) -> None:
165+
root = project(
166+
tmp_path / "src",
167+
{
168+
"requirements.txt": "pyyaml==5.3.1\n",
169+
"app/__init__.py": "",
170+
"app/config.py": "import yaml\n\ndef parse(text):\n return yaml.load(text)\n",
171+
"app/main.py": "from app.config import parse\n\ndef run():\n parse(input())\n",
172+
},
173+
)
174+
cache = ProjectCache(tmp_path / "cache")
175+
176+
first = engine.analyze_project(root, [PLUGINS], cache=cache)
177+
second = engine.analyze_project(root, [PLUGINS], cache=cache)
178+
179+
assert set(second.reused) == {"app", "app.config", "app.main"}
180+
assert rules(second.findings) == rules(first.findings)
181+
assert ("config.py", "reachable-vulnerability", 4) in rules(second.findings)
182+
assert ("main.py", "exploitable-vulnerability", 4) in rules(second.findings)
183+
184+
185+
def test_unsupported_and_syntax_notes_are_cached_too(tmp_path: Path) -> None:
186+
root = project(tmp_path / "src", {"nested.py": "def outer():\n def inner():\n pass\n return inner\n"})
187+
cache = ProjectCache(tmp_path / "cache")
188+
189+
first = engine.analyze_project(root, [PLUGINS], cache=cache)
190+
second = engine.analyze_project(root, [PLUGINS], cache=cache)
191+
192+
assert rules(first.findings) == [("nested.py", "unsupported-syntax", 1)]
193+
assert second.findings == first.findings
194+
assert second.reused == ("nested",)
195+
196+
197+
def test_corrupt_or_foreign_cache_entries_are_ignored(tmp_path: Path) -> None:
198+
root = standard(tmp_path)
199+
cache = ProjectCache(tmp_path / "cache")
200+
engine.analyze_project(root, [PLUGINS], cache=cache)
201+
for path in (tmp_path / "cache").glob("*.json"):
202+
path.write_text("{not json", encoding="utf-8")
203+
204+
result = engine.analyze_project(root, [PLUGINS], cache=cache)
205+
206+
assert result.reused == ()
207+
assert rules(result.findings) == [("main.py", "command-injection", 4)]
208+
209+
210+
def test_cache_directory_is_created_on_demand(tmp_path: Path) -> None:
211+
root = standard(tmp_path)
212+
cache = ProjectCache(tmp_path / "nested" / "cache")
213+
214+
engine.analyze_project(root, [PLUGINS], cache=cache)
215+
216+
assert (tmp_path / "nested" / "cache").is_dir()
217+
218+
219+
# --------------------------------------------------------------------------- CLI
220+
221+
222+
def test_check_accepts_a_cache_directory(tmp_path: Path, capsys) -> None: # type: ignore[no-untyped-def]
223+
root = standard(tmp_path)
224+
cache_dir = tmp_path / "cache"
225+
226+
first = main(["--check", str(root), "--plugins", str(PLUGINS), "--cache", str(cache_dir)])
227+
first_output = capsys.readouterr().out
228+
second = main(["--check", str(root), "--plugins", str(PLUGINS), "--cache", str(cache_dir)])
229+
second_output = capsys.readouterr().out
230+
231+
assert first == second == 1
232+
assert first_output == second_output
233+
assert "command-injection" in second_output
234+
235+
236+
def test_cache_option_requires_a_check(tmp_path: Path, capsys) -> None: # type: ignore[no-untyped-def]
237+
source = tmp_path / "x.py"
238+
source.write_text("", encoding="utf-8")
239+
240+
assert main(["--emit-ir", "--cache", str(tmp_path / "c"), str(source)]) == 2
241+
assert "--cache" in capsys.readouterr().err

0 commit comments

Comments
 (0)