Files
Open_Ultra_SAST/tests/test_shell_wrapper.py
T
Marius Ciepluch d1f61fbe6f fix(contributor-scan): the wrapper writes its runs to the user cache (item 5a)
The shell wrapper wrote the runs of the image to ./.ousast, inside the repository it
was scanning (the eighth session: three runs under it). It now creates
~/.cache/openultrasast/runs, mounts it at /reports, the one writable mount, and passes
-e OPENULTRASAST_RUNS_DIR=/reports to its compose run, so nothing is written to the
scanned directory.

The override is the maintainer's choice of 2026-11-27, on a measurement. Answer 4 of
the menu of 2026-11-26 was worded as the mount alone. Measured that day: compose sets
OPENULTRASAST_RUNS_DIR=/reports/runs in the service, so the mount alone put the run on
the host at ~/.cache/openultrasast/runs/runs/<run>, one level below a native run, and
nothing prunes it there (prune_old_runs removes only run directories directly under the
root whose names match the run id). With the override the run lands beside native runs.
Two things the choice does not change: a scan in the image never prunes, so the
wrapper's runs are removed by a later native scan on the default location; and the
wrapper writes to ~/.cache/openultrasast/runs whether or not XDG_CACHE_HOME or
OPENULTRASAST_RUNS_DIR moves a native run elsewhere.

ops/shell/ousast.ps1 gets the same change, the maintainer's second choice: it writes
%USERPROFILE%\.cache\openultrasast\runs and passes the same override. That file was
edited by reading and WAS NOT RUN: there is no PowerShell on this machine.

Tests: tests/test_shell_wrapper.py has a third test. It failed before the change (no
runs directory under HOME) and again until the override was passed. After: 3 passed.
Measured on 2026-11-27 through the shell wrapper with the real Docker, from a
directory without .env, both API keys unset: `ousast scan examples --mode quick` on a
copy of libpng's contrib/examples prints run_dir=/reports/<run>, the container prefix,
with file_targets=4, findings=4, exit 0; the run is on the host directly under
~/.cache/openultrasast/runs, owned by the user; the scanned directory stays empty.
The eight tests of tests/test_docs_site.py and tests/test_removed_plane_references.py
pass, `mkdocs build --strict` exits 0, ruff format, ruff check, mypy, the three gates
and compileall pass, and the gates print what they printed before the change. An
independent reviewer approved the change in one round; its remarks on the README
clause, on the test, on the evidence and on the width of one line are applied.

Left as it is, for the maintainer: with HOME unset the shell wrapper now fails, where
it wrote ./.ousast before ($HOME is empty, so the path is /.cache/openultrasast/runs,
and a failed mkdir is not checked, as it was not before).
2026-11-27 09:56:27 +01:00

71 lines
3.1 KiB
Python

"""The shell wrapper, `ops/shell/ousast.sh`, run by bash against stubs: no Joern, no venv, no Docker."""
import shlex
import subprocess
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
WRAPPER = ROOT / "ops/shell/ousast.sh"
STUB = """#!/bin/sh
echo "{name} cwd=$PWD args=$*${{OUSAST_REPORTS:+ reports=$OUSAST_REPORTS}}"
"""
def ousast(tmp_path, executables, *arguments):
"""`ousast <arguments>` in tmp_path/project, with tmp_path as HOME. OUSAST_HOME is a checkout that holds only what
the wrapper looks for, the interpreter under its .venv is a stub, and so is each executable named, and docker,
on PATH."""
checkout = tmp_path / "checkout"
checkout.mkdir()
(checkout / "docker-compose.yml").write_text("services: {}\n")
python = checkout / ".venv" / "bin" / "python"
python.parent.mkdir(parents=True)
python.write_text(STUB.format(name="python"))
python.chmod(0o755)
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
for name in ("docker", *executables):
(bin_dir / name).write_text(STUB.format(name=name))
(bin_dir / name).chmod(0o755)
project = tmp_path / "project"
project.mkdir()
script = f'source "{WRAPPER}" && ousast {" ".join(shlex.quote(a) for a in arguments)}'
return subprocess.run(
["bash", "-c", script],
cwd=project,
env={"PATH": f"{bin_dir}:/usr/bin:/bin", "OUSAST_HOME": str(checkout), "HOME": str(tmp_path)},
capture_output=True,
text=True,
)
def test_a_native_scan_runs_in_the_callers_directory(tmp_path):
"""The native path changed into the checkout first, so `ousast scan .` scanned the checkout, not the caller.
`-P` is passed because `-m` alone puts the working directory first on the interpreter's path, where a file
of the scanned tree could stand in for a module the CLI imports."""
result = ousast(tmp_path, ["joern", "joern-parse"], "scan", ".")
assert result.returncode == 0, result.stderr
assert result.stdout.splitlines() == [f"python cwd={tmp_path / 'project'} args=-P -m openultrasast.cli scan ."]
def test_the_native_path_needs_joern_as_well_as_joern_parse(tmp_path):
"""The batch queries run `joern`; with `joern-parse` alone every one fails, and the scan still exits 0."""
result = ousast(tmp_path, ["joern-parse"], "scan", ".")
assert result.returncode == 0, result.stderr
calls = [line.split()[0] for line in result.stdout.splitlines()]
assert "python" not in calls and "docker" in calls
def test_the_container_writes_its_runs_to_the_users_cache(tmp_path):
"""The wrapper wrote its runs to ./.ousast, inside the repository it was scanning."""
result = ousast(tmp_path, "scan", ".")
runs = tmp_path / ".cache" / "openultrasast" / "runs"
assert result.returncode == 0, result.stderr
assert runs.is_dir() and not any((tmp_path / "project").iterdir())
words = result.stdout.splitlines()[-1].split()
assert words[0] == "docker" and words[-1] == f"reports={runs}"
# Without the override the run lands under runs/runs, where no pruning looks.
assert words.index("OPENULTRASAST_RUNS_DIR=/reports") < words.index("ousast")