Files
gh-vanman2024-cli-builder-p…/skills/cli-testing-patterns/scripts/validate-test-coverage.sh
2025-11-30 09:04:14 +08:00

128 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
#
# Validate Test Coverage
#
# Checks that test coverage meets minimum thresholds
set -e
# Default thresholds
MIN_COVERAGE=${MIN_COVERAGE:-70}
echo "📊 Validating test coverage..."
# Detect project type
if [ -f "package.json" ]; then
PROJECT_TYPE="node"
elif [ -f "setup.py" ] || [ -f "pyproject.toml" ]; then
PROJECT_TYPE="python"
else
echo "❌ Error: Could not detect project type"
exit 1
fi
# Check coverage for Node.js projects
if [ "$PROJECT_TYPE" == "node" ]; then
echo "📦 Node.js project detected"
# Check if coverage data exists
if [ ! -d "coverage" ]; then
echo "❌ Error: No coverage data found"
echo " Run 'npm run test:coverage' first"
exit 1
fi
# Check if coverage summary exists
if [ ! -f "coverage/coverage-summary.json" ]; then
echo "❌ Error: coverage-summary.json not found"
exit 1
fi
# Extract coverage percentages using jq if available
if command -v jq &> /dev/null; then
LINES=$(jq '.total.lines.pct' coverage/coverage-summary.json)
STATEMENTS=$(jq '.total.statements.pct' coverage/coverage-summary.json)
FUNCTIONS=$(jq '.total.functions.pct' coverage/coverage-summary.json)
BRANCHES=$(jq '.total.branches.pct' coverage/coverage-summary.json)
echo ""
echo "Coverage Summary:"
echo " Lines: ${LINES}%"
echo " Statements: ${STATEMENTS}%"
echo " Functions: ${FUNCTIONS}%"
echo " Branches: ${BRANCHES}%"
echo ""
# Check thresholds
FAILED=0
if (( $(echo "$LINES < $MIN_COVERAGE" | bc -l) )); then
echo "❌ Lines coverage (${LINES}%) below threshold (${MIN_COVERAGE}%)"
FAILED=1
fi
if (( $(echo "$STATEMENTS < $MIN_COVERAGE" | bc -l) )); then
echo "❌ Statements coverage (${STATEMENTS}%) below threshold (${MIN_COVERAGE}%)"
FAILED=1
fi
if (( $(echo "$FUNCTIONS < $MIN_COVERAGE" | bc -l) )); then
echo "❌ Functions coverage (${FUNCTIONS}%) below threshold (${MIN_COVERAGE}%)"
FAILED=1
fi
if (( $(echo "$BRANCHES < $MIN_COVERAGE" | bc -l) )); then
echo "❌ Branches coverage (${BRANCHES}%) below threshold (${MIN_COVERAGE}%)"
FAILED=1
fi
if [ $FAILED -eq 1 ]; then
echo ""
echo "❌ Coverage validation failed"
exit 1
fi
echo "✅ Coverage thresholds met!"
else
echo "⚠️ jq not installed, skipping detailed validation"
echo " Install jq for detailed coverage validation"
fi
# Check coverage for Python projects
elif [ "$PROJECT_TYPE" == "python" ]; then
echo "🐍 Python project detected"
# Check if coverage data exists
if [ ! -f ".coverage" ]; then
echo "❌ Error: No coverage data found"
echo " Run 'pytest --cov' first"
exit 1
fi
# Generate coverage report
if command -v coverage &> /dev/null; then
echo ""
coverage report
# Get total coverage percentage
TOTAL_COVERAGE=$(coverage report | tail -1 | awk '{print $NF}' | sed 's/%//')
echo ""
echo "Total Coverage: ${TOTAL_COVERAGE}%"
echo "Minimum Required: ${MIN_COVERAGE}%"
# Compare coverage
if (( $(echo "$TOTAL_COVERAGE < $MIN_COVERAGE" | bc -l) )); then
echo ""
echo "❌ Coverage (${TOTAL_COVERAGE}%) below threshold (${MIN_COVERAGE}%)"
exit 1
fi
echo ""
echo "✅ Coverage thresholds met!"
else
echo "❌ Error: coverage tool not installed"
echo " Install with: pip install coverage"
exit 1
fi
fi
echo ""
echo "🎉 Coverage validation passed!"