| 1 |
test_that("defer_parent works", { |
| 2 |
local_file <- function(path) { |
| 3 |
file.create(path) |
| 4 |
defer_parent(unlink(path)) |
| 5 |
} |
| 6 |
|
| 7 |
# create tempfile path |
| 8 |
path <- tempfile() |
| 9 |
|
| 10 |
# use 'local_file' in a function |
| 11 |
local({ |
| 12 |
local_file(path) |
| 13 |
stopifnot(file.exists(path)) |
| 14 |
}) |
| 15 |
|
| 16 |
# file is deleted as we leave 'local' scope |
| 17 |
expect_false(file.exists(path)) |
| 18 |
}) |
| 19 |
|
| 20 |
test_that("defer()'s global env facilities work", { |
| 21 |
skip_if_not_installed("testthat", "3.2.0") |
| 22 |
|
| 23 |
expect_length(the$global_exits, 0) |
| 24 |
|
| 25 |
local_options(rlang_interactive = TRUE) |
| 26 |
Sys.setenv(abcdefg = "abcdefg") |
| 27 |
|
| 28 |
expect_snapshot( |
| 29 |
defer(print("howdy"), envir = globalenv()), |
| 30 |
cran = TRUE |
| 31 |
) |
| 32 |
expect_message( |
| 33 |
local_envvar(c(abcdefg = "tuvwxyz"), .local_envir = globalenv()), |
| 34 |
NA |
| 35 |
) |
| 36 |
|
| 37 |
h <- the$global_exits |
| 38 |
expect_length(h, 2) |
| 39 |
expect_equal(Sys.getenv("abcdefg"), "tuvwxyz") |
| 40 |
|
| 41 |
suppressMessages( |
| 42 |
expect_output(deferred_run(globalenv()), "howdy") |
| 43 |
) |
| 44 |
expect_equal(Sys.getenv("abcdefg"), "abcdefg") |
| 45 |
|
| 46 |
expect_message(defer(print("never going to happen"), envir = globalenv())) |
| 47 |
deferred_clear(globalenv()) |
| 48 |
|
| 49 |
expect_length(the$global_exits, 0) |
| 50 |
}) |
| 51 |
|
| 52 |
test_that("non-top-level global env is unwound like a normal env", { |
| 53 |
expect_silent( |
| 54 |
evalq(local_options(list(opt = "foo")), globalenv()) |
| 55 |
) |
| 56 |
|
| 57 |
# Check that handlers have been called |
| 58 |
expect_null(getOption("opt")) |
| 59 |
|
| 60 |
# Check that handlers were cleaned up |
| 61 |
expect_length(the$global_exits, 0) |
| 62 |
}) |
| 63 |
|
| 64 |
test_that("defered actions in global env are run on exit", { |
| 65 |
path <- local_tempfile() |
| 66 |
callr::r( |
| 67 |
function(path) { |
| 68 |
withr::defer(writeLines("a", path), env = globalenv()) |
| 69 |
}, |
| 70 |
list(path = path) |
| 71 |
) |
| 72 |
expect_equal(readLines(path), "a") |
| 73 |
}) |
| 74 |
|
| 75 |
test_that("defered actions in Rmd are run on exit", { |
| 76 |
skip_if_cannot_knit() |
| 77 |
|
| 78 |
rmd <- local_tempfile(fileext = ".Rmd") |
| 79 |
path <- local_tempfile() |
| 80 |
writeLines(rmd, text = c( |
| 81 |
"---", |
| 82 |
"title: test", |
| 83 |
"---", |
| 84 |
"```{r}", |
| 85 |
paste0("withr::defer(writeLines('a', ", encodeString(path, quote = "'"), "))"), |
| 86 |
"```" |
| 87 |
)) |
| 88 |
callr::r(function(path) rmarkdown::render(path), list(path = rmd)) |
| 89 |
expect_equal(readLines(path), "a") |
| 90 |
|
| 91 |
# And check when run from globalenv |
| 92 |
unlink(path) |
| 93 |
callr::r(function(path) rmarkdown::render(path, envir = globalenv()), list(path = rmd)) |
| 94 |
expect_equal(readLines(path), "a") |
| 95 |
|
| 96 |
}) |
| 97 |
|
| 98 |
test_that("defer executes all handlers even if there is an error in one of them", { |
| 99 |
|
| 100 |
old <- options("test_option" = 1) |
| 101 |
on.exit(options(old), add = TRUE) |
| 102 |
|
| 103 |
f <- function() { |
| 104 |
defer(stop("hi")) |
| 105 |
defer(options("test_option" = 2)) |
| 106 |
} |
| 107 |
|
| 108 |
expect_equal(getOption("test_option"), 1) |
| 109 |
|
| 110 |
err <- tryCatch(f(), error = identity) |
| 111 |
|
| 112 |
expect_equal(conditionMessage(err), "hi") |
| 113 |
|
| 114 |
expect_equal(getOption("test_option"), 2) |
| 115 |
}) |
| 116 |
|
| 117 |
test_that("defer works within source()", { |
| 118 |
local_options(withr.hook_source = TRUE) |
| 119 |
|
| 120 |
file <- local_tempfile() |
| 121 |
out <- NULL |
| 122 |
|
| 123 |
local_defer <- function(frame = parent.frame()) { |
| 124 |
defer(out <<- c(out, "local_defer"), envir = frame) |
| 125 |
} |
| 126 |
|
| 127 |
cat(file = file, " |
| 128 |
out <<- c(out, '1') |
| 129 |
defer(out <<- c(out, 'defer')) |
| 130 |
out <<- c(out, '2') |
| 131 |
identity(defer(out <<- c(out, 'identity(defer)'))) |
| 132 |
out <<- c(out, '3') |
| 133 |
local_defer() |
| 134 |
out <<- c(out, '4') |
| 135 |
evalq(defer(out <<- c(out, 'evalq(defer)'))) |
| 136 |
out <<- c(out, '5') |
| 137 |
") |
| 138 |
local( |
| 139 |
source(file, local = TRUE) |
| 140 |
) |
| 141 |
|
| 142 |
expect_equal(out, c( |
| 143 |
"1", |
| 144 |
"2", |
| 145 |
"3", |
| 146 |
"4", |
| 147 |
"evalq(defer)", |
| 148 |
"5", |
| 149 |
"local_defer", |
| 150 |
"identity(defer)", |
| 151 |
"defer" |
| 152 |
)) |
| 153 |
}) |
| 154 |
|
| 155 |
test_that("defer works within source()", { |
| 156 |
local_options(withr.hook_source = TRUE) |
| 157 |
|
| 158 |
out <- NULL |
| 159 |
|
| 160 |
file1 <- local_tempfile() |
| 161 |
file2 <- local_tempfile() |
| 162 |
|
| 163 |
cat(file = file1, " |
| 164 |
out <<- c(out, 'outer-1') |
| 165 |
defer(out <<- c(out, 'outer-before')) |
| 166 |
out <<- c(out, 'outer-2') |
| 167 |
local(source(file2, local = TRUE)) |
| 168 |
defer(out <<- c(out, 'outer-after')) |
| 169 |
out <<- c(out, 'outer-3') |
| 170 |
") |
| 171 |
cat(file = file2, " |
| 172 |
out <<- c(out, '1') |
| 173 |
defer(out <<- c(out, 'defer')) |
| 174 |
out <<- c(out, '2') |
| 175 |
") |
| 176 |
|
| 177 |
local( |
| 178 |
source(file1, local = TRUE) |
| 179 |
) |
| 180 |
|
| 181 |
expect_equal(out, c( |
| 182 |
"outer-1", |
| 183 |
"outer-2", |
| 184 |
"1", |
| 185 |
"2", |
| 186 |
"defer", |
| 187 |
"outer-3", |
| 188 |
"outer-after", |
| 189 |
"outer-before" |
| 190 |
)) |
| 191 |
}) |
| 192 |
|
| 193 |
test_that("don't need to enable source for the global env", { |
| 194 |
local_options(withr.hook_source = NULL) |
| 195 |
|
| 196 |
file <- local_tempfile() |
| 197 |
|
| 198 |
cat(file = file, " |
| 199 |
writeLines('1') |
| 200 |
withr::defer(writeLines('deferred')) |
| 201 |
writeLines('2') |
| 202 |
") |
| 203 |
|
| 204 |
expect_snapshot({ |
| 205 |
source(file, local = globalenv()) |
| 206 |
}) |
| 207 |
}) |
| 208 |
|
| 209 |
test_that("defer works within knitr::knit()", { |
| 210 |
skip_if_not_installed("knitr") |
| 211 |
out <- NULL |
| 212 |
evalq({ |
| 213 |
defer(out <- c(out, "first")) |
| 214 |
rmd <- " |
| 215 |
```{r} |
| 216 |
defer(out <- c(out, 'defer 1')) |
| 217 |
out <- c(out, '1') |
| 218 |
local({ |
| 219 |
withr::defer(out <<- c(out, 'local 1')) |
| 220 |
}) |
| 221 |
``` |
| 222 |
```{r} |
| 223 |
defer(out <- c(out, 'defer 2')) |
| 224 |
out <- c(out, '2') |
| 225 |
local({ |
| 226 |
withr::defer(out <<- c(out, 'local 2')) |
| 227 |
}) |
| 228 |
``` |
| 229 |
" |
| 230 |
knitr::knit(text = rmd, quiet = TRUE) |
| 231 |
defer(out <- c(out, "last")) |
| 232 |
}) |
| 233 |
expect_equal(out, c( |
| 234 |
"1", |
| 235 |
"local 1", |
| 236 |
"2", |
| 237 |
"local 2", |
| 238 |
"defer 2", |
| 239 |
"defer 1", |
| 240 |
"last", |
| 241 |
"first" |
| 242 |
)) |
| 243 |
}) |
| 244 |
|
| 245 |
test_that("defer() and on.exit() handlers can be meshed", { |
| 246 |
out <- list() |
| 247 |
|
| 248 |
local({ |
| 249 |
on.exit(out <<- append(out, 1), add = TRUE) |
| 250 |
defer(out <<- append(out, 2)) |
| 251 |
on.exit(out <<- append(out, 3), add = TRUE) |
| 252 |
on.exit(out <<- append(out, 4), add = TRUE, after = FALSE) |
| 253 |
defer(out <<- append(out, 5), priority = "last") |
| 254 |
}) |
| 255 |
|
| 256 |
expect_equal(out, list(4, 2, 1, 3, 5)) |
| 257 |
}) |