name: Test installation

on:
  pull_request:

  push:
    branches:
      - main

jobs:
  test-install:
    name: Test pip install

    defaults:
      run:
        shell: bash {0}

    strategy:
      fail-fast: false

      matrix:
          os: [ubuntu-latest, macos-latest, windows-latest]
          python-version: ["3.8", "3.12"]
          nocc: [false]

          include:
            # also test whether installation without C compiler works
            - os: ubuntu-latest
              python-version: "3.8"
              nocc: true

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v2

      - name: Set up Python ${{ matrix.python-version }} on ${{ matrix.os }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install CUDA
        if: runner.os == 'Linux' && matrix.nocc != 'true'
        run: |
          sudo apt-get update
          sudo apt-get install nvidia-cuda-toolkit

      - name: Test installing Veros
        run: |
          if ${{ matrix.nocc }}; then export CC=foobar; fi
          python -m pip install .

      - name: Build extensions
        if: matrix.nocc != 'true'
        run: |
          export VEROS_REQUIRE_CYTHON_EXT=1
          if [ ${{ runner.os }} = "Linux" ]; then
            export VEROS_REQUIRE_CUDA_EXT=1
          fi
          pip install -U setuptools wheel cython
          python setup.py build_ext --inplace

      - name: Test whether Cython extensions are present
        if: matrix.nocc != 'true'
        run: |
          if [ ${{ runner.os }} = "Windows" ]; then
            test -f veros/core/special/tdma_cython_.*.pyd
          else
            test -f veros/core/special/tdma_cython_.*.so
          fi

      - name: Test whether CUDA extensions are present
        if: runner.os == 'Linux' && matrix.nocc != 'true'
        run: |
          test -f veros/core/special/tdma_cuda_.*.so
