diff --git a/.github/workflows/unit-test-and-coverage-gate.yml b/.github/workflows/unit-test-and-coverage-gate.yml index 9e09247c..62d386a4 100644 --- a/.github/workflows/unit-test-and-coverage-gate.yml +++ b/.github/workflows/unit-test-and-coverage-gate.yml @@ -46,8 +46,15 @@ jobs: INPUT_COV_THRESHOLD: ${{ inputs.cov_threshold }} run: | # Read threshold from file, allow manual override - FILE_THRESHOLD=$(cat .coverage-threshold 2>/dev/null || echo "65.0") + # Use fallback 65.0 if file is missing, empty, or unreadable + FILE_THRESHOLD=$(cat .coverage-threshold 2>/dev/null | tr -d '[:space:]') + FILE_THRESHOLD="${FILE_THRESHOLD:-65.0}" COV_THRESHOLD="${INPUT_COV_THRESHOLD:-$FILE_THRESHOLD}" + # Validate it's a number, otherwise use fallback + if ! [[ "$COV_THRESHOLD" =~ ^[0-9]+\.?[0-9]*$ ]]; then + echo "::warning::Invalid threshold '$COV_THRESHOLD', using 65.0" + COV_THRESHOLD="65.0" + fi echo "cov_threshold=${COV_THRESHOLD}" >> "$GITHUB_OUTPUT" echo "build_id=${GITHUB_RUN_ID}" >> "$GITHUB_OUTPUT" if [[ -n "${INPUT_COV_THRESHOLD}" ]]; then @@ -89,7 +96,8 @@ jobs: # Extract numeric values using simpler patterns OVERALL=$(grep "Overall Coverage:" coverage_report.txt | sed 's/[^0-9.]*\([0-9.]\+\)%.*/\1/')% THRESHOLD=$(grep "Threshold:" coverage_report.txt | sed 's/[^0-9.]*\([0-9.]\+\)%.*/\1/')% - STATUS=$(grep "Status:" coverage_report.txt | sed 's/[^A-Z]*\([A-Z]\+\).*/\1/') + # Get last word on the Status line (PASSED or FAILED) + STATUS=$(grep "Status:" coverage_report.txt | awk '{print $NF}') # Status badge if [[ "$STATUS" == "PASSED" ]]; then diff --git a/Earthfile b/Earthfile index 6412ea5e..09a1d379 100644 --- a/Earthfile +++ b/Earthfile @@ -153,9 +153,13 @@ test: # Run the comprehensive coverage tests using our script # Args: COV_THRESHOLD PRINT_TS FAIL_ON_NO_TESTS DEBUG - # If COV_THRESHOLD not provided, read from .coverage-threshold file + # If COV_THRESHOLD not provided or empty, read from .coverage-threshold file RUN cd /work && \ - THRESHOLD="${COV_THRESHOLD:-$(cat .coverage-threshold 2>/dev/null || echo 65.0)}" && \ + FILE_THRESH=$(cat .coverage-threshold 2>/dev/null | tr -d '[:space:]') && \ + FILE_THRESH="${FILE_THRESH:-65.0}" && \ + THRESHOLD="${COV_THRESHOLD:-$FILE_THRESH}" && \ + THRESHOLD="${THRESHOLD:-65.0}" && \ + echo "Using coverage threshold: ${THRESHOLD}%" && \ ./scripts/run_coverage_tests.sh "${THRESHOLD}" "${PRINT_TS}" "${FAIL_ON_NO_TESTS}" # Save coverage artifacts locally @@ -176,9 +180,13 @@ test-debug: # Run the coverage tests with debug output (keeps temp files for inspection) # Args: COV_THRESHOLD PRINT_TS FAIL_ON_NO_TESTS DEBUG - # If COV_THRESHOLD not provided, read from .coverage-threshold file + # If COV_THRESHOLD not provided or empty, read from .coverage-threshold file RUN cd /work && \ - THRESHOLD="${COV_THRESHOLD:-$(cat .coverage-threshold 2>/dev/null || echo 65.0)}" && \ + FILE_THRESH=$(cat .coverage-threshold 2>/dev/null | tr -d '[:space:]') && \ + FILE_THRESH="${FILE_THRESH:-65.0}" && \ + THRESHOLD="${COV_THRESHOLD:-$FILE_THRESH}" && \ + THRESHOLD="${THRESHOLD:-65.0}" && \ + echo "Using coverage threshold: ${THRESHOLD}%" && \ ./scripts/run_coverage_tests.sh "${THRESHOLD}" "${PRINT_TS}" "${FAIL_ON_NO_TESTS}" "true" # Save coverage artifacts locally diff --git a/scripts/run_coverage_tests.sh b/scripts/run_coverage_tests.sh index e3180a35..3c03f10d 100755 --- a/scripts/run_coverage_tests.sh +++ b/scripts/run_coverage_tests.sh @@ -433,19 +433,23 @@ echo "Calculation method: ${COVERAGE_METHOD}" COVERAGE_NORMALIZED=$(printf '%.1f' "${OVERALL_COVERAGE}") THRESHOLD_NORMALIZED=$(printf '%.1f' "${COV_THRESHOLD}") +# Track coverage status separately from test status +COVERAGE_PASSED="true" if (( $(echo "${COVERAGE_NORMALIZED} >= ${THRESHOLD_NORMALIZED}" | bc -l) )); then echo -e "${GREEN}✓ Overall coverage PASSED threshold${NC}" else echo -e "${RED}✗ Overall coverage FAILED threshold${NC}" + COVERAGE_PASSED="false" OVERALL_EXIT_CODE=1 fi # Generate coverage reports for saving +# Note: Status reflects COVERAGE check only, not test failures echo "## Test Coverage Report" > coverage_report.txt echo "" >> coverage_report.txt echo "**Overall Coverage:** ${COVERAGE_NORMALIZED}%" >> coverage_report.txt echo "**Threshold:** ${THRESHOLD_NORMALIZED}% (applies to overall coverage only)" >> coverage_report.txt -echo "**Status:** $(if [[ ${OVERALL_EXIT_CODE} -eq 0 ]]; then echo "PASSED"; else echo "FAILED"; fi)" >> coverage_report.txt +echo "**Status:** $(if [[ "${COVERAGE_PASSED}" == "true" ]]; then echo "PASSED"; else echo "FAILED"; fi)" >> coverage_report.txt echo "" >> coverage_report.txt # FIXED: Use safer array check that works with set -u