Merge pull request #875 from gchq/feature/update_version_bug #1266
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Code Coverage Assessment | |
# Run in addition to unit tests without coverage assessment in case of weirdness around | |
# parallelised code. Also regenerates coverage badge. | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
branches: | |
- "**" | |
jobs: | |
coverage: | |
runs-on: ubuntu-latest | |
outputs: | |
percentage: ${{ steps.cov.outputs.percentage }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up base Python | |
uses: actions/setup-python@v5 | |
with: | |
# Note that this is just the version of Python that we use to run `uv` with. | |
# `uv` manages its own version of Python. | |
# For speed, we use the same version for both, but in principle these could differ. | |
python-version: 3.12 | |
- name: Set up uv cache directory location (Linux/Mac) | |
run: echo "UV_CACHE_DIR=${{ runner.temp }}/.uv-cache" >> $GITHUB_ENV | |
if: runner.os != 'Windows' | |
- name: Set up uv cache directory location (Windows) | |
run: echo "UV_CACHE_DIR=${{ runner.temp }}/.uv-cache" >> $env:GITHUB_ENV | |
if: runner.os == 'Windows' | |
- name: Restore uv cache | |
uses: actions/cache@v4 | |
with: | |
path: ${{ env.UV_CACHE_DIR }} | |
key: uv-${{ runner.os }}-${{ hashFiles('uv.lock') }}-${{ matrix.python-version }}-test | |
restore-keys: | | |
uv-${{ runner.os }}-${{ hashFiles('uv.lock') }}-${{ matrix.python-version }} | |
uv-${{ runner.os }}-${{ hashFiles('uv.lock') }} | |
uv-${{ runner.os }} | |
- name: Install latest versions of pip and uv | |
run: python -m pip install --upgrade pip uv | |
- name: Install test dependencies | |
run: uv sync --extra test --no-dev --locked | |
- name: Debug - uv pip freeze | |
run: uv pip freeze | |
- name: Assess coverage of unit tests | |
run: uv run pytest tests/unit --cov | |
- name: Extract total coverage percentage | |
id: cov | |
run: echo "percentage=$( coverage report --format=total )" >> $GITHUB_OUTPUT | |
- name: Minimize UV cache | |
run: uv cache prune --ci | |
if: always() | |
coverage-badge: | |
name: Update coverage badge | |
if: github.event_name == 'push' | |
# Push coverage badge config to a GitHub Gist. The Gist can currently only be hosted | |
# by a user rather than an organisation. A PAT with write Gist permissions needs to | |
# be saved as a secret of this repo. The PAT and Gist ID need updating if the host | |
# user changes. | |
needs: | |
- coverage | |
env: | |
percentage: ${{ needs.coverage.outputs.percentage }} | |
GITHUB_TOKEN: ${{ secrets.COVERAGE_GIST_KEY }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Choose badge colour | |
id: design | |
run: | | |
echo "colour=${{ | |
env.percentage >= 90 && 'brightgreen' || | |
env.percentage >= 70 && 'yellow' || | |
env.percentage >= 50 && 'orange' || | |
'red' | |
}}" >> $GITHUB_OUTPUT | |
- name: Generate badge config JSON | |
run: | | |
echo "coverage = ${{ env.percentage }}%" | |
echo "colour = ${{ steps.design.outputs.colour }}" | |
{ | |
echo "{" | |
echo " \"schemaVersion\": 1," | |
echo " \"label\": \"Coverage\"," | |
echo " \"message\": \"${{ env.percentage }}%\"," | |
echo " \"color\": \"${{ steps.design.outputs.colour }}\"" | |
echo "}" | |
} > badge.json | |
- name: Write Gist | |
env: | |
gist_id: 51dd332be75961a7dc903c67718028e1 | |
out_name: coreax_coverage.json | |
run: gh gist edit ${{ env.gist_id }} -a ${{ env.out_name }} badge.json |