Skip to content

Commit 6a378f4

Browse files
authored
Announce snapshot file before evaluation (#109)
Closes r-lib/testthat#1393
1 parent e56fb50 commit 6a378f4

File tree

7 files changed

+169
-6
lines changed

7 files changed

+169
-6
lines changed

DESCRIPTION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Imports:
3030
htmltools,
3131
lifecycle,
3232
rlang,
33-
testthat (>= 3.0.0),
33+
testthat (>= 3.0.2.9001),
3434
xml2 (>= 1.0.0)
3535
Suggests:
3636
covr,
@@ -39,6 +39,8 @@ Suggests:
3939
withr
4040
LinkingTo:
4141
cpp11
42+
Remotes:
43+
r-lib/testthat
4244
RoxygenNote: 7.1.1.9001
4345
Roxygen: list(markdown = TRUE)
4446
URL: https://vdiffr.r-lib.org/, https://github.com/r-lib/vdiffr

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# vdiffr (development version)
22

3+
* testthat no longer auto-deletes snapshot files that were not
4+
generated because of an unexpected error or a `skip()`
5+
(r-lib/testthat#1393).
6+
7+
38
# vdiffr 1.0.0
49

510
This release includes two major changes:

R/expect-doppelganger.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,16 @@ expect_doppelganger <- function(title,
9191
...,
9292
writer = write_svg,
9393
cran = FALSE) {
94+
testthat::local_edition(3)
95+
9496
fig_name <- str_standardise(title)
97+
file <- paste0(fig_name, ".svg")
98+
99+
# Announce snapshot file before touching `fig` in case evaluation
100+
# causes an error. This allows testthat to restore the files
101+
# (see r-lib/testthat#1393).
102+
testthat::announce_snapshot_file(name = file)
103+
95104
testcase <- make_testcase_file(fig_name)
96105
writer(fig, testcase, title)
97106

@@ -115,10 +124,6 @@ expect_doppelganger <- function(title,
115124
))
116125
}
117126

118-
testthat::local_edition(3)
119-
120-
file <- paste0(fig_name, ".svg")
121-
122127
withCallingHandlers(
123128
testthat::expect_snapshot_file(
124129
testcase,
@@ -134,7 +139,7 @@ expect_doppelganger <- function(title,
134139
))
135140
}
136141

137-
if (!is.null(snapshotter <- get_snapshotter())) {
142+
if (!is_null(snapshotter <- get_snapshotter())) {
138143
path_old <- snapshot_path(snapshotter, file)
139144
path_new <- snapshot_path(snapshotter, paste0(fig_name, ".new.svg"))
140145

tests/testthat/test-expect-doppelganger.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,21 @@ test_that("supports `grob` objects (#36)", {
8989
)
9090
expect_doppelganger("grob", circle)
9191
})
92+
93+
test_that("skips and unexpected errors reset snapshots (r-lib/testthat#1393)", {
94+
regenerate <- FALSE
95+
96+
if (regenerate) {
97+
withr::local_envvar(c(VDIFFR_REGENERATE_SNAPS = "true"))
98+
}
99+
100+
suppressMessages(
101+
test_file(
102+
test_path("test-snapshot", "test-snapshot.R"),
103+
reporter = NULL
104+
)
105+
)
106+
107+
expect_true(file.exists("test-snapshot/_snaps/snapshot/error-resets-snapshots.svg"))
108+
expect_true(file.exists("test-snapshot/_snaps/snapshot/skip-resets-snapshots.svg"))
109+
})
Lines changed: 59 additions & 0 deletions
Loading
Lines changed: 59 additions & 0 deletions
Loading
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
test_that("errors reset snapshots", {
2+
if (nzchar(Sys.getenv("VDIFFR_REGENERATE_SNAPS"))) {
3+
expect_doppelganger("error resets snapshots", function() plot(1:3))
4+
} else {
5+
expect_doppelganger("error resets snapshots", function() stop("failing"))
6+
}
7+
})
8+
9+
test_that("skips reset snapshots", {
10+
if (nzchar(Sys.getenv("VDIFFR_REGENERATE_SNAPS"))) {
11+
expect_doppelganger("skip resets snapshots", function() plot(1:3))
12+
} else {
13+
expect_doppelganger("skip resets snapshots", function() skip("skipping"))
14+
}
15+
})

0 commit comments

Comments
 (0)