Skip to content

Commit 6883737

Browse files
authored
Make it possible to muffle CRAN skips (#2225)
This shouldn't have any effect on user code, but we need some way to ensure that CRAN skips aren't propagated across multiple `test_code()` boundaries so that we can properly test. Also includes a few other CRAN related tweaks.
1 parent abbd07d commit 6883737

File tree

10 files changed

+24
-12
lines changed

10 files changed

+24
-12
lines changed

R/skip.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ skip_on_cran <- function() {
185185
#' @rdname skip
186186
#' @param on_cran Pretend we're on CRAN (`TRUE`) or not (`FALSE`).
187187
#' @param frame Calling frame to tie change to; expect use only.
188-
local_on_cran <- function(on_cran, frame = caller_env()) {
188+
local_on_cran <- function(on_cran = TRUE, frame = caller_env()) {
189189
check_bool(on_cran)
190190
withr::local_envvar(NOT_CRAN = tolower(!on_cran), .local_envir = frame)
191191
}

R/snapshot-file.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ expect_snapshot_file <- function(
122122

123123
announce_snapshot_file(name = name)
124124
if (!cran && on_cran()) {
125-
signal(class = "snapshot_on_cran")
125+
signal_snapshot_on_cran()
126126
return(invisible())
127127
}
128128

R/snapshot-reporter.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ SnapshotReporter <- R6::R6Class(
6363
return()
6464
}
6565

66-
self$test <- paste0(gsub("\n", "", test), collapse = " / ")
66+
self$test <- gsub("\n", "", test)
6767
},
6868

6969
# Called by expectation
@@ -163,6 +163,10 @@ SnapshotReporter <- R6::R6Class(
163163
}
164164

165165
# If expectation errors or skips, need to copy snapshots from old to cur
166+
# TODO: the logic is not correct here for subtests, probably because
167+
# the code was not written under the assumption that start_test()
168+
# generates a stack of tests. You can see the problem by running
169+
# local_on_cran() then testing describe.R.
166170
if (expectation_error(result) || expectation_skip(result)) {
167171
self$cur_snaps$reset(self$test, self$old_snaps)
168172
}

R/snapshot.R

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ expect_snapshot_helper <- function(
344344
trace_env = caller_env()
345345
) {
346346
if (!cran && on_cran()) {
347-
signal(class = "snapshot_on_cran")
347+
signal_snapshot_on_cran()
348348
return(invisible())
349349
}
350350

@@ -461,3 +461,10 @@ with_is_snapshotting <- function(code) {
461461
withr::local_envvar(TESTTHAT_IS_SNAPSHOT = "true")
462462
code
463463
}
464+
465+
signal_snapshot_on_cran <- function() {
466+
withRestarts(
467+
signal(class = "snapshot_on_cran"),
468+
muffle_cran_snapshot = function() {}
469+
)
470+
}

R/test-that.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ test_code <- function(code, env, reporter = NULL, skip_on_empty = TRUE) {
179179
},
180180
snapshot_on_cran = function(cnd) {
181181
snapshot_skipped <<- TRUE
182+
invokeRestart("muffle_cran_snapshot")
182183
},
183184
skip = handle_skip,
184185
warning = handle_warning,

man/skip.Rd

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

tests/testthat/_snaps/describe.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
# snapshot tests in describe / and in it
99

1010
Code
11-
1 + 1
11+
2 + 2
1212
Output
13-
[1] 2
13+
[1] 4
1414

1515
# has to have a valid description for the block
1616

tests/testthat/test-describe.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test_that("can write snaphot tests", {
1919
describe("snapshot tests in describe", {
2020
expect_snapshot(1 + 1)
2121
it("and in it", {
22-
expect_snapshot(1 + 1)
22+
expect_snapshot(2 + 2)
2323
})
2424
})
2525
})

tests/testthat/test-skip.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ test_that("skip_on_cran() works as expected", {
6363

6464
local({
6565
local_on_cran(TRUE)
66-
expect_snapshot_skip(skip_on_cran())
66+
expect_skip(skip_on_cran())
6767
})
6868

6969
withr::local_envvar(NOT_CRAN = NA)

tests/testthat/test-snapshot-reporter.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ test_that("can filter with desc", {
196196
snapper <- local_test_snapshotter(snap_dir = path)
197197
snapper$start_file("snapshot")
198198
snapper$start_test(test = "x")
199-
expect_snapshot_output(cat("x"))
199+
expect_snapshot_output(cat("x"), cran = TRUE)
200200
snapper$end_test()
201201
snapper$start_test(test = "y")
202-
expect_snapshot_output(cat("y"))
202+
expect_snapshot_output(cat("y"), cran = TRUE)
203203
snapper$end_test()
204204
snapper$end_file()
205205
})
@@ -209,7 +209,7 @@ test_that("can filter with desc", {
209209
snapper <- local_test_snapshotter(snap_dir = path, desc = "x")
210210
snapper$start_file("snapshot")
211211
snapper$start_test(test = "x")
212-
expect_snapshot_output(cat("x"))
212+
expect_snapshot_output(cat("x"), cran = TRUE)
213213
snapper$end_test()
214214
snapper$end_file()
215215
snaps_filtered <- readLines(file.path(path, "snapshot.md"))

0 commit comments

Comments
 (0)