diff --git a/NAMESPACE b/NAMESPACE index f2fdeaf38..7484031bd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/NEWS.md b/NEWS.md index 14a7fdbeb..b7c476749 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). diff --git a/R/skip.R b/R/skip.R index e36b12b64..3903a1c1d 100644 --- a/R/skip.R +++ b/R/skip.R @@ -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). #' @@ -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"`, @@ -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) { diff --git a/R/snapshot-file.R b/R/snapshot-file.R index 55178b8c3..cba752fc8 100644 --- a/R/snapshot-file.R +++ b/R/snapshot-file.R @@ -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") } diff --git a/R/snapshot.R b/R/snapshot.R index a143cac4a..a579c7010 100644 --- a/R/snapshot.R +++ b/R/snapshot.R @@ -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") } diff --git a/man/skip.Rd b/man/skip.Rd index e64ccfd8f..b61733f31 100644 --- a/man/skip.Rd +++ b/man/skip.Rd @@ -8,6 +8,7 @@ \alias{skip_unless_r} \alias{skip_if_offline} \alias{skip_on_cran} +\alias{local_on_cran} \alias{skip_on_os} \alias{skip_on_ci} \alias{skip_on_covr} @@ -29,6 +30,8 @@ skip_if_offline(host = "captive.apple.com") skip_on_cran() +local_on_cran(on_cran, frame = caller_env()) + skip_on_os(os, arch = NULL) skip_on_ci() @@ -54,6 +57,10 @@ should only be run on R versions 4.1.0 and later.} \item{host}{A string with a hostname to lookup} +\item{on_cran}{Pretend we're on CRAN (\code{TRUE}) or not (\code{FALSE}).} + +\item{frame}{Calling frame to tie change to; expect use only.} + \item{os}{Character vector of one or more operating systems to skip on. Supported values are \code{"windows"}, \code{"mac"}, \code{"linux"}, \code{"solaris"}, and \code{"emscripten"}.} @@ -91,7 +98,8 @@ difficult to install. \item \code{skip_on_bioc()} skips on Bioconductor (using the \code{IS_BIOC_BUILD_MACHINE} env var). \item \code{skip_on_cran()} skips on CRAN (using the \code{NOT_CRAN} env var set by -devtools and friends). +devtools and friends). \code{local_on_cran()} gives you the ability to +easily simulate what will happen on CRAN. \item \code{skip_on_covr()} skips when covr is running (using the \code{R_COVR} env var). \item \code{skip_on_ci()} skips on continuous integration systems like GitHub Actions, travis, and appveyor (using the \code{CI} env var). diff --git a/tests/testthat/_snaps/skip.md b/tests/testthat/_snaps/skip.md index 3868f8216..8fafca696 100644 --- a/tests/testthat/_snaps/skip.md +++ b/tests/testthat/_snaps/skip.md @@ -36,6 +36,10 @@ Reason: offline +# skip_on_cran generates useful message + + Reason: On CRAN + # skip_on_cran() works as expected Reason: On CRAN diff --git a/tests/testthat/test-skip.R b/tests/testthat/test-skip.R index efb114eb9..032475aff 100644 --- a/tests/testthat/test-skip.R +++ b/tests/testthat/test-skip.R @@ -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", {