Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Imports:
shiny,
withr
Suggests:
diffobj,
globals (>= 0.14.0),
plotly,
rstudioapi,
Expand Down
81 changes: 75 additions & 6 deletions R/expect-snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,80 @@ app__expect_snapshot_file <- function(
# Make it path safe so others are not created or accessed
name_safe <- fs::path_sanitize(name, "_")

testthat::expect_snapshot_file(
file,
name = name_safe,
cran = cran,
compare = compare,
variant = self$get_variant()
withCallingHandlers(
testthat::expect_snapshot_file(
file,
name = name_safe,
cran = cran,
compare = compare,
variant = self$get_variant()
),
expectation_failure = function(cnd) {
if (
# Only display text if allowed to be verbose
is_false(getOption("shinytest2.expectation_failure.quiet", FALSE)) &&
# Require `diffobj` to be installed
rlang::is_installed("diffobj") &&
# Only display text if compare fn is for text diffs
identical(compare, testthat::compare_file_text)
) {
# In vdiffr, check for snapshot version

# Display diffs similar to vdiffr
# https://github.com/r-lib/vdiffr/blob/fc03e91cccac04baa875063513b630d80c02e197/R/expect-doppelganger.R#L127-L169
snapshotter <- get_snapshotter()
if (!is.null(snapshotter)) {
path_old <- snapshot_path(snapshotter, name)
path_new <- fs::path_ext_set(path_old, paste0(".new.", fs::path_ext(path_old)))
if (all(file.exists(path_old, path_new))) {
diff <- diff_lines(path_old, path_new)
msg <- paste0(
"Diff in snapshot file `", snapshotter$file, name, "`\n",
paste0(diff, collapse = "\n")
)
# Defer the expectation to be thrown after re-throwing the original condition
# This prints the failure first, then a warning about the text diff
withr::defer({
testthat::exp_signal(
testthat::new_expectation("warning", msg)
)
})
}
}
}

# Re-throw original condition
stop(cnd)
}
)
}

get_snapshotter <- function() {
x <- getOption("testthat.snapshotter")
if (is.null(x)) {
return()
}

if (!x$is_active()) {
return()
}

x
}
snapshot_path <- function(snapshotter, file) {
file.path(snapshotter$snap_dir, snapshotter$file, file)
}

diff_lines <- function(before_path, after_path) {
before <- readLines(before_path)
after <- readLines(after_path)

diff <- diffobj::diffChr(
before,
after,
format = "raw",
# For reproducibility
disp.width = 80
)
diff
}
2 changes: 1 addition & 1 deletion tests/testthat/apps/hello/tests/testthat/test-app.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ test_that("basic website example works using testthat", {
# Take picture and record inputs / outputs
tmpfile <- tempfile()
app$get_screenshot(tmpfile)
expect_snapshot_file(tmpfile, name = "manual-screenshot.png")
expect_snapshot_file(tmpfile, name = "manual-screenshot.png", variant = app$get_variant())

values <- app$get_values()
expect_equal(values$output$greeting, "Hello Hadley!")
Expand Down