Skip to content

Commit 5aaea02

Browse files
committed
Make it easier to see what happens on CRAN
Fixes #2112
1 parent 1988ac8 commit 5aaea02

File tree

8 files changed

+56
-10
lines changed

8 files changed

+56
-10
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
* New `pass()` function to use in place of `succeed()` (#2113).
45
* `expectation()` is now a combination of `new_expectation()` and `exp_signal()` (#2125).
56
* `is_null()`/`matches()` deprecated in 2.0.0 (2017-12-19) and `is_true()`/`is_false()` deprecated in 2.1.0 (2019-04-23) have been removed (#2109).

R/skip.R

Lines changed: 15 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,13 @@ skip_on_cran <- function() {
178179
skip_if(on_cran(), "On CRAN")
179180
}
180181

182+
#' @export
183+
#' @rdname skip
184+
local_on_cran <- function(on_cran, frame = caller_env()) {
185+
check_bool(on_cran)
186+
withr::local_envvar(NOT_CRAN = tolower(!on_cran), .local_envir = frame)
187+
}
188+
181189
#' @export
182190
#' @param os Character vector of one or more operating systems to skip on.
183191
#' Supported values are `"windows"`, `"mac"`, `"linux"`, `"solaris"`,
@@ -292,7 +300,12 @@ on_bioc <- function() {
292300
env_var_is_true("IS_BIOC_BUILD_MACHINE")
293301
}
294302
on_cran <- function() {
295-
!interactive() && !env_var_is_true("NOT_CRAN")
303+
env <- Sys.getenv("NOT_CRAN")
304+
if (identical(env, "")) {
305+
!interactive()
306+
} else {
307+
!isTRUE(as.logical(env))
308+
}
296309
}
297310

298311
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
@@ -298,7 +298,7 @@ expect_snapshot_helper <- function(
298298
variant = NULL,
299299
trace_env = caller_env()
300300
) {
301-
if (!cran && !interactive() && on_cran()) {
301+
if (!cran && on_cran()) {
302302
skip("On CRAN")
303303
}
304304

man/skip.Rd

Lines changed: 3 additions & 0 deletions
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)