Skip to content

Commit 9bda65b

Browse files
committed
WIP auto-skip if package not installed
1 parent 13e102e commit 9bda65b

File tree

4 files changed

+42
-36
lines changed

4 files changed

+42
-36
lines changed

R/test-that.R

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,11 @@ test_code <- function(test, code, env, reporter, skip_on_empty = TRUE) {
124124
# to be able to debug with the DebugReporter
125125
register_expectation(e, 2)
126126

127-
e[["handled"]] <- TRUE
128127
test_error <<- e
128+
invokeRestart("end_test")
129129
}
130130
handle_fatal <- function(e) {
131131
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-
140132
register_expectation(e, 0)
141133
}
142134
handle_expectation <- function(e) {
@@ -175,7 +167,7 @@ test_code <- function(test, code, env, reporter, skip_on_empty = TRUE) {
175167

176168
debug_end <- if (inherits(e, "skip_empty")) -1 else 2
177169
register_expectation(e, debug_end)
178-
signalCondition(e)
170+
invokeRestart("end_test")
179171
}
180172

181173
test_env <- new.env(parent = env)
@@ -185,24 +177,30 @@ test_code <- function(test, code, env, reporter, skip_on_empty = TRUE) {
185177
withr::local_options(testthat_topenv = test_env)
186178

187179
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
180+
withRestarts(
181+
tryCatch(
182+
withCallingHandlers(
183+
{
184+
eval(code, test_env)
185+
if (!handled && !is.null(test)) {
186+
skip_empty()
187+
}
188+
},
189+
expectation = handle_expectation,
190+
packageNotFoundError = function(e) {
191+
if (on_cran()) {
192+
skip(paste0(e$package, " is not installed."))
193+
}
194+
},
195+
skip = handle_skip,
196+
warning = handle_warning,
197+
message = handle_message,
198+
error = handle_error
199+
),
200+
# some errors may need handling here, e.g., stack overflow
201+
error = handle_fatal
201202
),
202-
# some errors may need handling here, e.g., stack overflow
203-
error = handle_fatal,
204-
# skip silently terminate code
205-
skip = function(e) {}
203+
end_test = function() {}
206204
)
207205
after <- inspect_state()
208206

tests/testthat/test-snapshot-reporter.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ test_that("errors in test doesn't change snapshot", {
139139

140140
test_that("skips and unexpected errors reset snapshots", {
141141
regenerate <- FALSE
142-
143142
if (regenerate) {
144143
withr::local_envvar(c(TESTTHAT_REGENERATE_SNAPS = "true"))
145144
}
@@ -156,7 +155,7 @@ test_that("skips and unexpected errors reset snapshots", {
156155

157156
snaps <- snap_from_md(brio::read_lines(path))
158157
titles <- c("errors reset snapshots", "skips reset snapshots")
159-
expect_true(all(titles %in% names(snaps)))
158+
expect_in(titles, names(snaps))
160159
})
161160

162161
test_that("`expect_error()` can fail inside `expect_snapshot()`", {

tests/testthat/test-snapshot/_snaps/snapshot.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,3 @@
55
Output
66
[1] 1
77

8-
# skips reset snapshots
9-
10-
Code
11-
print(1)
12-
Output
13-
[1] 1
14-

tests/testthat/test-test-that.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,19 @@ test_that("no braces required in testthat 2e", {
147147
NA
148148
)
149149
})
150+
151+
test_that("missing packages cause a skip on CRAN", {
152+
local_on_cran(TRUE)
153+
# skip generated test_that, so need to wrap
154+
expect_skip(test_that("", {
155+
library(notinstalled)
156+
}))
157+
158+
local_on_cran(FALSE)
159+
expect_error(
160+
test_that("", {
161+
library(notinstalled)
162+
}),
163+
class = "packageNotFoundError"
164+
)
165+
})

0 commit comments

Comments
 (0)