Repository navigation
93 lines (80 loc) · 2.96 KB
/
Copy pathpytest_coverage.yml
File metadata and controls
93 lines (80 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
name: Pytest Coverage
on:
pull_request:
branches:
- main # Runs on PRs targeting the main branch
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
run_pytest_coverage:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ['3.11', '3.12', '3.13']
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ github.event_name == 'pull_request' && github.head_ref || 'main' }}
fetch-depth: 0
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install requirements and add python path
run: |
python -m pip install --upgrade pip
pip install -e .[dev]
- name: Create reports directory
run: |
mkdir -p build/reports
- name: Run pytest with coverage
run: |
pytest --cov --cov-report=html --cov-report=xml --cov-report=term
- name: Generate coverage badge
if: matrix.python-version == '3.12'
run: |
anybadge --value="$(coverage report --format=total)" --file=build/reports/coverage.svg --overwrite coverage
- name: Check if badge changed or is new
if: matrix.python-version == '3.12'
id: badge-check
run: |
# Check if the file exists in git (tracked)
if git ls-files --error-unmatch build/reports/coverage.svg 2>/dev/null; then
# File exists in git, check if it changed
if git diff --quiet HEAD -- build/reports/coverage.svg; then
echo "changed=false" >> $GITHUB_OUTPUT
echo "Badge exists and unchanged"
else
echo "changed=true" >> $GITHUB_OUTPUT
echo "Badge exists and changed"
fi
else
# File doesn't exist in git (new file)
echo "changed=true" >> $GITHUB_OUTPUT
echo "Badge is new file"
fi
- name: Commit and push coverage badge
if: matrix.python-version == '3.12' && steps.badge-check.outputs.changed == 'true' && github.event_name == 'pull_request'
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add build/reports/coverage.svg
git commit -m "Update coverage badge [skip ci]"
git push origin ${{ github.head_ref }}
- name: Upload coverage badge as artifact
if: matrix.python-version == '3.12'
uses: actions/upload-artifact@v4
with:
name: coverage-badge
path: build/reports/coverage.svg
retention-days: 30
- name: Upload coverage HTML report
if: matrix.python-version == '3.12'
uses: actions/upload-artifact@v4
with:
name: coverage-html-report
path: build/reports/coverage_html/
retention-days: 30