diff --git a/.github/workflows/shellcheck.yml b/.github/workflows/shellcheck.yml index 8be950c..6e96ae7 100644 --- a/.github/workflows/shellcheck.yml +++ b/.github/workflows/shellcheck.yml @@ -20,10 +20,13 @@ jobs: - name: Run ShellCheck on all shell scripts run: | - shellcheck pre-commit commit-msg pre-push post-checkout post-commit post-merge install.sh uninstall.sh test/install_test.sh test/commit_msg_test.sh + shellcheck pre-commit commit-msg pre-push post-checkout post-commit post-merge install.sh uninstall.sh test/install_test.sh test/commit_msg_test.sh test/pre_commit_test.sh - name: Run install script regression tests run: bash test/install_test.sh - name: Run commit message regression tests run: bash test/commit_msg_test.sh + + - name: Run pre-commit regression tests + run: bash test/pre_commit_test.sh diff --git a/test/pre_commit_test.sh b/test/pre_commit_test.sh new file mode 100644 index 0000000..8ea934e --- /dev/null +++ b/test/pre_commit_test.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash + +set -euo pipefail + +repo_root="$(cd "$(dirname "$0")/.." && pwd)" +pre_commit="$repo_root/pre-commit" +tmpdir="$(mktemp -d)" +trap 'rm -rf "$tmpdir"' EXIT + +run() { + local expected_status="$1" + shift + set +e + output=$("$@" 2>&1) + status=$? + set -e + if [ "$status" -ne "$expected_status" ]; then + echo "expected exit $expected_status, got $status" >&2 + echo "$output" >&2 + exit 1 + fi + printf '%s' "$output" +} + +assert_contains() { + local haystack="$1" + local needle="$2" + if ! printf '%s' "$haystack" | grep -Fq "$needle"; then + echo "expected output to contain: $needle" >&2 + echo "$haystack" >&2 + exit 1 + fi +} + +cd "$tmpdir" +git init --quiet +git config user.email test@example.com +git config user.name "Test User" + +printf 'small\n' > small.txt +git add small.txt +run 0 bash "$pre_commit" +git commit --quiet -m "test: add small file" + +rm small.txt +git add small.txt +run 0 bash "$pre_commit" +git commit --quiet -m "test: remove small file" + +dd if=/dev/zero of=large.bin bs=1M count=6 status=none +git add large.bin +output=$(run 1 bash "$pre_commit") +assert_contains "$output" "File large.bin is 6MiB" +assert_contains "$output" "Commit too large" + +echo "pre-commit regression tests passed"