#!/bin/bash set -euo pipefail # Edge Case Tests for Coverage Enforcement Script # Tests floating-point comparisons, missing dependencies, empty reports, etc. RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' TESTS_PASSED=0 TESTS_FAILED=0 print_test() { printf "${BLUE}[TEST]${NC} %s\n" "$1" } pass() { printf "${GREEN}✓ PASS${NC} %s\n" "$1" TESTS_PASSED=$((TESTS_PASSED + 1)) } fail() { printf "${RED}✗ FAIL${NC} %s\n" "$1" TESTS_FAILED=$((TESTS_FAILED + 1)) } # Test 1: Floating-point comparison with bc -l test_floating_point_comparison() { print_test "Floating-point comparison accuracy" # Test edge cases for coverage comparisons local test_cases=( "59.9 60 1" # Just below threshold (should be less) "60.0 60 0" # Exactly at threshold (should NOT be less) "60.1 60 0" # Just above threshold (should NOT be less) "74.9 75 1" # Just below target (should be less) "75.0 75 0" # Exactly at target (should NOT be less) "0.0 60 1" # Zero coverage (should be less) "100.0 60 0" # Perfect coverage (should NOT be less) ) for test_case in "${test_cases[@]}"; do local value=$(echo "$test_case" | cut -d' ' -f1) local threshold=$(echo "$test_case" | cut -d' ' -f2) local expected=$(echo "$test_case" | cut -d' ' -f3) local result=0 if (( $(echo "$value < $threshold" | bc -l) )); then result=1 fi if [ "$result" -eq "$expected" ]; then pass "Comparison: $value < $threshold = $result (expected $expected)" else fail "Comparison: $value < $threshold = $result (expected $expected)" fi done } # Test 2: Handle missing llvm-cov gracefully test_missing_llvm_cov() { print_test "Missing llvm-cov dependency handling" # Test that the enforcement script checks for cargo-llvm-cov if grep -q "command -v cargo-llvm-cov" scripts/enforce_coverage.sh; then pass "Script checks for cargo-llvm-cov availability" else fail "Script does not check for cargo-llvm-cov" return fi # Test that script exits on missing dependency if grep -A 3 "command -v cargo-llvm-cov" scripts/enforce_coverage.sh | grep -q "exit 1"; then pass "Script exits gracefully when cargo-llvm-cov is missing" else fail "Script does not exit when cargo-llvm-cov is missing" fi # Test for user-friendly error message if grep -A 3 "cargo-llvm-cov not found" scripts/enforce_coverage.sh | grep -q "Install with"; then pass "Script provides installation instructions for missing dependency" else fail "Script does not provide installation instructions" fi } # Test 3: Handle empty coverage reports test_empty_coverage_report() { print_test "Empty coverage report handling" # Create a temporary empty JSON file local temp_json="/tmp/empty_coverage.json" echo '{"data":[]}' > "$temp_json" # Test extraction from empty report local lines_covered=$(jq '.data[0].totals.lines.covered' "$temp_json" 2>/dev/null || echo "0") local lines_total=$(jq '.data[0].totals.lines.count' "$temp_json" 2>/dev/null || echo "1") if [ "$lines_covered" == "null" ] || [ "$lines_covered" == "0" ]; then pass "Empty report handled without crash (coverage=$lines_covered)" else fail "Empty report not handled properly" fi rm "$temp_json" } # Test 4: Handle malformed JSON gracefully test_malformed_json() { print_test "Malformed JSON handling" # Create a malformed JSON file local temp_json="/tmp/malformed.json" echo '{invalid json' > "$temp_json" # Test jq error handling if jq '.data[0].totals.lines.covered' "$temp_json" 2>/dev/null; then fail "Malformed JSON not detected" else pass "Malformed JSON detected and handled" fi rm "$temp_json" } # Test 5: Handle division by zero test_division_by_zero() { print_test "Division by zero protection" # Test with zero total lines local lines_covered=0 local lines_total=0 # The script should check for zero before dividing if [ "$lines_total" == "0" ]; then pass "Zero division prevented (total lines = 0)" else fail "Zero division check failed" fi # Test bc division by zero local result=$(echo "scale=2; 10 / 1" | bc 2>/dev/null || echo "ERROR") if [ "$result" != "ERROR" ]; then pass "bc calculation works with valid divisor" else fail "bc calculation failed with valid divisor" fi } # Test 6: Module-level threshold validation test_module_threshold_validation() { print_test "Module-level threshold validation" # Production modules should have 75% threshold local production_modules=( "trading_engine" "risk" "config" "common" "services/trading_service" "services/api_gateway" ) # Test threshold assignment logic for module in "${production_modules[@]}"; do local is_production=false # Check if module matches production pattern (simulating script logic) for prod_module in "${production_modules[@]}"; do if [[ "$module" == *"$prod_module"* ]]; then is_production=true break fi done if [ "$is_production" = true ]; then pass "Module $module correctly identified as production" else fail "Module $module not identified as production" fi done } # Test 7: Handle extremely large coverage values test_large_coverage_values() { print_test "Large coverage value handling" # Test with coverage > 100% (shouldn't happen, but let's be defensive) local test_values=("99.99" "100.00" "100.01") for value in "${test_values[@]}"; do # Check if bc can handle the comparison local result=$(echo "$value < 60" | bc -l) if [ "$result" == "0" ]; then pass "Large value $value handled correctly" else fail "Large value $value caused comparison error" fi done } # Test 8: Handle negative coverage values test_negative_coverage_values() { print_test "Negative coverage value handling" # Test with negative values (shouldn't happen, but let's be defensive) local test_values=("-1.0" "0.0") for value in "${test_values[@]}"; do local result=$(echo "$value < 60" | bc -l) if [ "$result" == "1" ]; then pass "Value $value correctly identified as below threshold" else fail "Value $value comparison failed" fi done } # Test 9: Validate JSON output structure test_json_structure() { print_test "JSON output structure validation" # Create a sample module report local temp_json="/tmp/module_test.json" cat > "$temp_json" << 'EOF' [ { "package": "test_package", "coverage": 65.5, "threshold": 60, "is_production": false, "status": "PASS" } ] EOF # Validate JSON structure if jq empty "$temp_json" 2>/dev/null; then pass "JSON structure is valid" else fail "JSON structure is invalid" fi # Validate required fields local required_fields=("package" "coverage" "threshold" "is_production" "status") for field in "${required_fields[@]}"; do local has_field=$(jq -r ".[0] | has(\"$field\")" "$temp_json" 2>/dev/null) if [ "$has_field" == "true" ]; then pass "Required field '$field' present" else fail "Required field '$field' missing" fi done rm "$temp_json" } # Test 10: Validate bc availability and functionality test_bc_availability() { print_test "bc calculator availability" if command -v bc &> /dev/null; then pass "bc is installed" else fail "bc is not installed (required for floating-point math)" return fi # Test bc -l (library mode) functionality local result=$(echo "scale=2; 10 / 3" | bc -l) if [[ "$result" =~ ^3\.3 ]]; then pass "bc -l works correctly for floating-point division" else fail "bc -l not working correctly (got: $result)" fi # Test bc comparison if (( $(echo "5.5 < 6.0" | bc -l) )); then pass "bc comparison works correctly" else fail "bc comparison not working" fi } # Test 11: Validate script error handling test_error_handling() { print_test "Script error handling" # Test that script uses set -euo pipefail if grep -q "set -euo pipefail" scripts/enforce_coverage.sh; then pass "Script uses strict error handling (set -euo pipefail)" else fail "Script missing strict error handling" fi # Test for proper exit codes if grep -q "exit 1" scripts/enforce_coverage.sh; then pass "Script has explicit error exit codes" else fail "Script missing explicit error exit codes" fi } # Test 12: Validate output file creation test_output_files() { print_test "Output file validation" local required_outputs=( "COVERAGE_JSON=\"coverage_report.json\"" "COVERAGE_HTML=\"coverage_html\"" "MODULE_REPORT=\"module_coverage.json\"" ) for output in "${required_outputs[@]}"; do if grep -q "$output" scripts/enforce_coverage.sh; then pass "Output file defined: $output" else fail "Output file not defined: $output" fi done } # Test 13: Validate color output test_color_output() { print_test "Color output validation" local colors=("RED" "GREEN" "YELLOW" "BLUE" "NC") for color in "${colors[@]}"; do if grep -q "^$color=" scripts/enforce_coverage.sh; then pass "Color variable $color defined" else fail "Color variable $color not defined" fi done } # Test 14: Validate workspace member parsing test_workspace_parsing() { print_test "Workspace member parsing" # Test that the script can parse workspace members if grep -q "cargo metadata --no-deps" scripts/enforce_coverage.sh; then pass "Script uses cargo metadata for workspace parsing" else fail "Script missing cargo metadata usage" fi # Test for proper jq parsing if grep -q "jq -r '.workspace_members" scripts/enforce_coverage.sh; then pass "Script uses jq to parse workspace members" else fail "Script missing jq workspace member parsing" fi } # Test 15: Validate timeout handling test_timeout_handling() { print_test "Timeout handling" # Check for timeout parameter in coverage command if grep -q "timeout" scripts/enforce_coverage.sh; then pass "Script includes timeout handling" else fail "Script missing timeout handling" fi # Verify timeout value is reasonable (10 minutes = 600 seconds) if grep -q "timeout 600" scripts/enforce_coverage.sh; then pass "Timeout value is reasonable (600 seconds)" else fail "Timeout value not found or unreasonable" fi } # Main test execution main() { echo -e "${BLUE}========================================${NC}" echo -e "${BLUE} Coverage Edge Case Test Suite${NC}" echo -e "${BLUE}========================================${NC}" echo "" test_floating_point_comparison test_missing_llvm_cov test_empty_coverage_report test_malformed_json test_division_by_zero test_module_threshold_validation test_large_coverage_values test_negative_coverage_values test_json_structure test_bc_availability test_error_handling test_output_files test_color_output test_workspace_parsing test_timeout_handling echo "" echo -e "${BLUE}========================================${NC}" echo -e "${BLUE} Edge Case Test Results${NC}" echo -e "${BLUE}========================================${NC}" echo -e "${GREEN}Passed: $TESTS_PASSED${NC}" echo -e "${RED}Failed: $TESTS_FAILED${NC}" if [ $TESTS_FAILED -eq 0 ]; then echo "" echo -e "${GREEN}✓ All edge case tests passed!${NC}" echo -e "${GREEN}Coverage enforcement is robust.${NC}" exit 0 else echo "" echo -e "${RED}✗ Some edge case tests failed.${NC}" echo -e "${YELLOW}Please review the failures above.${NC}" exit 1 fi } main "$@"