mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-03 18:28:19 +00:00
feat(contributor-scan): a scan says how many source files it left out under its runs directory (3.1, follow-up)
`56c4620` made a scan skip its runs directory when it lies inside the scanned path, and said nothing about it. By the maintainer's decision of 2026-11-25, recorded in the handover, the scan now prints one notice. The decision, as the handover records it: "When source files lie under the runs directory inside the scanned path, the scan prints one notice: line naming the path and the number left out, the way run retention does." The line is `notice: skipped <n> source files under the runs directory <path>` (`source file` at one). `ousast scan` prints it, the `openultrasast.scan` tool returns it among its notices, and `openultrasast.benchmark` returns the notices of the scan it runs. Nothing is added to the manifest or the report. The count follows the scanned tree's own rules, so it is the number the skip took: a source file that the target's root `.gitignore` already keeps out, or that lies under a directory the scan ignores, brings no notice, and a `.gitignore` inside the runs directory does not hide what was left out. To that end `enumerate_source_files` in src/openultrasast/preprocess.py and the new `count_skipped_source_files` share one private predicate, `_is_read_as_source`. The enumeration returns the lists it returned before (the skip test now comes first, so a path under the skipped directory is no longer stat'ed). The count is taken after the preprocess stage, so a tree whose ignore file cannot be read stops the scan inside that stage, as before. It costs one more read of the target's root `.gitignore` and, where there is one, one more `git ls-files`, and only when the runs directory lies inside the scanned path. Nothing is said when no source file is left out, when the runs directory lies outside the scanned path, or when it is the target or lies above it. Tests: the notice tests failed on the code as it was (6 failed) and pass after the change. Four more failed on a first cut that counted by the runs directory's own `.gitignore` and pass on this one: a `.gitignore` inside the runs directory does not hide what the scan left out, nothing is left out where the target already ignores the runs directory, nothing under a directory the scan ignores, and a directory named `.gitignore` costs the scan nothing. The test that a target inside the runs directory is read as it was now also pins that nothing is said; a count that covered that target failed it. The 23 test files around the change pass (241 passed); ruff, mypy, the three gates and compileall pass, and the gates print what they printed before the change. An independent reviewer approved the change in two rounds. Its first round found the count by the wrong rules, which this cut closes. Its second round named two things that are left open and recorded in the handover: were the target's root `.gitignore` to become unreadable between the preprocess stage and the count, the scan would stop with no failed stage recorded in its trace and no manifest; and no test pins that a tracked source file under an ignored runs directory is counted.
This commit is contained in:
@@ -13,7 +13,7 @@ import pytest
|
||||
|
||||
from openultrasast import cli
|
||||
from openultrasast.cli import main
|
||||
from openultrasast.config import ResolvedConfig
|
||||
from openultrasast.config import ResolvedConfig, load_config
|
||||
from openultrasast.preprocess import enumerate_source_files
|
||||
|
||||
LEDGER = ".openultrasast/calibration/false_positive_learnings.json"
|
||||
@@ -300,8 +300,11 @@ def test_a_target_that_lies_inside_the_runs_directory_is_read_as_it_was(
|
||||
|
||||
assert _scan(repo) == 0
|
||||
|
||||
(printed,) = re.findall(r"^run_dir=(.+)$", capsys.readouterr().out, flags=re.MULTILINE)
|
||||
out = capsys.readouterr().out
|
||||
(printed,) = re.findall(r"^run_dir=(.+)$", out, flags=re.MULTILINE)
|
||||
assert _files_read(Path(printed)) == ["app.py", "pkg/lib.py"]
|
||||
# Nothing is left out there, so nothing is said.
|
||||
assert "notice:" not in out
|
||||
|
||||
|
||||
def test_the_runs_directory_is_found_under_a_relative_root(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
@@ -318,3 +321,113 @@ def test_no_test_can_write_a_scan_run_into_the_developers_cache(tmp_path_factory
|
||||
"""tests/conftest.py redirects the override for every test, so one that forgets to name a location cannot
|
||||
write into the real `~/.cache/openultrasast/runs`."""
|
||||
assert Path(ResolvedConfig().runs_dir).is_relative_to(tmp_path_factory.getbasetemp())
|
||||
|
||||
|
||||
def _notices(capsys: pytest.CaptureFixture[str]) -> list[str]:
|
||||
return re.findall(r"^notice: (.+)$", capsys.readouterr().out, flags=re.MULTILINE)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("form", ["override", "default_location", "symlinked_override"])
|
||||
@pytest.mark.parametrize("left_out", [2, 1])
|
||||
def test_a_scan_says_how_many_source_files_it_left_out_under_its_runs_directory(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], form: str, left_out: int
|
||||
) -> None:
|
||||
repo = _target(tmp_path)
|
||||
runs = _runs_inside(repo, tmp_path, monkeypatch, form)
|
||||
(runs / "earlier").mkdir()
|
||||
(runs / "earlier" / "copy.py").write_text((repo / "app.py").read_text())
|
||||
# What a run keeps: no source file, so none of these is counted.
|
||||
(runs / "earlier" / "report.md").write_text("# report\n")
|
||||
(runs / "earlier" / "findings.json").write_text("{}\n")
|
||||
if left_out == 2:
|
||||
(runs / "earlier" / "pkg").mkdir()
|
||||
(runs / "earlier" / "pkg" / "lib.py").write_text((repo / "pkg" / "lib.py").read_text())
|
||||
|
||||
assert _scan(repo) == 0
|
||||
|
||||
runs_dir = load_config(None).runs_dir
|
||||
files = "file" if left_out == 1 else "files"
|
||||
assert _notices(capsys) == [f"skipped {left_out} source {files} under the runs directory {runs_dir}"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("where", ["inside", "outside"])
|
||||
def test_nothing_is_said_when_no_source_file_is_left_out(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], where: str
|
||||
) -> None:
|
||||
# Inside the scanned path a run keeps no source file, and outside it nothing is skipped: neither is said.
|
||||
repo = _target(tmp_path)
|
||||
if where == "inside":
|
||||
runs = _runs_inside(repo, tmp_path, monkeypatch, "override")
|
||||
kept = ("report.md", "findings.json")
|
||||
else:
|
||||
runs = tmp_path / "elsewhere" / "runs"
|
||||
runs.mkdir(parents=True)
|
||||
monkeypatch.setenv("OPENULTRASAST_RUNS_DIR", str(runs))
|
||||
kept = ("copy.py",)
|
||||
(runs / "earlier").mkdir()
|
||||
for name in kept:
|
||||
(runs / "earlier" / name).write_text("x = 1\n")
|
||||
|
||||
assert _scan(repo) == 0
|
||||
|
||||
assert _notices(capsys) == []
|
||||
|
||||
|
||||
def _left_out_copy(repo: Path, runs: Path) -> None:
|
||||
"""A source file under the runs directory, where a run of this package keeps none."""
|
||||
(runs / "earlier").mkdir(parents=True)
|
||||
(runs / "earlier" / "copy.py").write_text((repo / "app.py").read_text())
|
||||
|
||||
|
||||
def test_a_gitignore_inside_the_runs_directory_does_not_hide_what_the_scan_left_out(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
||||
) -> None:
|
||||
# The scan reads the target's root .gitignore and no other: one inside the runs directory hides nothing from
|
||||
# the count.
|
||||
repo = _target(tmp_path)
|
||||
runs = repo / "pkg"
|
||||
(runs / ".gitignore").write_text("*.py\n")
|
||||
monkeypatch.setenv("OPENULTRASAST_RUNS_DIR", str(runs))
|
||||
|
||||
assert _scan(repo) == 0
|
||||
|
||||
assert _notices(capsys) == [f"skipped 1 source file under the runs directory {runs}"]
|
||||
|
||||
|
||||
def test_nothing_is_left_out_where_the_target_already_ignores_the_runs_directory(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
||||
) -> None:
|
||||
repo = _target(tmp_path)
|
||||
(repo / ".gitignore").write_text("__pycache__/\n.runs/\n")
|
||||
runs = _runs_inside(repo, tmp_path, monkeypatch, "override")
|
||||
_left_out_copy(repo, runs)
|
||||
|
||||
assert _scan(repo) == 0
|
||||
|
||||
assert _notices(capsys) == []
|
||||
|
||||
|
||||
def test_nothing_is_left_out_under_a_directory_the_scan_ignores(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
||||
) -> None:
|
||||
repo = _target(tmp_path)
|
||||
runs = repo / ".openultrasast" / "runs"
|
||||
_left_out_copy(repo, runs)
|
||||
monkeypatch.setenv("OPENULTRASAST_RUNS_DIR", str(runs))
|
||||
|
||||
assert _scan(repo) == 0
|
||||
|
||||
assert _notices(capsys) == []
|
||||
|
||||
|
||||
def test_a_directory_named_gitignore_in_the_runs_directory_costs_the_scan_nothing(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
||||
) -> None:
|
||||
# The count reads what the preprocess stage read, and nothing of its own under the runs directory.
|
||||
repo = _target(tmp_path)
|
||||
runs = _runs_inside(repo, tmp_path, monkeypatch, "override")
|
||||
(runs / ".gitignore").mkdir()
|
||||
|
||||
assert _scan(repo) == 0
|
||||
|
||||
assert _notices(capsys) == []
|
||||
|
||||
Reference in New Issue
Block a user