| 1 |
|
| 2 |
# Code that needs to run once, before a suite of tests is run. |
| 3 |
# Here, "suite of tests" might also mean "a single test" interactively. |
| 4 |
renv_tests_setup <- function(scope = parent.frame()) { |
| 5 |
|
| 6 |
# make sure this only runs once |
| 7 |
if (!once()) |
| 8 |
return() |
| 9 |
|
| 10 |
# remove automatic tasks so we can capture explicitly in tests |
| 11 |
renv_task_unload() |
| 12 |
|
| 13 |
# cache path before working directory gets changed |
| 14 |
renv_tests_root() |
| 15 |
|
| 16 |
# make sure required packages are loaded |
| 17 |
# (not scoped to the environment since packages can't reliably be unloaded) |
| 18 |
renv_tests_setup_packages() |
| 19 |
|
| 20 |
# fix up the library paths if needed for testing |
| 21 |
renv_tests_setup_libpaths(scope = scope) |
| 22 |
|
| 23 |
# make sure we clean up sandbox on exit |
| 24 |
renv_tests_setup_sandbox(scope = scope) |
| 25 |
|
| 26 |
# initialize test repositories |
| 27 |
renv_tests_setup_repos(scope = scope) |
| 28 |
|
| 29 |
# scope relevant environment variables |
| 30 |
renv_tests_setup_envvars(scope = scope) |
| 31 |
renv_tests_setup_options(scope = scope) |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
renv_tests_setup_envvars <- function(scope = parent.frame()) { |
| 37 |
|
| 38 |
# set up root directory |
| 39 |
root <- renv_scope_tempfile("renv-root-", scope = scope) |
| 40 |
ensure_directory(root) |
| 41 |
|
| 42 |
# set up sandbox directory |
| 43 |
sandbox <- file.path(root, "sandbox") |
| 44 |
ensure_directory(sandbox) |
| 45 |
|
| 46 |
renv_scope_envvars( |
| 47 |
RENV_AUTOLOAD_ENABLED = FALSE, |
| 48 |
RENV_CONFIG_LOCKING_ENABLED = FALSE, |
| 49 |
RENV_DOWNLOAD_METHOD = NULL, |
| 50 |
RENV_PATHS_ROOT = root, |
| 51 |
RENV_PATHS_LIBRARY = NULL, |
| 52 |
RENV_PATHS_LIBRARY_ROOT = NULL, |
| 53 |
RENV_PATHS_LOCAL = NULL, |
| 54 |
RENV_PATHS_LOCKFILE = NULL, |
| 55 |
RENV_PATHS_RENV = NULL, |
| 56 |
RENV_PATHS_SANDBOX = sandbox, |
| 57 |
RENV_WATCHDOG_ENABLED = FALSE, |
| 58 |
RENV_WATCHDOG_DEBUG = FALSE, |
| 59 |
scope = scope |
| 60 |
) |
| 61 |
|
| 62 |
envvars <- Sys.getenv() |
| 63 |
configvars <- grep("^RENV_CONFIG_", names(envvars), value = TRUE) |
| 64 |
renv_scope_envvars( |
| 65 |
list = rep_named(configvars, list(NULL)), |
| 66 |
scope = scope |
| 67 |
) |
| 68 |
} |
| 69 |
|
| 70 |
renv_tests_setup_options <- function(scope = parent.frame()) { |
| 71 |
|
| 72 |
renv_scope_options( |
| 73 |
renv.bootstrap.quiet = TRUE, |
| 74 |
renv.caution.verbose = interactive(), |
| 75 |
renv.config.install.transactional = FALSE, |
| 76 |
renv.config.sandbox.enabled = TRUE, |
| 77 |
renv.config.user.library = FALSE, |
| 78 |
renv.consent = TRUE, |
| 79 |
renv.tests.running = TRUE, |
| 80 |
restart = NULL, |
| 81 |
scope = scope |
| 82 |
) |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
# Force loading of packages from current .libPaths(); needed for packages |
| 87 |
# that would otherwise loaded in a renv_tests_scope() |
| 88 |
renv_tests_setup_packages <- function() { |
| 89 |
|
| 90 |
# load recursive dependencies of testthat |
| 91 |
deps <- renv_package_dependencies("testthat") |
| 92 |
for (dep in names(deps)) |
| 93 |
requireNamespace(dep, quietly = TRUE) |
| 94 |
|
| 95 |
# also load remotes |
| 96 |
requireNamespace("remotes", quietly = TRUE) |
| 97 |
|
| 98 |
# pak needs a little special handling |
| 99 |
if (renv_package_installed("pak")) { |
| 100 |
|
| 101 |
# set environment variables that influence pak |
| 102 |
usr <- file.path(tempdir(), "usr-cache") |
| 103 |
ensure_directory(file.path(usr, "R/renv")) |
| 104 |
|
| 105 |
pkg <- file.path(tempdir(), "pkg-cache") |
| 106 |
ensure_directory(pkg) |
| 107 |
|
| 108 |
renv_scope_envvars( |
| 109 |
R_USER_CACHE_DIR = usr, |
| 110 |
R_PKG_CACHE_DIR = pkg |
| 111 |
) |
| 112 |
|
| 113 |
# load pak now |
| 114 |
requireNamespace("pak", quietly = TRUE) |
| 115 |
|
| 116 |
# trigger package load in pak subprocess |
| 117 |
# |
| 118 |
# TODO(Kevin): This fails for me with: |
| 119 |
# |
| 120 |
# Error in `source_file()`: |
| 121 |
# ! In path: "/Users/kevin/r/pkg/renv/tests/testthat/helper-zzz.R" |
| 122 |
# Caused by error in `pak$remote()`: |
| 123 |
# ! Subprocess is busy or cannot start |
| 124 |
tryCatch({ |
| 125 |
pak <- renv_namespace_load("pak") |
| 126 |
pak$remote(function() {}) |
| 127 |
}, error = function(e) { |
| 128 |
options(renv.pak.enabled = FALSE) |
| 129 |
}) |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
renv_tests_setup_libpaths <- function(scope = parent.frame()) { |
| 136 |
|
| 137 |
# remove the sandbox from the library paths, just in case we tried |
| 138 |
# to run tests from an R session where the sandbox was active |
| 139 |
old <- .libPaths() |
| 140 |
new <- grep("renv/sandbox", old, fixed = TRUE, invert = TRUE, value = TRUE) |
| 141 |
renv_scope_libpaths(new, scope = scope) |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
renv_tests_setup_sandbox <- function(scope = parent.frame()) { |
| 146 |
renv_scope_options(renv.sandbox.locking_enabled = FALSE) |
| 147 |
defer(renv_sandbox_unlock(), scope = scope) |
| 148 |
} |
| 149 |
|
| 150 |
renv_tests_setup_repos <- function(scope = parent.frame()) { |
| 151 |
|
| 152 |
# use internal tar implementations here; on Windows, external is too slow |
| 153 |
# note the environment variable names are not case sensitive on Windows |
| 154 |
if (renv_platform_windows()) { |
| 155 |
renv_scope_envvars(TAR = "internal") |
| 156 |
} else { |
| 157 |
renv_scope_envvars(TAR = "internal", tar = "internal") |
| 158 |
} |
| 159 |
|
| 160 |
# also prefer using internal R copy method |
| 161 |
renv_scope_options(renv.config.copy.method = "r") |
| 162 |
|
| 163 |
# generate our dummy repository |
| 164 |
repopath <- renv_tests_repopath() |
| 165 |
if (file.exists(repopath)) |
| 166 |
return() |
| 167 |
|
| 168 |
# create repository source directory |
| 169 |
contrib <- file.path(repopath, "src/contrib") |
| 170 |
ensure_directory(contrib) |
| 171 |
|
| 172 |
# copy package stuff to tempdir (because we'll mutate them a bit) |
| 173 |
source <- renv_tests_path("packages") |
| 174 |
target <- renv_scope_tempfile("renv-packages-", scope = scope) |
| 175 |
renv_file_copy(source, target) |
| 176 |
if (!file.exists(target)) |
| 177 |
stopf("failed to copy '%s' to '%s'", source, target) |
| 178 |
renv_scope_wd(target) |
| 179 |
|
| 180 |
# update the local packrat package version to match what's available |
| 181 |
version <- tryCatch( |
| 182 |
renv_package_version("packrat") %||% "0.9.2", |
| 183 |
error = function(cnd) "0.9.2" |
| 184 |
) |
| 185 |
|
| 186 |
dcf <- renv_dcf_read(file = "packrat/DESCRIPTION") |
| 187 |
dcf$Version <- version |
| 188 |
renv_dcf_write(dcf, file = "packrat/DESCRIPTION") |
| 189 |
|
| 190 |
# helper function for 'uploading' a package to our test repo |
| 191 |
upload <- function(path, root, subdir = FALSE) { |
| 192 |
|
| 193 |
# create package tarball |
| 194 |
desc <- renv_description_read(path) |
| 195 |
package <- basename(path) |
| 196 |
tarball <- sprintf("%s_%s.tar.gz", package, desc$Version) |
| 197 |
tar(tarball, package, compression = "gzip") |
| 198 |
|
| 199 |
# copy into repository tree |
| 200 |
components <- c(root, if (subdir) package, tarball) |
| 201 |
target <- paste(components, collapse = "/") |
| 202 |
ensure_parent_directory(target) |
| 203 |
renv_file_move(tarball, target, overwrite = TRUE) |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
# just in case? |
| 208 |
renv_scope_options(renv.config.filebacked.cache = FALSE) |
| 209 |
|
| 210 |
# copy in packages |
| 211 |
paths <- list.files(getwd(), full.names = TRUE) |
| 212 |
subdirs <- file.path(getRversion(), "Recommended") |
| 213 |
for (path in paths) { |
| 214 |
|
| 215 |
# upload the 'regular' package |
| 216 |
upload(path, contrib, subdir = FALSE) |
| 217 |
|
| 218 |
# upload a subdir (mocking what R does during upgrades) |
| 219 |
upload(path, file.path(contrib, subdirs), subdir = FALSE) |
| 220 |
|
| 221 |
# generate an 'old' version of the packages |
| 222 |
descpath <- file.path(path, "DESCRIPTION") |
| 223 |
desc <- renv_description_read(descpath) |
| 224 |
desc$Version <- "0.0.1" |
| 225 |
write.dcf(desc, file = descpath) |
| 226 |
|
| 227 |
# place packages at top level (simulating packages with multiple |
| 228 |
# versions at the top level of the repository) |
| 229 |
upload(path, contrib, subdir = FALSE) |
| 230 |
|
| 231 |
# generate an 'old' version of the packages |
| 232 |
descpath <- file.path(path, "DESCRIPTION") |
| 233 |
desc <- renv_description_read(descpath) |
| 234 |
desc$Version <- "0.1.0" |
| 235 |
desc$Depends <- gsub("99.99.99", "1.0.0", desc$Depends %||% "", fixed = TRUE) |
| 236 |
write.dcf(desc, file = descpath) |
| 237 |
|
| 238 |
# place these packages into the archive |
| 239 |
upload(path, file.path(contrib, "Archive"), subdir = TRUE) |
| 240 |
|
| 241 |
} |
| 242 |
|
| 243 |
# update PACKAGES metadata |
| 244 |
tools::write_PACKAGES( |
| 245 |
dir = contrib, |
| 246 |
subdirs = subdirs, |
| 247 |
type = "source", |
| 248 |
fields = "Remotes", |
| 249 |
latestOnly = FALSE |
| 250 |
) |
| 251 |
|
| 252 |
# ── Binary packages ────────────────────────────────────────── |
| 253 |
# Build binary versions of all test packages so tests can |
| 254 |
# exercise the binary installation code path. |
| 255 |
# Skipped on Linux where .Platform$pkgType is "source". |
| 256 |
if (!identical(.Platform$pkgType, "source")) { |
| 257 |
|
| 258 |
# platform-appropriate binary contrib path |
| 259 |
bincontrib <- paste0(repopath, contrib.url("", type = "binary")) |
| 260 |
ensure_directory(bincontrib) |
| 261 |
|
| 262 |
# temporary library for building binary packages; |
| 263 |
# R CMD INSTALL --build installs into this library then packs |
| 264 |
# the result as a binary archive in the working directory |
| 265 |
tmplib <- renv_scope_tempfile("renv-binlib-", scope = scope) |
| 266 |
dir.create(tmplib) |
| 267 |
|
| 268 |
# ensure the temp library is on the search path so |
| 269 |
# R CMD INSTALL can resolve dependencies across builds |
| 270 |
renv_scope_envvars(R_LIBS = tmplib) |
| 271 |
|
| 272 |
# latest-version source tarballs (test packages are all 1.0.0) |
| 273 |
srcfiles <- list.files( |
| 274 |
contrib, |
| 275 |
pattern = "_1\\.0\\.0\\.tar\\.gz$", |
| 276 |
full.names = TRUE |
| 277 |
) |
| 278 |
|
| 279 |
# build from bincontrib so binary output lands there directly |
| 280 |
owd <- setwd(bincontrib) |
| 281 |
|
| 282 |
# build in dependency order via retry: try each tarball, |
| 283 |
# retry failures until all succeed or no further progress |
| 284 |
remaining <- srcfiles |
| 285 |
while (length(remaining) > 0L) { |
| 286 |
built <- character() |
| 287 |
for (src in remaining) { |
| 288 |
status <- system2( |
| 289 |
file.path(R.home("bin"), "R"), |
| 290 |
args = c("CMD", "INSTALL", "--build", |
| 291 |
"-l", shQuote(tmplib), |
| 292 |
shQuote(src)), |
| 293 |
stdout = FALSE, stderr = FALSE |
| 294 |
) |
| 295 |
if (identical(status, 0L)) |
| 296 |
built <- c(built, src) |
| 297 |
} |
| 298 |
if (length(built) == 0L) break |
| 299 |
remaining <- setdiff(remaining, built) |
| 300 |
} |
| 301 |
|
| 302 |
setwd(owd) |
| 303 |
|
| 304 |
# write PACKAGES index for the binary arm |
| 305 |
wptype <- if (renv_platform_windows()) "win.binary" else "mac.binary" |
| 306 |
tools::write_PACKAGES(bincontrib, type = wptype) |
| 307 |
|
| 308 |
} |
| 309 |
|
| 310 |
# return path to on-disk repository |
| 311 |
repopath |
| 312 |
|
| 313 |
} |
| 314 |
|