Skip to content

Commit bb67ac5

Browse files
committed
ci(tests): add choice of OS + coverage
1 parent 26c41d7 commit bb67ac5

File tree

1 file changed

+71
-55
lines changed

1 file changed

+71
-55
lines changed

.github/workflows/tests.yaml

Lines changed: 71 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,47 @@
1-
# This file runs the tests in `tests/`
2-
# They will run in parallel, and run on three operating systems:
3-
# Ubuntu, Windows and Mac.
4-
51
name: tests
62

73
on:
84
push:
95
branches: [main]
106
workflow_dispatch:
7+
inputs:
8+
operating_systems:
9+
description: 'Operating systems to test on'
10+
type: choice
11+
default: 'all'
12+
options:
13+
- all
14+
- ubuntu-latest
15+
- windows-latest
16+
- macos-latest
1117

1218
jobs:
13-
14-
# Run tests on Ubuntu
15-
tests-on-ubuntu:
19+
# Generate matrix based on input
20+
setup-matrix:
1621
runs-on: ubuntu-latest
22+
outputs:
23+
matrix: ${{ steps.set-matrix.outputs.matrix }}
1724
steps:
18-
19-
- name: Check out repository
20-
uses: actions/checkout@v4
21-
22-
- name: Install python and dependencies
23-
uses: actions/setup-python@v4
24-
with:
25-
python-version: '3.13'
26-
cache: 'pip'
27-
- run: pip install -r requirements.txt
28-
29-
- name: Run tests
30-
run: pytest
31-
32-
- name: List the environment variables
33-
run: env
34-
35-
# Run tests on Windows
36-
tests-on-windows:
37-
runs-on: windows-latest
25+
- name: Set matrix
26+
id: set-matrix
27+
run: |
28+
if [[ "${{ inputs.operating_systems }}" == "all" || "${{ github.event_name }}" == "push" ]]; then
29+
echo 'matrix=["ubuntu-latest", "windows-latest", "macos-latest"]' >> $GITHUB_OUTPUT
30+
else
31+
echo 'matrix=["${{ inputs.operating_systems }}"]' >> $GITHUB_OUTPUT
32+
fi
33+
34+
# Run tests using matrix strategy
35+
tests:
36+
permissions:
37+
contents: write
38+
needs: setup-matrix
39+
runs-on: ${{ matrix.os }}
40+
strategy:
41+
matrix:
42+
os: ${{ fromJSON(needs.setup-matrix.outputs.matrix) }}
43+
3844
steps:
39-
4045
- name: Check out repository
4146
uses: actions/checkout@v4
4247

@@ -45,31 +50,42 @@ jobs:
4550
with:
4651
python-version: '3.13'
4752
cache: 'pip'
48-
- run: python -m pip install -r requirements.txt
49-
50-
- name: Run tests
51-
run: pytest
52-
53-
- name: List the environment variables
54-
run: env
55-
56-
# Run tests on Mac
57-
tests-on-macos:
58-
runs-on: macos-latest
59-
steps:
60-
61-
- name: Check out repository
62-
uses: actions/checkout@v4
63-
64-
- name: Install python and dependencies
65-
uses: actions/setup-python@v4
53+
54+
- name: Install requirements (Windows)
55+
if: runner.os == 'Windows'
56+
run: python -m pip install -r requirements.txt
57+
58+
- name: Install requirements (Unix)
59+
if: runner.os != 'Windows'
60+
run: pip install -r requirements.txt
61+
62+
- name: Run tests (with coverage for linux)
63+
run: |
64+
if [ "${{ matrix.os }}" = "ubuntu-latest" ]; then
65+
pytest --cov --cov-report=xml
66+
else
67+
pytest
68+
fi
69+
shell: bash
70+
71+
- name: Generate coverage badge (for-the-badge style)
72+
if: matrix.os == 'ubuntu-latest'
73+
run: genbadge coverage -i coverage.xml -o images/coverage-badge.svg
74+
75+
- name: Upload coverage badge
76+
if: matrix.os == 'ubuntu-latest'
77+
uses: actions/upload-artifact@v4
6678
with:
67-
python-version: '3.13'
68-
cache: 'pip'
69-
- run: pip install -r requirements.txt
70-
71-
- name: Run tests
72-
run: pytest
73-
74-
- name: List the environment variables
75-
run: env
79+
name: coverage-badge
80+
path: images/coverage-badge.svg
81+
82+
- name: Commit coverage badge
83+
if: matrix.os == 'ubuntu-latest'
84+
run: |
85+
git config --local user.email "github-actions[bot]@users.noreply.github.com"
86+
git config --local user.name "github-actions[bot]"
87+
git add images/coverage-badge.svg
88+
git commit -m "ci(tests): update coverage badge" || echo "No changes to commit"
89+
git push
90+
env:
91+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)