set -e
echo "๐ Running pre-commit checks..."
echo " ๐ Checking code formatting..."
cargo fmt --check --quiet || {
echo "โ Run 'cargo fmt' to fix formatting"
exit 1
}
echo " ๐ Running clippy..."
cargo clippy --all-targets --quiet -- -D warnings || {
echo "โ Fix clippy warnings"
exit 1
}
echo " ๐งช Running fast tests..."
cargo test --lib --bins --quiet || {
echo "โ Tests failed"
exit 1
}
SKIP_COVERAGE="${SKIP_COVERAGE:-0}"
if [ "$SKIP_COVERAGE" != "1" ]; then
echo " ๐ Checking coverage threshold (90%)..."
if command -v cargo-llvm-cov >/dev/null 2>&1; then
if [ -f ~/.cargo/config.toml ]; then
mv ~/.cargo/config.toml ~/.cargo/config.toml.cov-backup
fi
cargo llvm-cov clean --workspace 2>/dev/null || true
COVERAGE_OUTPUT="$(cargo llvm-cov --ignore-filename-regex '(gpu_|main\.rs)' nextest --no-tests=warn --lib --bins 2>&1)" || true
if [ -f ~/.cargo/config.toml.cov-backup ]; then
mv ~/.cargo/config.toml.cov-backup ~/.cargo/config.toml
fi
COVERAGE="$(printf '%s\n' "$COVERAGE_OUTPUT" | grep '^TOTAL' | awk '{print $10}' | tr -d '%')"
if [ -n "$COVERAGE" ]; then
echo " Line coverage: ${COVERAGE}%"
BELOW_THRESHOLD="$(printf '%s < 90\n' "$COVERAGE" | bc -l)"
if [ "$BELOW_THRESHOLD" = "1" ]; then
echo "โ Coverage ${COVERAGE}% is below 90% threshold"
echo " Run 'make coverage' for detailed report"
exit 1
fi
else
echo "โ ๏ธ Could not determine coverage (skipping check)"
fi
else
echo "โ ๏ธ cargo-llvm-cov not installed (skipping coverage check)"
echo " Install with: cargo install cargo-llvm-cov"
fi
fi
echo "โ
All pre-commit checks passed!"