name: Test

on:
  push:
    branches:
      - main
  pull_request:
  workflow_dispatch:
    # allow manual runs on branches without a PR

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
  cancel-in-progress: true

permissions: {}

jobs:
  pre-commit:
    name: Pre-commit checks (Black, Flake8, MyPy, ...)
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          persist-credentials: false
      - uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
        with:
          python-version: "3.10"
      - uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1

  test-dist:
    name: Test SDist & wheel
    needs: pre-commit
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - name: Checkout
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          persist-credentials: false
      - name: Install CPython 3.10
        uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
        with:
          python-version: "3.10"
          architecture: x64
      - name: Run tests
        run: pipx run nox -s test-sdist-3.10 test-wheel-3.10

  test:
    name: CPython ${{ matrix.python }} ${{ matrix.platform[0] }} on ${{ matrix.platform[1] }}
    needs: test-dist
    runs-on: ${{ matrix.platform[1] }}
    permissions:
      contents: read
    strategy:
      fail-fast: false
      matrix:
        platform:
          - [ 'x86_64', 'ubuntu-24.04' ]
        python: [ '3.10', '3.11', '3.12', '3.13', '3.14' ]
        include:
          - platform: [ 'aarch64', 'ubuntu-24.04-arm' ]
            python: '3.12'
          - platform: [ 'i686', 'ubuntu-24.04' ]
            python: '3.12'
          - platform: [ 'armv7l', 'ubuntu-24.04-arm' ]
            python: '3.12'
          - platform: [ 'ppc64le', 'ubuntu-24.04' ]
            python: '3.12'
            qemu: true
          - platform: [ 's390x', 'ubuntu-24.04' ]
            python: '3.12'
            qemu: true
          - platform: [ 'riscv64', 'ubuntu-24.04' ]
            python: '3.12'
            qemu: true
    steps:
      - name: Checkout
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          persist-credentials: false
      - name: Setup cache
        uses: actions/cache@9255dc7a253b0ccc959486e2bca901246202afeb # v5.0.1
        with:
          path: ~/.cache/auditwheel_tests
          key: python${{ matrix.python }}-${{ matrix.platform[0] }}-${{ hashFiles('**/test_manylinux.py') }}
          restore-keys: python${{ matrix.python }}-${{ matrix.platform[0] }}-
      - name: Install CPython ${{ matrix.python }}
        uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
        with:
          python-version: "${{ matrix.python }}"
          allow-prereleases: true
      - name: Set up QEMU
        if: matrix.qemu
        uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3.7.0
      - name: Run tests
        run: pipx run nox -s tests-${{ matrix.python }}
        env:
          AUDITWHEEL_ARCH: ${{ matrix.platform[0] }}
          AUDITWHEEL_QEMU: ${{ matrix.qemu }}
      - name: Upload coverage to codecov
        uses: codecov/codecov-action@671740ac38dd9b0130fbe1cec585b89eea48d3de # v5.5.2
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
