Skip to content

Commit

Permalink
add flag to dock_from_renv function (#60)
Browse files Browse the repository at this point in the history
* add keep_renv_version flag to dock_from_renv function and associated test

* fixed typo

* Update dock_from_renv.R
  • Loading branch information
campbead authored Oct 29, 2023
1 parent ab1a559 commit e3222d4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
22 changes: 20 additions & 2 deletions R/dock_from_renv.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ available_distros <- c(
#' @param repos character. The URL(s) of the repositories to use for `options("repos")`.
#' @param extra_sysreqs character vector. Extra debian system requirements.
#' Will be installed with apt-get install.
#' @param keep_renv_version boolean. If `TRUE` the version of renv in the lockfile
#' will be used for the `renv::restore()` command
#' @importFrom utils getFromNamespace
#' @return A R6 object of class `Dockerfile`.
#' @details
Expand Down Expand Up @@ -52,7 +54,8 @@ dock_from_renv <- function(
sysreqs = TRUE,
repos = c(CRAN = "https://cran.rstudio.com/"),
expand = FALSE,
extra_sysreqs = NULL
extra_sysreqs = NULL,
keep_renv_version = FALSE
) {
distro <- match.arg(distro, available_distros)

Expand All @@ -72,6 +75,9 @@ dock_from_renv <- function(
AS = AS
)

# get renv version
renv_version <- lock$data()$Packages$renv$Version

distro_args <- switch(
distro,
centos7 = list(
Expand Down Expand Up @@ -235,7 +241,19 @@ dock_from_renv <- function(
)
)

dock$RUN("R -e 'install.packages(c(\"renv\",\"remotes\"))'")
# check if keep_renv_version is true
if (keep_renv_version){
dock$RUN("R -e 'install.packages(\"remotes\")'")
install_renv_string <- paste0(
"R -e 'remotes::install_version(\"renv\", version = ",
renv_version,
")'"
)
dock$RUN(install_renv_string)

} else {
dock$RUN("R -e 'install.packages(c(\"renv\",\"remotes\"))'")
}

dock$COPY(basename(lockfile), "renv.lock")
dock$RUN(r(renv::restore()))
Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/test-dock_from_renv.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,36 @@ renv_file <- readLines(file.path(dir_build, "renv.lock"))
renv_file[grep("Version", renv_file)[1]] <- ' "Version": "4.1.2",'
writeLines(renv_file, file.path(dir_build, "renv.lock"))

test_that("dock_from_renv works with old renv", {
out <- dock_from_renv(
lockfile = the_lockfile,
distro = "focal",
FROM = "rocker/verse",
keep_renv_version = TRUE
)

out$write(
file.path(
dir_build,
"Dockerfile"
)
)

dock_created <- readLines(
file.path(
dir_build,
"Dockerfile"
)
)

test_string <- "remotes::install.version\\(\"renv\", version = 1.0.2\\)"
message(test_string)
expect_length(
grep(test_string , dock_created), 1
)

})

# dock_from_renv ----
test_that("dock_from_renv works", {
# skip_if_not(interactive())
Expand Down

0 comments on commit e3222d4

Please sign in to comment.