Skip to content

Commit 32096d8

Browse files
committed
Log vdiffr failures
1 parent 21ce228 commit 32096d8

File tree

3 files changed

+127
-4
lines changed

3 files changed

+127
-4
lines changed

R/testthat-ui.R

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ expect_doppelganger <- function(title,
8080

8181
testthat::local_edition(3)
8282

83+
file <- paste0(fig_name, ".svg")
84+
8385
withCallingHandlers(
8486
testthat::expect_snapshot_file(
8587
testcase,
86-
name = paste0(fig_name, ".svg"),
88+
name = file,
8789
binary = FALSE,
8890
cran = FALSE
8991
),
@@ -94,9 +96,34 @@ expect_doppelganger <- function(title,
9496
"i" = "Please update your snapshots."
9597
))
9698
}
99+
100+
if (!is.null(snapshotter <- get_snapshotter())) {
101+
path_old <- snapshot_path(snapshotter, file)
102+
path_new <- snapshot_path(snapshotter, paste0(fig_name, ".new.svg"))
103+
104+
if (all(file.exists(path_old, path_new))) {
105+
push_log(fig_name, path_old, path_new)
106+
}
107+
}
97108
}
98109
)
110+
}
111+
112+
# From testthat
113+
get_snapshotter <- function() {
114+
x <- getOption("testthat.snapshotter")
115+
if (is.null(x)) {
116+
return()
117+
}
99118

119+
if (!x$is_active()) {
120+
return()
121+
}
122+
123+
x
124+
}
125+
snapshot_path <- function(snapshotter, file) {
126+
file.path(snapshotter$snap_dir, snapshotter$file, file)
100127
}
101128

102129
is_graphics_engine_stale <- function() {
@@ -112,10 +139,13 @@ str_standardise <- function(s, sep = "-") {
112139
}
113140

114141
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)
142+
if (is_null(snapshotter <- get_snapshotter())) {
143+
return(FALSE)
144+
}
118145

146+
file <- paste0(str_standardise(title), ".svg")
147+
path <- snapshot_path(snapshotter, file)
148+
119149
if (!file.exists(path)) {
120150
return(FALSE)
121151
}

R/utils.R

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,65 @@ paste_line <- function(..., trailing = FALSE) {
1212
}
1313
lines
1414
}
15+
16+
push_log <- function(name, old_path, new_path) {
17+
log_path <- Sys.getenv("VDIFFR_LOG_PATH")
18+
19+
# If no envvar is set, check if we are running under R CMD check. In
20+
# that case, always push a log file.
21+
if (!nzchar(log_path)) {
22+
if (!is_checking_remotely()) {
23+
return(invisible(FALSE))
24+
}
25+
log_path <- testthat::test_path("..", "vdiffr.Rout.fail")
26+
}
27+
28+
log_exists <- file.exists(log_path)
29+
30+
file <- file(log_path, "a")
31+
on.exit(close(file))
32+
33+
if (!log_exists) {
34+
cat_line(
35+
file = file,
36+
"Environment:",
37+
vdiffr_info(),
38+
""
39+
)
40+
}
41+
42+
diff_lines <- diff_lines(name, old_path, new_path)
43+
cat_line(file = file, "", !!!diff_lines, "")
44+
}
45+
is_checking_remotely <- function() {
46+
nzchar(Sys.getenv("CI")) || !nzchar(Sys.getenv("NOT_CRAN"))
47+
}
48+
49+
diff_lines <- function(name,
50+
before_path,
51+
after_path) {
52+
before <- readLines(before_path)
53+
after <- readLines(after_path)
54+
55+
diff <- diffobj::diffChr(
56+
before,
57+
after,
58+
format = "raw",
59+
# For reproducibility
60+
disp.width = 80
61+
)
62+
lines <- as.character(diff)
63+
64+
paste_line(
65+
glue("Failed doppelganger: {name} ({before_path})"),
66+
"",
67+
!!!lines
68+
)
69+
}
70+
71+
vdiffr_info <- function() {
72+
glue(
73+
"- vdiffr-svg-engine: { SVG_ENGINE_VER }
74+
- vdiffr: { utils::packageVersion('vdiffr') }"
75+
)
76+
}

tests/testthat/test-log.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
vdiffr_skip_stale()
2+
3+
test_that("Failures are logged", {
4+
reporter <- testthat::SilentReporter$new()
5+
6+
path <- create_mock_pkg()
7+
vars <- c("CI" = "true")
8+
suppressMessages(withr::with_envvar(vars,
9+
testthat::test_dir(
10+
file.path(path, "tests", "testthat"),
11+
reporter = reporter,
12+
stop_on_failure = FALSE,
13+
stop_on_warning = FALSE
14+
)
15+
))
16+
17+
log_path <- file.path(path, "tests", "vdiffr.Rout.fail")
18+
if (!file.exists(log_path)) {
19+
abort("Can't find log.")
20+
}
21+
22+
on.exit(file.remove(log_path))
23+
24+
log <- readLines(log_path)
25+
26+
results <- reporter$expectations()
27+
n_expected <- sum(purrr::map_lgl(results, inherits, "expectation_failure"))
28+
29+
n_logged <- length(grep("Failed doppelganger:", log))
30+
expect_equal(n_logged, n_expected)
31+
})

0 commit comments

Comments
 (0)