Skip to content

Commit 21ce228

Browse files
committed
Skip stale snapshots
1 parent b600896 commit 21ce228

File tree

4 files changed

+106
-16
lines changed

4 files changed

+106
-16
lines changed

R/testthat-ui.R

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,23 @@ expect_doppelganger <- function(title,
8080

8181
testthat::local_edition(3)
8282

83-
testthat::expect_snapshot_file(
84-
testcase,
85-
name = paste0(fig_name, ".svg"),
86-
binary = FALSE,
87-
cran = FALSE
83+
withCallingHandlers(
84+
testthat::expect_snapshot_file(
85+
testcase,
86+
name = paste0(fig_name, ".svg"),
87+
binary = FALSE,
88+
cran = FALSE
89+
),
90+
expectation_failure = function(cnd) {
91+
if (is_snapshot_stale(title, testcase)) {
92+
testthat::skip(paste_line(
93+
"SVG snapshot generated under a different vdiffr version.",
94+
"i" = "Please update your snapshots."
95+
))
96+
}
97+
}
8898
)
99+
89100
}
90101

91102
is_graphics_engine_stale <- function() {
@@ -99,3 +110,33 @@ str_standardise <- function(s, sep = "-") {
99110
s <- gsub(paste0("^", sep, "|", sep, "$"), "", s)
100111
s
101112
}
113+
114+
is_snapshot_stale <- function(title, testcase) {
115+
ctxt <- testthat::get_reporter()$.context
116+
file <- paste0(str_standardise(title), ".svg")
117+
path <- testthat::test_path("_snaps", ctxt, file)
118+
119+
if (!file.exists(path)) {
120+
return(FALSE)
121+
}
122+
123+
lines <- readLines(path)
124+
125+
match <- regexec(
126+
"data-engine-version='([0-9.]+)'",
127+
lines
128+
)
129+
match <- Filter(length, regmatches(lines, match))
130+
131+
# Old vdiffr snapshot that doesn't embed a version
132+
if (!length(match)) {
133+
return(TRUE)
134+
}
135+
136+
if (length(match) > 1) {
137+
abort("Found multiple vdiffr engine versions in SVG snapshot.")
138+
}
139+
140+
snapshot_version <- match[[1]][[2]]
141+
svg_engine_ver() != snapshot_version
142+
}
Lines changed: 35 additions & 0 deletions
Loading

tests/testthat/helper-vdiffr.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
regenerate_snapshots <- function() {
2+
FALSE
3+
}

tests/testthat/test-expectations.R

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,27 @@ test_that("grid doppelgangers pass", {
2626
expect_doppelganger("Grid doppelganger", p_grid)
2727
})
2828

29-
skip("TODO")
30-
31-
test_that("skip mismatches if vdiffr is stale", {
32-
withr::local_envvar(c(NOT_CRAN = "true"))
33-
mock_dir <- create_mock_pkg("mock-pkg-skip-stale")
34-
35-
mock_test_dir <- file.path(mock_dir, "tests", "testthat")
36-
test_results <- testthat::test_dir(mock_test_dir, reporter = "silent")
37-
result <- test_results[[1]]$results[[1]]
29+
test_that("stale snapshots are skipped", {
30+
plot <- ggplot2::ggplot() + ggplot2::labs()
31+
title <- "stale snapshots are skipped"
32+
new_path <- test_path("_snaps", "expectations", "stale-snapshots-are-skipped.new.svg")
33+
34+
if (regenerate_snapshots()) {
35+
expect_doppelganger(title, plot)
36+
37+
# Update engine field to a stale version
38+
file <- new_path
39+
if (!file.exists(file)) {
40+
file <- test_path("_snaps", "expectations", "stale-snapshots-are-skipped.svg")
41+
}
42+
lines <- readLines(file)
43+
lines <- sub("data-engine-version='[0-9.]+'", "data-engine-version='0.1'", lines)
44+
writeLines(lines, file)
45+
46+
return()
47+
}
3848

39-
expect_is(result, "expectation_skip")
40-
expect_match(result$message, "The vdiffr engine is too old")
49+
cnd <- catch_cnd(expect_doppelganger(title, plot))
50+
expect_s3_class(cnd, "skip")
51+
file.remove(new_path)
4152
})

0 commit comments

Comments
 (0)