Skip to content

Commit eca73da

Browse files
committed
Actually fail with new snapshots on CI
By defaulting `fail_on_new` to `NULL` in more Continuation of #2149. Fixes #2176.
1 parent edfd684 commit eca73da

File tree

5 files changed

+16
-14
lines changed

5 files changed

+16
-14
lines changed

R/parallel.R

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

8989
test_files_reporter_parallel <- function(reporter, .env = parent.frame()) {
9090
lister <- ListReporter$new()
91-
snapshotter <- MainprocessSnapshotReporter$new("_snaps", fail_on_new = FALSE)
91+
snapshotter <- MainprocessSnapshotReporter$new("_snaps")
9292
reporters <- list(
9393
find_reporter(reporter),
9494
lister, # track data
@@ -303,10 +303,7 @@ queue_process_setup <- function(
303303

304304
queue_task <- function(path) {
305305
withr::local_envvar("TESTTHAT_IS_PARALLEL" = "true")
306-
snapshotter <- SubprocessSnapshotReporter$new(
307-
snap_dir = "_snaps",
308-
fail_on_new = FALSE
309-
)
306+
snapshotter <- SubprocessSnapshotReporter$new(snap_dir = "_snaps")
310307
withr::local_options(testthat.snapshotter = snapshotter)
311308
reporters <- list(
312309
SubprocessReporter$new(),

R/snapshot-file.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,13 @@ snapshot_file_equal <- function(
194194
snap_variant,
195195
path,
196196
file_equal = compare_file_binary,
197-
fail_on_new = FALSE,
197+
fail_on_new = NULL,
198198
trace_env = caller_env()
199199
) {
200200
if (!file.exists(path)) {
201201
abort(paste0("`", path, "` not found"))
202202
}
203+
fail_on_new <- fail_on_new %||% on_ci()
203204

204205
cur_path <- file.path(snap_test_dir, snap_name)
205206
new_path <- new_name(cur_path)
@@ -224,7 +225,10 @@ snapshot_file_equal <- function(
224225
snap_name,
225226
"'"
226227
)
227-
if (fail_on_new) {
228+
229+
# We want to fail on CI since this suggests that the user has failed
230+
# to record the value locally
231+
if (on_ci()) {
228232
return(fail(message, trace_env = trace_env))
229233
} else {
230234
testthat_warn(message)

R/snapshot-reporter.R

Lines changed: 6 additions & 5 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,15 +82,16 @@ 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",
90+
if (fail_on_new) " in CI",
9091
":\n",
9192
value_enc
9293
)
93-
if (self$fail_on_new) {
94+
if (fail_on_new) {
9495
return(fail(message, trace_env = trace_env))
9596
} else {
9697
testthat_warn(message)
@@ -200,7 +201,7 @@ get_snapshotter <- function() {
200201
local_snapshotter <- function(
201202
snap_dir = NULL,
202203
cleanup = FALSE,
203-
fail_on_new = FALSE,
204+
fail_on_new = NULL,
204205
.env = parent.frame()
205206
) {
206207
snap_dir <- snap_dir %||% withr::local_tempdir(.local_envir = .env)

R/test-files.R

Lines changed: 1 addition & 1 deletion
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)),

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.

0 commit comments

Comments
 (0)