name: Build, install, and test wheels.

on:
  workflow_call:
    inputs:
      python-version:
        required: true
        type: string
      r-version:
        required: true
        type: string
      os:
        required: true
        type: string
jobs:
  test:
    runs-on: ${{ inputs.os }}
    continue-on-error: ${{ startsWith(inputs.os, 'windows') && inputs.r-version == 'release' }}
    steps:
    - uses: actions/checkout@v3
    - name: Set up Python ${{ inputs.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ inputs.python-version }}
    - name: Set up R ${{ inputs.r-version }}
      uses: r-lib/actions/setup-r@v2
      with:
        r-version: ${{ inputs.r-version }}
        use-public-rspm: true
    - name: Set virtualenv activation command (Windows)
      if: startsWith(inputs.os, 'windows')
      shell: bash
      run: |
        echo "VENV_ACTIVATE=pyenv_base/Scripts/activate" >> $GITHUB_ENV
        echo "R_LIBRARY=''" >> $GITHUB_ENV
        echo "R_HOME=$(R RHOME)" >> $GITHUB_ENV
    - name: Set virtualenv activation command (POSIX)
      if: "!startsWith(inputs.os, 'windows')"
      run: |
        echo "VENV_ACTIVATE=source pyenv_base/bin/activate" >> $GITHUB_ENV
        echo "R_LIBRARY=export LD_LIBRARY_PATH=$(python -m rpy2.situation LD_LIBRARY_PATH)" >> $GITHUB_ENV
    - name: Create base virtualenv
      run: |
        python -m venv pyenv_base
        ${{ env.VENV_ACTIVATE }}
        python -m  pip install -U pip wheel
    - uses: actions/download-artifact@v4.1.7
      with:
        pattern: rpy2-rinterface-${{ inputs.os }}-py${{ inputs.python-version }}-r${{ inputs.r-version }}
        path: rpy2-rinterface/dist/
    - uses: actions/download-artifact@v4.1.7
      with:
        pattern: rpy2-robjects-${{ inputs.os }}
        path: rpy2-robjects/dist/
    - name: Built package path.
      shell: bash
      run: |
        ls rpy2-rinterface/dist/rpy2-rinterface-${{ inputs.os }}-py${{ inputs.python-version }}-r${{ inputs.r-version }}/*.whl
        ls rpy2-rinterface/dist/
        echo "RPY2_RINTERFACE_WHL_DIST=$(ls -1 rpy2-rinterface/dist/rpy2-rinterface-${{ inputs.os }}-py${{ inputs.python-version }}-r${{ inputs.r-version }}/*.whl | tail -n 1)" >> $GITHUB_ENV
        echo "RPY2_ROBJECTS_WHL_DIST=$(ls -1 rpy2-robjects/dist/rpy2-robjects-${{ inputs.os }}/*.whl | tail -n 1)" >> $GITHUB_ENV
    - name: Install package
      run: |
        ${{ env.VENV_ACTIVATE }}
        pip install ${{ env.RPY2_RINTERFACE_WHL_DIST }}
        pip install --pre ${{ env.RPY2_ROBJECTS_WHL_DIST }}
        python -m rpy2.situation
    - name: Set R_LIBRARY (Windows)
      if: startsWith(inputs.os, 'windows')
      run: |
        ${{ env.VENV_ACTIVATE }}
        echo "R_LIBRARY=''" >> $GITHUB_ENV
    - name: Set R_LIBRARY (POSIX)
      if: "!startsWith(inputs.os, 'windows')"
      run: |
        ${{ env.VENV_ACTIVATE }}
        echo "R_LIBRARY=export LD_LIBRARY_PATH=$(python -m rpy2.situation LD_LIBRARY_PATH):${LD_LIBRARY_PATH}" >> $GITHUB_ENV
    - name: Test with minimal dependencies
      run: |
        ${{ env.VENV_ACTIVATE }}
        echo "With requirements for rpy2-rinterface."
        python3 -m pip install --pre ${{ env.RPY2_RINTERFACE_WHL_DIST }}'[test]'
        ${{ env.R_LIBRARY }}
        python -c 'from rpy2.rinterface_lib import openrlib; print(openrlib.cffi_mode)'
        bash -e ./scripts/run_test_rinterface_min_deps.sh
        echo "With dev for rpy2-robjects."
        python3 -m pip install --pre ${{ env.RPY2_ROBJECTS_WHL_DIST }}'[test_minimal]'
        bash -e ./scripts/run_test_robjects_min_deps.sh
    - name: Test with numpy
      run: |
        ${{ env.VENV_ACTIVATE }}
        python -m pip install --pre ${{ env.RPY2_ROBJECTS_WHL_DIST}}'[numpy]'
        ${{ env.R_LIBRARY }}
        bash -e ./scripts/run_test_numpy.sh
    - name: Test with pandas
      run: |
        ${{ env.VENV_ACTIVATE }}
        python -m pip install --pre ${{ env.RPY2_ROBJECTS_WHL_DIST}}'[pandas]'
        ${{ env.R_LIBRARY }}
        bash -e ./scripts/run_test_pandas.sh
    - name: Test with ipython
      run: |
        ${{ env.VENV_ACTIVATE }}
        python -m pip install --pre ${{ env.RPY2_ROBJECTS_WHL_DIST}}'[test]'
        ${{ env.R_LIBRARY }}
        bash -e ./scripts/run_test_ipython.sh
    - name: Set RSPM
      if: startsWith(inputs.os, 'ubuntu')
      run: |
        echo "RSPM=\"https://packagemanager.rstudio.com/cran/__linux__/$(lsb_release -sc)/latest\"" >> $GITHUB_ENV
    - name: Install R dependencies
      run: |
        install.packages(c("ggplot2", "dplyr", "tidyr", "dbplyr", "lazyeval", "rlang"))
      shell: Rscript {0}
    - name: Test with R deps
      if: "!startsWith(inputs.os, 'windows')"
      run: |
        ${{ env.VENV_ACTIVATE }}
        ${{ env.R_LIBRARY }}
        bash -e ./scripts/run_test_rdeps.sh
    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v4.6.0
      if: inputs.os == 'ubuntu-22.04' && inputs.python-version == '3.10' && inputs.r-version == 'release'
      env:
        OS: ${{ runner.os }}
        PYTHON: ${{ inputs.python-version }}
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
        env_vars: OS,PYTHON
