Skip to content

Commit 2910813

Browse files
authored
Make it easier to see what happens on CRAN (#2131)
Fixes #2112
1 parent f28b7b0 commit 2910813

File tree

8 files changed

+64
-11
lines changed

8 files changed

+64
-11
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export(it)
156156
export(local_edition)
157157
export(local_mock)
158158
export(local_mocked_bindings)
159+
export(local_on_cran)
159160
export(local_reproducible_output)
160161
export(local_snapshotter)
161162
export(local_test_context)

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+
* New `local_on_cran(TRUE)` allows you to simulate how your tests will run on CRAN (#2112).
34
* `expect_no_*()` now executes the entire code block, rather than stopping at the first message or warning (#1991).
45
* `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 (#)
56
* New `pass()` function to use in place of `succeed()` (#2113).

R/skip.R

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
#' env var).
2828
#'
2929
#' * `skip_on_cran()` skips on CRAN (using the `NOT_CRAN` env var set by
30-
#' devtools and friends).
30+
#' devtools and friends). `local_on_cran()` gives you the ability to
31+
#' easily simulate what will happen on CRAN.
3132
#'
3233
#' * `skip_on_covr()` skips when covr is running (using the `R_COVR` env var).
3334
#'
@@ -178,6 +179,15 @@ skip_on_cran <- function() {
178179
skip_if(on_cran(), "On CRAN")
179180
}
180181

182+
#' @export
183+
#' @rdname skip
184+
#' @param on_cran Pretend we're on CRAN (`TRUE`) or not (`FALSE`).
185+
#' @param frame Calling frame to tie change to; expect use only.
186+
local_on_cran <- function(on_cran, frame = caller_env()) {
187+
check_bool(on_cran)
188+
withr::local_envvar(NOT_CRAN = tolower(!on_cran), .local_envir = frame)
189+
}
190+
181191
#' @export
182192
#' @param os Character vector of one or more operating systems to skip on.
183193
#' Supported values are `"windows"`, `"mac"`, `"linux"`, `"solaris"`,
@@ -292,7 +302,12 @@ on_bioc <- function() {
292302
env_var_is_true("IS_BIOC_BUILD_MACHINE")
293303
}
294304
on_cran <- function() {
295-
!interactive() && !env_var_is_true("NOT_CRAN")
305+
env <- Sys.getenv("NOT_CRAN")
306+
if (identical(env, "")) {
307+
!interactive()
308+
} else {
309+
!isTRUE(as.logical(env))
310+
}
296311
}
297312

298313
env_var_is_true <- function(x) {

R/snapshot-file.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ expect_snapshot_file <- function(
9595
variant = NULL
9696
) {
9797
edition_require(3, "expect_snapshot_file()")
98-
if (!cran && !interactive() && on_cran()) {
98+
if (!cran && on_cran()) {
9999
skip("On CRAN")
100100
}
101101

R/snapshot.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ expect_snapshot_helper <- function(
305305
variant = NULL,
306306
trace_env = caller_env()
307307
) {
308-
if (!cran && !interactive() && on_cran()) {
308+
if (!cran && on_cran()) {
309309
skip("On CRAN")
310310
}
311311

man/skip.Rd

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/skip.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636

3737
Reason: offline
3838

39+
# skip_on_cran generates useful message
40+
41+
Reason: On CRAN
42+
3943
# skip_on_cran() works as expected
4044

4145
Reason: On CRAN

tests/testthat/test-skip.R

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,42 @@ test_that("skip_if_not_installed() works as expected", {
5555
expect_snapshot_skip(skip_if_offline())
5656
})
5757

58+
test_that("skip_on_cran generates useful message", {
59+
withr::local_envvar(NOT_CRAN = "false")
60+
expect_snapshot_skip(skip_on_cran())
61+
})
62+
5863
test_that("skip_on_cran() works as expected", {
59-
skip_on_cran()
64+
local({
65+
local_on_cran(FALSE)
66+
expect_no_skip(skip_on_cran())
67+
})
68+
69+
local({
70+
local_on_cran(TRUE)
71+
expect_snapshot_skip(skip_on_cran())
72+
})
6073

61-
withr::local_envvar(NOT_CRAN = "true")
74+
withr::local_envvar(NOT_CRAN = NA)
75+
local_mocked_bindings(interactive = function() TRUE)
6276
expect_no_skip(skip_on_cran())
6377

64-
withr::local_envvar(NOT_CRAN = "false")
6578
local_mocked_bindings(interactive = function() FALSE)
66-
expect_snapshot_skip(skip_on_cran(), cran = TRUE)
79+
expect_skip(skip_on_cran())
80+
})
6781

68-
local_mocked_bindings(interactive = function() TRUE)
69-
expect_no_skip(skip_on_cran())
82+
test_that("local_on_cran sets NOT_CRAN", {
83+
local({
84+
local_on_cran(TRUE)
85+
expect_equal(on_cran(), TRUE)
86+
expect_equal(Sys.getenv("NOT_CRAN"), "false")
87+
})
88+
89+
local({
90+
local_on_cran(FALSE)
91+
expect_equal(on_cran(), FALSE)
92+
expect_equal(Sys.getenv("NOT_CRAN"), "true")
93+
})
7094
})
7195

7296
test_that("skip_on_ci() works as expected", {

0 commit comments

Comments
 (0)