Files
Open_Ultra_SAST/tests/test_semantic_extra.py
T
Marius Ciepluch 6edae3a585 feat(contributor-scan): a scan writes its run to the user cache, and the ledger only when it learns (3.1, part A)
Where a scan writes. A native scan now creates its run under the user cache,
~/.cache/openultrasast/runs/<scan-id>/ by default ($XDG_CACHE_HOME when it is
set and absolute), prints run_dir=<path> once, and no longer writes anything
under the analysed tree. OPENULTRASAST_RUNS_DIR still wins: an absolute value
is used as given, a relative one resolves against the working directory.
Pre-push storage is unchanged.

The calibration ledger, by maintainer decision: .openultrasast/calibration/
stays the store, and a scan writes the false-positive ledger into the tree
only when its content would change. On a read-only tree the scan records
ledger_not_written, with a sentence in the report, and completes; before, a
read-only target with one rejected finding crashed before report.md existed.

benchmarks/repos/measure.py follows the printed run directory. It read the
run from under the scanned copy, which would have recorded every repository
as failed, and it also read a stale run left in a copy.

Tests: tests/test_scan_output_location.py drives the real CLI and compares
the whole tree before and after; tests/test_repo_measure.py pins the
measurement script; an autouse fixture keeps every test's scan runs out of
the developer's cache; fourteen test files follow the run to its new place.
README, docs/examples.md and the scan skill name the ledger as the one write
a scan makes. _run_scan is touched by one line and stays over the complexity
cap.

Task 3.1 stays open for part B, the readable summary.
2026-11-24 15:33:35 +01:00

73 lines
3.0 KiB
Python

"""Semantic extra packaging and extra-free overlay parse."""
from __future__ import annotations
import json
import tomllib
from pathlib import Path
import pytest
from openultrasast.cli import main
from openultrasast.semantic.extra import grammar_for, has_semantic_extra
from openultrasast.semantic.ir import parse_file
def test_core_dependencies_are_pyyaml_only_and_semantic_is_optional() -> None:
payload = tomllib.loads(Path("pyproject.toml").read_text())
assert payload["project"]["dependencies"] == ["pyyaml>=6.0"] # ai-service-plane task 1: manifests are YAML
extra = payload["project"]["optional-dependencies"]["semantic"]
joined = " ".join(extra)
assert "tree-sitter" in joined
assert "tree-sitter-javascript" in joined
assert "tree-sitter-c" in joined
assert "tree-sitter-java" in joined
assert "tree-sitter-php>=0.23" in extra
assert "semantic:" in " ".join(payload["tool"]["pytest"]["ini_options"]["markers"])
def test_cli_import_does_not_load_tree_sitter(assert_cold_import) -> None: # type: ignore[no-untyped-def]
proc_code = (
"import sys\n"
"import openultrasast.cli\n"
"leaked = sorted(m for m in sys.modules if m == 'tree_sitter' or m.startswith('tree_sitter_'))\n"
"assert not leaked, leaked\n"
)
assert_cold_import(proc_code)
def test_probe_off_yields_no_javascript_grammar(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("OPENULTRASAST_TREE_SITTER_PROBE", "0")
assert has_semantic_extra() is False
assert grammar_for("javascript") is None
ir = parse_file("app.js", "eval(req.query.x)\n", "javascript")
assert ir.parse_ok is False
assert ir.reason == "language_unsupported"
def test_standard_javascript_is_unadjudicated_without_extra(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("OPENULTRASAST_TREE_SITTER_PROBE", "0")
repo = tmp_path / "repo"
repo.mkdir()
monkeypatch.setenv("OPENULTRASAST_RUNS_DIR", str(repo / ".runs"))
(repo / "app.js").write_text("eval(req.query.x)\n")
assert main(["scan", str(repo), "--mode", "standard"]) == 0
run_dir = sorted((repo / ".runs").iterdir())[-1]
overlay = json.loads((run_dir / "overlay.json").read_text())["records"]
assert overlay
assert all(item["disposition"] == "unadjudicated" for item in overlay)
assert all(item["reason"] in {"language_unsupported", "parse_failed"} for item in overlay)
def test_quick_scan_without_extra_still_inventories(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("OPENULTRASAST_TREE_SITTER_PROBE", "0")
repo = tmp_path / "repo"
repo.mkdir()
monkeypatch.setenv("OPENULTRASAST_RUNS_DIR", str(repo / ".runs"))
(repo / "app.py").write_text("def run():\n return eval(request.args)\n")
assert main(["scan", str(repo), "--mode", "quick"]) == 0
run_dir = sorted((repo / ".runs").iterdir())[-1]
findings = json.loads((run_dir / "findings.json").read_text())["findings"]
assert findings
assert not (run_dir / "overlay.json").exists()