From eebf5333c4914e682cd3769abfa9fb90c3c3067a Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Thu, 7 Aug 2025 13:59:07 -0400 Subject: [PATCH] Implement `snapshot_reject()` Fixes #1923 --- NAMESPACE | 1 + NEWS.md | 1 + R/snapshot-manage.R | 17 +++++++++++++++++ man/snapshot_accept.Rd | 4 ++++ tests/testthat/_snaps/snapshot-manage.md | 9 +++++++++ tests/testthat/test-snapshot-manage.R | 7 +++++++ 6 files changed, 39 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index ee6dcd8be..14db449be 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -192,6 +192,7 @@ export(skip_on_os) export(skip_on_travis) export(skip_unless_r) export(snapshot_accept) +export(snapshot_reject) export(snapshot_review) export(source_dir) export(source_file) diff --git a/NEWS.md b/NEWS.md index dd2080f0e..6f56bac41 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # testthat (development version) +* New `snapshot_reject()` rejects all modified snapshots by deleting the `.new` variants (#1923). * New `SlowReporter` makes it easier to find the slowest tests in your package. The easiest way to run it is with `devtools::test(reporter = "slow")` (#1466). * Power `expect_mapequal()` with `waldo::compare(list_as_map = TRUE)` (#1521). * On CRAN, `test_that()` now automatically skips if a package is not installed (#1585). Practically, this means that you no longer need to check that suggested packages are installed. (We don't do this in the tidyverse because we think it has limited payoff, but other styles advise differently.) diff --git a/R/snapshot-manage.R b/R/snapshot-manage.R index a7949e9ae..c843f9046 100644 --- a/R/snapshot-manage.R +++ b/R/snapshot-manage.R @@ -1,6 +1,7 @@ #' Accept or reject modified snapshots #' #' * `snapshot_accept()` accepts all modified snapshots. +#' * `snapshot_reject()` rejects all modified snapshots by deleting the `.new` variants. #' * `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()`. @@ -26,6 +27,22 @@ snapshot_accept <- function(files = NULL, path = "tests/testthat") { invisible() } +#' @rdname snapshot_accept +#' @export +snapshot_reject <- function(files = NULL, path = "tests/testthat") { + changed <- snapshot_meta(files, path) + if (nrow(changed) == 0) { + inform("No snapshots to reject") + return(invisible()) + } + + inform(c("Rejecting snapshots:", changed$name)) + unlink(changed$new) + + rstudio_tickle() + invisible() +} + #' @rdname snapshot_accept #' @export snapshot_review <- function(files = NULL, path = "tests/testthat") { diff --git a/man/snapshot_accept.Rd b/man/snapshot_accept.Rd index 0e4e6fbc3..64b65eb24 100644 --- a/man/snapshot_accept.Rd +++ b/man/snapshot_accept.Rd @@ -2,11 +2,14 @@ % Please edit documentation in R/snapshot-manage.R \name{snapshot_accept} \alias{snapshot_accept} +\alias{snapshot_reject} \alias{snapshot_review} \title{Accept or reject modified snapshots} \usage{ snapshot_accept(files = NULL, path = "tests/testthat") +snapshot_reject(files = NULL, path = "tests/testthat") + snapshot_review(files = NULL, path = "tests/testthat") } \arguments{ @@ -19,6 +22,7 @@ This can be a snapshot name (e.g. \code{foo} or \code{foo.md}), a snapshot file \description{ \itemize{ \item \code{snapshot_accept()} accepts all modified snapshots. +\item \code{snapshot_reject()} rejects all modified snapshots by deleting the \code{.new} variants. \item \code{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 \code{expect_snapshot_file()}. diff --git a/tests/testthat/_snaps/snapshot-manage.md b/tests/testthat/_snaps/snapshot-manage.md index 1a76f78af..d6dad619d 100644 --- a/tests/testthat/_snaps/snapshot-manage.md +++ b/tests/testthat/_snaps/snapshot-manage.md @@ -54,3 +54,12 @@ Updating snapshots: * foo/a.md +# snapshot_reject deletes .new files + + Code + snapshot_reject(path = path) + Message + Rejecting snapshots: + * a.md + * b.md + diff --git a/tests/testthat/test-snapshot-manage.R b/tests/testthat/test-snapshot-manage.R index 540db8fc4..7269c4d13 100644 --- a/tests/testthat/test-snapshot-manage.R +++ b/tests/testthat/test-snapshot-manage.R @@ -34,6 +34,13 @@ test_that("can work with variants", { expect_equal(dir(file.path(path, "_snaps", "foo")), "a.md") }) +test_that("snapshot_reject deletes .new files", { + path <- local_snapshot_dir(c("a.md", "a.new.md", "b.md", "b.new.md")) + + expect_snapshot(snapshot_reject(path = path)) + expect_equal(dir(file.path(path, "_snaps")), c("a.md", "b.md")) +}) + # snapshot_meta -----------------------------------------------------------