name: Test

on: 
  push:
  pull_request:

jobs:

  msys2:
    runs-on: windows-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - msystem: MINGW64
            pkgprefix: mingw-w64-x86_64
          - msystem: CLANG64
            pkgprefix: mingw-w64-clang-x86_64
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: setup-msys2
        uses: msys2/setup-msys2@v2
        with:
          msystem: ${{ matrix.msystem }}
          update: true
          install: >-
            ${{ matrix.pkgprefix }}-cairo
            ${{ matrix.pkgprefix }}-python
            ${{ matrix.pkgprefix }}-python-pip
            ${{ matrix.pkgprefix }}-python-poetry
            ${{ matrix.pkgprefix }}-cc
            ${{ matrix.pkgprefix }}-meson
            ${{ matrix.pkgprefix }}-ninja
            ${{ matrix.pkgprefix }}-pkgconf

      - name: Install dependencies
        shell: msys2 {0}
        run: |
          # XXX: set CC for setuptools so it finds clang
          export CC=cc
          poetry install

      - name: Build & Test with meson
        shell: msys2 {0}
        run: |
          # XXX: pass python, so global python3 doesn't win
          poetry run meson setup -Dpython=python --werror _build
          poetry run meson compile -C _build
          poetry run meson test -v -C _build
          rm -Rf _build

      - name: Build & Test with poetry
        shell: msys2 {0}
        run: |
          export CFLAGS="-Werror -coverage"
          export PYTHONDEVMODE=1
          poetry run pip install --no-build-isolation -e .
          poetry run coverage run --branch -m pytest
          poetry run coverage xml -i

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v4

  ubuntu:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ['3.9', '3.10', '3.11', '3.12', '3.13', 'pypy-3.10']
    steps:
    - uses: actions/checkout@v4

    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
        allow-prereleases: true

    - name: Install dependencies
      run: |
        sudo apt-get update -q
        sudo apt-get install -y libcairo2-dev ninja-build
        pipx install poetry
        pipx install meson
        poetry install
        poetry run pip install --upgrade pygame || true
        poetry run pip install --upgrade numpy || true

    - name: Build & Test with meson
      run: |
        poetry run meson setup --werror _build
        poetry run meson compile -C _build
        poetry run meson test -v -C _build
        rm -Rf _build

    - name: Build & Test with poetry
      run: |
        export CFLAGS="-Werror -coverage"
        export PYTHONDEVMODE=1
        poetry run pip install --no-build-isolation -e .
        poetry run coverage run --branch -m pytest
        poetry run coverage xml -i

    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v4

    - name: Run linters
      if: ${{ matrix.python-version != 'pypy-3.10' }}
      run: |
        poetry run flake8
        poetry run mypy .

    - name: Build docs
      if: ${{ matrix.python-version != 'pypy-3.10' }}
      run: |
        poetry run python -m sphinx -W -a -E -b html -n docs docs/_build

  msvc:
    runs-on: windows-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']
        architecture: ['x64']
        include:
          - python-version: '3.9'
            architecture: 'x86'
          - python-version: '3.13'
            architecture: 'x86'

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python ${{ matrix.python-version }}
        id: setup-python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
          architecture: ${{ matrix.architecture }}
          allow-prereleases: true

      - name: Setup MSVC
        uses: ilammy/msvc-dev-cmd@v1.13.0
        with:
          arch : ${{ matrix.architecture }}

      - name: Download and extract Cairo Binary
        run: |
          python .ci/download-cairo-win32.py

      - name: Install dependencies
        run: |
          pipx install --python "${{ steps.setup-python.outputs.python-path }}" poetry
          pipx install --python "${{ steps.setup-python.outputs.python-path }}" meson
          poetry install

      - name: Build & Test with meson
        env:
          PKG_CONFIG: ${{ github.workspace }}/cairo-prebuild/bin/pkgconf.exe
          PKG_CONFIG_PATH: ${{ github.workspace }}/cairo-prebuild/lib/pkgconfig
          CFLAGS: "-DCAIRO_WIN32_STATIC_BUILD=1"
        run: |
          # XXX: pass python, so global python3 doesn't win
          poetry run meson setup -Dpython=python --werror _build
          poetry run meson compile -C _build
          poetry run meson test -v -C _build
          if (-not $?) { exit 1 }
          rm -r _build

      - name: Build & Test with poetry
        env:
          PKG_CONFIG: ${{ github.workspace }}/cairo-prebuild/bin/pkgconf.exe
          PKG_CONFIG_PATH: ${{ github.workspace }}/cairo-prebuild/lib/pkgconfig
          CFLAGS: "-DCAIRO_WIN32_STATIC_BUILD=1"
          PYTHONDEVMODE: 1
        run: |
          poetry run pip install --no-build-isolation -e .
          poetry run coverage run --branch -m pytest
          if (-not $?) { exit 1 }
          poetry run coverage xml -i

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v4

  macos:
    runs-on: macos-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install dependencies
        run: |
          brew install pkg-config cairo python meson poetry ninja
          poetry install

      - name: Build & Test with meson
        run: |
          poetry run meson setup --werror _build
          poetry run meson compile -C _build
          poetry run meson test -v -C _build
          rm -Rf _build

      - name: Build & Test with poetry
        run: |
          export CFLAGS="-Werror -coverage"
          export PYTHONDEVMODE=1
          poetry run pip install --no-build-isolation -e .
          poetry run coverage run --branch -m pytest
          poetry run coverage xml -i

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v4
