Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export(skip_on_covr)
export(skip_on_cran)
export(skip_on_os)
export(skip_on_travis)
export(skip_unless_r)
export(snapshot_accept)
export(snapshot_review)
export(source_dir)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* `expect_s4_class()` now supports unquoting (@stibu81, #2064).
* `it()` now finds the correct evaluation environment in more cases (@averissimo, #2085).
* Fixed an issue preventing compilation from succeeding due to deprecation / removal of `std::uncaught_exception()` (@kevinushey, #2047).
* New `skip_unless_r()` to skip running tests on unsuitable versions of R, e.g. `skip_unless_r(">= 4.1.0")` to skip tests that require `...names` (@MichaelChirico, #2022)

# testthat 3.2.3

Expand Down
3 changes: 3 additions & 0 deletions R/expect-self-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ expect_no_failure <- function(expr) {
expect_snapshot_skip <- function(x, cran = FALSE) {
expect_snapshot_error(x, class = "skip", cran = cran)
}
expect_skip <- function(code) {
expect_condition(code, class = "skip")
}
expect_no_skip <- function(code) {
expect_no_condition(code, class = "skip")
}
Expand Down
22 changes: 22 additions & 0 deletions R/skip.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,28 @@ package_version <- function(x) {
utils::packageVersion(x)
}

#' @export
#' @param spec A version specification like '>= 4.1.0' denoting that this test
#' should only be run on R versions 4.1.0 and later.
#' @rdname skip
skip_unless_r <- function(spec) {
parts <- unlist(strsplit(spec, " ", fixed = TRUE))
if (length(parts) != 2L) {
cli::cli_abort("{.arg spec} should be a comparison like '>=' and an R version separated by a space.")
}
comparator <- match.fun(parts[1L])
required_version <- numeric_version(parts[2L])

current_version <- getRversion()
skip_if_not(
comparator(current_version, required_version),
sprintf(
"R version requirement not satisfied: %s %s %s",
current_version, parts[1L], required_version
)
)
}

#' @export
#' @rdname skip
skip_if_offline <- function(host = "captive.apple.com") {
Expand Down
6 changes: 6 additions & 0 deletions man/skip.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-skip.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,13 @@ test_that("can refine os with arch", {
expect_no_skip(skip_on_os("windows", "x86_64"))
expect_no_skip(skip_on_os("linux", "i386"))
})

test_that("skip_unless_r works as expected", {
expect_no_skip(skip_unless_r(">= 0.0.0"))
expect_no_skip(skip_unless_r(paste("==", getRversion())))
expect_no_skip(skip_unless_r("<= 999.999.999"))

expect_skip(skip_unless_r(">= 999.999.999"))
expect_skip(skip_unless_r("== 0.0.0"))
expect_skip(skip_unless_r("<= 0.0.0"))
})
Loading