#!/usr/bin/env bash # test-patch-sanity.sh — unit test for verify-patch-sanity.py (the malformed- # patch detector + recount fixer). Zero network. set -u DIR="$(cd "$(dirname "$0")" && pwd)" LINT="$DIR/verify-patch-sanity.py" T="$(mktemp -d)"; trap 'rm -rf "$T"' EXIT PASS=0; FAIL=0 ok(){ PASS=$((PASS+1)); printf ' ok %s\n' "$1"; } no(){ FAIL=$((FAIL+1)); printf ' FAIL %s\n %s\n' "$1" "${2:-}"; } # a WELL-FORMED patch (counts match body: 3 old / 4 new) cat > "$T/good.patch" <<'EOF' --- a/f.c +++ b/f.c @@ -1,3 +1,4 @@ a +b c d EOF # a MALFORMED patch: header says +1,7 but only 4 new lines present (the fatal class) cat > "$T/bad.patch" <<'EOF' --- a/f.c +++ b/f.c @@ -1,3 +1,7 @@ a +b c d EOF # an EMPTY placeholder (no hunks) — must NOT be flagged : > "$T/empty.patch" python3 "$LINT" --quiet "$T/good.patch" >/dev/null 2>&1 && ok "well-formed passes" || no "well-formed passes" python3 "$LINT" --quiet "$T/bad.patch" >/dev/null 2>&1 && no "malformed detected" "bad.patch not flagged" || ok "malformed detected" python3 "$LINT" --quiet "$T/empty.patch" >/dev/null 2>&1 && ok "empty placeholder not flagged" || no "empty placeholder not flagged" # --fix must repair the count-only malformation and make it pass python3 "$LINT" --fix "$T/bad.patch" >/dev/null 2>&1 python3 "$LINT" --quiet "$T/bad.patch" >/dev/null 2>&1 && ok "--fix repairs count error" || no "--fix repairs count error" grep -q '@@ -1,3 +1,4 @@' "$T/bad.patch" && ok "--fix recounted header to +1,4" || no "--fix recounted header" "$(grep '@@' "$T/bad.patch")" # --fix must not touch the body grep -qx '+b' "$T/bad.patch" && ok "--fix left the body intact" || no "--fix left body intact" echo " PASS: $PASS FAIL: $FAIL" [ "$FAIL" -eq 0 ] && { echo " ALL GREEN"; exit 0; } || exit 1