|
root / hooks / test / cases / 01-git-in-hg-blocks.sh
01-git-in-hg-blocks.sh Bash 32 lines 692 B
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env bash
# Case 01: 'git status' inside an .hg repository must be blocked.
set -u

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

mkdir -p "$tmp/repo/.hg"
mkdir -p "$tmp/repo/sub"

input=$(jq -n --arg cwd "$tmp/repo/sub" --arg cmd "git status" '{
  cwd: $cwd,
  hook_event_name: "PreToolUse",
  tool_name: "Bash",
  tool_input: { command: $cmd }
}')

# Run hook; capture stderr and exit code.
err=$(echo "$input" | "$HOOK_SCRIPT" 2>&1 >/dev/null)
rc=$?

if [[ $rc -ne 2 ]]; then
    echo "expected exit 2 (blocked), got $rc"
    echo "stderr was: $err"
    exit 1
fi

if ! grep -q "hg status" <<<"$err"; then
    echo "expected stderr to suggest 'hg status', got: $err"
    exit 1
fi