|
| 1 | +name: Coverage |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + paths: |
| 6 | + - 'django/**/*.py' |
| 7 | + - 'tests/**/*.py' |
| 8 | + branches: |
| 9 | + - main |
| 10 | + |
| 11 | +concurrency: |
| 12 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 13 | + cancel-in-progress: true |
| 14 | + |
| 15 | +permissions: |
| 16 | + contents: read |
| 17 | + pull-requests: write |
| 18 | + |
| 19 | +jobs: |
| 20 | + diff-coverage: |
| 21 | + if: github.repository == 'django/django' |
| 22 | + name: Diff Coverage (Windows) |
| 23 | + runs-on: windows-latest |
| 24 | + steps: |
| 25 | + - name: Checkout |
| 26 | + uses: actions/checkout@v5 |
| 27 | + with: |
| 28 | + fetch-depth: 0 |
| 29 | + persist-credentials: false |
| 30 | + |
| 31 | + - name: Set up Python |
| 32 | + uses: actions/setup-python@v6 |
| 33 | + with: |
| 34 | + python-version: '3.14' |
| 35 | + cache: 'pip' |
| 36 | + cache-dependency-path: 'tests/requirements/py3.txt' |
| 37 | + |
| 38 | + - name: Install dependencies |
| 39 | + run: | |
| 40 | + python -m pip install --upgrade pip wheel |
| 41 | + python -m pip install -r tests/requirements/py3.txt -e . |
| 42 | + python -m pip install 'coverage[toml]' diff-cover |
| 43 | +
|
| 44 | + - name: Run tests with coverage |
| 45 | + env: |
| 46 | + PYTHONPATH: ${{ github.workspace }}/tests |
| 47 | + COVERAGE_PROCESS_START: ${{ github.workspace }}/tests/.coveragerc |
| 48 | + RUNTESTS_DIR: ${{ github.workspace }}/tests |
| 49 | + run: | |
| 50 | + python -Wall tests/runtests.py -v2 |
| 51 | +
|
| 52 | + - name: Generate coverage report |
| 53 | + if: success() |
| 54 | + env: |
| 55 | + COVERAGE_RCFILE: ${{ github.workspace }}/tests/.coveragerc |
| 56 | + RUNTESTS_DIR: ${{ github.workspace }}/tests |
| 57 | + run: | |
| 58 | + python -m coverage combine |
| 59 | + python -m coverage report --show-missing |
| 60 | + python -m coverage xml -o tests/coverage.xml |
| 61 | +
|
| 62 | + - name: Run diff-cover |
| 63 | + if: success() |
| 64 | + run: | |
| 65 | + if (Test-Path 'tests/coverage.xml') { |
| 66 | + diff-cover tests/coverage.xml --compare-branch=origin/main --fail-under=0 > tests/diff-cover-report.md |
| 67 | + } else { |
| 68 | + Set-Content -Path tests/diff-cover-report.md -Value 'No coverage.xml found; skipping diff-cover.' |
| 69 | + } |
| 70 | +
|
| 71 | + - name: Post/update PR comment |
| 72 | + if: success() |
| 73 | + uses: actions/github-script@v8 |
| 74 | + with: |
| 75 | + script: | |
| 76 | + const fs = require('fs'); |
| 77 | + const reportPath = 'tests/diff-cover-report.md'; |
| 78 | + let body = 'No coverage data available.'; |
| 79 | + if (fs.existsSync(reportPath)) { |
| 80 | + body = fs.readFileSync(reportPath, 'utf8'); |
| 81 | + } |
| 82 | + const commentBody = '### 📊 Coverage Report for Changed Files\n\n```\n' + body + '\n```\n\n**Note:** Missing lines are warnings only. Some lines may not be covered by SQLite tests as they are database-specific.\n\nFor more information about code coverage on pull requests, see the [contributing documentation](https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/unit-tests/#code-coverage-on-pull-requests).'; |
| 83 | +
|
| 84 | + const { data: comments } = await github.rest.issues.listComments({ |
| 85 | + owner: context.repo.owner, |
| 86 | + repo: context.repo.repo, |
| 87 | + issue_number: context.issue.number, |
| 88 | + }); |
| 89 | + for (const c of comments) { |
| 90 | + if ((c.body || '').includes('📊 Coverage Report for Changed Files')) { |
| 91 | + await github.rest.issues.deleteComment({ |
| 92 | + owner: context.repo.owner, |
| 93 | + repo: context.repo.repo, |
| 94 | + comment_id: c.id, |
| 95 | + }); |
| 96 | + } |
| 97 | + } |
| 98 | +
|
| 99 | + await github.rest.issues.createComment({ |
| 100 | + owner: context.repo.owner, |
| 101 | + repo: context.repo.repo, |
| 102 | + issue_number: context.issue.number, |
| 103 | + body: commentBody, |
| 104 | + }); |
0 commit comments