Skip to content

Commit

Permalink
👷 Improve coverage badge to be fully automatic
Browse files Browse the repository at this point in the history
  • Loading branch information
astariul committed Jun 18, 2024
1 parent 05ff69d commit 1b1fd50
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 35 deletions.
21 changes: 0 additions & 21 deletions .github/badges/coverage.svg

This file was deleted.

43 changes: 43 additions & 0 deletions .github/workflows/auto_coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: auto_coverage

on:
push:
branches:
- main

jobs:
auto_coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.9"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e .[test]
pip install coverage-badge~=1.0
env:
GH_PAT: ${{ secrets.AUTH_TOKEN }}
- name: Test with pytest
timeout-minutes: 5
run: python -m pytest --cov-fail-under=0
- name: Generate the coverage badge & update the gist
run: |
export GIST_ID=$(cat README.md| grep coverage.svg | grep gist.githubusercontent.com | cut -d/ -f5)
git config --global user.email "[email protected]"
git config --global user.name "GitHub Action"
git clone https://${{ secrets.GIST_ACCESS_TOKEN }}@gist.github.com/$GIST_ID.git
coverage-badge -o $GIST_ID/coverage.svg -f
cd $GIST_ID
git add coverage.svg
if git diff --cached --exit-code --quiet; then
echo "No changes to commit"
else
git commit -m "Update coverage badge"
git push
fi
22 changes: 11 additions & 11 deletions .github/workflows/pytest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on: pull_request
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v4
with: # Use deploy key to ensure pushed change trigger checks as well : https://github.com/peter-evans/create-pull-request/blob/master/docs/concepts-guidelines.md#workarounds-to-trigger-further-workflow-runs
Expand All @@ -21,15 +24,12 @@ jobs:
GH_PAT: ${{ secrets.AUTH_TOKEN }}
- name: Test with pytest
timeout-minutes: 5
run: python -m pytest
- name: Check coverage updated
run: |
prev_cov=$(grep -Po '\d+%' .github/badges/coverage.svg | tail -1 | sed 's/.$//')
coverage-badge -o .github/badges/coverage.svg -f
curr_cov=$(grep -Po '\d+%' .github/badges/coverage.svg | tail -1 | sed 's/.$//')
if [[ "$prev_cov" != "$curr_cov" ]]
then
echo "Coverage badge is not up-to-date ! Please run the tests ('python -m pytest') and then re-create the badge with 'coverage-badge -o .github/badges/coverage.svg -f'"
exit 1
fi
# Enable pipefail option, so if pytest fail, the job will fail as well
set -o pipefail
python -m pytest --junitxml=pytest.xml --cov-report=term-missing:skip-covered | tee pytest-coverage.txt
- name: Add coverage information to the PR
uses: MishaKav/pytest-coverage-comment@main
with:
pytest-coverage-path: ./pytest-coverage.txt
junitxml-path: ./pytest.xml
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ repos:
rev: "v4.5.0"
hooks:
- id: trailing-whitespace
exclude: "coverage.svg$"
args: [--markdown-linebreak-ext=md]
- id: end-of-file-fixer
exclude: "coverage_report/.*"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A small framework to test and compare mobile keyboards
<a href="https://github.com/FleksySDK/kebbie/releases"><img src="https://img.shields.io/github/release/FleksySDK/kebbie.svg" alt="GitHub release" /></a>
<a href="https://github.com/FleksySDK/kebbie/actions/workflows/pytest.yaml"><img src="https://github.com/FleksySDK/kebbie/actions/workflows/pytest.yaml/badge.svg" alt="Test status" /></a>
<a href="https://github.com/FleksySDK/kebbie/actions/workflows/lint.yaml"><img src="https://github.com/FleksySDK/kebbie/actions/workflows/lint.yaml/badge.svg" alt="Lint status" /></a>
<img src=".github/badges/coverage.svg" alt="Coverage status" />
<img src="https://gist.githubusercontent.com/astariul/e15968f6a51f62a67f9a8c9a553390bc/raw/coverage.svg" alt="Coverage status" />
<a href="https://FleksySDK.github.io/kebbie"><img src="https://img.shields.io/website?down_message=failing&label=docs&up_color=green&up_message=passing&url=https%3A%2F%2FFleksySDK.github.io%2Fkebbie" alt="Docs" /></a>
<a href="https://github.com/FleksySDK/kebbie/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="licence" /></a>
</p>
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
]

extras_require = {
"test": ["pytest~=8.0", "pytest-cov~=5.0", "coverage-badge~=1.0"],
"test": ["pytest~=8.0", "pytest-cov~=5.0"],
"hook": ["pre-commit~=3.0"],
"lint": ["ruff~=0.2"],
"docs": ["mkdocs-material~=9.0", "mkdocstrings[python]~=0.18", "mike~=2.0"],
Expand Down

0 comments on commit 1b1fd50

Please sign in to comment.