name: test

on:
  pull_request:
  push:
    branches: ["main", "v[0-9]*"]
    tags: ["v[0-9]*"]
  workflow_dispatch:
  schedule:
    # Weekly test (on branch main) every Thursday at 12:00 UTC.
    # (Used to monitor compatibility with Django patches/dev and other dependencies.)
    - cron: "0 12 * * 4"

jobs:
  get-envlist:
    runs-on: ubuntu-22.04
    outputs:
      envlist: ${{ steps.generate-envlist.outputs.envlist }}
    steps:
      - name: Get code
        uses: actions/checkout@v4
      - name: Setup default Python
        # Change default Python version to something consistent
        # for installing/running tox
        uses: actions/setup-python@v5
        with:
          python-version: "3.13"
      - name: Install tox-gh-matrix
        run: |
          python -m pip install 'tox<4' 'tox-gh-matrix<0.3'
          python -m tox --version
      - name: Generate tox envlist
        id: generate-envlist
        run: |
          python -m tox --gh-matrix
          python -m tox --gh-matrix-dump  # for debugging

  test:
    runs-on: ubuntu-22.04
    needs: get-envlist
    strategy:
      matrix:
        tox: ${{ fromJSON(needs.get-envlist.outputs.envlist) }}
      fail-fast: false

    name: ${{ matrix.tox.name }} ${{ matrix.tox.ignore_outcome && 'allow-failures' || '' }}
    timeout-minutes: 15
    steps:
      - name: Get code
        uses: actions/checkout@v4
      - name: Setup Python ${{ matrix.tox.python.version }}
        # Ensure matrix Python version is installed and available for tox
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.tox.python.spec }}
          cache: "pip"
      - name: Setup default Python
        # Change default Python version back to something consistent
        # for installing/running tox
        uses: actions/setup-python@v5
        with:
          python-version: "3.13"
      - name: Install tox
        run: |
          set -x
          python -VV
          python -m pip install 'tox<4'
          python -m tox --version
      - name: Test ${{ matrix.tox.name }}
        run: |
          python -m tox -e ${{ matrix.tox.name }}
        continue-on-error: ${{ matrix.tox.ignore_outcome == true }}
        env:
          CONTINUOUS_INTEGRATION: true
          TOX_OVERRIDE_IGNORE_OUTCOME: false
