Skip to content

Commit 4e402c1

Browse files
committed
Use withr::defer() instead of on.exit()
1 parent b99e3a3 commit 4e402c1

File tree

13 files changed

+30
-33
lines changed

13 files changed

+30
-33
lines changed

R/mock.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ with_mock <- function(..., .env = topenv()) {
4040
mock_funs <- lapply(dots[!code_pos], eval, parent.frame())
4141
mocks <- extract_mocks(mock_funs, .env = .env)
4242

43-
on.exit(lapply(mocks, reset_mock), add = TRUE)
43+
withr::defer(lapply(mocks, reset_mock))
4444
lapply(mocks, set_mock)
4545

4646
# Evaluate the code
@@ -60,7 +60,7 @@ local_mock <- function(..., .env = topenv(), .local_envir = parent.frame()) {
6060

6161
mocks <- extract_mocks(list(...), .env = .env)
6262
on_exit <- bquote(
63-
on.exit(lapply(.(mocks), .(reset_mock)), add = TRUE),
63+
withr::defer(lapply(.(mocks), .(reset_mock))),
6464
)
6565

6666
lapply(mocks, set_mock)

R/reporter-debug.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ DebugReporter <- R6::R6Class(
1313
if (!expectation_success(result) && !is.null(result$start_frame)) {
1414
if (sink_number() > 0) {
1515
sink(self$out)
16-
on.exit(sink(), add = TRUE)
16+
withr::defer(sink())
1717
}
1818

1919
recover2(
@@ -53,7 +53,7 @@ recover2 <- function(start_frame = 1L, end_frame = sys.nframe()) {
5353

5454
if (.isMethodsDispatchOn()) {
5555
tState <- tracingState(FALSE)
56-
on.exit(tracingState(tState))
56+
withr::defer(tracingState(tState))
5757
}
5858
from <- min(end_frame, length(calls))
5959

R/reporter-zzz.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ with_reporter <- function(reporter, code, start_end_reporter = TRUE) {
3939
reporter <- find_reporter(reporter)
4040

4141
old <- set_reporter(reporter)
42-
on.exit(set_reporter(old), add = TRUE)
42+
withr::defer(set_reporter(old))
4343

4444
if (start_end_reporter) {
4545
reporter$start_reporter()

R/source.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ source_file <- function(
3333
## We need to parse from a connection, because parse() has a bug,
3434
## and converts the input to the native encoding, if the text arg is used
3535
con <- textConnection(lines, encoding = "UTF-8")
36-
on.exit(try(close(con), silent = TRUE), add = TRUE)
36+
withr::defer(try(close(con), silent = TRUE))
3737
exprs <- parse(con, n = -1, srcfile = srcfile, encoding = "UTF-8")
3838
exprs <- filter_desc(exprs, desc, error_call = error_call)
3939

@@ -44,7 +44,7 @@ source_file <- function(
4444

4545
if (chdir) {
4646
old_dir <- setwd(dirname(path))
47-
on.exit(setwd(old_dir), add = TRUE)
47+
withr::defer(setwd(old_dir))
4848
}
4949

5050
withr::local_options(testthat_topenv = env, testthat_path = path)

R/teardown.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ teardown_run <- function(path = ".") {
7373
}
7474

7575
old_dir <- setwd(path)
76-
on.exit(setwd(old_dir), add = TRUE)
76+
withr::defer(setwd(old_dir))
7777

7878
lapply(file_teardown_env$queue, function(f) try(f()))
7979
teardown_reset()

R/test-files.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ test_one_file <- function(
341341
error_call = caller_env()
342342
) {
343343
reporter <- get_reporter()
344-
on.exit(teardown_run(), add = TRUE)
344+
withr::defer(teardown_run())
345345

346346
reporter$start_file(path)
347347
source_file(

R/test-that.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ test_code <- function(test, code, env, reporter, skip_on_empty = TRUE) {
6565

6666
if (!is.null(test)) {
6767
reporter$start_test(context = reporter$.context, test = test)
68-
on.exit(reporter$end_test(context = reporter$.context, test = test))
68+
withr::defer(reporter$end_test(context = reporter$.context, test = test))
6969
}
7070

7171
ok <- TRUE
@@ -111,7 +111,7 @@ test_code <- function(test, code, env, reporter, skip_on_empty = TRUE) {
111111
# case of a stack overflow. This is important for the DebugReporter.
112112
# Call options() manually, avoid withr overhead.
113113
options(expressions = expressions_opt_new)
114-
on.exit(options(expressions = expressions_opt), add = TRUE)
114+
withr::defer(options(expressions = expressions_opt))
115115

116116
# Add structured backtrace to the expectation
117117
if (can_entrace(e)) {
@@ -180,7 +180,7 @@ test_code <- function(test, code, env, reporter, skip_on_empty = TRUE) {
180180

181181
test_env <- new.env(parent = env)
182182
old <- options(rlang_trace_top_env = test_env)[[1]]
183-
on.exit(options(rlang_trace_top_env = old), add = TRUE)
183+
withr::defer(options(rlang_trace_top_env = old))
184184

185185
withr::local_options(testthat_topenv = test_env)
186186

tests/test-catch.R

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local({
1212

1313
perform_test <- function(pkgName, catchEnabled) {
1414
owd <- setwd(tempdir())
15-
on.exit(setwd(owd), add = TRUE)
15+
withr::defer(setwd(owd))
1616

1717
pkgPath <- file.path(tempdir(), pkgName)
1818
libPath <- file.path(tempdir(), "rlib")
@@ -21,13 +21,10 @@ local({
2121
}
2222
.libPaths(c(libPath, .libPaths()))
2323

24-
on.exit(
25-
{
26-
unlink(pkgPath, recursive = TRUE)
27-
unlink(libPath, recursive = TRUE)
28-
},
29-
add = TRUE
30-
)
24+
withr::defer({
25+
unlink(pkgPath, recursive = TRUE)
26+
unlink(libPath, recursive = TRUE)
27+
})
3128

3229
quietly(usethis::create_package(pkgPath, open = FALSE))
3330
quietly(testthat::use_catch(pkgPath))

tests/testthat/test-expect-inheritance.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test_that("test_s4_class respects class hierarchy", {
3131
A <- methods::setClass("A", contains = "list")
3232
B <- methods::setClass("B", contains = "list")
3333
C <- methods::setClass("C", contains = c("A", "B"))
34-
on.exit({
34+
withr::defer({
3535
methods::removeClass("A")
3636
methods::removeClass("B")
3737
methods::removeClass("C")

tests/testthat/test-expect-known.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local_edition(2)
44

55
test_that("uses specified width", {
66
old <- options(width = 20)
7-
on.exit(options(old), add = TRUE)
7+
withr::defer(options(old))
88

99
x <- 1:100
1010
expect_known_output(print(x), "width-80.txt")
@@ -51,7 +51,7 @@ test_that("Warning for non-UTF-8 reference files", {
5151
Encoding(x) <- "latin1"
5252

5353
tmp <- tempfile()
54-
on.exit(unlink(tmp), add = TRUE)
54+
withr::defer(unlink(tmp))
5555
writeBin(x, tmp)
5656

5757
suppressWarnings(
@@ -81,7 +81,7 @@ test_that("first run is successful", {
8181

8282
test_that("equal_to_ref does not overwrite existing", {
8383
tmp_rds <- tempfile(fileext = ".rds")
84-
on.exit(unlink(tmp_rds))
84+
withr::defer(unlink(tmp_rds))
8585
ref_obj1 <- 1:3
8686
ref_obj2 <- 2:4
8787
saveRDS(ref_obj1, tmp_rds)
@@ -99,7 +99,7 @@ test_that("equal_to_ref does not overwrite existing", {
9999

100100
test_that("serializes to version 2 by default", {
101101
tmp_rds <- tempfile(fileext = ".rds")
102-
on.exit(unlink(tmp_rds))
102+
withr::defer(unlink(tmp_rds))
103103

104104
expect_warning(
105105
expect_known_value("a", tmp_rds),
@@ -111,7 +111,7 @@ test_that("serializes to version 2 by default", {
111111

112112
test_that("version 3 is possible", {
113113
tmp_rds <- tempfile(fileext = ".rds")
114-
on.exit(unlink(tmp_rds))
114+
withr::defer(unlink(tmp_rds))
115115

116116
expect_warning(
117117
expect_known_value("a", tmp_rds, version = 3),

0 commit comments

Comments
 (0)