Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept only existing editions #1551

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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