Skip to content

Commit a3ad893

Browse files
authored
Implement snapshot_reject() (#2189)
Fixes #1923
1 parent edfd684 commit a3ad893

File tree

6 files changed

+39
-0
lines changed

6 files changed

+39
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ export(skip_on_os)
192192
export(skip_on_travis)
193193
export(skip_unless_r)
194194
export(snapshot_accept)
195+
export(snapshot_reject)
195196
export(snapshot_review)
196197
export(source_dir)
197198
export(source_file)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# testthat (development version)
22

3+
* New `snapshot_reject()` rejects all modified snapshots by deleting the `.new` variants (#1923).
34
* 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).
45
* Power `expect_mapequal()` with `waldo::compare(list_as_map = TRUE)` (#1521).
56
* 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.)

R/snapshot-manage.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#' Accept or reject modified snapshots
22
#'
33
#' * `snapshot_accept()` accepts all modified snapshots.
4+
#' * `snapshot_reject()` rejects all modified snapshots by deleting the `.new` variants.
45
#' * `snapshot_review()` opens a Shiny app that shows a visual diff of each
56
#' modified snapshot. This is particularly useful for whole file snapshots
67
#' created by `expect_snapshot_file()`.
@@ -26,6 +27,22 @@ snapshot_accept <- function(files = NULL, path = "tests/testthat") {
2627
invisible()
2728
}
2829

30+
#' @rdname snapshot_accept
31+
#' @export
32+
snapshot_reject <- function(files = NULL, path = "tests/testthat") {
33+
changed <- snapshot_meta(files, path)
34+
if (nrow(changed) == 0) {
35+
inform("No snapshots to reject")
36+
return(invisible())
37+
}
38+
39+
inform(c("Rejecting snapshots:", changed$name))
40+
unlink(changed$new)
41+
42+
rstudio_tickle()
43+
invisible()
44+
}
45+
2946
#' @rdname snapshot_accept
3047
#' @export
3148
snapshot_review <- function(files = NULL, path = "tests/testthat") {

man/snapshot_accept.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/snapshot-manage.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,12 @@
5454
Updating snapshots:
5555
* foo/a.md
5656

57+
# snapshot_reject deletes .new files
58+
59+
Code
60+
snapshot_reject(path = path)
61+
Message
62+
Rejecting snapshots:
63+
* a.md
64+
* b.md
65+

tests/testthat/test-snapshot-manage.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ test_that("can work with variants", {
3434
expect_equal(dir(file.path(path, "_snaps", "foo")), "a.md")
3535
})
3636

37+
test_that("snapshot_reject deletes .new files", {
38+
path <- local_snapshot_dir(c("a.md", "a.new.md", "b.md", "b.new.md"))
39+
40+
expect_snapshot(snapshot_reject(path = path))
41+
expect_equal(dir(file.path(path, "_snaps")), c("a.md", "b.md"))
42+
})
43+
3744

3845
# snapshot_meta -----------------------------------------------------------
3946

0 commit comments

Comments
 (0)