name: Tests
on:
  push:
  pull_request:
  schedule:
    - cron: '0 6 * * 1'
jobs:
  all:
    if: github.event_name != 'schedule' || github.repository == 'libmbd/libmbd'
    name: All
    strategy:
      fail-fast: false
      matrix:
        include:
          - type: ubuntu
            mpi-nodes: 2
          - type: macos
            mpi-nodes: 2
          - type: conda
          - type: conda
            mpi: openmpi
            mpi-nodes: 8
            elsi: elsi
          - type: conda
            mpi: mpich
            mpi-nodes: 2
          - type: conda
            python-version: =3.6
            cmake-version: =3.14
            gfortran-version: =8
            mpi: openmpi
            mpi-nodes: 8
          - type: conda
            python-version: =3.6
            cmake-version: =3.14
            gfortran-version: =8
            mpi: mpich=3.4.3=\*_100
            mpi-nodes: 2
            elsi: elsi
    runs-on: ${{ fromJSON('{"ubuntu":"ubuntu-latest","macos":"macos-latest","conda":"ubuntu-latest"}')[matrix.type] }}
    env:
      CONDA_ALWAYS_YES: "true"
      CONDA_QUIET: "true"
    steps:
      - name: Set environment variables
        run: |
          case ${{ runner.os }} in
            Linux)
              XDG_CACHE_HOME=$HOME/.cache
              ;;
            macOS)
              XDG_CACHE_HOME=$HOME/Library/Caches
              ;;
          esac
          echo YEAR_MONTH=$(date +"%Y-%m") >>$GITHUB_ENV
          echo XDG_CACHE_HOME="$XDG_CACHE_HOME" >>$GITHUB_ENV
          if [[ "${{ matrix.mpi-nodes }}" ]]; then
            echo MPI_NODES=${{ matrix.mpi-nodes }} >>$GITHUB_ENV
          fi
          if [[ "${{ matrix.mpi }}" == "openmpi" || "${{ matrix.type }}" == "ubuntu" ]]; then
            echo MPIEXEC_EXTRA_FLAGS=--oversubscribe >>$GITHUB_ENV
          fi
          echo VIRTUAL_ENV=$HOME/env >>$GITHUB_ENV
          echo CONDA_PKGS_DIRS="$XDG_CACHE_HOME/conda/pkgs" >>$GITHUB_ENV
          echo $HOME/env/bin >>$GITHUB_PATH
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - uses: actions/cache@v2
        with:
          path: |
            ${{ env.XDG_CACHE_HOME }}/conda/pkgs
            ${{ env.XDG_CACHE_HOME }}/pip
          key: ${{ matrix.type }}-${{ matrix.python-version }}-${{ matrix.cmake-version }}-${{ matrix.gfortran-version }}-${{ matrix.mpi }}-${{ env.YEAR_MONTH }}-${{ hashFiles('pyproject.toml') }}
      - name: Install libMBD dependencies
        if: matrix.type == 'ubuntu'
        run: sudo apt-get install -yq --no-install-suggests --no-install-recommends gfortran libblas-dev liblapack-dev mpi-default-dev mpi-default-bin libscalapack-mpi-dev
      - name: Install libMBD dependencies
        if: matrix.type == 'macos'
        run: brew install open-mpi scalapack
      - name: Create Conda environment
        if: matrix.type == 'conda'
        run: conda create -p ${{ env.VIRTUAL_ENV }} -c conda-forge python${{ matrix.python-version }} cmake${{ matrix.cmake-version }} gfortran_linux-64${{ matrix.gfortran-version }} openblas ${{ matrix.mpi }} scalapack elsi numpy scipy mpi4py
      - name: Create Python virtual environment
        if: matrix.type != 'conda'
        run: |
          python3 -m venv ${{ env.VIRTUAL_ENV }}
          ${{ env.VIRTUAL_ENV }}/bin/pip install -U pip
      - name: Install coverage Python package
        run: pip install -U coverage
      - name: Report environment
        run: |
          type python
          type pip
          python --version
          cmake --version
          pip --version
          git describe --tags --dirty=.dirty
      - name: Set CMAKE_ARGS
        run: |
          FFLAGS=('-fprofile-arcs' '-ftest-coverage')
          CMAKE_ARGS=()
          if [[ "${{ matrix.mpi-nodes }}" ]]; then
              CMAKE_ARGS+=('-DENABLE_SCALAPACK_MPI=ON')
          fi
          if [[ "${{ matrix.elsi }}" == "elsi" ]]; then
              CMAKE_ARGS+=('-DENABLE_ELSI=ON')
          fi
          if [[ "${{ matrix.type }}" == "macos" ]]; then
              CMAKE_ARGS+=('-DCMAKE_Fortran_COMPILER=gfortran-12')
          fi
          if [[ "${{ matrix.type }}" == "ubuntu" ]]; then
            CMAKE_ARGS+=('-DCMAKE_IGNORE_PATH="/usr/lib/cmake/scalapack-2.1.0.openmpi;/lib/cmake/scalapack-2.1.0.openmpi"')
          fi
          echo CMAKE_ARGS=${CMAKE_ARGS[@]}
          echo CMAKE_ARGS=${CMAKE_ARGS[@]} >>$GITHUB_ENV
          echo FFLAGS=${FFLAGS[@]}
          echo FFLAGS=${FFLAGS[@]} >>$GITHUB_ENV
      - name: Run Cmake
        run: |
          [[ "${{ matrix.type }}" != "conda" ]] || { source $CONDA/etc/profile.d/conda.sh && conda activate $HOME/env && export CMAKE_ARGS="$CONDA_BACKUP_CMAKE_ARGS"; }
          make run_cmake
      - name: Build libMBD
        run: make build_libmbd -o run_cmake
      - name: Install libMBD
        run: make install_libmbd -o build_libmbd
      - name: Build & install pyMBD
        run: |
          [[ "${{ matrix.type }}" != "conda" ]] || { source $CONDA/etc/profile.d/conda.sh && conda activate $HOME/env; }
          make install -o install_libmbd
      - run: pip list
      - name: Test libMBD
        run: make test_libmbd
      - name: Test pyMBD
        run: |
          make test -o test_libmbd RUN_CMD="coverage run -m" | tee output
          ! grep failed output >/dev/null
      - name: Upload to Codecov
        run: bash <(curl -s https://codecov.io/bash) -f "!*#tests#*"
