Skip to content

Commit 5d8f725

Browse files
committed
βš™οΈ ci: add coverage check with margin tolerance
β€’ πŸ› οΈ Replace strict coverage --fail-under with custom script β€’ πŸ“Š Use MIN_COVERAGE and MARGIN env vars for flexible thresholding β€’ 🚦 Fail only if coverage < (MIN_COVERAGE - COVERAGE_TOLERANCE_MARGIN), warn if within margin β€’ πŸ”§ Improve CI stability by allowing minor coverage fluctuations
1 parent e838a14 commit 5d8f725

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

β€Ž.github/workflows/1_tests.ymlβ€Ž

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ permissions: {} # deny all by default
1414
env:
1515
UV_SYSTEM_PYTHON: 1
1616
MIN_COVERAGE: 70
17+
COVERAGE_TOLERANCE_MARGIN: 5
1718

1819
jobs:
1920
filter:
@@ -102,11 +103,7 @@ jobs:
102103
run: uv sync --locked --extra coverage
103104

104105
- name: Combine and report coverage
105-
run: |
106-
uv run coverage combine
107-
uv run coverage report --format=markdown >> $GITHUB_STEP_SUMMARY
108-
uv run coverage report --fail-under=$MIN_COVERAGE
109-
106+
run: ./scripts/check_coverage.sh
110107
check:
111108
name: Did all tests pass?
112109
if: always()

β€Žscripts/check_coverage.shβ€Ž

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
MIN_COVERAGE="${MIN_COVERAGE:-70}"
5+
COVERAGE_TOLERANCE_MARGIN="${COVERAGE_TOLERANCE_MARGIN:-5}"
6+
7+
# Combine coverage files if multiple exist
8+
if compgen -G ".coverage.*" > /dev/null; then
9+
echo "πŸ”„ Found multiple coverage files, combining..."
10+
uv run coverage combine
11+
else
12+
echo "ℹ️ No coverage files to combine."
13+
fi
14+
15+
# Add markdown report to GitHub summary if available
16+
if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then
17+
uv run coverage report --format=markdown >> "$GITHUB_STEP_SUMMARY"
18+
fi
19+
20+
# Extract total coverage percentage (last column) - more robust
21+
COVERAGE_LINE=$(uv run coverage report | grep TOTAL || true)
22+
if [[ -z "$COVERAGE_LINE" ]]; then
23+
echo "❌ Failed to extract coverage data"
24+
exit 1
25+
fi
26+
27+
COVERAGE=$(echo "$COVERAGE_LINE" | awk '{print $NF}' | tr -d '%' || echo "0")
28+
29+
echo "πŸ“Š Reported coverage: ${COVERAGE}%"
30+
echo "πŸ”’ Minimum required: ${MIN_COVERAGE}%"
31+
echo "βž– Tolerance margin: ${COVERAGE_TOLERANCE_MARGIN}%"
32+
33+
if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then
34+
{
35+
echo "### πŸ§ͺ Coverage Check"
36+
echo "- Total coverage: **${COVERAGE}%**"
37+
echo "- Minimum required: **${MIN_COVERAGE}%**"
38+
echo "- Allowed margin: **${COVERAGE_TOLERANCE_MARGIN}%**"
39+
} >> "$GITHUB_STEP_SUMMARY"
40+
fi
41+
42+
# Check coverage against threshold with margin
43+
if (( $(echo "$COVERAGE < $MIN_COVERAGE - $COVERAGE_TOLERANCE_MARGIN" | bc -l) )); then
44+
echo "❌ Coverage ${COVERAGE}% is below allowed margin. Failing..."
45+
exit 1
46+
elif (( $(echo "$COVERAGE < $MIN_COVERAGE" | bc -l) )); then
47+
echo "⚠️ Coverage ${COVERAGE}% is below threshold but within margin."
48+
else
49+
echo "βœ… Coverage ${COVERAGE}% meets the threshold."
50+
fi
51+
52+
# Clean up coverage files after reporting
53+
rm -f .coverage.* .coverage

0 commit comments

Comments
Β (0)