Skip to content

Commit d0b7491

Browse files
Check inputs to local_edition() (#1551)
Fixes #1547. --------- Co-authored-by: Hadley Wickham <[email protected]>
1 parent 6bb8053 commit d0b7491

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# testthat (development version)
22

3+
* `local_edition()` now gives a useful error for bad values (#1547).
34
* testthat now requires R 4.1.
45
* `expect_s4_class()` now supports unquoting (@stibu81, #2064).
56
* `it()` now finds the correct evaluation environment in more cases (@averissimo, #2085).

R/edition.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ edition_name <- function(x) {
6161
#' @param x Edition Should be a single integer.
6262
#' @param .env Environment that controls scope of changes. For expert use only.
6363
local_edition <- function(x, .env = parent.frame()) {
64-
stopifnot(is_zap(x) || (is.numeric(x) && length(x) == 1))
65-
64+
check_number_whole(x, min = 2, max = 3)
6665
local_bindings(edition = x, .env = the, .frame = .env)
6766
}
6867

tests/testthat/_snaps/edition.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# checks its inputs
2+
3+
Code
4+
local_edition("x")
5+
Condition
6+
Error in `local_edition()`:
7+
! `x` must be a whole number, not the string "x".
8+
Code
9+
local_edition(5)
10+
Condition
11+
Error in `local_edition()`:
12+
! `x` must be a whole number between 2 and 3, not the number 5.
13+
114
# deprecation only fired for newer edition
215

316
Code

tests/testthat/test-edition.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ test_that("can locally override edition", {
66
expect_equal(edition_get(), 2)
77
})
88

9+
test_that("checks its inputs", {
10+
expect_snapshot(error = TRUE, {
11+
local_edition("x")
12+
local_edition(5)
13+
})
14+
})
15+
916
test_that("deprecation only fired for newer edition", {
1017
local_edition(2)
1118
expect_warning(edition_deprecate(3, "old stuff"), NA)
@@ -34,7 +41,8 @@ test_that("edition for non-package dir is 2", {
3441
})
3542

3643
test_that("can set the edition via an environment variable", {
37-
local_edition(zap())
44+
local_bindings(edition = zap(), .env = the)
45+
3846
withr::local_envvar(TESTTHAT_EDITION = 2)
3947
expect_equal(edition_get(), 2)
4048

0 commit comments

Comments
 (0)