# ==================================================================== #
# TITLE:                                                               #
# AMR: An R Package for Working with Antimicrobial Resistance Data     #
#                                                                      #
# SOURCE CODE:                                                         #
# https://github.com/msberends/AMR                                     #
#                                                                      #
# PLEASE CITE THIS SOFTWARE AS:                                        #
# Berends MS, Luz CF, Friedrich AW, et al. (2022).                     #
# AMR: An R Package for Working with Antimicrobial Resistance Data.    #
# Journal of Statistical Software, 104(3), 1-31.                       #
# https://doi.org/10.18637/jss.v104.i03                                #
#                                                                      #
# Developed at the University of Groningen and the University Medical  #
# Center Groningen in The Netherlands, in collaboration with many      #
# colleagues from around the world, see our website.                   # 
#                                                                      #
# This R package is free software; you can freely use and distribute   #
# it for both personal and commercial purposes under the terms of the  #
# GNU General Public License version 2.0 (GNU GPL-2), as published by  #
# the Free Software Foundation.                                        #
# We created this package for both routine data analysis and academic  #
# research and it was publicly released in the hope that it will be    #
# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY.              #
#                                                                      #
# Visit our website for the full manual and a complete tutorial about  #
# how to conduct AMR data analysis: https://msberends.github.io/AMR/   #
# ==================================================================== #

on:
  push:
     # only run after a git push on any branch in this repo
    branches: '**'

name: check-old

jobs:
  R-code-check:
    runs-on: ${{ matrix.config.os }}

    continue-on-error: ${{ matrix.config.allowfail }}

    name: ${{ matrix.config.os }} (R-${{ matrix.config.r }})

    strategy:
      fail-fast: false
      matrix:
        config:
          # Test all old versions of R >= 3.0, we support them all!
          # For these old versions, dependencies and vignettes will not be checked.
          # For recent R versions, see check-recent.yaml (r-lib and tidyverse support the latest 5 major R releases).
          - {os: ubuntu-latest, r: '3.6', allowfail: false}
          # - {os: windows-latest, r: '3.5', allowfail: true} # always fails, horrible with UTF-8
          - {os: ubuntu-latest, r: '3.4', allowfail: false}
          - {os: ubuntu-latest, r: '3.3', allowfail: false}
          - {os: ubuntu-latest, r: '3.2', allowfail: false}
          - {os: ubuntu-latest, r: '3.1', allowfail: false}
          - {os: ubuntu-latest, r: '3.0', allowfail: false}

    env:
      R_REMOTES_NO_ERRORS_FROM_WARNINGS: true

    steps:
      - uses: actions/checkout@v3

      - uses: r-lib/actions/setup-r@v2
        with:
          r-version: ${{ matrix.config.r }}

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

      - name: Install tinytest from CRAN
        run: |
          install.packages("tinytest", repos = "https://cran.r-project.org")
        shell: Rscript {0}

      - name: Show session info
        if: always()
        run: |
          options(width = 100)
          utils::sessionInfo()
          as.data.frame(utils::installed.packages())[, "Version", drop = FALSE]
        shell: Rscript {0}

      - name: Remove vignettes
        if: always() # matrix.config.r == '3.0' || matrix.config.r == '3.1' || matrix.config.r == '3.2' || matrix.config.r == '3.3'
        # writing to DESCRIPTION2 and then moving to DESCRIPTION is required for R <= 3.3 as writeLines() cannot overwrite
        run: |
          rm -rf vignettes
          Rscript -e "writeLines(readLines('DESCRIPTION')[!grepl('VignetteBuilder', readLines('DESCRIPTION'))], 'DESCRIPTION2')"
          rm DESCRIPTION
          mv DESCRIPTION2 DESCRIPTION
        shell: bash

      - name: Run R CMD check
        if: always()
        env:
          # see https://rstudio.github.io/r-manuals/r-ints/Tools.html for an overview
          _R_CHECK_CRAN_INCOMING_: false
          _R_CHECK_DEPENDS_ONLY_: true
          _R_CHECK_LENGTH_1_CONDITION_: verbose
          _R_CHECK_LENGTH_1_LOGIC2_: verbose
          # no check for old R versions - these packages require higher R versions
          _R_CHECK_RD_XREFS_: false
          _R_CHECK_FORCE_SUGGESTS_: false
          R_CHECK_CONSTANTS: 5
          R_JIT_STRATEGY: 3
          # during 'R CMD check', R_LIBS_USER will be overwritten, so:
          R_LIBS_USER_GH_ACTIONS: ${{ env.R_LIBS_USER }}
          # this is a required value to run the unit tests:
          R_RUN_TINYTEST: true
        run: |
          cd ..
          R CMD build AMR
          R CMD check --as-cran --no-manual --run-donttest --run-dontrun AMR_*.tar.gz
        shell: bash

      - name: Show tinytest output
        if: always()
        run: |
          cd ../AMR.Rcheck
          find . -name 'tinytest.Rout*' -exec cat '{}' \; || true
        shell: bash