Debian Codegraph @

tests/testthat/test-vendor.R

raw
1
2 test_that("renv itself doesn't mark itself as embedded", {
3 expect_false(renv_metadata_embedded())
4 expect_equal(
5 c(renv_metadata_version()),
6 c(renv_namespace_version("renv"))
7 )
8 })
9
10 test_that("renv can be vendored into an R package", {
11 skip_on_cran()
12 skip_slow()
13
14 # create a dummy R package
15 project <- renv_tests_scope()
16
17 desc <- heredoc("
18 Type: Package
19 Package: test.renv.embedding
20 Version: 0.1.0
21 ")
22
23 writeLines(desc, con = "DESCRIPTION")
24 file.create("NAMESPACE")
25
26 # vendor renv
27 vendor()
28
29 # make sure renv is initializes in .onLoad()
30 code <- heredoc('
31 .onLoad <- function(libname, pkgname) {
32 renv$initialize(libname, pkgname)
33 }
34 ')
35
36 ensure_directory("R")
37 writeLines(code, con = "R/zzz.R")
38
39 # try installing the package
40 r_cmd_install("test.renv.embedding", getwd())
41
42 # test that we can load the package and initialize renv
43 code <- substitute({
44
45 # make sure renv isn't visible on library paths
46 base <- .BaseNamespaceEnv
47 base$.libPaths(path)
48
49 # load the package, and check that renv realizes it's embedded
50 namespace <- base$asNamespace("test.renv.embedding")
51 embedded <- namespace$renv$renv_metadata_embedded()
52 if (!embedded)
53 stop("internal error: renv is embedded but doesn't realize it")
54
55 # let parent process know we succeeded
56 writeLines(as.character(embedded))
57
58 }, list(path = .libPaths()[1]))
59
60 script <- renv_scope_tempfile("renv-script-", fileext = ".R")
61 writeLines(deparse(code), con = script)
62
63 # attempt to run script
64 output <- renv_system_exec(R(), c("--vanilla", "-s", "-f", renv_shell_path(script)))
65 expect_equal(output, "TRUE")
66
67 # test that we can use the embedded renv to run snapshot
68 code <- substitute({
69
70 # make sure renv isn't visible on library paths
71 base <- .BaseNamespaceEnv
72 base$.libPaths(path)
73
74 # try to list
75 ns <- base$asNamespace("test.renv.embedding")
76 deps <- ns$renv$dependencies()
77 saveRDS(deps, file = "dependencies.rds")
78
79 }, list(path = .libPaths()[1]))
80
81 script <- renv_scope_tempfile("renv-script-", fileext = ".R")
82 writeLines(deparse(code), con = script)
83
84 # attempt to run script
85 output <- renv_system_exec(R(), c("--vanilla", "-s", "-f", renv_shell_path(script)), quiet = FALSE)
86 expect_true(file.exists("dependencies.rds"))
87
88 })