name: pytest
on:
  workflow_call:

jobs:
  pytest:
    runs-on: ${{ matrix.os }}-latest
    env:
      PYTEST_ADDOPTS: "--run-integration --showlocals -vv --durations=10 --reruns 5 --only-rerun subprocess.CalledProcessError"
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu
          - macos
          - windows
        py:
          - "pypy3.9-v7.3.14"
          - "pypy3.10-v7.3.19"
          - "pypy3.11-v7.3.19"
          - "3.15"
          - "3.14"
          - "3.13"
          - "3.12"
          - "3.11"
          - "3.10"
          - "3.9"
        tox-target:
          - "tox"
          - "min"

    continue-on-error: >- # jobs not required in branch protection
      ${{
        (
          startsWith(matrix.py, 'pypy')
          && matrix.os == 'windows'
        )
        && true
        || false
      }}

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Setup uv
        uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0

      - name: Install tox (uv)
        run: uv tool install tox --with tox-uv

      - name: Setup python for test ${{ matrix.py }}
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6
        with:
          python-version: ${{ matrix.py }}
          allow-prereleases: true

      - name: Pick environment to run
        run: |
          import platform
          import os
          import sys

          if platform.python_implementation() == "PyPy":
              base = f"pypy{sys.version_info.major}{sys.version_info.minor}"
          else:
              base = f"py{sys.version_info.major}{sys.version_info.minor}"
          env = f"BASE={base}\n"
          print(f"Picked:\n{env}for {sys.version}")
          with open(os.environ["GITHUB_ENV"], "a", encoding="utf-8") as file:
              file.write(env)
        shell: python

      - name: Run test suite via tox
        if: matrix.tox-target == 'tox'
        shell: bash
        run: |
          tox -vv --notest -e $BASE
          tox -e $BASE --skip-pkg-install

      - name: Run minimum version test
        if: matrix.tox-target == 'min'
        shell: bash
        run: tox -e $BASE-${{ matrix.tox-target }}

      - name: Run path test
        if: matrix.tox-target == 'tox' && matrix.py == '3.10'
        run: tox -e path

      - name: Combine coverage files
        if: always()
        run: tox -e coverage

      - uses: codecov/codecov-action@57e3a136b779b570ffcdbf80b3bdc90e7fab3de2 # v6.0.0
        if: always()
        env:
          PYTHON: ${{ matrix.python }}
        with:
          files: .tox/coverage.xml
          flags: tests
          env_vars: PYTHON
          name: ${{ matrix.py }} - ${{ matrix.os }}
