|
| 1 | +name: Coverage Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [ "**" ] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + pull-requests: read |
| 10 | + |
| 11 | +jobs: |
| 12 | + coverage-check: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + |
| 15 | + steps: |
| 16 | + - uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + |
| 20 | + - name: Set up Python |
| 21 | + uses: actions/setup-python@v4 |
| 22 | + with: |
| 23 | + python-version: "3.12" |
| 24 | + |
| 25 | + - name: Install dependencies |
| 26 | + run: | |
| 27 | + python -m pip install --upgrade pip |
| 28 | + pip install -e ".[dev]" |
| 29 | +
|
| 30 | + - name: Get base branch coverage |
| 31 | + run: | |
| 32 | + git checkout origin/mainline |
| 33 | + pip install -e ".[dev]" |
| 34 | + python -m pytest tests/ -m "not integration" --cov=src --cov-report=json:coverage-base.json --tb=no -q || true |
| 35 | + BASE_COVERAGE=$(python -c "import json; print(json.load(open('coverage-base.json'))['totals']['percent_covered'])") |
| 36 | + echo "BASE_COVERAGE=$BASE_COVERAGE" >> $GITHUB_ENV |
| 37 | + echo "Base branch coverage: $BASE_COVERAGE%" |
| 38 | +
|
| 39 | + - name: Get PR coverage |
| 40 | + run: | |
| 41 | + git checkout ${{ github.event.pull_request.head.sha }} |
| 42 | + pip install -e ".[dev]" |
| 43 | + python -m pytest tests/ -m "not integration" --cov=src --cov-report=json:coverage-pr.json --tb=no -q || true |
| 44 | + PR_COVERAGE=$(python -c "import json; print(json.load(open('coverage-pr.json'))['totals']['percent_covered'])") |
| 45 | + echo "PR_COVERAGE=$PR_COVERAGE" >> $GITHUB_ENV |
| 46 | + echo "PR branch coverage: $PR_COVERAGE%" |
| 47 | +
|
| 48 | + - name: Compare coverage and enforce increase |
| 49 | + run: | |
| 50 | + python -c " |
| 51 | + import sys |
| 52 | + base = float('${{ env.BASE_COVERAGE }}') |
| 53 | + pr = float('${{ env.PR_COVERAGE }}') |
| 54 | + diff = pr - base |
| 55 | +
|
| 56 | + print(f'Coverage Report') |
| 57 | + print(f'Base coverage: {base:.2f}%') |
| 58 | + print(f'PR coverage: {pr:.2f}%') |
| 59 | + print(f'Coverage change: {diff:+.2f}%') |
| 60 | + print() |
| 61 | +
|
| 62 | + if diff < -0.01: # Allow small floating point differences |
| 63 | + print('❌ Coverage decreased! This PR reduces test coverage.') |
| 64 | + print('Please add tests to maintain or improve coverage.') |
| 65 | + sys.exit(1) |
| 66 | + elif diff > 0.01: |
| 67 | + print('✅ Coverage increased! Great job improving test coverage.') |
| 68 | + else: |
| 69 | + print('➡️ Coverage unchanged (within tolerance).') |
| 70 | + " |
0 commit comments