Skip to content

Commit 676234f

Browse files
authored
Actually fail with new snapshots on CI (#2187)
By defaulting `fail_on_new` to `NULL` in more places. Also involved fixing confusing errors by correctly propagating failures, and reduced minimised overall failure error message. Continuation of #2149. Fixes #2176.
1 parent 5925478 commit 676234f

File tree

12 files changed

+63
-31
lines changed

12 files changed

+63
-31
lines changed

R/parallel.R

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ test_files_parallel <- function(
8383

8484
test_files_reporter_parallel <- function(reporter, .env = parent.frame()) {
8585
lister <- ListReporter$new()
86-
snapshotter <- MainprocessSnapshotReporter$new("_snaps", fail_on_new = FALSE)
86+
snapshotter <- MainprocessSnapshotReporter$new("_snaps")
8787
reporters <- list(
8888
find_reporter(reporter),
8989
lister, # track data
@@ -301,10 +301,7 @@ queue_process_setup <- function(
301301

302302
queue_task <- function(path) {
303303
withr::local_envvar("TESTTHAT_IS_PARALLEL" = "true")
304-
snapshotter <- SubprocessSnapshotReporter$new(
305-
snap_dir = "_snaps",
306-
fail_on_new = FALSE
307-
)
304+
snapshotter <- SubprocessSnapshotReporter$new(snap_dir = "_snaps")
308305
withr::local_options(testthat.snapshotter = snapshotter)
309306
reporters <- list(
310307
SubprocessReporter$new(),

R/snapshot-file.R

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ expect_snapshot_file <- function(
139139
variant = variant,
140140
trace_env = caller_env()
141141
)
142+
if (inherits(equal, "expectation_failure")) {
143+
return(equal)
144+
}
145+
142146
hint <- snapshot_review_hint(snapshotter$file, name)
143147

144148
if (!equal) {
@@ -196,7 +200,7 @@ snapshot_file_equal <- function(
196200
snap_variant, # variant (optional)
197201
path, # path to new file
198202
file_equal = compare_file_binary,
199-
fail_on_new = FALSE,
203+
fail_on_new = NULL,
200204
trace_env = caller_env()
201205
) {
202206
if (!file.exists(path)) {
@@ -208,6 +212,7 @@ snapshot_file_equal <- function(
208212
} else {
209213
snap_test_dir <- file.path(snap_dir, snap_variant, snap_test)
210214
}
215+
fail_on_new <- fail_on_new %||% on_ci()
211216

212217
cur_path <- file.path(snap_test_dir, snap_name)
213218
new_path <- file.path(snap_test_dir, new_name(snap_name))
@@ -232,12 +237,13 @@ snapshot_file_equal <- function(
232237
snap_name,
233238
"'"
234239
)
240+
241+
# We want to fail on CI since this suggests that the user has failed
242+
# to record the value locally
235243
if (fail_on_new) {
236244
return(fail(message, trace_env = trace_env))
237-
} else {
238-
testthat_warn(message)
239245
}
240-
246+
testthat_warn(message)
241247
TRUE
242248
}
243249
}

R/snapshot-reporter.R

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ SnapshotReporter <- R6::R6Class(
88
test_file_seen = character(),
99
snap_file_seen = character(),
1010
variants_changed = FALSE,
11-
fail_on_new = FALSE,
11+
fail_on_new = NULL,
1212

1313
old_snaps = NULL,
1414
cur_snaps = NULL,
1515
new_snaps = NULL,
1616

17-
initialize = function(snap_dir = "_snaps", fail_on_new = FALSE) {
17+
initialize = function(snap_dir = "_snaps", fail_on_new = NULL) {
1818
self$snap_dir <- normalizePath(snap_dir, mustWork = FALSE)
1919
self$fail_on_new <- fail_on_new
2020
},
@@ -82,19 +82,18 @@ SnapshotReporter <- R6::R6Class(
8282
value_enc <- save(value)
8383

8484
self$cur_snaps$append(self$test, variant, value_enc)
85+
fail_on_new <- self$fail_on_new %||% on_ci()
8586

8687
message <- paste0(
8788
"Adding new snapshot",
8889
if (variant != "_default") paste0(" for variant '", variant, "'"),
89-
if (self$fail_on_new) " in CI",
9090
":\n",
9191
value_enc
9292
)
93-
if (self$fail_on_new) {
93+
if (fail_on_new) {
9494
return(fail(message, trace_env = trace_env))
95-
} else {
96-
testthat_warn(message)
9795
}
96+
testthat_warn(message)
9897
character()
9998
}
10099
},
@@ -196,7 +195,7 @@ get_snapshotter <- function() {
196195
local_snapshotter <- function(
197196
snap_dir = NULL,
198197
cleanup = FALSE,
199-
fail_on_new = FALSE,
198+
fail_on_new = NULL,
200199
.env = parent.frame()
201200
) {
202201
snap_dir <- snap_dir %||% withr::local_tempdir(.local_envir = .env)

R/snapshot.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ expect_snapshot_helper <- function(
331331
variant = variant,
332332
trace_env = trace_env
333333
)
334+
if (inherits(comp, "expectation_failure")) {
335+
return(comp)
336+
}
334337

335338
if (!identical(variant, "_default")) {
336339
variant_lab <- paste0(" (variant '", variant, "')")

R/test-files.R

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ test_files_reporter <- function(reporter, .env = parent.frame()) {
311311
reporters <- list(
312312
find_reporter(reporter),
313313
lister, # track data
314-
local_snapshotter("_snaps", fail_on_new = on_ci(), .env = .env)
314+
local_snapshotter("_snaps", .env = .env)
315315
)
316316
list(
317317
multi = MultiReporter$new(reporters = compact(reporters)),
@@ -325,10 +325,18 @@ test_files_check <- function(
325325
stop_on_warning = FALSE
326326
) {
327327
if (stop_on_failure && !all_passed(results)) {
328-
cli::cli_abort("Test failures.")
328+
cli::cli_abort(
329+
"Test failures.",
330+
call = NULL,
331+
trace = data.frame()
332+
)
329333
}
330334
if (stop_on_warning && any_warnings(results)) {
331-
cli::cli_abort("Tests generated warnings.")
335+
cli::cli_abort(
336+
"Tests generated warnings.",
337+
call = NULL,
338+
trace = data.frame()
339+
)
332340
}
333341

334342
invisible(results)

man/local_snapshotter.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.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
R4.6
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# variants save different values
2+
3+
Code
4+
r_version()
5+
Output
6+
[1] "R4.6"
7+

tests/testthat/_snaps/test-files.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Code
44
test_dir(test_path("test_dir"), reporter = "silent")
55
Condition
6-
Error in `test_files_check()`:
6+
Error:
77
! Test failures.
88

99
# runs all tests and records output
@@ -40,15 +40,15 @@
4040
Code
4141
test_error(stop_on_failure = TRUE)
4242
Condition
43-
Error in `test_files_check()`:
43+
Error:
4444
! Test failures.
4545

4646
# can control if warnings errors
4747

4848
Code
4949
test_warning(stop_on_warning = TRUE)
5050
Condition
51-
Error in `test_files_check()`:
51+
Error:
5252
! Tests generated warnings.
5353

5454
# complains if file doesn't exist

tests/testthat/test-parallel.R

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ test_that("fail", {
6060

6161
test_that("snapshots", {
6262
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
63-
withr::defer(unlink(tmp, recursive = TRUE))
64-
dir.create(tmp <- tempfile("testthat-snap-"))
63+
tmp <- withr::local_tempdir("testthat-snap-")
6564
file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
6665
# we cannot run these with the silent reporter, because it is not
6766
# parallel compatible, and they'll not run in parallel
@@ -82,11 +81,11 @@ test_that("snapshots", {
8281
})
8382

8483
test_that("new snapshots are added", {
85-
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
86-
withr::defer(unlink(tmp, recursive = TRUE))
87-
dir.create(tmp <- tempfile("testthat-snap-"))
84+
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE", CI = "false"))
85+
tmp <- withr::local_tempdir("testthat-snap-")
8886
file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
8987
unlink(file.path(tmp, "snap", "tests", "testthat", "_snaps", "snap-2.md"))
88+
9089
# we cannot run these with the silent reporter, because it is not
9190
# parallel compatible, and they'll not run in parallel
9291
capture.output(suppressMessages(
@@ -107,13 +106,13 @@ test_that("new snapshots are added", {
107106

108107
test_that("snapshots are removed if test file has no snapshots", {
109108
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
110-
withr::defer(unlink(tmp, recursive = TRUE))
111-
dir.create(tmp <- tempfile("testthat-snap-"))
109+
tmp <- withr::local_tempdir("testthat-snap-")
112110
file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
113111
writeLines(
114112
"test_that(\"2\", { expect_true(TRUE) })",
115113
file.path(tmp, "snap", "tests", "testthat", "test-snap-2.R")
116114
)
115+
117116
# we cannot run these with the silent reporter, because it is not
118117
# parallel compatible, and they'll not run in parallel
119118
capture.output(suppressMessages(

0 commit comments

Comments
 (0)