1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#' Snapshot management
#'
#' * `snapshot_accept()` accepts all modified snapshots.
#' * `snapshot_review()` opens a Shiny app that shows a visual diff of each
#' modified snapshot. This is particularly useful for whole file snapshots
#' created by `expect_snapshot_file()`.
#'
#' @param files Optionally, filter effects to snapshots from specified files.
#' This can be a snapshot name (e.g. `foo` or `foo.md`), a snapshot file name
#' (e.g. `testfile/foo.txt`), or a snapshot file directory (e.g. `testfile/`).
#'
#' @param path Path to tests.
#' @export
snapshot_accept <- function(files = NULL, path = "tests/testthat") {
changed <- snapshot_meta(files, path)
if (nrow(changed) == 0) {
inform("No snapshots to update")
return(invisible())
}
inform(c("Updating snapshots:", changed$name))
unlink(changed$cur)
file.rename(changed$new, changed$cur)
rstudio_tickle()
invisible()
}
#' @rdname snapshot_accept
#' @export
snapshot_review <- function(files = NULL, path = "tests/testthat") {
check_installed(c("shiny", "diffviewer"), "to use snapshot_review()")
changed <- snapshot_meta(files, path)
if (nrow(changed) == 0) {
inform("No snapshots to update")
return(invisible())
}
review_app(changed$name, changed$cur, changed$new)
rstudio_tickle()
invisible()
}
review_app <- function(name, old_path, new_path) {
stopifnot(
length(name) == length(old_path),
length(old_path) == length(new_path)
)
n <- length(name)
case_index <- stats::setNames(seq_along(name), name)
handled <- rep(FALSE, n)
ui <- shiny::fluidPage(style = "margin: 0.5em",
shiny::fluidRow(style = "display: flex",
shiny::div(style = "flex: 1 1",
shiny::selectInput("cases", NULL, case_index, width = "100%")
),
shiny::div(class = "btn-group", style = "margin-left: 1em; flex: 0 0 auto",
shiny::actionButton("skip", "Skip"),
shiny::actionButton("accept", "Accept", class = "btn-success"),
)
),
shiny::fluidRow(
diffviewer::visual_diff_output("diff")
)
)
server <- function(input, output, session) {
i <- shiny::reactive(as.numeric(input$cases))
output$diff <- diffviewer::visual_diff_render({
diffviewer::visual_diff(old_path[[i()]], new_path[[i()]])
})
# Handle buttons - after clicking update move input$cases to next case,
# and remove current case (for accept/reject). If no cases left, close app
shiny::observeEvent(input$reject, {
inform(paste0("Rejecting snapshot: '", new_path[[i()]], "'"))
unlink(new_path[[i()]])
update_cases()
})
shiny::observeEvent(input$accept, {
inform(paste0("Accepting snapshot: '", old_path[[i()]], "'"))
file.rename(new_path[[i()]], old_path[[i()]])
update_cases()
})
shiny::observeEvent(input$skip, {
i <- next_case()
shiny::updateSelectInput(session, "cases", selected = i)
})
update_cases <- function() {
handled[[i()]] <<- TRUE
i <- next_case()
shiny::updateSelectInput(session, "cases",
choices = case_index[!handled],
selected = i
)
}
next_case <- function() {
if (all(handled)) {
inform("Review complete")
shiny::stopApp()
return()
}
# Find next case;
remaining <- case_index[!handled]
next_cases <- which(remaining > i())
if (length(next_cases) == 0) remaining[[1]] else remaining[[next_cases[[1]]]]
}
}
inform(c(
"Starting Shiny app for snapshot review",
i = "Use Ctrl + C to quit"
))
shiny::runApp(
shiny::shinyApp(ui, server),
quiet = TRUE,
launch.browser = shiny::paneViewer()
)
invisible()
}
# helpers -----------------------------------------------------------------
snapshot_meta <- function(files = NULL, path = "tests/testthat") {
all <- dir(file.path(path, "_snaps"), recursive = TRUE, full.names = TRUE)
cur <- all[!grepl("\\.new\\.", all)]
snap_file <- basename(dirname(cur)) != "_snaps"
snap_test <- ifelse(snap_file, basename(dirname(cur)), gsub("\\.md$", "", basename(cur)))
if (length(cur) == 0) {
new <- character()
} else {
new <- paste0(tools::file_path_sans_ext(cur), ".new.", tools::file_ext(cur))
new[!file.exists(new)] <- NA
}
snap_name <- ifelse(snap_file,
file.path(snap_test, basename(cur)),
basename(cur)
)
out <- data.frame(
test = snap_test,
name = snap_name,
cur = cur,
new = new,
stringsAsFactors = FALSE
)
out <- out[!is.na(out$new), , drop = FALSE]
out <- out[order(out$test, out$cur), , drop = FALSE]
rownames(out) <- NULL
if (!is.null(files)) {
is_dir <- substr(files, nchar(files), nchar(files)) == "/"
dirs <- files[is_dir]
files <- files[!is_dir]
dirs <- substr(dirs, 1, nchar(dirs) - 1)
files <- ifelse(tools::file_ext(files) == "", paste0(files, ".md"), files)
out <- out[out$name %in% files | out$test %in% dirs, , drop = FALSE]
}
out
}
|