diff --git a/ops/shell/ousast.sh b/ops/shell/ousast.sh index b90859f2..ffea08b9 100644 --- a/ops/shell/ousast.sh +++ b/ops/shell/ousast.sh @@ -47,6 +47,18 @@ ousast() { return 1 fi + # The runs go under HOME. Both checks come before the build: on a cold one it pulls ~2 GB, and a run that + # cannot write its runs should say so first. + if [ -z "${HOME:-}" ]; then + echo "ousast: HOME is not set, so there is no ~/.cache/openultrasast/runs to write the runs to" >&2 + return 1 + fi + local runs="$HOME/.cache/openultrasast/runs" + if ! mkdir -p "$runs"; then + echo "ousast: cannot create $runs, where the runs are written" >&2 + return 1 + fi + # Build once, the first time. The run says why it waits, then the build's progress shows: on a cold build it # is the only sign that a long download is running. if ! docker image inspect "openultrasast:${OUSAST_VERSION:-dev}" >/dev/null 2>&1; then @@ -72,8 +84,6 @@ ousast() { fi done - local runs="$HOME/.cache/openultrasast/runs" - mkdir -p "$runs" # /reports is the runs directory itself here, not a reports directory with runs under it: the override puts # the run beside a native one, where the pruning of the default location finds it. TARGET="$PWD" OUSAST_REPORTS="$runs" \ diff --git a/tests/test_shell_wrapper.py b/tests/test_shell_wrapper.py index c7bb668c..351cf062 100644 --- a/tests/test_shell_wrapper.py +++ b/tests/test_shell_wrapper.py @@ -12,10 +12,10 @@ echo "{name} cwd=$PWD args=$*${{OUSAST_REPORTS:+ reports=$OUSAST_REPORTS}}" """ -def ousast(tmp_path, executables, *arguments): - """`ousast ` 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.""" +def ousast(tmp_path, executables, *arguments, without_home=False): + """`ousast ` in tmp_path/project, with tmp_path as HOME unless `without_home` is passed. 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") @@ -26,18 +26,20 @@ def ousast(tmp_path, executables, *arguments): bin_dir = tmp_path / "bin" bin_dir.mkdir() for name in ("docker", *executables): - (bin_dir / name).write_text(STUB.format(name=name)) + stub = STUB.format(name=name) + if name == "docker": + # `image inspect` fails, as for an image that is not built, so the wrapper takes its build branch. + shebang, body = stub.split("\n", 1) + stub = f'{shebang}\n[ "$1" = image ] && exit 1\n{body}' + (bin_dir / name).write_text(stub) (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, - ) + environment = {"PATH": f"{bin_dir}:/usr/bin:/bin", "OUSAST_HOME": str(checkout), "HOME": str(tmp_path)} + if without_home: + del environment["HOME"] + return subprocess.run(["bash", "-c", script], cwd=project, env=environment, capture_output=True, text=True) def test_a_native_scan_runs_in_the_callers_directory(tmp_path): @@ -68,3 +70,24 @@ def test_the_container_writes_its_runs_to_the_users_cache(tmp_path): 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") + + +def test_the_container_path_stops_when_home_is_not_set(tmp_path): + """With HOME unset, `$HOME/.cache` became `/.cache`: the wrapper went on to the container just the same.""" + result = ousast(tmp_path, (), "scan", ".", without_home=True) + message = "ousast: HOME is not set, so there is no ~/.cache/openultrasast/runs to write the runs to" + assert result.returncode == 1, result.stderr + assert result.stderr.splitlines() == [message] + # Neither the build nor the container is started. + assert result.stdout == "" + + +def test_a_runs_directory_that_cannot_be_made_stops_the_wrapper(tmp_path): + """`mkdir -p` was not checked: the wrapper went on to the container with a runs directory that did not exist.""" + (tmp_path / ".cache").write_text("") + result = ousast(tmp_path, (), "scan", ".") + runs = tmp_path / ".cache" / "openultrasast" / "runs" + assert result.returncode == 1, result.stderr + # mkdir says why first; the wrapper's line is the last. + assert result.stderr.splitlines()[-1] == f"ousast: cannot create {runs}, where the runs are written" + assert result.stdout == ""