Skip to content

Commit

Permalink
merge screenshots
Browse files Browse the repository at this point in the history
  • Loading branch information
maxheld83 committed Nov 3, 2024
1 parent 89b10b1 commit f28cdae
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 9 deletions.
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export(examples_app)
export(get_screenshot_args_attr)
export(get_screenshot_from_app)
export(glue_regexp_screenshot_files)
export(map_snaps_animate)
export(image_animate_snaps)
export(image_write_snaps)
export(mixed_react_tree_app)
export(mixed_react_tree_server)
export(mixed_react_tree_ui)
Expand Down
25 changes: 19 additions & 6 deletions R/shiny2screenshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,22 +236,35 @@ glue_regexp_screenshot_files <- function(name = NULL, auto_numbered = TRUE) {
}

#' @describeIn get_shinytest_screenshots
#' Return screenshot.
#' If several, merge into a gif first.
#' @return Path to image.
#' Read in screenshot.
#' If several, animate into a gif.
#' @param snaps
#' Vector of file names, as returned by [dir_ls_snaps()]
#' @inheritParams magick::image_animate
#' @inheritDotParams magick::image_animate
#' @return A `magick-image`.
#' @export
map_snaps_animate <- function(snaps = fs::path()) {
image_animate_snaps <- function(snaps = fs::path(), fps = 5,...) {

Check warning on line 247 in R/shiny2screenshot.R

View workflow job for this annotation

GitHub Actions / Test, Check, Lint and Document Package (rlint)

file=R/shiny2screenshot.R,line=247,col=61,[commas_linter] Commas should always have a space after.
if (any(!fs::file_exists(snaps))) rlang::abort("File could not be found.")
names(snaps) <- fs::path_file(snaps)
check_installed_magick()
res <- magick::image_read(snaps)
if (length(snaps) == 1) {
return(snaps)
res
} else {
check_installed_magick()
magick::image_animate(res, fps = fps, ...)
}
}

#' @describeIn get_shinytest_screenshots
#' Write out (merged) screenshots to new path.
#' @inheritParams magick::image_write
#' @return Path to the (merged) screenshots.
#' @export
image_write_snaps <- function(image, path = withr::local_tempfile()) {
magick::image_write(image = image, path = path)
}

check_installed_magick <- function() {
rlang::check_installed(
"magick",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/_snaps/linux/shiny2screenshot/single.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/_snaps/mac/shiny2screenshot/single.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 19 additions & 2 deletions tests/testthat/test-shiny2screenshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe("map_snaps_animate", {
expect_error(map_snaps_animate("i-do-not-exist"))
expect_error(map_snaps_animate(c("i-do-not-exist", "me-neither")))
})
it("reads in single file", {
it("reads in single screenshot", {
snaps <- dir_ls_snaps(
test_file = "helpers",
regexp = glue_regexp_screenshot_files(
Expand All @@ -126,10 +126,27 @@ describe("map_snaps_animate", {
),
variant = variant
)
path <- withr::local_tempfile()
testthat::expect_snapshot_file(
path = map_snaps_animate(snaps),
path = image_animate_snaps(snaps) |> image_write_snaps(path = path),
name = "single.png",
variant = variant
)
})
it("reads in multiple screenshots", {
snaps <- dir_ls_snaps(
test_file = "helpers",
regexp = glue_regexp_screenshot_files(
name = "bins",
auto_numbered = FALSE
),
variant = variant
)
path <- withr::local_tempfile()
testthat::expect_snapshot_file(
path = image_animate_snaps(snaps) |> image_write_snaps(path = path),
name = "multiple.gif",
variant = variant
)
})
})

0 comments on commit f28cdae

Please sign in to comment.