Files
RedBear-OS/local/scripts/test-patch-sanity.sh
T
vasilito cb424d7448 build: static patch-sanity linter (shift-left the malformed-patch class)
verify-patch-sanity.py validates every active recipe .patch has internally-
consistent hunk line counts — catching the 'malformed patch at line N' failure
at commit/CI/preflight time instead of hours into a cook. This cycle hit that
class three times (qtwaylandscanner, sddm, xwayland), each only discovered when
cookbook tried to apply the patch.

Running it across the repo found 29 latent malformed patches (validated against
GNU patch: e.g. relibc/P3-sysv-ipc reproduces 'malformed patch at line 22').
They were harmless only because they sit in vendored recipes (baked, not re-
applied) — but would fail on any version-bump re-derivation. --fix recounts the
hunk headers (body untouched) and repaired all 29.

Wired into build-preflight.sh (Phase 1.0D) and redbear-ci.yml, with a unit test
(test-patch-sanity.sh). Skips archived/legacy trees and unvalidatable formats
(empty placeholders, bare-@@ git hunks).
2026-08-01 05:13:02 +03:00

50 lines
1.7 KiB
Bash
Executable File

#!/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