|
| 1 | +#!/usr/bin/env bash |
| 2 | +# review_checks.sh: semgrep NOT CHECKED when every chunk scanned 0 files (paths.scanned empty, no findings): one not-checked row, |
| 3 | +# "NOT CHECKED: semgrep scanned 0 files" on stderr, semgrep out of the "ran" list. When another chunk scanned files normally, semgrep |
| 4 | +# is not reported as NOT CHECKED. Stub semgrep (one JSON per call) on a hermetic PATH, fictional content only. |
| 5 | +# REVIEW_CHECKS=<path> runs another copy of the script (used to prove the test fails against the pre-fix script). |
| 6 | +set -euo pipefail |
| 7 | +ROOT=$(cd "$(dirname "$0")/.." && pwd) |
| 8 | +RC=${REVIEW_CHECKS:-$ROOT/.claude/skills/deep-code-review/scripts/review_checks.sh} |
| 9 | +T=$(mktemp -d "${TMPDIR:-/tmp}/sgzero.XXXXXX") && [ -n "$T" ] && [ -d "$T" ] || { echo "no temp dir" >&2; exit 1; } |
| 10 | +trap 'cd /; rm -r -- "$T"' EXIT |
| 11 | +fail=0 |
| 12 | +export DCR_CACHE_ROOT="$T/nocache" DCR_USER_BIN= PERUN_HEAVY_DIR="$T/heavy" REVIEW_LEASE_WAIT=0 REVIEW_SEMGREP_REGISTRY=0 |
| 13 | +unset REVIEW_OSV REVIEW_OSV_DB_DIR OSV_SCANNER_LOCAL_DB_CACHE_DIRECTORY REVIEW_SEMGREP_CONFIG REVIEW_FIXTURE_ROWS REVIEW_TEST_CMD |
| 14 | +G() { git -c user.email=t@example.com -c user.name=t "$@"; } |
| 15 | +mkdir -p "$T/bin" "$T/sys" |
| 16 | +for b in bash env git grep sed head tail cat rm mkdir mktemp dirname basename python3 perl timeout gtimeout jq true sort uniq paste tr cut wc awk date cp mv ls find touch tee readlink sleep cmp nice uname; do |
| 17 | + p=$(type -P "$b") || continue; ln -s "$p" "$T/sys/$b" # only the system binaries review_checks.sh needs, never the host PATH |
| 18 | +done |
| 19 | +# stub: call N (1-based) prints $SG_DIR/N.json |
| 20 | +cat >"$T/bin/semgrep" <<'S' |
| 21 | +#!/usr/bin/env bash |
| 22 | +n=$(cat "$SG_DIR/n" 2>/dev/null || echo 0); n=$((n + 1)); echo "$n" >"$SG_DIR/n"; cat "$SG_DIR/$n.json" |
| 23 | +S |
| 24 | +chmod +x "$T/bin/semgrep" |
| 25 | +mkdir -p "$T/repo" && cd "$T/repo" && git init -q . && G commit -q --allow-empty -m base && git branch base |
| 26 | +mkdir src; for i in $(seq 1 201); do printf 'x = %s\n' "$i" >"src/f$i.py"; done |
| 27 | +G add -A && G commit -q -m change |
| 28 | +ZERO='{"results":[],"errors":[],"paths":{"scanned":[]}}' |
| 29 | +OKJ='{"results":[],"errors":[],"paths":{"scanned":["src/f1.py"]}}' |
| 30 | +# go J1 J2: run the hub (201 changed files = two semgrep chunks of 200 + 1); call 1 prints J1, call 2 prints J2 |
| 31 | +go() { |
| 32 | + mkdir -p "$T/sg"; rm -f "$T/sg/n"; printf '%s\n' "$1" >"$T/sg/1.json"; printf '%s\n' "$2" >"$T/sg/2.json" |
| 33 | + PATH="$T/bin:$T/sys" SG_DIR="$T/sg" bash "$RC" --base base >"$T/out" 2>"$T/err" || true |
| 34 | +} |
| 35 | +eq() { if [ "$1" = "$2" ]; then echo "ok - $3"; else echo "FAIL - $3 (got $1, want $2)"; fail=1; fi; } |
| 36 | +nc() { jq '[.[] | select(.tool=="semgrep" and .rule=="not-checked")] | length' "$T/out"; } |
| 37 | +msg() { grep -c '^review_checks: not run: semgrep (NOT CHECKED: semgrep scanned 0 files)' "$T/err" || true; } |
| 38 | + |
| 39 | +go "$ZERO" "$ZERO" # every chunk scanned nothing |
| 40 | +eq "$(msg)" 1 "all chunks zero: one NOT CHECKED line (not one per chunk)" |
| 41 | +eq "$(nc)" 1 "all chunks zero: one semgrep not-checked row" |
| 42 | + |
| 43 | +go "$ZERO" "$OKJ" # one chunk zero, one scanned normally |
| 44 | +eq "$(msg)" 0 "mixed chunks: semgrep not reported as scanning 0 files" |
| 45 | +eq "$(nc)" 0 "mixed chunks: no semgrep not-checked row" |
| 46 | + |
| 47 | +ERRJ='{"results":[],"errors":[{"type":"Timeout","level":"error","path":"src/f2.py"}],"paths":{"scanned":[]}}' |
| 48 | +go "$ERRJ" "$ZERO" # chunk 1 errored with nothing scanned, chunk 2 scanned 0: errors must not read as zero-files |
| 49 | +eq "$(msg)" 0 "errors + empty scanned: not reported as scanning 0 files" |
| 50 | +eq "$(jq '[.[]|select(.tool=="semgrep" and .rule=="not-checked")]|length' "$T/out")" 1 "errors + empty scanned: errored not-checked row kept" |
| 51 | +exit $fail |
0 commit comments