---
# Github Actions workflow to analyze CmdStanR code, test coverage
# yamllint disable rule:line-length

name: Test coverage

'on':
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  test-coverage-ubuntu:
    name: "Linux"
    if: "! contains(github.event.head_commit.message, '[ci skip]')"
    runs-on: ubuntu-20.04
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
      NOT_CRAN: true

    steps:
      - name: cmdstan env vars
        run: |
          echo "CMDSTAN_PATH=${HOME}/.cmdstan" >> $GITHUB_ENV
        shell: bash

      - uses: n1hility/cancel-previous-runs@v3
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          workflow: Test-coverage.yml
        if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'"
      - uses: actions/checkout@v3

      - uses: r-lib/actions/setup-r@v2.6.4
      - uses: r-lib/actions/setup-pandoc@v2.6.4

      - name: Install Ubuntu dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libcurl4-openssl-dev
          sudo apt-get install -y openmpi-bin openmpi-common openmpi-doc libopenmpi-dev

      - name: Query dependencies
        run: |
          install.packages('remotes')
          saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
        shell: Rscript {0}

      - name: Cache R packages
        uses: actions/cache@v3
        with:
          path: ${{ env.R_LIBS_USER }}
          key: ubuntu-r-4.0-1-${{ hashFiles('.github/depends.Rds') }}
          restore-keys: ubuntu-r-4.0-1-

      - name: Install dependencies
        run: |
          install.packages(c("remotes", "curl"), dependencies = TRUE)
          remotes::install_local(path = ".", INSTALL_opts = "--no-test-load")
          remotes::install_deps(dependencies = TRUE)
          remotes::install_cran("covr")
          remotes::install_cran("gridExtra")
        shell: Rscript {0}

      - name: Install cmdstan
        run: |
          cmdstanr::check_cmdstan_toolchain(fix = TRUE)
          cmdstanr::install_cmdstan(cores = 2)
        shell: Rscript {0}

      - name: Test coverage
        run: covr::codecov(type = "tests")
        shell: Rscript {0}

  test-coverage-windows:
    name: "Windows"
    if: "! contains(github.event.head_commit.message, '[ci skip]')"
    runs-on: windows-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
      NOT_CRAN: true
    steps:
      - uses: actions/checkout@v3

      - uses: r-lib/actions/setup-r@v2.6.4
        with:
          r-version: 'release'
          rtools-version: '42'

      - uses: r-lib/actions/setup-pandoc@v2.6.4

      - name: Query dependencies
        run: |
          install.packages('remotes')
          saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
        shell: Rscript {0}

      - name: Install dependencies
        run: |
          install.packages(c("remotes", "curl"), dependencies = TRUE)
          remotes::install_local(path = ".")
          cmdstanr::check_cmdstan_toolchain(fix = TRUE)
          cmdstanr::install_cmdstan(cores = 2, overwrite = TRUE)
          remotes::install_deps(dependencies = TRUE)
          remotes::install_cran("covr")
          remotes::install_cran("gridExtra")
        shell: Rscript {0}

      - name: Test coverage
        run: |
          options(covr.gcov = 'C:/rtools40/mingw64/bin/gcov.exe');
          covr::codecov(type = "tests", function_exclusions = "sample_mpi")
        shell: Rscript {0}
