Skip to content

Commit 2270136

Browse files
committed
Snapshot refactoring
* Generalise `test_file_reporter()` so we no longer need `test_files_reporter_parallel()` * Remove code needed for old R * Drop deprecated argument `local_snapshotter()` is [only used in devtools](https://github.com/search?q=org%3Acran+local_snapshotter+&type=code). In preparation for #2066
1 parent 7eb22e8 commit 2270136

File tree

6 files changed

+31
-39
lines changed

6 files changed

+31
-39
lines changed

R/parallel.R

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ test_files_parallel <- function(
6363
)
6464

6565
withr::with_dir(test_dir, {
66-
reporters <- test_files_reporter_parallel(reporter)
66+
reporters <- test_files_reporter(reporter, "parallel")
6767
with_reporter(reporters$multi, {
6868
parallel_updates <- reporter$capabilities$parallel_updates
6969
if (parallel_updates) {
@@ -81,23 +81,6 @@ test_files_parallel <- function(
8181
})
8282
}
8383

84-
test_files_reporter_parallel <- function(reporter, .env = parent.frame()) {
85-
lister <- ListReporter$new()
86-
snapshotter <- MainprocessSnapshotReporter$new("_snaps", fail_on_new = FALSE)
87-
reporters <- list(
88-
find_reporter(reporter),
89-
lister, # track data
90-
snapshotter
91-
)
92-
withr::local_options(
93-
"testthat.snapshotter" = snapshotter,
94-
.local_envir = .env
95-
)
96-
list(
97-
multi = MultiReporter$new(reporters = compact(reporters)),
98-
list = lister
99-
)
100-
}
10184

10285
default_num_cpus <- function() {
10386
# Use common option, if set

R/snapshot-file-snaps.R

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ FileSnaps <- R6::R6Class(
3939
},
4040

4141
append = function(test, variant, data) {
42-
if (!has_name(self$snaps, variant)) {
43-
# Needed for R < 3.6
44-
self$snaps[[variant]] <- list()
45-
}
46-
4742
self$snaps[[variant]][[test]] <- c(self$snaps[[variant]][[test]], data)
4843
length(self$snaps[[variant]][[test]])
4944
},

R/snapshot-reporter.R

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ SnapshotReporter <- R6::R6Class(
130130
return()
131131
}
132132

133-
# If expectation errors or skips, need to reset remaining snapshots
133+
# If expectation errors or skips, need to copy snapshots from old to cur
134134
if (expectation_error(result) || expectation_skip(result)) {
135135
self$cur_snaps$reset(self$test, self$old_snaps)
136136
}
@@ -194,19 +194,16 @@ get_snapshotter <- function() {
194194
#' @export
195195
#' @keywords internal
196196
local_snapshotter <- function(
197+
reporter = SnapshotReporter,
197198
snap_dir = NULL,
198-
cleanup = FALSE,
199199
fail_on_new = FALSE,
200200
.env = parent.frame()
201201
) {
202202
snap_dir <- snap_dir %||% withr::local_tempdir(.local_envir = .env)
203-
reporter <- SnapshotReporter$new(
203+
reporter <- reporter$new(
204204
snap_dir = snap_dir,
205205
fail_on_new = fail_on_new
206206
)
207-
if (!identical(cleanup, FALSE)) {
208-
cli::cli_warn("{.arg cleanup} is deprecated.")
209-
}
210207

211208
withr::local_options(
212209
"testthat.snapshotter" = reporter,

R/test-files.R

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ test_files_serial <- function(
213213
local_testing_env(env)
214214

215215
test_files_setup_state(test_dir, test_package, load_helpers, env)
216-
reporters <- test_files_reporter(reporter)
216+
reporters <- test_files_reporter(reporter, "serial")
217217

218218
with_reporter(
219219
reporters$multi,
@@ -306,15 +306,32 @@ test_files_setup_state <- function(
306306
withr::defer(source_test_teardown(".", env), frame) # old school
307307
}
308308

309-
test_files_reporter <- function(reporter, .env = parent.frame()) {
309+
test_files_reporter <- function(
310+
reporter,
311+
mode = c("serial", "parallel"),
312+
.env = parent.frame()
313+
) {
314+
# User selected reporter
315+
user <- find_reporter(reporter)
316+
317+
# Track all
310318
lister <- ListReporter$new()
311-
reporters <- list(
312-
find_reporter(reporter),
313-
lister, # track data
314-
local_snapshotter("_snaps", fail_on_new = on_ci(), .env = .env)
319+
320+
snap_base <- if (mode == "parallel") {
321+
MainprocessSnapshotReporter
322+
} else {
323+
SnapshotReporter
324+
}
325+
snap <- local_snapshotter(
326+
snap_base,
327+
snap_dir = "_snaps",
328+
fail_on_new = on_ci(),
329+
.env = .env
315330
)
331+
332+
reporters <- compact(list(user, lister, snap))
316333
list(
317-
multi = MultiReporter$new(reporters = compact(reporters)),
334+
multi = MultiReporter$new(reporters = reporters),
318335
list = lister
319336
)
320337
}

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.

tests/testthat/test-snapshot-reporter.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ test_that("can establish local snapshotter for testing", {
99

1010
test_that("basic workflow", {
1111
path <- withr::local_tempdir()
12-
snapper <- local_snapshotter(path, fail_on_new = FALSE)
12+
snapper <- local_snapshotter(snap_dir = path, fail_on_new = FALSE)
1313
snapper$start_file("snapshot-2")
1414
# output if not active (because test not set here)
1515
expect_snapshot_output("x") |>
@@ -98,7 +98,7 @@ test_that("only reverting change in variant deletes .new", {
9898

9999
test_that("removing tests removes snap file", {
100100
path <- withr::local_tempdir()
101-
snapper <- local_snapshotter(path, fail_on_new = FALSE)
101+
snapper <- local_snapshotter(snap_dir = path, fail_on_new = FALSE)
102102
snapper$start_file("snapshot-3", "test")
103103
expect_warning(expect_snapshot_output("x"), "Adding new")
104104
snapper$end_file()

0 commit comments

Comments
 (0)