| 1 |
|
| 2 |
# a wrapper around testthat::test_that(), which also tries |
| 3 |
# to confirm that the test hasn't mutated any global state |
| 4 |
test_that <- function(desc, code) { |
| 5 |
|
| 6 |
# skip tests when run on CRAN's macOS machine |
| 7 |
cran <- !interactive() && !identical(Sys.getenv("NOT_CRAN"), "true") |
| 8 |
testthat::skip_if(cran && renv_platform_macos()) |
| 9 |
|
| 10 |
# record global state before test execution |
| 11 |
before <- renv_test_state(cran) |
| 12 |
|
| 13 |
# run the test with a condition handler that skips on download errors |
| 14 |
# under the assumption that such errors are spotty / intermittent |
| 15 |
call <- sys.call() |
| 16 |
call[[1L]] <- quote(testthat::test_that) |
| 17 |
call[[3L]] <- renv_test_wrap(call[[3L]]) |
| 18 |
eval(call, envir = parent.frame()) |
| 19 |
|
| 20 |
# record global state after test execution |
| 21 |
after <- renv_test_state(cran) |
| 22 |
|
| 23 |
# check for unexpected changes |
| 24 |
diffs <- waldo::compare(before, after) |
| 25 |
if (length(diffs)) { |
| 26 |
fdiffs <- paste(format(diffs), collapse = "\n\n") |
| 27 |
stopf("Test '%s' has modified global state:\n%s\n", desc, fdiffs) |
| 28 |
} |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
renv_test_wrap <- function(code) { |
| 33 |
|
| 34 |
expr( |
| 35 |
withCallingHandlers( |
| 36 |
!!code, |
| 37 |
error = function(cnd) { |
| 38 |
msg <- conditionMessage(cnd) |
| 39 |
if (is.character(msg)) { |
| 40 |
pattern <- "SSL connect error|code 22|code 56" |
| 41 |
if (any(grepl(pattern, msg))) { |
| 42 |
testthat::skip(msg) |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
) |
| 47 |
) |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
renv_test_state <- function(cran) { |
| 52 |
|
| 53 |
repopath <- renv_tests_repopath() |
| 54 |
userpath <- file.path(renv_bootstrap_user_dir(), "library") |
| 55 |
|
| 56 |
opts <- options() |
| 57 |
opts <- opts[grep("^diffobj", names(opts), invert = TRUE)] |
| 58 |
opts$ambiguousMethodSelection <- NULL |
| 59 |
opts$restart <- NULL |
| 60 |
opts$repos[opts$repos == "@CRAN@"] <- "https://cloud.r-project.org" |
| 61 |
opts$mc.cores <- NULL |
| 62 |
opts <- opts[csort(names(opts))] |
| 63 |
|
| 64 |
envvars <- as.list(Sys.getenv()) |
| 65 |
envvars <- envvars[grep("^RENV_DEFAULT_", names(envvars), invert = TRUE)] |
| 66 |
envvars <- envvars[grep("^R_PACKRAT_", names(envvars), invert = TRUE)] |
| 67 |
envvars <- envvars[grep("^_R_", names(envvars), invert = TRUE)] |
| 68 |
envvars <- envvars[grep("^CALLR_", names(envvars), invert = TRUE)] |
| 69 |
envvars$RETICULATE_MINICONDA_PYTHON_ENVPATH <- NULL |
| 70 |
envvars$OMP_NUM_THREADS <- NULL |
| 71 |
envvars$OPENBLAS <- NULL |
| 72 |
envvars <- envvars[csort(names(envvars))] |
| 73 |
|
| 74 |
list( |
| 75 |
libpaths = if (!cran) .libPaths(), |
| 76 |
connections = getAllConnections(), |
| 77 |
options = opts, |
| 78 |
repofiles = list.files(repopath, all.files = TRUE, no.. = TRUE), |
| 79 |
userfiles = list.files(userpath, all.files = TRUE, no.. = TRUE), |
| 80 |
envvars = envvars |
| 81 |
) |
| 82 |
|
| 83 |
} |