| 1 |
|
| 2 |
renv_scoped_sandbox <- function(scope = parent.frame()) { |
| 3 |
renv_scope_options(renv.config.sandbox.enabled = TRUE, scope = scope) |
| 4 |
|
| 5 |
sandbox <- renv_scope_tempfile(scope = scope) |
| 6 |
renv_scope_envvars(RENV_PATHS_SANDBOX = sandbox, scope = scope) |
| 7 |
|
| 8 |
old <- list(.Library.site, .Library, .libPaths()) |
| 9 |
defer(scope = scope, { |
| 10 |
unlink(sandbox, recursive = TRUE, force = TRUE) |
| 11 |
renv_binding_replace(base, ".Library.site", old[[1L]]) |
| 12 |
renv_binding_replace(base, ".Library", old[[2L]]) |
| 13 |
.libPaths(old[[3]]) |
| 14 |
}) |
| 15 |
} |
| 16 |
|
| 17 |
test_that("the sandbox can be activated and deactivated", { |
| 18 |
|
| 19 |
renv_scoped_sandbox() |
| 20 |
|
| 21 |
# save current library paths |
| 22 |
libpaths <- .libPaths() |
| 23 |
|
| 24 |
# after activating the sandbox, .Library should be changed |
| 25 |
syslib <- renv_libpaths_system() |
| 26 |
renv_sandbox_activate() |
| 27 |
expect_false(identical(syslib, .Library)) |
| 28 |
|
| 29 |
# after deactivating the sandbox, .Library should be restored |
| 30 |
renv_sandbox_deactivate() |
| 31 |
expect_equal(syslib, .Library) |
| 32 |
|
| 33 |
# the library paths should be restored as well |
| 34 |
expect_equal(libpaths, .libPaths()) |
| 35 |
|
| 36 |
}) |
| 37 |
|
| 38 |
test_that("multiple attempts to activate sandbox are handled", { |
| 39 |
|
| 40 |
renv_scoped_sandbox() |
| 41 |
|
| 42 |
libpaths <- .libPaths() |
| 43 |
syslib <- renv_libpaths_system() |
| 44 |
|
| 45 |
# calls to renv_sandbox_activate() should be idempotent |
| 46 |
renv_sandbox_activate() |
| 47 |
renv_sandbox_activate() |
| 48 |
renv_sandbox_activate() |
| 49 |
expect_false(identical(syslib, .Library)) |
| 50 |
|
| 51 |
# deactivate the sandbox and assert we've restored state |
| 52 |
renv_sandbox_deactivate() |
| 53 |
expect_equal(syslib, .Library) |
| 54 |
expect_equal(libpaths, .libPaths()) |
| 55 |
|
| 56 |
}) |
| 57 |
|
| 58 |
test_that(".Library.site isn't used even when sandbox is disabled", { |
| 59 |
|
| 60 |
skip_if(renv_platform_windows() || empty(.Library.site)) |
| 61 |
|
| 62 |
renv_scope_options(renv.config.sandbox.enabled = FALSE) |
| 63 |
|
| 64 |
sandbox <- renv_scope_tempfile("renv-sandbox-") |
| 65 |
renv_scope_envvars(RENV_PATHS_SANDBOX = sandbox) |
| 66 |
|
| 67 |
sitelib <- setdiff(.Library.site, .Library) |
| 68 |
renv_sandbox_activate() |
| 69 |
expect_false(any(sitelib %in% .libPaths())) |
| 70 |
renv_sandbox_deactivate() |
| 71 |
|
| 72 |
}) |
| 73 |
|
| 74 |
test_that("renv repairs library paths on load if sandbox is active", { |
| 75 |
|
| 76 |
renv_scoped_sandbox() |
| 77 |
|
| 78 |
libpaths <- .libPaths() |
| 79 |
syslib <- renv_libpaths_system() |
| 80 |
|
| 81 |
# initialize the sandbox |
| 82 |
renv_sandbox_activate() |
| 83 |
expect_false(identical(syslib, .Library)) |
| 84 |
|
| 85 |
# check for sandbox marker file |
| 86 |
marker <- file.path(.Library, ".renv-sandbox") |
| 87 |
expect_true(file.exists(marker)) |
| 88 |
|
| 89 |
# set up R_LIBS, and then run a child process that reports |
| 90 |
# the library paths after loading renv |
| 91 |
script <- renv_test_code({ |
| 92 |
renv:::summon() |
| 93 |
writeLines(.libPaths()) |
| 94 |
}) |
| 95 |
|
| 96 |
output <- local({ |
| 97 |
renv_scope_envvars(R_LIBS = paste(.libPaths(), collapse = .Platform$path.sep)) |
| 98 |
renv_system_exec( |
| 99 |
command = R(), |
| 100 |
args = c("--vanilla", "-s", "-f", renv_shell_path(script)), |
| 101 |
action = "testing sandbox" |
| 102 |
) |
| 103 |
}) |
| 104 |
|
| 105 |
expect_equal(output, .libPaths()) |
| 106 |
|
| 107 |
# deactivate the sandbox and assert we've restored state |
| 108 |
renv_sandbox_deactivate() |
| 109 |
expect_equal(syslib, .Library) |
| 110 |
expect_equal(libpaths, .libPaths()) |
| 111 |
|
| 112 |
}) |
| 113 |
|
| 114 |
test_that("multiple processes can attempt to acquire the sandbox", { |
| 115 |
|
| 116 |
skip_on_cran() |
| 117 |
skip_on_ci() |
| 118 |
|
| 119 |
renv_scoped_sandbox() |
| 120 |
|
| 121 |
# number of processes |
| 122 |
n <- 20 |
| 123 |
|
| 124 |
server <- tryCatch(renv_socket_server(), error = skip) |
| 125 |
defer(close(server$socket)) |
| 126 |
|
| 127 |
project <- renv_tests_scope() |
| 128 |
|
| 129 |
script <- renv_test_code({ |
| 130 |
renv:::summon() |
| 131 |
renv_sandbox_activate(project) |
| 132 |
conn <- renv_socket_connect(port = port, open = "wb") |
| 133 |
defer(close(conn)) |
| 134 |
writeLines(paste(Sys.getpid()), con = conn) |
| 135 |
}, list(project = project, file = file, port = server$port)) |
| 136 |
|
| 137 |
for (i in seq_len(n)) { |
| 138 |
system2( |
| 139 |
command = R(), |
| 140 |
args = c("--vanilla", "-s", "-f", renv_shell_path(script)), |
| 141 |
stdout = FALSE, |
| 142 |
stderr = FALSE, |
| 143 |
wait = FALSE |
| 144 |
) |
| 145 |
} |
| 146 |
|
| 147 |
stk <- stack() |
| 148 |
for (i in seq_len(n)) local({ |
| 149 |
conn <- renv_socket_accept(server$socket, open = "rb", timeout = 10) |
| 150 |
defer(close(conn)) |
| 151 |
stk$push(readLines(conn)) |
| 152 |
}) |
| 153 |
|
| 154 |
expect_length(stk$data(), n) |
| 155 |
|
| 156 |
}) |