name: CI

on:
  push:
    branches:
    - main
  pull_request:
    branches:
    - main

concurrency:
    group: ${{ github.workflow }}-${{ github.ref }}
    cancel-in-progress: True

jobs:
    test:
        name: "Python ${{ matrix.python-version }} on ${{ matrix.os }}"
        runs-on: ${{ matrix.os }}

        strategy:
            fail-fast: false
            matrix:
                include:
                    - os: ubuntu-latest
                      python-version: "3.7"
                      envfile: ".github/environment-minimal.yml"
                      channel-priority: "flexible"
                      name: "Minimal dependencies"

                    - os: ubuntu-latest
                      python-version: "3.8"
                      channel-priority: "strict"
                      envfile: ".github/environment-ci.yml"

                    - os: ubuntu-latest
                      python-version: "3.9"
                      channel-priority: "strict"
                      envfile: ".github/environment-ci.yml"

                    - os: ubuntu-latest
                      python-version: "3.10"
                      channel-priority: "strict"
                      envfile: ".github/environment-ci.yml"

                    - os: ubuntu-latest
                      python-version: "3.11"
                      channel-priority: "strict"
                      envfile: ".github/environment-ci.yml"

                    - os: ubuntu-latest
                      python-version: "3.12"
                      channel-priority: "strict"
                      envfile: ".github/environment-ci.yml"

                    - os: macos-latest
                      python-version: "3.12"
                      channel-priority: "flexible"
                      envfile: ".github/environment-ci.yml"

                    - os: windows-latest
                      python-version: "3.12"
                      channel-priority: "strict"
                      envfile: ".github/environment-ci.yml"

        steps:
        - uses: actions/checkout@v4
          with:
            submodules: true

        - name: Install OS dependencies
          shell: bash -l {0}
          run: |
            case "${{ runner.os }}" in
            Linux)
              sudo apt-get update -yy
              sudo apt-get install -yy  libsamplerate0
              ;; 
            macOS)
              brew install libsamplerate
              ;;
            esac

        - name: Cache conda
          uses: actions/cache@v4
          env:
            # Increase this value to reset cache if etc/example-environment.yml has not changed
            CACHE_NUMBER: 0
          with:
            path: ~/conda_pkgs_dir
            key: ${{ runner.os }}-${{ matrix.python-version }}-conda-${{ env.CACHE_NUMBER }}-${{ hashFiles( matrix.envfile ) }}

        - name: Install Conda environment
          uses: conda-incubator/setup-miniconda@v3
          with:
            auto-update-conda: true
            python-version: ${{ matrix.python-version }}
            add-pip-as-python-dependency: true
            auto-activate-base: false
            activate-environment: test
            channel-priority: ${{ matrix.channel-priority }}
            environment-file: ${{ matrix.envfile }}
            use-only-tar-bz2: false # IMPORTANT: This needs to be set for caching to work properly!

        - name: Conda info
          shell: bash -l {0}
          run: |
            conda info -a
            conda list
            python -c "import scipy; scipy.show_config()"

        - name: Install librosa
          shell: bash -l {0}
          run: python -m pip install --upgrade-strategy only-if-needed -e .[tests]

        - name: Run pytest
          shell: bash -l {0}
          run: pytest

        - name: Upload coverage to Codecov
          uses: codecov/codecov-action@v4
          with:
            token: ${{ secrets.CODECOV_TOKEN }}
            files: ./coverage.xml
            flags: unittests
            env_vars: OS,PYTHON
            name: codecov-umbrella
            fail_ci_if_error: true
            verbose: true
