Merge pull request 'Hooks: PHPUnit in Pre-Commit (#67)' (#70) from feature/issue-67-phpunit-precommit into main
All checks were successful
Lint / PHP Syntax Check (push) Successful in 47s
PHPUnit / PHP Unit Tests (push) Successful in 56s
Lint / HTML Lint (htmlhint) (push) Successful in 1m28s
Lint / CSS Lint (stylelint) (push) Successful in 1m30s

This commit is contained in:
2026-06-04 02:41:15 +02:00
3 changed files with 85 additions and 13 deletions

View File

@@ -1 +1,3 @@
npx lint-staged # Delegiert an scripts/pre-commit-checks.sh
# (gleiche Logik wie safe-commit.sh-Safety-Net, nur einmalig)
./scripts/pre-commit-checks.sh

78
scripts/pre-commit-checks.sh Executable file
View File

@@ -0,0 +1,78 @@
#!/usr/bin/env bash
# pre-commit-checks.sh Pre-Commit Checks (Lint + PHPUnit)
# Wird vom Husky-Hook (.husky/pre-commit) und von scripts/safe-commit.sh aufgerufen.
#
# Abbruch mit Exit-Code != 0 bei Fehler.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$REPO_ROOT"
# ─────────────────────────────────────────────────────────────
# 1) lint-staged (HTML, CSS, JS, JSON, MD, PHP-Syntax)
# ─────────────────────────────────────────────────────────────
if command -v npx >/dev/null 2>&1 && [ -f "node_modules/.bin/lint-staged" ]; then
echo "🔍 Pre-Commit: lint-staged laeuft..."
if ! npx lint-staged; then
echo ""
echo "❌ Pre-Commit: lint-staged fehlgeschlagen. Commit abgebrochen."
exit 1
fi
echo "✅ Pre-Commit: lint-staged OK"
fi
# ─────────────────────────────────────────────────────────────
# 2) PHPUnit nur, wenn PHP-relevante Dateien gestaged sind
# - *.php Quellcode & Tests
# - phpunit.xml Test-Konfiguration
# - composer.json PHP-Abhaengigkeiten
# - composer.lock Lock-File
# ─────────────────────────────────────────────────────────────
# PHP-relevante Files (Array-basiert, robust gegen Spaces/Newlines in Filenames)
mapfile -t PHP_TOUCHED_ARR < <(git diff --cached --name-only --diff-filter=ACMR \
| grep -E '\.(php)$|^phpunit\.xml$|^composer\.(json|lock)$' || true)
if [ "${#PHP_TOUCHED_ARR[@]}" -gt 0 ]; then
echo ""
echo "==> PHP-Dateien geaendert -> PHPUnit wird ausgefuehrt"
echo " Betroffene Dateien:"
printf ' - %s\n' "${PHP_TOUCHED_ARR[@]}"
# Safety-Check: alle gestaged PHP-Dateien muessen auf der Disk existieren.
# Sonst wuerde PHPUnit eine inkonsistente Codebasis testen (Staged != Working Tree).
MISSING=()
for f in "${PHP_TOUCHED_ARR[@]}"; do
[ -f "$f" ] || MISSING+=("$f")
done
if [ "${#MISSING[@]}" -gt 0 ]; then
echo ""
echo "FEHLER: Gestaged PHP-Dateien existieren nicht auf der Disk:"
printf ' - %s\n' "${MISSING[@]}"
echo " Loesung: 'git restore --staged <datei>' oder Working-Tree synchronisieren."
exit 1
fi
# Composer-Deps nur installieren, falls noetig (Cache-Hit fuer wiederholte Commits)
if [ ! -f "vendor/bin/phpunit" ]; then
echo ""
echo "==> vendor/ fehlt -> composer install laeuft"
if ! command -v composer >/dev/null 2>&1; then
echo "FEHLER: 'composer' ist nicht installiert."
echo " Installation: https://getcomposer.org/download/"
exit 1
fi
composer install --no-interaction --prefer-dist --no-progress
fi
echo ""
echo "==> PHPUnit laeuft..."
if ! vendor/bin/phpunit; then
echo ""
echo "FEHLER: PHPUnit-Tests fehlgeschlagen. Commit abgebrochen."
echo " Behebe die Fehler und versuche es erneut."
exit 1
fi
echo ""
echo "==> PHPUnit OK"
fi

View File

@@ -2,7 +2,7 @@
# safe-commit.sh Commit with pre-commit hooks guaranteed to run # safe-commit.sh Commit with pre-commit hooks guaranteed to run
# Usage: ./scripts/safe-commit.sh "commit message" # Usage: ./scripts/safe-commit.sh "commit message"
# #
# This script ensures lint checks always execute, even when committing # This script ensures lint + PHPUnit checks always execute, even when committing
# from non-interactive contexts (CI, AI agents, etc.). # from non-interactive contexts (CI, AI agents, etc.).
set -euo pipefail set -euo pipefail
@@ -21,17 +21,9 @@ if [ -d ".husky" ]; then
git config core.hooksPath .husky git config core.hooksPath .husky
fi fi
# Run lint-staged manually as a safety net (in case hook is skipped) # Run pre-commit checks manually as a safety net (in case hook is skipped)
if command -v npx &>/dev/null && [ -f "node_modules/.bin/lint-staged" ]; then # Same logic as .husky/pre-commit, just in case the hook is bypassed.
echo "🔍 Running pre-commit lint checks..." ./scripts/pre-commit-checks.sh
npx lint-staged || {
echo ""
echo "❌ Lint checks failed. Commit aborted."
echo " Fix the errors above and try again."
exit 1
}
echo "✅ All lint checks passed."
fi
# Commit with hooks enabled (no --no-verify) # Commit with hooks enabled (no --no-verify)
git commit -m "$MSG" git commit -m "$MSG"