Skip to content

Commit 5ee89c5

Browse files
grstflying-sheep
andauthored
Get test matrix from hatch (#390)
* Get test matrix from hatch * Fix test command * Fix test command * Create env * fix create env * Format * install python * Fix jq * try officiall spelling * try uv python-version * try only `include` * switch to script * Revert "switch to script" This reverts commit 6135fc4. * define matrix * fmt * I think I got it! * Use alls-green * add name to alls-green Co-authored-by: Gregor Sturm <mail@gregor-sturm.de> * Add comments to test.yaml --------- Co-authored-by: Philipp A. <flying-sheep@web.de>
1 parent e37e90c commit 5ee89c5

2 files changed

Lines changed: 58 additions & 18 deletions

File tree

{{cookiecutter.project_name}}/.github/workflows/test.yaml

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,48 @@ defaults:
1818
shell: bash -euo pipefail {0}
1919

2020
jobs:
21+
# Get the test environment from hatch as defined in pyproject.toml.
22+
# This ensures that the pyproject.toml is the single point of truth for test definitions and the same tests are
23+
# run locally and on continuous integration.
24+
# Check [[tool.hatch.envs.hatch-test.matrix]] in pyproject.toml and https://hatch.pypa.io/latest/environment/ for
25+
# more details.
26+
get-environments:
27+
runs-on: ubuntu-latest
28+
outputs:
29+
envs: ${{ steps.get-envs.outputs.envs }}
30+
steps:
31+
- uses: actions/checkout@v4
32+
with:
33+
filter: blob:none
34+
fetch-depth: 0
35+
- name: Install uv
36+
uses: astral-sh/setup-uv@v5
37+
- name: Get test environments
38+
id: get-envs
39+
run: |
40+
ENVS_JSON=$(uvx hatch env show --json | jq -c 'to_entries
41+
| map(
42+
select(.key | startswith("hatch-test"))
43+
| {
44+
name: .key,
45+
label: (if (.key | contains("pre")) then .key + " (PRE-RELEASE DEPENDENCIES)" else .key end),
46+
python: .value.python
47+
}
48+
)')
49+
echo "envs=${ENVS_JSON}" | tee $GITHUB_OUTPUT
50+
51+
# Run tests through hatch. Spawns a separate runner for each environment defined in the hatch matrix obtained above.
2152
test:
22-
runs-on: ${{ matrix.os }}
53+
needs: get-environments
2354

2455
strategy:
2556
fail-fast: false
2657
matrix:
27-
include:
28-
- os: ubuntu-latest
29-
python: "3.10"
30-
- os: ubuntu-latest
31-
python: "3.12"
32-
- os: ubuntu-latest
33-
python: "3.12"
34-
pip-flags: "--pre"
35-
name: PRE-RELEASE DEPENDENCIES
36-
37-
name: ${{ matrix.name }} Python ${{ matrix.python }}
38-
39-
env:
40-
OS: ${{ matrix.os }}
41-
PYTHON: ${{ matrix.python }}
58+
os: [ubuntu-latest]
59+
env: ${{ fromJSON(needs.get-environments.outputs.envs) }}
60+
61+
name: ${{ matrix.env.label }}
62+
runs-on: ${{ matrix.os }}
4263

4364
steps:
4465
- uses: actions/checkout@v4
@@ -48,14 +69,31 @@ jobs:
4869
- name: Install uv
4970
uses: astral-sh/setup-uv@v5
5071
with:
72+
python-version: ${{ matrix.env.python }}
5173
cache-dependency-glob: pyproject.toml
74+
- name: create hatch environment
75+
run: uvx hatch env create ${{ matrix.env.name }}
5276
- name: run tests using hatch
5377
env:
5478
MPLBACKEND: agg
5579
PLATFORM: ${{ matrix.os }}
5680
DISPLAY: :42
57-
run: uvx hatch test --cover --python ${{ matrix.python }}
81+
run: uvx hatch run ${{ matrix.env.name }}:run-cov
5882
- name: generate coverage report
59-
run: uvx hatch run hatch-test.py${{ matrix.python }}:coverage xml
83+
run: uvx hatch run ${{ matrix.env.name }}:coverage xml
6084
- name: Upload coverage
6185
uses: codecov/codecov-action@v4
86+
87+
# Check that all tests defined above pass. This makes it easy to set a single "required" test in branch
88+
# protection instead of having to update it frequently. See https://github.com/re-actors/alls-green#why.
89+
check:
90+
name: Tests pass in all hatch environments
91+
if: always()
92+
needs:
93+
- get-environments
94+
- test
95+
runs-on: ubuntu-latest
96+
steps:
97+
- uses: re-actors/alls-green@release/v1
98+
with:
99+
jobs: ${{ toJSON(needs) }}

{{cookiecutter.project_name}}/pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ scripts.build = "sphinx-build -M html docs docs/_build {args}"
7272
scripts.open = "python -m webbrowser -t docs/_build/html/index.html"
7373
scripts.clean = "git clean -fdX -- {args:docs}"
7474

75+
[[tool.hatch.envs.hatch-test.matrix]]
76+
python = [ "3.10", "3.13" ]
7577
[tool.hatch.envs.hatch-test]
7678
features = [ "test" ]
7779

0 commit comments

Comments
 (0)