Skip to content

Commit 8cfa2e2

Browse files
committed
Update view_images_apps() to work with _snaps folder
1 parent 822964d commit 8cfa2e2

File tree

1 file changed

+55
-35
lines changed

1 file changed

+55
-35
lines changed

R/view-test-images.R

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,46 @@
33
#' @param repo_dir directory to the shinycoreci repo
44
#' @export
55
view_test_images <- function(repo_dir = ".") {
6-
repo_dir <- normalizePath(repo_dir, mustWork = TRUE)
7-
apps_folder <- file.path(repo_dir, "inst/apps")
6+
app_folders <- Filter(repo_apps_paths(repo_dir), f = function(app_folder) {
7+
dir.exists(file.path(app_folder, "tests/testthat/_snaps"))
8+
})
89

9-
all_files <- dir(apps_folder, recursive = TRUE, full.names = TRUE)
10+
png_dt <-
11+
dplyr::bind_rows(lapply(app_folders, function(app_folder) {
12+
app_files <- dir(app_folder, recursive = TRUE, full.names = TRUE)
1013

11-
# Only keep snapshot files
12-
all_files <- all_files[grepl("tests/testthat/_snaps/", all_files)]
13-
# Only png files
14-
# No debug snapshots
15-
all_pngs <- all_files[grepl("[^_]\\.png", all_files)]
16-
# Not new png files
17-
all_pngs <- all_pngs[!grepl("\\.new\\.png", all_pngs)]
14+
# Only png files
15+
app_pngs <- app_files[grepl("\\.png$", app_files)]
16+
# No debug snapshots
17+
# Not new png files
18+
app_pngs <- app_pngs[!grepl("(_|\\.new)\\.png$", app_pngs)]
1819

19-
m <- regexec(file.path(apps_folder, "(.*)", "tests"), all_pngs)
20-
all_png_app_names <- vapply(regmatches(all_pngs, m), function(x) x[2], character(1))
20+
test_name = dirname(app_pngs)
21+
variant = dirname(test_name)
22+
23+
dplyr::tibble(
24+
app_name = basename(app_folder),
25+
variant = basename(variant),
26+
png_name = basename(app_pngs),
27+
test_name = basename(test_name),
28+
path = app_pngs
29+
)
30+
}))
2131

2232
ui <- shiny::fluidPage(
2333
shiny::wellPanel(
24-
shiny::selectInput("app_name", "Choose a testing app", unique(all_png_app_names))
34+
shiny::selectInput("app_name", "Choose a testing app", unique(png_dt$app_name))
2535
),
2636
shiny::uiOutput("images")
2737
)
2838

2939
server <- function(input, output, session) {
3040
app_png_idx <- shiny::reactive({
3141
shiny::req(input$app_name)
32-
grep(input$app_name, all_pngs)
42+
input$app_name == png_dt$app_name
3343
})
34-
app_pngs <- shiny::reactive(all_pngs[app_png_idx()])
44+
app_png_info <- shiny::reactive(png_dt[app_png_idx(), ])
45+
app_pngs <- shiny::reactive(app_png_info()$path)
3546

3647
shiny::observe({
3748
lapply(app_pngs(), function(x) {
@@ -41,27 +52,36 @@ view_test_images <- function(repo_dir = ".") {
4152
})
4253
})
4354
output$images <- shiny::renderUI({
44-
png_names <- app_pngs()
45-
row_names <- unique(basename(png_names))
46-
row_tags <- lapply(row_names, function(row) {
47-
row_pngs <- png_names[basename(png_names) %in% row]
48-
row_images <- lapply(row_pngs, function(png) {
49-
test_name <- basename(dirname(png))
50-
test_name <- sub("-expected", "", test_name)
51-
img_tag <- shiny::div(
52-
shiny::tags$p(test_name),
53-
shiny::imageOutput(png, height = "auto")
54-
)
55-
shiny::column(
56-
max(3, round(12 / length(row_pngs))),
57-
img_tag
55+
app_png_dt <- app_png_info()
56+
test_names <- unique(app_png_dt[, c("png_name", "test_name")])
57+
images <- Map(
58+
test_names$png_name,
59+
test_names$test_name,
60+
f = function(png_name_, test_name_) {
61+
test_dt <- dplyr::filter(app_png_dt, png_name == png_name_, test_name == test_name_)
62+
# row_pngs <- png_names[basename(png_names) %in% row]
63+
row_images <-
64+
Map(
65+
test_dt$variant,
66+
test_dt$path,
67+
f = function(variant, png_path) {
68+
shiny::column(
69+
max(3, round(12 / nrow(test_dt))),
70+
shiny::div(
71+
shiny::tags$p(paste0(variant, "/", test_name_)),
72+
shiny::imageOutput(png_path, height = "auto")
73+
)
74+
)
75+
}
76+
)
77+
row_images <- unname(row_images)
78+
shiny::wellPanel(
79+
shiny::h3(paste("Screenshot", png_name_)),
80+
shiny::br(), shiny::fluidRow(!!!row_images)
5881
)
59-
})
60-
shiny::wellPanel(
61-
shiny::h3(paste("Screenshot", english::english(match(row, row_names)))),
62-
shiny::br(), shiny::fluidRow(!!!row_images)
63-
)
64-
})
82+
}
83+
)
84+
unname(images)
6585
})
6686
}
6787

0 commit comments

Comments
 (0)