From 1b1fd50f5340b960c7d88491e8a3d32a63b4c1f4 Mon Sep 17 00:00:00 2001 From: Astariul Date: Tue, 18 Jun 2024 14:41:01 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20Improve=20coverage=20badge=20to?= =?UTF-8?q?=20be=20fully=20automatic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/badges/coverage.svg | 21 -------------- .github/workflows/auto_coverage.yaml | 43 ++++++++++++++++++++++++++++ .github/workflows/pytest.yaml | 22 +++++++------- .pre-commit-config.yaml | 1 - README.md | 2 +- setup.py | 2 +- 6 files changed, 56 insertions(+), 35 deletions(-) delete mode 100644 .github/badges/coverage.svg create mode 100644 .github/workflows/auto_coverage.yaml diff --git a/.github/badges/coverage.svg b/.github/badges/coverage.svg deleted file mode 100644 index 9029937..0000000 --- a/.github/badges/coverage.svg +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - - - - - coverage - coverage - 76% - 76% - - diff --git a/.github/workflows/auto_coverage.yaml b/.github/workflows/auto_coverage.yaml new file mode 100644 index 0000000..a126311 --- /dev/null +++ b/.github/workflows/auto_coverage.yaml @@ -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 "action@github.com" + 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 diff --git a/.github/workflows/pytest.yaml b/.github/workflows/pytest.yaml index 4e06499..15c7e2c 100644 --- a/.github/workflows/pytest.yaml +++ b/.github/workflows/pytest.yaml @@ -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 @@ -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 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a831ac3..e399110 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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/.*" diff --git a/README.md b/README.md index 38a06f0..05d023e 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A small framework to test and compare mobile keyboards GitHub release Test status Lint status - Coverage status + Coverage status Docs licence

diff --git a/setup.py b/setup.py index 129eb9d..d10e57f 100644 --- a/setup.py +++ b/setup.py @@ -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"],