Skip to content

Commit 4c6ef87

Browse files
committed
Skip snapshot on CRAN at end of test
This ensures that the snapshots are not deleted. This also made me realise I missed the analgous update in `expect_snapshot_file()` and it's also clear that it should announce the file so it doesn't get deleted. With these changes in place I can now run all the tests and ensure they pass on CRAN. This fixes #2214
1 parent c45ce93 commit 4c6ef87

File tree

7 files changed

+39
-4
lines changed

7 files changed

+39
-4
lines changed

R/snapshot-file.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,11 @@ expect_snapshot_file <- function(
9999
check_bool(cran)
100100

101101
edition_require(3, "expect_snapshot_file()")
102+
103+
announce_snapshot_file(name = name)
102104
if (!cran && on_cran()) {
103-
skip("On CRAN")
105+
signal(class = "snapshot_on_cran")
106+
return(invisible())
104107
}
105108

106109
check_variant(variant)

R/snapshot.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ expect_snapshot_helper <- function(
313313
trace_env = caller_env()
314314
) {
315315
if (!cran && on_cran()) {
316+
signal(class = "snapshot_on_cran")
316317
return(invisible())
317318
}
318319

R/test-that.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ test_code <- function(code, env, reporter = NULL, skip_on_empty = TRUE) {
6565
starting_expectations <- the$test_expectations
6666

6767
ok <- TRUE
68+
snapshot_skipped <- FALSE
6869

6970
# @param debug_end How many frames should be skipped to find the
7071
# last relevant frame call. Only useful for the DebugReporter.
@@ -164,7 +165,9 @@ test_code <- function(code, env, reporter = NULL, skip_on_empty = TRUE) {
164165
{
165166
eval(code, test_env)
166167
new_expectations <- the$test_expectations > starting_expectations
167-
if (!new_expectations && skip_on_empty) {
168+
if (snapshot_skipped) {
169+
skip("On CRAN")
170+
} else if (!new_expectations && skip_on_empty) {
168171
skip_empty()
169172
}
170173
},
@@ -174,6 +177,9 @@ test_code <- function(code, env, reporter = NULL, skip_on_empty = TRUE) {
174177
skip(paste0("{", e$package, "} is not installed."))
175178
}
176179
},
180+
snapshot_on_cran = function(cnd) {
181+
snapshot_skipped <<- TRUE
182+
},
177183
skip = handle_skip,
178184
warning = handle_warning,
179185
message = handle_message,

tests/testthat/test-parallel.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ test_that("fail", {
6262

6363
test_that("snapshots", {
6464
skip_on_covr()
65+
skip_on_cran()
6566
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
67+
6668
tmp <- withr::local_tempdir("testthat-snap-")
6769
file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
6870
# we cannot run these with the silent reporter, because it is not
@@ -85,7 +87,9 @@ test_that("snapshots", {
8587

8688
test_that("new snapshots are added", {
8789
skip_on_covr()
90+
skip_on_cran()
8891
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE", CI = "false"))
92+
8993
tmp <- withr::local_tempdir("testthat-snap-")
9094
file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
9195
unlink(file.path(tmp, "snap", "tests", "testthat", "_snaps", "snap-2.md"))
@@ -110,7 +114,9 @@ test_that("new snapshots are added", {
110114

111115
test_that("snapshots are removed if test file has no snapshots", {
112116
skip_on_covr()
117+
skip_on_cran()
113118
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
119+
114120
tmp <- withr::local_tempdir("testthat-snap-")
115121
file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
116122
writeLines(
@@ -138,6 +144,8 @@ test_that("snapshots are removed if test file has no snapshots", {
138144

139145
test_that("snapshots are removed if test file is removed", {
140146
skip_on_covr()
147+
skip_on_cran()
148+
141149
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
142150
withr::defer(unlink(tmp, recursive = TRUE))
143151
dir.create(tmp <- tempfile("testthat-snap-"))

tests/testthat/test-snapshot-file.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ test_that("expect_snapshot_file works in a different directory", {
3030
})
3131

3232
test_that("expect_snapshot_file works with variant", {
33+
local_on_cran(FALSE)
34+
3335
expect_snapshot_file(
3436
write_tmp_lines(r_version()),
3537
"version.txt",
@@ -39,6 +41,8 @@ test_that("expect_snapshot_file works with variant", {
3941
})
4042

4143
test_that("expect_snapshot_file finds duplicate snapshot files", {
44+
local_on_cran(FALSE)
45+
4246
expect_snapshot(
4347
expect_snapshot_file(
4448
write_tmp_lines(r_version()),
@@ -50,6 +54,7 @@ test_that("expect_snapshot_file finds duplicate snapshot files", {
5054
})
5155

5256
test_that("basic workflow", {
57+
local_on_cran(FALSE)
5358
snapper <- local_test_snapshotter()
5459

5560
path <- write_tmp_lines(letters)

tests/testthat/test-snapshot-reporter.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ test_that("can establish local snapshotter for testing", {
88
})
99

1010
test_that("basic workflow", {
11+
local_on_cran(FALSE)
12+
1113
path <- withr::local_tempdir()
1214
snapper <- local_test_snapshotter(snap_dir = path)
1315
snapper$start_file("snapshot-2")
@@ -39,6 +41,7 @@ test_that("basic workflow", {
3941
})
4042

4143
test_that("defaults to failing on CI", {
44+
local_on_cran(FALSE)
4245
withr::local_envvar(CI = "true")
4346

4447
path <- withr::local_tempdir()
@@ -50,6 +53,8 @@ test_that("defaults to failing on CI", {
5053
})
5154

5255
test_that("only create new files for changed variants", {
56+
local_on_cran(FALSE)
57+
5358
snapper <- local_test_snapshotter()
5459
snapper$start_file("variants", "test")
5560
expect_warning(expect_snapshot_output("x"), "Adding new")
@@ -86,6 +91,8 @@ test_that("only create new files for changed variants", {
8691
})
8792

8893
test_that("only reverting change in variant deletes .new", {
94+
local_on_cran(FALSE)
95+
8996
snapper <- local_test_snapshotter()
9097
snapper$start_file("v", "test")
9198
expect_warning(expect_snapshot_output("x", variant = "a"), "Adding new")
@@ -108,6 +115,8 @@ test_that("only reverting change in variant deletes .new", {
108115

109116

110117
test_that("removing tests removes snap file", {
118+
local_on_cran(FALSE)
119+
111120
path <- withr::local_tempdir()
112121
snapper <- local_test_snapshotter(snap_dir = path)
113122
snapper$start_file("snapshot-3", "test")
@@ -121,6 +130,7 @@ test_that("removing tests removes snap file", {
121130
})
122131

123132
test_that("errors in test doesn't change snapshot", {
133+
local_on_cran(FALSE)
124134
snapper <- local_test_snapshotter()
125135

126136
# First run

tests/testthat/test-snapshot.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ test_that("hint is informative", {
176176

177177
test_that("expect_snapshot requires a non-empty test label", {
178178
local_description_set()
179+
local_on_cran(FALSE)
179180

180181
test_that("", {
181182
expect_error(expect_snapshot(1 + 1))
@@ -212,13 +213,14 @@ test_that("expect_snapshot_warning validates its inputs", {
212213
})
213214
})
214215

215-
test_that("on CRAN, snapshots are not run but don't skill entire test", {
216+
test_that("on CRAN, snapshots generate skip at end of test", {
216217
local_on_cran(TRUE)
217218

218219
expectations <- capture_expectations(test_that("", {
219220
expect_snapshot(1 + 1)
220221
expect_true(TRUE)
221222
}))
222-
expect_length(expectations, 1)
223+
expect_length(expectations, 2)
223224
expect_s3_class(expectations[[1]], "expectation_success")
225+
expect_s3_class(expectations[[2]], "expectation_skip")
224226
})

0 commit comments

Comments
 (0)