Debian Codegraph @

scripts/sabotage-check.sh

raw
1 #!/usr/bin/env bash
2
3 set -euo pipefail
4
5 usage() {
6 cat >&2 <<'EOF'
7 usage: scripts/sabotage-check.sh --patch <patch-file> --test-cmd <command>
8
9 Applies a deliberate sabotage patch, requires the test command to fail, reverts
10 the patch, then requires the same test command to pass. The working tree must be
11 clean before running.
12 EOF
13 }
14
15 patch_file=""
16 test_cmd=""
17
18 while [[ $# -gt 0 ]]; do
19 case "$1" in
20 --patch)
21 [[ $# -ge 2 ]] || { usage; exit 2; }
22 patch_file="$2"
23 shift 2
24 ;;
25 --test-cmd)
26 [[ $# -ge 2 ]] || { usage; exit 2; }
27 test_cmd="$2"
28 shift 2
29 ;;
30 -h|--help)
31 usage
32 exit 0
33 ;;
34 *)
35 usage
36 exit 2
37 ;;
38 esac
39 done
40
41 [[ -n "$patch_file" && -n "$test_cmd" ]] || { usage; exit 2; }
42 [[ -f "$patch_file" ]] || { echo "error: patch file not found: $patch_file" >&2; exit 2; }
43
44 if [[ -n "$(git status --short)" ]]; then
45 echo "error: working tree must be clean before sabotage check" >&2
46 exit 1
47 fi
48
49 cleanup() {
50 if git apply --reverse --check "$patch_file" >/dev/null 2>&1; then
51 git apply --reverse "$patch_file"
52 fi
53 }
54 trap cleanup EXIT
55
56 git apply --check "$patch_file"
57 git apply "$patch_file"
58
59 set +e
60 bash -lc "$test_cmd"
61 status=$?
62 set -e
63
64 if [[ "$status" -eq 0 ]]; then
65 echo "error: sabotage patch did not make the test command fail" >&2
66 exit 1
67 fi
68
69 cleanup
70 trap - EXIT
71
72 bash -lc "$test_cmd"
73 echo "sabotage check passed: sabotage failed as expected, restored test passed"