Skip to content
Merged
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export(it)
export(local_edition)
export(local_mock)
export(local_mocked_bindings)
export(local_on_cran)
export(local_reproducible_output)
export(local_snapshotter)
export(local_test_context)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# testthat (development version)

* New `local_on_cran(TRUE)` allows you to simulate how your tests will run on CRAN (#2112).
* `expect_no_*()` now executes the entire code block, rather than stopping at the first message or warning (#1991).
* `expect_no_failures()` and `expect_no_successes()` are now deprecated as `expect_success()` now test for no failures and `expect_failure()` tests for no successes (#)
* New `pass()` function to use in place of `succeed()` (#2113).
Expand Down
19 changes: 17 additions & 2 deletions R/skip.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
#' env var).
#'
#' * `skip_on_cran()` skips on CRAN (using the `NOT_CRAN` env var set by
#' devtools and friends).
#' devtools and friends). `local_on_cran()` gives you the ability to
#' easily simulate what will happen on CRAN.
#'
#' * `skip_on_covr()` skips when covr is running (using the `R_COVR` env var).
#'
Expand Down Expand Up @@ -178,6 +179,15 @@ skip_on_cran <- function() {
skip_if(on_cran(), "On CRAN")
}

#' @export
#' @rdname skip
#' @param on_cran Pretend we're on CRAN (`TRUE`) or not (`FALSE`).
#' @param frame Calling frame to tie change to; expect use only.
local_on_cran <- function(on_cran, frame = caller_env()) {
check_bool(on_cran)
withr::local_envvar(NOT_CRAN = tolower(!on_cran), .local_envir = frame)
}

#' @export
#' @param os Character vector of one or more operating systems to skip on.
#' Supported values are `"windows"`, `"mac"`, `"linux"`, `"solaris"`,
Expand Down Expand Up @@ -292,7 +302,12 @@ on_bioc <- function() {
env_var_is_true("IS_BIOC_BUILD_MACHINE")
}
on_cran <- function() {
!interactive() && !env_var_is_true("NOT_CRAN")
env <- Sys.getenv("NOT_CRAN")
if (identical(env, "")) {
!interactive()
} else {
!isTRUE(as.logical(env))
}
}

env_var_is_true <- function(x) {
Expand Down
2 changes: 1 addition & 1 deletion R/snapshot-file.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ expect_snapshot_file <- function(
variant = NULL
) {
edition_require(3, "expect_snapshot_file()")
if (!cran && !interactive() && on_cran()) {
if (!cran && on_cran()) {
skip("On CRAN")
}

Expand Down
2 changes: 1 addition & 1 deletion R/snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ expect_snapshot_helper <- function(
variant = NULL,
trace_env = caller_env()
) {
if (!cran && !interactive() && on_cran()) {
if (!cran && on_cran()) {
skip("On CRAN")
}

Expand Down
10 changes: 9 additions & 1 deletion man/skip.Rd

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

4 changes: 4 additions & 0 deletions tests/testthat/_snaps/skip.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@

Reason: offline

# skip_on_cran generates useful message

Reason: On CRAN

# skip_on_cran() works as expected

Reason: On CRAN
Expand Down
36 changes: 30 additions & 6 deletions tests/testthat/test-skip.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,42 @@ test_that("skip_if_not_installed() works as expected", {
expect_snapshot_skip(skip_if_offline())
})

test_that("skip_on_cran generates useful message", {
withr::local_envvar(NOT_CRAN = "false")
expect_snapshot_skip(skip_on_cran())
})

test_that("skip_on_cran() works as expected", {
skip_on_cran()
local({
local_on_cran(FALSE)
expect_no_skip(skip_on_cran())
})

local({
local_on_cran(TRUE)
expect_snapshot_skip(skip_on_cran())
})

withr::local_envvar(NOT_CRAN = "true")
withr::local_envvar(NOT_CRAN = NA)
local_mocked_bindings(interactive = function() TRUE)
expect_no_skip(skip_on_cran())

withr::local_envvar(NOT_CRAN = "false")
local_mocked_bindings(interactive = function() FALSE)
expect_snapshot_skip(skip_on_cran(), cran = TRUE)
expect_skip(skip_on_cran())
})

local_mocked_bindings(interactive = function() TRUE)
expect_no_skip(skip_on_cran())
test_that("local_on_cran sets NOT_CRAN", {
local({
local_on_cran(TRUE)
expect_equal(on_cran(), TRUE)
expect_equal(Sys.getenv("NOT_CRAN"), "false")
})

local({
local_on_cran(FALSE)
expect_equal(on_cran(), FALSE)
expect_equal(Sys.getenv("NOT_CRAN"), "true")
})
})

test_that("skip_on_ci() works as expected", {
Expand Down
Loading