Skip to content

Commit 39dc80d

Browse files
authored
Use restart in test_code() (#2153)
1 parent ff62586 commit 39dc80d

File tree

1 file changed

+22
-45
lines changed

1 file changed

+22
-45
lines changed

R/test-that.R

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ test_code <- function(test, code, env, reporter, skip_on_empty = TRUE) {
8989
reporter$add_result(context = reporter$.context, test = test, result = e)
9090
}
9191

92-
# Any error will be assigned to this variable first
93-
# In case of stack overflow, no further processing (not even a call to
94-
# signalCondition() ) might be possible
95-
test_error <- NULL
96-
9792
expressions_opt <- getOption("expressions")
9893
expressions_opt_new <- min(expressions_opt + 500L, 500000L)
9994

@@ -104,39 +99,21 @@ test_code <- function(test, code, env, reporter, skip_on_empty = TRUE) {
10499

105100
handle_error <- function(e) {
106101
handled <<- TRUE
107-
# First thing: Collect test error
108-
test_error <<- e
109102

110103
# Increase option(expressions) to handle errors here if possible, even in
111-
# case of a stack overflow. This is important for the DebugReporter.
112-
# Call options() manually, avoid withr overhead.
113-
options(expressions = expressions_opt_new)
114-
withr::defer(options(expressions = expressions_opt))
104+
# case of a stack overflow. This is important for the DebugReporter.
105+
local_options(expressions = expressions_opt_new)
115106

116107
# Add structured backtrace to the expectation
117108
if (can_entrace(e)) {
118109
e <- cnd_entrace(e)
119110
}
120111

121-
test_error <<- e
122-
123-
# Error will be handled by handle_fatal() if this fails; need to do it here
124-
# to be able to debug with the DebugReporter
125112
register_expectation(e, 2)
126-
127-
e[["handled"]] <- TRUE
128-
test_error <<- e
113+
invokeRestart("end_test")
129114
}
130115
handle_fatal <- function(e) {
131116
handled <<- TRUE
132-
# Error caught in handle_error() has precedence
133-
if (!is.null(test_error)) {
134-
e <- test_error
135-
if (isTRUE(e[["handled"]])) {
136-
return()
137-
}
138-
}
139-
140117
register_expectation(e, 0)
141118
}
142119
handle_expectation <- function(e) {
@@ -162,7 +139,6 @@ test_code <- function(test, code, env, reporter, skip_on_empty = TRUE) {
162139
}
163140

164141
register_expectation(e, 5)
165-
166142
tryInvokeRestart("muffleWarning")
167143
}
168144
handle_message <- function(e) {
@@ -175,7 +151,7 @@ test_code <- function(test, code, env, reporter, skip_on_empty = TRUE) {
175151

176152
debug_end <- if (inherits(e, "skip_empty")) -1 else 2
177153
register_expectation(e, debug_end)
178-
signalCondition(e)
154+
invokeRestart("end_test")
179155
}
180156

181157
test_env <- new.env(parent = env)
@@ -185,24 +161,25 @@ test_code <- function(test, code, env, reporter, skip_on_empty = TRUE) {
185161
withr::local_options(testthat_topenv = test_env)
186162

187163
before <- inspect_state()
188-
tryCatch(
189-
withCallingHandlers(
190-
{
191-
eval(code, test_env)
192-
if (!handled && !is.null(test)) {
193-
skip_empty()
194-
}
195-
},
196-
expectation = handle_expectation,
197-
skip = handle_skip,
198-
warning = handle_warning,
199-
message = handle_message,
200-
error = handle_error
164+
withRestarts(
165+
tryCatch(
166+
withCallingHandlers(
167+
{
168+
eval(code, test_env)
169+
if (!handled && !is.null(test)) {
170+
skip_empty()
171+
}
172+
},
173+
expectation = handle_expectation,
174+
skip = handle_skip,
175+
warning = handle_warning,
176+
message = handle_message,
177+
error = handle_error
178+
),
179+
# some errors may need handling here, e.g., stack overflow
180+
error = handle_fatal
201181
),
202-
# some errors may need handling here, e.g., stack overflow
203-
error = handle_fatal,
204-
# skip silently terminate code
205-
skip = function(e) {}
182+
end_test = function() {}
206183
)
207184
after <- inspect_state()
208185

0 commit comments

Comments
 (0)