#!/bin/bash set -euo pipefail # Test Coverage Enforcement Script # Validates that the coverage enforcement system works correctly 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: Check dependencies test_dependencies() { print_test "Checking dependencies" if command -v cargo-llvm-cov &> /dev/null; then pass "cargo-llvm-cov is installed" else fail "cargo-llvm-cov is not installed" fi if command -v jq &> /dev/null; then pass "jq is installed" else fail "jq is not installed" fi if command -v bc &> /dev/null; then pass "bc is installed" else fail "bc is not installed" fi } # Test 2: Verify script exists and is executable test_script_exists() { print_test "Verifying enforce_coverage.sh exists" if [ -f "scripts/enforce_coverage.sh" ]; then pass "enforce_coverage.sh exists" else fail "enforce_coverage.sh not found" return fi if [ -x "scripts/enforce_coverage.sh" ]; then pass "enforce_coverage.sh is executable" else fail "enforce_coverage.sh is not executable" fi } # Test 3: Verify workflow file test_workflow_exists() { print_test "Verifying coverage.yml workflow" if [ -f ".github/workflows/coverage.yml" ]; then pass "coverage.yml workflow exists" else fail "coverage.yml workflow not found" return fi # Check for key configurations if grep -q "MIN_COVERAGE: 60" .github/workflows/coverage.yml; then pass "Minimum coverage threshold is 60%" else fail "Minimum coverage threshold not set to 60%" fi if grep -q "TARGET_COVERAGE: 75" .github/workflows/coverage.yml; then pass "Target coverage is 75%" else fail "Target coverage not set to 75%" fi if grep -q "enforce_coverage.sh" .github/workflows/coverage.yml; then pass "Workflow uses enforce_coverage.sh" else fail "Workflow does not use enforce_coverage.sh" fi } # Test 4: Verify README badge test_readme_badge() { print_test "Verifying README.md coverage badge" if [ -f "README.md" ]; then pass "README.md exists" else fail "README.md not found" return fi if grep -iq "coverage.*img.shields.io" README.md; then pass "Coverage badge present in README.md" else fail "Coverage badge not found in README.md" fi if grep -q "60%.*minimum" README.md; then pass "Coverage thresholds documented in README.md" else fail "Coverage thresholds not documented in README.md" fi } # Test 5: Verify module coverage tracking test_module_tracking() { print_test "Verifying per-module coverage tracking" # Check workflow has module-coverage job if grep -q "module-coverage:" .github/workflows/coverage.yml; then pass "Module coverage job exists in workflow" else fail "Module coverage job not found in workflow" fi # Check for production modules local production_modules=("trading_engine" "risk" "api_gateway" "trading_service") for module in "${production_modules[@]}"; do if grep -q "$module" .github/workflows/coverage.yml; then pass "Production module $module tracked" else fail "Production module $module not tracked" fi done } # Test 6: Verify coverage trend tracking test_trend_tracking() { print_test "Verifying coverage trend tracking" if grep -q "coverage-trends:" .github/workflows/coverage.yml; then pass "Coverage trends job exists" else fail "Coverage trends job not found" fi if grep -q "coverage-history" .github/workflows/coverage.yml; then pass "Coverage history tracking configured" else fail "Coverage history tracking not configured" fi } # Test 7: Verify PR comment functionality test_pr_comments() { print_test "Verifying PR comment functionality" if grep -q "Comment PR with coverage" .github/workflows/coverage.yml; then pass "PR comment step exists" else fail "PR comment step not found" fi if grep -q "github-script" .github/workflows/coverage.yml; then pass "GitHub script for PR comments configured" else fail "GitHub script for PR comments not configured" fi } # Test 8: Dry run of coverage enforcement (fast check) test_dry_run() { print_test "Testing coverage enforcement script (dry run)" # Create a simple test environment mkdir -p /tmp/coverage_test cd /tmp/coverage_test # Mock basic structure cat > test.sh << 'EOF' #!/bin/bash # Test if script can be sourced for function tests source scripts/enforce_coverage.sh 2>/dev/null && echo "Script parseable" EOF if bash -n "$OLDPWD/scripts/enforce_coverage.sh"; then pass "Script has valid bash syntax" else fail "Script has syntax errors" fi cd - > /dev/null rm -rf /tmp/coverage_test } # Test 9: Verify artifact generation test_artifacts() { print_test "Verifying artifact generation configuration" local artifacts=( "html-coverage-report" "lcov-report" "json-reports" "coverage-summary" ) for artifact in "${artifacts[@]}"; do if grep -q "name: $artifact" .github/workflows/coverage.yml; then pass "Artifact $artifact configured" else fail "Artifact $artifact not configured" fi done } # Test 10: Verify coverage thresholds in script test_script_thresholds() { print_test "Verifying thresholds in enforcement script" if grep -q "MIN_COVERAGE=60" scripts/enforce_coverage.sh; then pass "Script has MIN_COVERAGE=60" else fail "Script does not have MIN_COVERAGE=60" fi if grep -q "TARGET_COVERAGE=75" scripts/enforce_coverage.sh; then pass "Script has TARGET_COVERAGE=75" else fail "Script does not have TARGET_COVERAGE=75" fi if grep -q "PRODUCTION_COVERAGE=75" scripts/enforce_coverage.sh; then pass "Script has PRODUCTION_COVERAGE=75" else fail "Script does not have PRODUCTION_COVERAGE=75" fi } # Main test execution main() { echo -e "${BLUE}======================================${NC}" echo -e "${BLUE} Coverage Enforcement Test Suite${NC}" echo -e "${BLUE}======================================${NC}" echo "" test_dependencies test_script_exists test_workflow_exists test_readme_badge test_module_tracking test_trend_tracking test_pr_comments test_dry_run test_artifacts test_script_thresholds echo "" echo -e "${BLUE}======================================${NC}" echo -e "${BLUE} 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 tests passed!${NC}" echo -e "${GREEN}Coverage enforcement system is ready.${NC}" exit 0 else echo "" echo -e "${RED}✗ Some tests failed.${NC}" echo -e "${YELLOW}Please fix the issues above.${NC}" exit 1 fi } main "$@"