|
| 1 | +name: Test |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + pull_request: |
| 7 | + branches: [main] |
| 8 | + schedule: |
| 9 | + - cron: "0 5 1,15 * *" |
| 10 | + |
| 11 | +concurrency: |
| 12 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 13 | + cancel-in-progress: true |
| 14 | + |
| 15 | +jobs: |
| 16 | + # Get the test environment from hatch as defined in pyproject.toml. |
| 17 | + # This ensures that the pyproject.toml is the single point of truth for test definitions and the same tests are |
| 18 | + # run locally and on continuous integration. |
| 19 | + # Check [[tool.hatch.envs.hatch-test.matrix]] in pyproject.toml and https://hatch.pypa.io/latest/environment/ for |
| 20 | + # more details. |
| 21 | + get-environments: |
| 22 | + runs-on: ubuntu-latest |
| 23 | + outputs: |
| 24 | + envs: ${{ steps.get-envs.outputs.envs }} |
| 25 | + steps: |
| 26 | + - uses: actions/checkout@v5 |
| 27 | + with: |
| 28 | + filter: blob:none |
| 29 | + fetch-depth: 0 |
| 30 | + - name: Install uv |
| 31 | + uses: astral-sh/setup-uv@v7 |
| 32 | + - name: Get test environments |
| 33 | + id: get-envs |
| 34 | + run: | |
| 35 | + ENVS_JSON=$(uvx hatch env show --json | jq -c 'to_entries |
| 36 | + | map( |
| 37 | + select(.key | startswith("hatch-test")) |
| 38 | + | { |
| 39 | + name: .key, |
| 40 | + label: (if (.key | contains("pre")) then .key + " (PRE-RELEASE DEPENDENCIES)" else .key end), |
| 41 | + python: .value.python |
| 42 | + } |
| 43 | + )') |
| 44 | + echo "envs=${ENVS_JSON}" | tee $GITHUB_OUTPUT |
| 45 | +
|
| 46 | + # Run tests through hatch. Spawns a separate runner for each environment defined in the hatch matrix obtained above. |
| 47 | + test: |
| 48 | + needs: get-environments |
| 49 | + |
| 50 | + strategy: |
| 51 | + fail-fast: false |
| 52 | + matrix: |
| 53 | + os: [ubuntu-latest] |
| 54 | + env: ${{ fromJSON(needs.get-environments.outputs.envs) }} |
| 55 | + |
| 56 | + name: ${{ matrix.env.label }} |
| 57 | + runs-on: ${{ matrix.os }} |
| 58 | + |
| 59 | + steps: |
| 60 | + - uses: actions/checkout@v5 |
| 61 | + with: |
| 62 | + filter: blob:none |
| 63 | + fetch-depth: 0 |
| 64 | + - name: Install uv |
| 65 | + uses: astral-sh/setup-uv@v7 |
| 66 | + with: |
| 67 | + python-version: ${{ matrix.env.python }} |
| 68 | + - name: create hatch environment |
| 69 | + run: uvx hatch env create ${{ matrix.env.name }} |
| 70 | + - name: run tests using hatch |
| 71 | + env: |
| 72 | + MPLBACKEND: agg |
| 73 | + PLATFORM: ${{ matrix.os }} |
| 74 | + DISPLAY: :42 |
| 75 | + run: uvx hatch run ${{ matrix.env.name }}:run-cov -v --color=yes -n auto |
| 76 | + - name: generate coverage report |
| 77 | + run: | |
| 78 | + # See https://coverage.readthedocs.io/en/latest/config.html#run-patch |
| 79 | + test -f .coverage || uvx hatch run ${{ matrix.env.name }}:cov-combine |
| 80 | + uvx hatch run ${{ matrix.env.name }}:cov-report # report visibly |
| 81 | + uvx hatch run ${{ matrix.env.name }}:coverage xml # create report for upload |
| 82 | + - name: Upload coverage |
| 83 | + uses: codecov/codecov-action@v5 |
| 84 | + |
| 85 | + # Check that all tests defined above pass. This makes it easy to set a single "required" test in branch |
| 86 | + # protection instead of having to update it frequently. See https://github.com/re-actors/alls-green#why. |
| 87 | + check: |
| 88 | + name: Tests pass in all hatch environments |
| 89 | + if: always() |
| 90 | + needs: |
| 91 | + - get-environments |
| 92 | + - test |
| 93 | + runs-on: ubuntu-latest |
| 94 | + steps: |
| 95 | + - uses: re-actors/alls-green@release/v1 |
| 96 | + with: |
| 97 | + jobs: ${{ toJSON(needs) }} |
0 commit comments