Skip to content

Commit

Permalink
Accept only existing editions
Browse files Browse the repository at this point in the history
An error is thrown when trying to set an edition that does not exist.
Fixes r-lib#1547.
  • Loading branch information
ALanguillaume committed Jan 11, 2022
1 parent 4b1b727 commit b506ba6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion R/edition.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ edition_name <- function(x) {
}
}

is_valid_edition <- function(x) {
if (is_zap(x)) {
TRUE
} else {
is.numeric(x) && length(x) == 1 && x %in% c(2, 3)
}
}

#' Temporarily change the active testthat edition
#'
#' `local_edition()` allows you to temporarily (within a single test or
Expand All @@ -62,7 +70,9 @@ edition_name <- function(x) {
#' @param .env Environment that controls scope of changes. For expert use only.
#' @keywords internal
local_edition <- function(x, .env = parent.frame()) {
stopifnot(is_zap(x) || (is.numeric(x) && length(x) == 1))
if (!is_valid_edition(x)) {
stop("Available editions are 2 and 3", call. = FALSE)
}
old <- edition_set(x)
withr::defer(edition_set(old), envir = .env)
}
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-edition.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ test_that("can locally override edition", {
expect_equal(edition_get(), 2)
})

test_that("only existing editions can be set", {
expect_error(
local_edition(5),
regexp = "Available editions are 2 and 3"
)
expect_error(
local_edition(-1),
regexp = "Available editions are 2 and 3"
)
})

test_that("deprecation only fired for newer edition", {
local_edition(2)
expect_warning(edition_deprecate(3, "old stuff"), NA)
Expand Down

0 comments on commit b506ba6

Please sign in to comment.