Files
Open_Ultra_SAST/tests/test_degradation_matrix.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

79 lines
3.5 KiB
Python

"""What every optional dependency's absence looks like, and that the gates never move (task 5.1, Req 11.4, 11.5).
The core install has no dependencies. Every capability beyond it is optional, and the rule for all of them is the
same: when it is missing the run says so by name and finishes, rather than crashing or — worse — quietly producing
a smaller number that reads like a result. Each absence is exercised once here, with the reason it records.
The gate baseline is the other half. Three gates are the only place in this project where a detector change is
allowed to be invisible; if one of them moves, something that was never supposed to change did.
"""
from __future__ import annotations
import subprocess
import sys
from pathlib import Path
import pytest
BASELINE = Path("benchmarks/measurements/2026-09-06-gate-baseline.txt")
MARKER = "# --- output begins ---\n"
def test_the_three_gate_outputs_match_the_committed_pre_feature_baseline() -> None:
"""Req 11.5. The baseline was captured at `71efc9c`, before the first line of this feature."""
assert BASELINE.is_file()
expected = BASELINE.read_text().split(MARKER, 1)[1]
produced = "".join(
subprocess.run( # noqa: S603 — this repository's own modules, no shell
[sys.executable, "-m", f"openultrasast.{module}"],
capture_output=True,
text=True,
check=False,
).stdout
for module in ("gate", "map_gate", "pair_gate")
)
assert produced == expected, "a gate moved; diff this against benchmarks/measurements/2026-09-06-gate-baseline.txt"
def test_the_baseline_records_where_it_came_from() -> None:
head = BASELINE.read_text().split(MARKER, 1)[0]
assert "71efc9c" in head and "openultrasast.pair_gate" in head
# --- one optional dependency at a time ---------------------------------------
def test_without_the_network_a_pointer_pair_is_skipped_by_name(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
from openultrasast.pairs import evaluate_catalog, load_pair_catalog, select_slice
monkeypatch.delenv("OPENULTRASAST_PAIRS_NETWORK", raising=False)
pointers = [case for case in select_slice(load_pair_catalog(), "vibe-py") if not case.vendored][:2]
assert pointers, "the vibe-py slice carries pointer rows; without them this proves nothing"
result = evaluate_catalog(pointers, pointers=False)
assert {item["reason"] for item in result.degradations} == {"pointer_pair_skipped"}
assert result.outcomes == ()
def test_without_the_semantic_extra_an_unsupported_language_is_named_not_guessed(monkeypatch: pytest.MonkeyPatch) -> None:
from openultrasast.semantic import extra as extra_module
from openultrasast.semantic.ir import parse_file
monkeypatch.setattr(extra_module, "has_semantic_extra", lambda: False)
ir = parse_file("app.go", "package main\n\nfunc main() {}\n", "go")
assert ir.reason == "language_unsupported" and ir.parse_ok is False
def test_retired_regress_stage_is_skipped_by_name(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
import json
from openultrasast.cli import main
(tmp_path / "app.py").write_text("import os\n\n\ndef run(cmd):\n os.system(cmd)\n")
monkeypatch.setenv("OPENULTRASAST_RUNS_DIR", str(tmp_path / "runs"))
assert main(["scan", str(tmp_path), "--mode", "deep"]) in {0, 1}
runs = sorted((tmp_path / "runs").iterdir())
manifest = json.loads((runs[-1] / "manifest.json").read_text())
reasons = {item["reason"] for item in manifest.get("degradations", [])}
assert "regress_retired" in reasons