GitLab CI with r testthat package
Asked Answered
L

2

9

Can anyone run testthat tests for a minimal R package using GitLab.com continuous integration tools? My attempt:
https://gitlab.com/djchapman/CI_example
This is the .gitlab-CI.yml text I am using,

image: rocker/rstudio
test:
   script:
    - R -e 'install.packages(c("devtools", "testthat"))'
    - R CMD build . --no-build-vignettes --no-manual
    - PKG_FILE_NAME=$(ls -1t *.tar.gz | head -n 1)
    - R CMD check "${PKG_FILE_NAME}" --no-build-vignettes --no-manual
    - R -e 'devtools::test()'

It is adapted from this website. I realize that devtools has dependencies which may need to be included as packages are installed, and I tried that, but the libraries for git2r didn't seem to install correctly, and now I wonder if I'm going about it wrong. Thanks.

Lucrece answered 15/8, 2018 at 21:49 Comment(1)
Do you get any errors? Why do you call devtools::test()? Checking a package includes running the tests.Carollcarolle
C
7

You do not need to run the tests via devtools since R CMD check does that already. The following should work:

image: rocker/rstudio
test:
   script:
    - R -e 'install.packages(c("testthat"))'
    - R CMD build . --no-build-vignettes --no-manual
    - PKG_FILE_NAME=$(ls -1t *.tar.gz | head -n 1)
    - R CMD check "${PKG_FILE_NAME}" --no-build-vignettes --no-manual

Alternatively you could use an image that allows for binary installs:

image: rocker/r-base
test:
   script:
    - apt-get update
    - apt-get install --yes --no-install-recommends r-cran-testthat r-cran-devtools
    - R -e "devtools::install_deps()"
    - R CMD build . --no-build-vignettes --no-manual
    - PKG_FILE_NAME=$(ls -1t *.tar.gz | head -n 1)
    - R CMD check "${PKG_FILE_NAME}" --no-build-vignettes --no-manual

This is useful if you have dependencies that have not been packaged for Debian, or if you don't want to update your CI script when you add a new dependency.

Carollcarolle answered 16/8, 2018 at 9:41 Comment(2)
Thank you Ralf! I tested both solutions and they passed. I did not fully understand R CMD check, and am inexperienced with yaml.Lucrece
Noting that it should read "devtools::install_deps(dependencies = TRUE)" if you have any tests that rely on packages that are in the suggests section of your DESCRIPTION file. The default behaviour is to ignore packages that are suggested.Vacla
L
2

For me this didn't work as expected. I found out that the problem was I had vignettes. Using the following content of my .gitlab-ci.yml, I worked around that problem:

image: rocker/r-base
gitlab:
   script:
    - apt-get update
    # install dependencies for package
    - apt-get install --yes --no-install-recommends r-cran-xml2 r-cran-testthat r-cran-devtools
    - R -e 'devtools::install_deps(dependencies = c("Depends", "Imports", "Suggests"))'
    # remove vignettes folder and get VignetteBuilder field out of DESCRIPTION file
    - rm -rf vignettes
    - R -e 'd <- read.dcf("DESCRIPTION"); d[, colnames(d) == "VignetteBuilder"] <- NA; write.dcf(d, "DESCRIPTION")'
    - R CMD build . --no-build-vignettes --no-manual
    - PKG_FILE_NAME=$(ls -1t *.tar.gz | head -n 1)
    - R CMD check "${PKG_FILE_NAME}" --no-build-vignettes --no-manual --as-cran
    # update code coverage
    - apt-get install --yes git
    - R -e "covr::codecov(token = 'mytoken')"

It removes the vignettes folder and removes the VignetteBuilder field from the DESCRIPTION file before it starts building. Quite convenient, although I now cannot test the contents of it.

After a while I found out that covr::codecov() works really well if you give the token as input (from https://codecov.io/gl/yourname/yourproject/settings) and install git on beforehand.

Lau answered 23/10, 2018 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.