#!/usr/bin/env bash # Track mutation testing scores over time set -euo pipefail # Configuration HISTORY_FILE="mutation_history.json" TEMP_FILE="mutants.out/outcomes.json" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo "🧬 Running mutation testing..." echo "" # Run mutation tests if ! cargo mutants --no-shuffle --test-tool nextest --output-json; then echo "" echo -e "${RED}❌ Mutation testing failed${NC}" exit 1 fi echo "" echo "📊 Analyzing results..." # Check if outcomes.json exists if [ ! -f "$TEMP_FILE" ]; then echo -e "${RED}❌ No mutation results found${NC}" exit 1 fi # Extract stats from outcomes.json using jq TOTAL_MUTANTS=$(jq '[.outcomes[] | select(.scenario != "Baseline")] | length' "$TEMP_FILE") CAUGHT=$(jq '[.outcomes[] | select(.scenario != "Baseline") | select(.summary == "CaughtMutant")] | length' "$TEMP_FILE") MISSED=$(jq '[.outcomes[] | select(.scenario != "Baseline") | select(.summary == "MissedMutant")] | length' "$TEMP_FILE") TIMEOUT=$(jq '[.outcomes[] | select(.scenario != "Baseline") | select(.summary == "Timeout")] | length' "$TEMP_FILE") UNVIABLE=$(jq '[.outcomes[] | select(.scenario != "Baseline") | select(.summary == "Unviable")] | length' "$TEMP_FILE") # Calculate viable mutants (total - unviable) VIABLE=$((TOTAL_MUTANTS - UNVIABLE)) # Calculate mutation score (caught / viable * 100) if [ "$VIABLE" -eq 0 ]; then SCORE=0 else SCORE=$(echo "scale=2; $CAUGHT * 100 / $VIABLE" | bc) fi # Get current timestamp and git info TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") GIT_COMMIT=$(git rev-parse HEAD 2>/dev/null || echo "unknown") GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown") # Create history entry ENTRY=$(cat < "$HISTORY_FILE" fi # Append entry to history jq ". += [$ENTRY]" "$HISTORY_FILE" > "${HISTORY_FILE}.tmp" mv "${HISTORY_FILE}.tmp" "$HISTORY_FILE" # Display results echo "" echo "═══════════════════════════════════════════════════════" echo " MUTATION TESTING RESULTS" echo "═══════════════════════════════════════════════════════" echo "" echo "Total mutants: $TOTAL_MUTANTS" echo "├─ Viable: $VIABLE" echo "├─ Caught: $CAUGHT ($(echo "scale=1; $CAUGHT * 100 / $VIABLE" | bc)%)" echo "├─ Missed: $MISSED" echo "├─ Timeout: $TIMEOUT" echo "└─ Unviable: $UNVIABLE" echo "" # Color the score based on value if (( $(echo "$SCORE >= 90" | bc -l) )); then COLOR=$GREEN STATUS="✅ EXCELLENT" elif (( $(echo "$SCORE >= 80" | bc -l) )); then COLOR=$YELLOW STATUS="⚠️ GOOD" else COLOR=$RED STATUS="❌ NEEDS IMPROVEMENT" fi echo -e "${COLOR}Mutation Score: ${SCORE}% ${STATUS}${NC}" echo "" # Show previous score if available PREV_SCORE=$(jq -r '.[-2].stats.score // empty' "$HISTORY_FILE" 2>/dev/null || echo "") if [ -n "$PREV_SCORE" ]; then DIFF=$(echo "scale=2; $SCORE - $PREV_SCORE" | bc) if (( $(echo "$DIFF > 0" | bc -l) )); then echo -e "${GREEN}⬆ Improved by ${DIFF}% from previous run${NC}" elif (( $(echo "$DIFF < 0" | bc -l) )); then echo -e "${RED}⬇ Decreased by ${DIFF#-}% from previous run${NC}" else echo "➡ No change from previous run" fi echo "" fi echo "═══════════════════════════════════════════════════════" echo "" echo "📝 Results saved to: $HISTORY_FILE" echo "🔬 Full report: mutants.out/" echo "" # Exit with failure if score is below threshold THRESHOLD=85 if (( $(echo "$SCORE < $THRESHOLD" | bc -l) )); then echo -e "${RED}⚠️ Mutation score ($SCORE%) is below threshold ($THRESHOLD%)${NC}" exit 1 fi echo -e "${GREEN}✅ Mutation testing passed!${NC}"