name: CI

on:
  push:
    branches:
      - master
      - v[0-9]*.*
    paths:
      - apischema/**
      - examples/**
      - scripts/**
      - tests/**
  pull_request:
    paths:
      - apischema/**
      - examples/**
      - scripts/**
      - tests/**

jobs:
  test:
    name: Test ${{ matrix.python-version }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', '3.13-dev', 'pypy-3.9', 'pypy-3.10']
        include:
          - python-version: '3.12'
            pytest-args: --cov=apischema --cov-branch --cov-report=xml --cov-report=html
    steps:
      - uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('tests/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
      - uses: actions/checkout@v4
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install requirements
        run: pip install -r tests/requirements.txt
      - name: Generate tests from documentation example
        run: scripts/generate_tests_from_examples.py
      - name: Run tests
        run: pytest tests ${{ matrix.pytest-args }}
      - uses: codecov/codecov-action@v4
        # https://github.community/t/run-step-if-file-exists/16445/3
        if: hashFiles('coverage.xml') != ''
      - uses: actions/upload-artifact@v4
        if: hashFiles('coverage.xml') != ''
        with:
          name: coverage
          path: |
            coverage.xml
            htmlcov
      - name: Cythonize
        run: scripts/cythonize.sh
        if: matrix.python-version != 'pypy-3.9' && matrix.python-version != 'pypy-3.10'
      - name: Compile
        run: pip install -e .
        if: matrix.python-version != 'pypy-3.9' && matrix.python-version != 'pypy-3.10'
      - name: Run tests (compiled)
        run: pytest tests
        if: matrix.python-version != 'pypy-3.9' && matrix.python-version != 'pypy-3.10'

concurrency:
  group: ci-${{ github.head_ref }}
  cancel-in-progress: true
