Skip to content

Commit f20d526

Browse files
committed
Print testcase at the REPL if verbose is TRUE
1 parent 68bc43e commit f20d526

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

R/testthat-ui.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
#' @param verbose If \code{TRUE}, the contents of the SVG files for
3030
#' the comparison plots are printed during testthat checks. This is
3131
#' useful to investigate errors when testing remotely.
32+
#'
33+
#' Note that it is not possible to print the original SVG during
34+
#' interactive use. This is because there is no way of figuring out
35+
#' in which directory this SVG lives. Consequently, only the test
36+
#' case is printed.
3237
#' @export
3338
#' @examples
3439
#' ver <- gdtools::version_freetype()
@@ -76,6 +81,7 @@ expect_doppelganger <- function(title, fig, path = NULL, ...,
7681
} else {
7782
case <- new_case(case)
7883
maybe_collect_case(case)
84+
maybe_print_svgs(case)
7985
msg <- paste0("Figure not generated yet: ", fig_name, ".svg")
8086
exp <- expectation_new(msg, case)
8187
}
@@ -112,6 +118,14 @@ compare_figs <- function(case) {
112118
exp
113119
}
114120

121+
# Print only if we're not collecting. The testthat reporter prints
122+
# verbose cases at a later point.
123+
maybe_print_svgs <- function(case, pkg_path = NULL) {
124+
if (case$verbose && is_null(active_collecter())) {
125+
meow(svg_files_lines(case, pkg_path))
126+
}
127+
}
128+
115129
expectation_new <- function(msg, case) {
116130
exp <- testthat::expectation("skip", msg)
117131
classes <- c(class(exp), "vdiffr_new")

R/utils.R

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,22 @@ svg_files_lines <- function(case, pkg_path = NULL) {
9595
original_path <- from_test_dir(pkg_path, case$path)
9696
}
9797

98-
chr_lines(
99-
glue(
100-
">> Failed doppelganger: { case$name }
101-
> Original SVG:"
102-
),
103-
readLines(original_path),
104-
"",
98+
# If called from interactive use, we most likely can't figure out
99+
# where the original SVG lives (most likely in a subdirectory within
100+
# the `figs` folder) so we don't print it.
101+
if (file.exists(original_path)) {
102+
original_lines <- chr_lines(
103+
"> Original SVG:",
104+
readLines(original_path),
105+
""
106+
)
107+
} else {
108+
original_lines <- ""
109+
}
110+
111+
lines <- chr_lines(
112+
glue(">> Failed doppelganger: { case$name }"),
113+
original_lines,
105114
"> Testcase SVG:",
106115
readLines(case$testcase),
107116
""

man/expect_doppelganger.Rd

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)