Skip to content

Commit b506ba6

Browse files
committed
Accept only existing editions
An error is thrown when trying to set an edition that does not exist. Fixes #1547.
1 parent 4b1b727 commit b506ba6

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

R/edition.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ edition_name <- function(x) {
5151
}
5252
}
5353

54+
is_valid_edition <- function(x) {
55+
if (is_zap(x)) {
56+
TRUE
57+
} else {
58+
is.numeric(x) && length(x) == 1 && x %in% c(2, 3)
59+
}
60+
}
61+
5462
#' Temporarily change the active testthat edition
5563
#'
5664
#' `local_edition()` allows you to temporarily (within a single test or
@@ -62,7 +70,9 @@ edition_name <- function(x) {
6270
#' @param .env Environment that controls scope of changes. For expert use only.
6371
#' @keywords internal
6472
local_edition <- function(x, .env = parent.frame()) {
65-
stopifnot(is_zap(x) || (is.numeric(x) && length(x) == 1))
73+
if (!is_valid_edition(x)) {
74+
stop("Available editions are 2 and 3", call. = FALSE)
75+
}
6676
old <- edition_set(x)
6777
withr::defer(edition_set(old), envir = .env)
6878
}

tests/testthat/test-edition.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ test_that("can locally override edition", {
66
expect_equal(edition_get(), 2)
77
})
88

9+
test_that("only existing editions can be set", {
10+
expect_error(
11+
local_edition(5),
12+
regexp = "Available editions are 2 and 3"
13+
)
14+
expect_error(
15+
local_edition(-1),
16+
regexp = "Available editions are 2 and 3"
17+
)
18+
})
19+
920
test_that("deprecation only fired for newer edition", {
1021
local_edition(2)
1122
expect_warning(edition_deprecate(3, "old stuff"), NA)

0 commit comments

Comments
 (0)