Skip to content

Commit

Permalink
fix regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxheld83 committed Oct 28, 2024
1 parent fb2c7e7 commit 892b1c5
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 35 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export(dir_ls_snaps)
export(examples_app)
export(get_screenshot_args_attr)
export(get_screenshot_from_app)
export(glue_regexp_screenshot_files)
export(mixed_react_tree_app)
export(mixed_react_tree_server)
export(mixed_react_tree_ui)
Expand Down
61 changes: 38 additions & 23 deletions R/shiny2screenshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ get_screenshot_from_app_strictly <- function(appDir,
#' You can deposit several screenshots of a shiny app using
#' [shinytest2::AppDriver] in testing.
#' Use this function to identify all the resulting images.
#' Typically used for *consecutive* screenshots.
#' @param test_file
#' Name of the test file, in which the snapshots are generated,
#' *without*:
Expand All @@ -167,43 +168,57 @@ get_screenshot_from_app_strictly <- function(appDir,
#' which you are currently testing.
#' @inheritParams shinytest2::AppDriver
#' @inheritParams testthat::expect_snapshot_file
#' @param strictly_numbered
#' If `TRUE`, filter for snapshot files numbered by [shinytest2::AppDriver].
#' If you pass a `name` only to `shinytest2::AppDriver$new()` (recommended),
#' and then invoke several `shinytest2::AppDriver$expect_snapshot()`,
#' they resulting snapshots will all have the same name,
#' appended by a counter from `000` to `999`.
#' If `FALSE`, any filename `{name}*.png` will be selected.
#' You may need to set `FALSE`
#' if you pass a name to`shinytest2::AppDriver$expect_snapshot()`
#' directly.
#' @inheritParams fs::dir_ls
#' @family documentation
#' @export
dir_ls_snaps <- function(test_file = character(),
name = NULL,
variant = shinytest2::platform_variant(),
strictly_numbered = TRUE) {
regexp = glue_regexp_screenshot_files(),
variant = shinytest2::platform_variant()) {
checkmate::assert_string(test_file)
checkmate::assert_string(name)
checkmate::assert_flag(strictly_numbered)
test_path <- testthat::test_path(
"_snaps",
variant,
test_file
)
# shinytest2 docs have `NULL` as default, but glue does not like `NULL`s
if (strictly_numbered) {
regexp <- glue::glue("^.*[\\\\/]{name}-\\d{{3}}\\.png$")
} else {
regexp <- glue::glue("^.*[\\\\/]{name}.*\\.png$")
}
if (is.null(name)) name <- character()
fs::dir_ls(
test_path,
all = FALSE,
recurse = FALSE,
type = "file",
# shinytest2 only defaults to png
regexp = regexp
)
}

#' Build the regular expression to match consecutive screenshots
#'
#' [shinytest2::AppDriver] uses several schemes to
#' name consecutive screenshot files.
#' Use this regex to capture paths of screenshots.
#' @param name
#' The `name` passed to [shinytest2::AppDriver] to be used for screenshots.
#' Can be `NULL`, for no filtering by name.
#' @param auto_numbered
#' If `TRUE`, filter for snapshot files automatically numbered
#' according to the scheme used by [shinytest2::AppDriver].
#' If you pass a `name` only to `shinytest2::AppDriver$new()` (recommended),
#' and then invoke several `shinytest2::AppDriver$expect_snapshot()`,
#' they resulting snapshots will all have the same name,
#' appended by a counter from `000` to `999`.
#' If `FALSE`, any filename `{name}*.png` will be selected.
#' You may need to set `FALSE`
#' if you pass a name to`shinytest2::AppDriver$expect_snapshot()`
#' directly.
#' @family documentation
#' @export
glue_regexp_screenshot_files <- function(name = NULL, auto_numbered = TRUE) {
checkmate::assert_string(name, null.ok = TRUE)
checkmate::assert_flag(auto_numbered)
glue::glue(
"^.*[\\\\/]", # path
if (is.null(name)) "" else "{name}",
if (!is.null(name) && auto_numbered) "-" else "",
if (auto_numbered) "\\d{{3}}" else ".*",
# shinytest2 only defaults to png
"\\.png$"
)
}
20 changes: 17 additions & 3 deletions tests/testthat/_snaps/linux/shiny2screenshot.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# dir_ls_snaps: finds multiple, named screenshots
# dir_ls_snaps: finds manually numbered, named screenshots

Code
snaps
Expand All @@ -10,10 +10,24 @@
_snaps/linux/helpers/bins-28.png _snaps/linux/helpers/bins-29.png
_snaps/linux/helpers/bins-30.png

# dir_ls_snaps: finds single, named screenshots
# dir_ls_snaps: finds automatically numbered, named screenshots

Code
snaps
Output
_snaps/linux/helpers/mpg-001.png
_snaps/linux/helpers/mpg-001.png _snaps/linux/helpers/mpg-002.png

# dir_ls_snaps: finds automatically numbered, unnamed screenshots

Code
snaps
Output
_snaps/linux/helpers/001.png _snaps/linux/helpers/002.png

# dir_ls_snaps: finds non-numbered, named screenshots

Code
snaps
Output
_snaps/linux/helpers/foo.png

20 changes: 17 additions & 3 deletions tests/testthat/_snaps/mac/shiny2screenshot.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# dir_ls_snaps: finds multiple, named screenshots
# dir_ls_snaps: finds manually numbered, named screenshots

Code
snaps
Expand All @@ -10,10 +10,24 @@
_snaps/mac/helpers/bins-28.png _snaps/mac/helpers/bins-29.png
_snaps/mac/helpers/bins-30.png

# dir_ls_snaps: finds single, named screenshots
# dir_ls_snaps: finds automatically numbered, named screenshots

Code
snaps
Output
_snaps/mac/helpers/mpg-001.png
_snaps/mac/helpers/mpg-001.png _snaps/mac/helpers/mpg-002.png

# dir_ls_snaps: finds automatically numbered, unnamed screenshots

Code
snaps
Output
_snaps/mac/helpers/001.png _snaps/mac/helpers/002.png

# dir_ls_snaps: finds non-numbered, named screenshots

Code
snaps
Output
_snaps/mac/helpers/foo.png

36 changes: 30 additions & 6 deletions tests/testthat/test-shiny2screenshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,43 @@ test_that("screenshots fail according to `strict` setting", {

describe("dir_ls_snaps", {
variant <- shinytest2::platform_variant(r_version = FALSE)
it("finds multiple, named screenshots", {
it("finds manually numbered, named screenshots", {
snaps <- dir_ls_snaps(
test_file = "helpers",
name = "bins",
variant = variant,
strictly_numbered = FALSE
regexp = glue_regexp_screenshot_files(
name = "bins",
auto_numbered = FALSE
),
variant = variant
)
expect_snapshot(snaps, variant = variant)
})
it("finds automatically numbered, named screenshots", {
snaps <- dir_ls_snaps(
test_file = "helpers",
regexp = glue_regexp_screenshot_files(
name = "mpg",
auto_numbered = FALSE
),
variant = variant
)
expect_snapshot(snaps, variant = variant)
})
it("finds automatically numbered, unnamed screenshots", {
snaps <- dir_ls_snaps(
test_file = "helpers",
regexp = glue_regexp_screenshot_files(),
variant = variant
)
expect_snapshot(snaps, variant = variant)
})
it("finds single, named screenshots", {
it("finds non-numbered, named screenshots", {
snaps <- dir_ls_snaps(
test_file = "helpers",
name = "mpg",
regexp = glue_regexp_screenshot_files(
name = "foo",
auto_numbered = FALSE
),
variant = variant
)
expect_snapshot(snaps, variant = variant)
Expand Down

0 comments on commit 892b1c5

Please sign in to comment.