| 1 |
test_that("local_options works", { |
| 2 |
expect_false(getOption("scipen") == 999) |
| 3 |
local({ |
| 4 |
local_options(c(scipen=999)) |
| 5 |
expect_equal(getOption("scipen"), 999) |
| 6 |
}) |
| 7 |
expect_false(getOption("scipen") == 999) |
| 8 |
|
| 9 |
expect_false(identical(getOption("zyxxyzyx"), "qwrbbl")) |
| 10 |
local({ |
| 11 |
local_options(c(zyxxyzyx="qwrbbl")) |
| 12 |
expect_equal(getOption("zyxxyzyx"), "qwrbbl") |
| 13 |
}) |
| 14 |
expect_false(identical(getOption("zyxxyzyx"), "qwrbbl")) |
| 15 |
}) |
| 16 |
|
| 17 |
test_that("local_options(error = ) works", { |
| 18 |
f <- function(...) 1 |
| 19 |
oopt <- options("error") |
| 20 |
on.exit(options(oopt)) |
| 21 |
options(error = f) |
| 22 |
local({ |
| 23 |
local_options(list(error = function(...) 2)) |
| 24 |
expect_identical(2, eval(getOption("error"))) |
| 25 |
}) |
| 26 |
expect_identical(1, eval(getOption("error"))) |
| 27 |
}) |
| 28 |
|
| 29 |
test_that("local_libpaths works and resets library", { |
| 30 |
lib <- .libPaths() |
| 31 |
new_lib <- "." |
| 32 |
local({ |
| 33 |
local_libpaths(new_lib) |
| 34 |
expect_equal(normalizePath(new_lib), normalizePath(.libPaths()[[1L]])) |
| 35 |
}) |
| 36 |
expect_equal(lib, .libPaths()) |
| 37 |
}) |
| 38 |
|
| 39 |
test_that("local_temp_libpaths works and resets library", { |
| 40 |
lib <- .libPaths() |
| 41 |
local({ |
| 42 |
local_temp_libpaths() |
| 43 |
expect_equal(.libPaths()[-1], lib) |
| 44 |
}) |
| 45 |
expect_equal(lib, .libPaths()) |
| 46 |
}) |
| 47 |
|
| 48 |
test_that("local_ works", { |
| 49 |
res <- NULL |
| 50 |
set <- function(new) { |
| 51 |
res <<- c(res, 1L) |
| 52 |
} |
| 53 |
reset <- function(old) { |
| 54 |
res <<- c(res, 3L) |
| 55 |
} |
| 56 |
local_res <- local_(set, reset) |
| 57 |
local({ |
| 58 |
local_res(NULL) |
| 59 |
res <<- c(res, 2L) |
| 60 |
}) |
| 61 |
expect_equal(res, 1L:3L) |
| 62 |
}) |
| 63 |
|
| 64 |
test_that("local_ works on functions without arguments", { |
| 65 |
res <- NULL |
| 66 |
set <- function() { |
| 67 |
res <<- c(res, 1L) |
| 68 |
} |
| 69 |
reset <- function(x) { |
| 70 |
res <<- c(res, 3L) |
| 71 |
} |
| 72 |
local_res <- local_(set, reset) |
| 73 |
local({ |
| 74 |
local_res() |
| 75 |
res <<- c(res, 2L) |
| 76 |
}) |
| 77 |
expect_equal(res, 1L:3L) |
| 78 |
}) |
| 79 |
|
| 80 |
test_that("local_libpaths works and resets library", { |
| 81 |
lib <- .libPaths() |
| 82 |
new_lib <- "." |
| 83 |
local({ |
| 84 |
local_libpaths(new_lib) |
| 85 |
expect_equal(normalizePath(new_lib), normalizePath(.libPaths()[[1L]], mustWork = FALSE)) |
| 86 |
}) |
| 87 |
expect_equal(lib, .libPaths()) |
| 88 |
}) |
| 89 |
|
| 90 |
test_that("local_locale works and resets locales", { |
| 91 |
current <- Sys.getlocale("LC_CTYPE") |
| 92 |
new <- "C" |
| 93 |
local({ |
| 94 |
local_locale(c(LC_CTYPE = new)) |
| 95 |
expect_equal(new, Sys.getlocale("LC_CTYPE")) |
| 96 |
}) |
| 97 |
expect_equal(current, Sys.getlocale("LC_CTYPE")) |
| 98 |
}) |
| 99 |
|
| 100 |
test_that("local_locale fails with LC_ALL", { |
| 101 |
local({ |
| 102 |
expect_error(local_locale(c(LC_ALL = "C")), "LC_ALL") |
| 103 |
}) |
| 104 |
}) |
| 105 |
|
| 106 |
test_that("local_collate works and resets collate", { |
| 107 |
current <- Sys.getlocale("LC_COLLATE") |
| 108 |
new <- "C" |
| 109 |
local({ |
| 110 |
local_collate(new) |
| 111 |
expect_equal(new, Sys.getlocale("LC_COLLATE")) |
| 112 |
}) |
| 113 |
expect_equal(current, Sys.getlocale("LC_COLLATE")) |
| 114 |
}) |
| 115 |
|
| 116 |
test_that("local_makevars works and resets the Makevars file", { |
| 117 |
current <- tempfile() |
| 118 |
writeLines(con = current, c("CFLAGS=-03"), sep = "\n") |
| 119 |
new <- c(CFLAGS = "-O0") |
| 120 |
local({ |
| 121 |
local_makevars(new, .path = current) |
| 122 |
expect_equal("CFLAGS=-O0", readLines(Sys.getenv("R_MAKEVARS_USER"))) |
| 123 |
}) |
| 124 |
expect_equal("CFLAGS=-03", readLines(current)) |
| 125 |
}) |
| 126 |
|
| 127 |
test_that("local_makevars changes only the defined variables", { |
| 128 |
current_name <- tempfile() |
| 129 |
current <- c("CFLAGS=-03", "LDFLAGS=-lz") |
| 130 |
writeLines(con = current_name, current, sep = "\n") |
| 131 |
new <- c(CFLAGS = "-O0") |
| 132 |
local({ |
| 133 |
local_makevars(new, .path = current_name) |
| 134 |
expect_equal(c("CFLAGS=-O0", "LDFLAGS=-lz"), readLines(Sys.getenv("R_MAKEVARS_USER"))) |
| 135 |
}) |
| 136 |
expect_equal(current, readLines(current_name)) |
| 137 |
}) |
| 138 |
|
| 139 |
test_that("local_makevars works with alternative assignments", { |
| 140 |
current <- tempfile() |
| 141 |
writeLines(con = current, c("CFLAGS=-03"), sep = "\n") |
| 142 |
new <- c(CFLAGS = "-O0") |
| 143 |
local({ |
| 144 |
local_makevars(new, .path = current, .assignment = "+=") |
| 145 |
expect_equal("CFLAGS+=-O0", readLines(Sys.getenv("R_MAKEVARS_USER"))) |
| 146 |
}) |
| 147 |
expect_equal("CFLAGS=-03", readLines(current)) |
| 148 |
}) |
| 149 |
|
| 150 |
test_that("local_makevars uses the existing R_MAKEVARS_USER by default", { |
| 151 |
tf <- tempfile() |
| 152 |
local_envvar("R_MAKEVARS_USER" = tf) |
| 153 |
on.exit(unlink(tf)) |
| 154 |
writeLines(con = tf, c("CFLAGS=-O3", "CXXFLAGS=-O3"), sep = "\n") |
| 155 |
new <- c(CFLAGS = "-O0") |
| 156 |
local({ |
| 157 |
local_makevars(new) |
| 158 |
expect_equal(readLines(Sys.getenv("R_MAKEVARS_USER")), c("CFLAGS=-O0", "CXXFLAGS=-O3")) |
| 159 |
}) |
| 160 |
expect_equal(readLines(tf), c("CFLAGS=-O3", "CXXFLAGS=-O3")) |
| 161 |
}) |
| 162 |
|
| 163 |
test_that("local_dir works as expected", { |
| 164 |
old <- normalizePath(getwd()) |
| 165 |
local({ |
| 166 |
local_dir("..") |
| 167 |
expect_equal(normalizePath(getwd()), normalizePath(file.path(old, ".."))) |
| 168 |
}) |
| 169 |
expect_equal(normalizePath(getwd()), normalizePath(old)) |
| 170 |
}) |
| 171 |
|
| 172 |
test_that("supplying a getter to `local_()` shields against early exits", { |
| 173 |
my_get <- function(x) { |
| 174 |
out <- as.list(state)[names(x)] |
| 175 |
names(out) <- names(x) |
| 176 |
out |
| 177 |
} |
| 178 |
my_set <- function(x) { |
| 179 |
old <- my_get(x) |
| 180 |
|
| 181 |
mapply(function(nm, val) state[[nm]] <- val, names(x), x) |
| 182 |
rlang::signal("", "my_cnd") |
| 183 |
|
| 184 |
invisible(old) |
| 185 |
} |
| 186 |
|
| 187 |
state <- new.env() |
| 188 |
my_local_unsafe <- withr::local_(my_set) |
| 189 |
my_local_safe <- withr::local_(my_set, get = my_get) |
| 190 |
|
| 191 |
my_with_unsafe <- function(new, expr) { |
| 192 |
my_local_unsafe(new) |
| 193 |
expr |
| 194 |
} |
| 195 |
my_with_safe <- function(new, expr) { |
| 196 |
my_local_safe(new) |
| 197 |
expr |
| 198 |
} |
| 199 |
|
| 200 |
expect_safe_and_unsafe_unwinding( |
| 201 |
state, |
| 202 |
my_with_unsafe, |
| 203 |
my_with_safe |
| 204 |
) |
| 205 |
|
| 206 |
|
| 207 |
# `...` code path |
| 208 |
|
| 209 |
state <- new.env() |
| 210 |
my_local_unsafe <- withr::local_(my_set, dots = TRUE) |
| 211 |
my_local_safe <- withr::local_(my_set, get = my_get, dots = TRUE) |
| 212 |
|
| 213 |
my_with_unsafe <- function(new, expr) { |
| 214 |
my_local_unsafe(new) |
| 215 |
expr |
| 216 |
} |
| 217 |
my_with_safe <- function(new, expr) { |
| 218 |
my_local_safe(new) |
| 219 |
expr |
| 220 |
} |
| 221 |
|
| 222 |
expect_safe_and_unsafe_unwinding( |
| 223 |
state, |
| 224 |
my_with_unsafe, |
| 225 |
my_with_safe |
| 226 |
) |
| 227 |
}) |