Skip to content

Commit 2f0d2a7

Browse files
committed
test: test dock_from_renv with moked binding
why - local user install must not influence test output - update flat to reflect change in test file
1 parent d320329 commit 2f0d2a7

File tree

5 files changed

+57
-103
lines changed

5 files changed

+57
-103
lines changed

R/dock_from_renv.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,3 @@ dock_from_renv <- function(
245245
dock
246246
}
247247

248-

dev/flat_dock_from_renv.Rmd

Lines changed: 33 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -176,27 +176,32 @@ dock_from_renv <- function(
176176
}
177177
)
178178
179-
180-
pkg_sysreqs <- attempt::map_try_catch(
179+
180+
pkg_sysreqs <- unlist(attempt::map_try_catch(
181181
pkg_os,
182182
function(x) {
183-
do.call(
184-
pkg_sysreqs_mem,
185-
x
186-
) |>
187-
pluck("packages") |>
188-
keep_at("system_packages")
183+
keep_at(
184+
pluck(
185+
do.call(pkg_sysreqs_mem, x),
186+
"packages"
187+
),
188+
"system_packages"
189+
)
189190
},
190191
.e = ~ character(0)
191-
) |>
192-
unlist()
192+
))
193193
194194
195195
196196
197197
198-
pkg_installs <- unique(pkg_sysreqs) |>
199-
lapply( function(.x) {paste0(install_cmd, " ", .x)})
198+
pkg_installs <-
199+
lapply(
200+
X = unique(pkg_sysreqs),
201+
FUN = function(.x) {
202+
paste0(install_cmd, " ", .x)
203+
}
204+
)
200205
201206
if (length(unlist(pkg_installs)) == 0) {
202207
cat_bullet(
@@ -279,7 +284,6 @@ dock_from_renv <- function(
279284
dock
280285
}
281286
282-
283287
```
284288

285289
```{r example-dock_from_renv}
@@ -327,16 +331,23 @@ writeLines(renv_file, file.path(dir_build, "renv.lock"))
327331
328332
# dock_from_renv ----
329333
test_that("dock_from_renv works", {
330-
331-
334+
332335
# testthat::skip_on_cran()
333336
# skip_if_not(interactive())
334337
# Create Dockerfile
335338
skip_if(is_rdevel, "skip on R-devel")
336-
out <- dock_from_renv(
337-
lockfile = the_lockfile,
338-
FROM = "rocker/verse",
339+
340+
testthat::with_mocked_bindings(code = {
341+
out <- dock_from_renv(
342+
lockfile = the_lockfile,
343+
FROM = "rocker/verse",
344+
renv_version = "0.0.0"
345+
)
346+
},
347+
compact_sysreqs = function(...) "fake sys reqs",
348+
repos_as_character = function(...) "fake repos"
339349
)
350+
340351
expect_s3_class(
341352
out,
342353
"Dockerfile"
@@ -346,7 +357,6 @@ test_that("dock_from_renv works", {
346357
"R6"
347358
)
348359
349-
350360
# read Dockerfile
351361
out$write(
352362
file.path(
@@ -361,48 +371,17 @@ test_that("dock_from_renv works", {
361371
"Dockerfile"
362372
)
363373
)
364-
expect_equal(
365-
dock_created[1],
366-
"FROM rocker/verse:4.1.2"
367-
)
368374
369-
expect_length(
370-
grep("RUN R -e 'renv::restore\\(\\)'", dock_created),
371-
1
375+
dock_expected <- readLines(
376+
testthat::test_path("renv_Dockerfile")
372377
)
373378
379+
expect_equal(dock_created, dock_expected)
380+
374381
skip_if(is_rdevel, "Skip R-devel")
375382
#python3 is not a direct dependencies from custom_packages
376383
expect_false( any(grepl("python3",out$Dockerfile)))
377-
378-
# System dependencies are different when build in interactive environment?
379-
# yes. strange.
380-
skip_if_not(interactive())
381-
dir.create(
382-
file.path(
383-
dir_build,
384-
"inst"
385-
)
386-
)
387-
file.copy(
388-
file.path(
389-
dir_build,
390-
"Dockerfile"
391-
),
392-
file.path(
393-
dir_build,
394-
"inst"
395-
),
396-
overwrite = TRUE
397-
)
398-
dock_expected <- readLines(
399-
system.file(
400-
"renv_Dockefile",
401-
package = "dockerfiler"
402-
)
403-
)
404384
405-
expect_equal(dock_created, dock_expected)
406385
})
407386
# rstudioapi::navigateToFile(file.path(dir_build, "Dockerfile"))
408387

inst/renv_Dockefile

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/testthat/renv_Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM rocker/verse:4.1.2
2+
RUN fake sys reqs
3+
RUN mkdir -p /usr/local/lib/R/etc/ /usr/lib/R/etc/
4+
RUN echo "options(renv.config.pak.enabled = FALSE, repos = fake repos, download.file.method = 'libcurl', Ncpus = 4)" | tee /usr/local/lib/R/etc/Rprofile.site | tee /usr/lib/R/etc/Rprofile.site
5+
RUN R -e 'install.packages("remotes")'
6+
RUN R -e 'remotes::install_version("renv", version = "0.0.0")'
7+
COPY renv.lock renv.lock
8+
RUN R -e 'renv::restore()'

tests/testthat/test-dock_from_renv.R

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,23 @@ writeLines(renv_file, file.path(dir_build, "renv.lock"))
3636

3737
# dock_from_renv ----
3838
test_that("dock_from_renv works", {
39-
40-
39+
4140
# testthat::skip_on_cran()
4241
# skip_if_not(interactive())
4342
# Create Dockerfile
4443
skip_if(is_rdevel, "skip on R-devel")
45-
out <- dock_from_renv(
46-
lockfile = the_lockfile,
47-
FROM = "rocker/verse",
44+
45+
testthat::with_mocked_bindings(code = {
46+
out <- dock_from_renv(
47+
lockfile = the_lockfile,
48+
FROM = "rocker/verse",
49+
renv_version = "0.0.0"
50+
)
51+
},
52+
compact_sysreqs = function(...) "fake sys reqs",
53+
repos_as_character = function(...) "fake repos"
4854
)
55+
4956
expect_s3_class(
5057
out,
5158
"Dockerfile"
@@ -55,7 +62,6 @@ test_that("dock_from_renv works", {
5562
"R6"
5663
)
5764

58-
5965
# read Dockerfile
6066
out$write(
6167
file.path(
@@ -70,48 +76,17 @@ test_that("dock_from_renv works", {
7076
"Dockerfile"
7177
)
7278
)
73-
expect_equal(
74-
dock_created[1],
75-
"FROM rocker/verse:4.1.2"
76-
)
7779

78-
expect_length(
79-
grep("RUN R -e 'renv::restore\\(\\)'", dock_created),
80-
1
80+
dock_expected <- readLines(
81+
testthat::test_path("renv_Dockerfile")
8182
)
8283

84+
expect_equal(dock_created, dock_expected)
85+
8386
skip_if(is_rdevel, "Skip R-devel")
8487
#python3 is not a direct dependencies from custom_packages
8588
expect_false( any(grepl("python3",out$Dockerfile)))
86-
87-
# System dependencies are different when build in interactive environment?
88-
# yes. strange.
89-
skip_if_not(interactive())
90-
dir.create(
91-
file.path(
92-
dir_build,
93-
"inst"
94-
)
95-
)
96-
file.copy(
97-
file.path(
98-
dir_build,
99-
"Dockerfile"
100-
),
101-
file.path(
102-
dir_build,
103-
"inst"
104-
),
105-
overwrite = TRUE
106-
)
107-
dock_expected <- readLines(
108-
system.file(
109-
"renv_Dockefile",
110-
package = "dockerfiler"
111-
)
112-
)
11389

114-
expect_equal(dock_created, dock_expected)
11590
})
11691
# rstudioapi::navigateToFile(file.path(dir_build, "Dockerfile"))
11792

0 commit comments

Comments
 (0)