|
22 | 22 | #---------------------------------------------- |
23 | 23 | - name: Check out repository |
24 | 24 | uses: actions/checkout@v4 |
| 25 | + with: |
| 26 | + fetch-depth: 0 # Needed for coverage comparison |
| 27 | + ref: ${{ github.event.pull_request.head.ref || github.ref_name }} |
| 28 | + repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }} |
25 | 29 | - name: Set up python |
26 | 30 | id: setup-python |
27 | 31 | uses: actions/setup-python@v5 |
|
50 | 54 | # install dependencies if cache does not exist |
51 | 55 | #---------------------------------------------- |
52 | 56 | - name: Install dependencies |
| 57 | + if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true' |
| 58 | + run: poetry install --no-interaction --no-root |
| 59 | + #---------------------------------------------- |
| 60 | + # install your root project, if required |
| 61 | + #---------------------------------------------- |
| 62 | + - name: Install library |
53 | 63 | run: poetry install --no-interaction --all-extras |
54 | 64 | #---------------------------------------------- |
55 | | - # run test suite |
| 65 | + # run all tests with coverage |
| 66 | + #---------------------------------------------- |
| 67 | + - name: Run tests with coverage |
| 68 | + run: | |
| 69 | + poetry run python -m pytest \ |
| 70 | + tests/unit tests/e2e \ |
| 71 | + --cov=src --cov-report=xml --cov-report=term -v |
| 72 | + #---------------------------------------------- |
| 73 | + # check for coverage override |
| 74 | + #---------------------------------------------- |
| 75 | + - name: Check for coverage override |
| 76 | + id: override |
| 77 | + run: | |
| 78 | + OVERRIDE_COMMENT=$(echo "${{ github.event.pull_request.body }}" | grep -E "SKIP_COVERAGE_CHECK\s*=" || echo "") |
| 79 | + if [ -n "$OVERRIDE_COMMENT" ]; then |
| 80 | + echo "override=true" >> $GITHUB_OUTPUT |
| 81 | + REASON=$(echo "$OVERRIDE_COMMENT" | sed -E 's/.*SKIP_COVERAGE_CHECK\s*=\s*(.+)/\1/') |
| 82 | + echo "reason=$REASON" >> $GITHUB_OUTPUT |
| 83 | + echo "Coverage override found in PR description: $REASON" |
| 84 | + else |
| 85 | + echo "override=false" >> $GITHUB_OUTPUT |
| 86 | + echo "No coverage override found" |
| 87 | + fi |
| 88 | + #---------------------------------------------- |
| 89 | + # check coverage percentage |
| 90 | + #---------------------------------------------- |
| 91 | + - name: Check coverage percentage |
| 92 | + if: steps.override.outputs.override == 'false' |
| 93 | + run: | |
| 94 | + COVERAGE_FILE="coverage.xml" |
| 95 | + if [ ! -f "$COVERAGE_FILE" ]; then |
| 96 | + echo "ERROR: Coverage file not found at $COVERAGE_FILE" |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | + |
| 100 | + # Install xmllint if not available |
| 101 | + if ! command -v xmllint &> /dev/null; then |
| 102 | + sudo apt-get update && sudo apt-get install -y libxml2-utils |
| 103 | + fi |
| 104 | + |
| 105 | + COVERED=$(xmllint --xpath "string(//coverage/@lines-covered)" "$COVERAGE_FILE") |
| 106 | + TOTAL=$(xmllint --xpath "string(//coverage/@lines-valid)" "$COVERAGE_FILE") |
| 107 | + PERCENTAGE=$(python3 -c "covered=${COVERED}; total=${TOTAL}; print(round((covered/total)*100, 2))") |
| 108 | + |
| 109 | + echo "Branch Coverage: $PERCENTAGE%" |
| 110 | + echo "Required Coverage: 85%" |
| 111 | + |
| 112 | + # Use Python to compare the coverage with 85 |
| 113 | + python3 -c "import sys; sys.exit(0 if float('$PERCENTAGE') >= 85 else 1)" |
| 114 | + if [ $? -eq 1 ]; then |
| 115 | + echo "ERROR: Coverage is $PERCENTAGE%, which is less than the required 85%" |
| 116 | + exit 1 |
| 117 | + else |
| 118 | + echo "SUCCESS: Coverage is $PERCENTAGE%, which meets the required 85%" |
| 119 | + fi |
| 120 | + |
| 121 | + #---------------------------------------------- |
| 122 | + # coverage enforcement summary |
56 | 123 | #---------------------------------------------- |
57 | | - - name: Run e2e tests |
58 | | - run: poetry run python -m pytest tests/e2e |
| 124 | + - name: Coverage enforcement summary |
| 125 | + run: | |
| 126 | + if [ "${{ steps.override.outputs.override }}" == "true" ]; then |
| 127 | + echo "⚠️ Coverage checks bypassed: ${{ steps.override.outputs.reason }}" |
| 128 | + echo "Please ensure this override is justified and temporary" |
| 129 | + else |
| 130 | + echo "✅ Coverage checks enforced - minimum 85% required" |
| 131 | + fi |
0 commit comments