Skip to content

Commit 4b1b727

Browse files
authored
Better handling of snapshot selection (#1546)
Fixes #1545
1 parent 9a95b9a commit 4b1b727

File tree

8 files changed

+57
-19
lines changed

8 files changed

+57
-19
lines changed

NEWS.md

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

3+
* `snapshot_accept()` and `snapshot_review()` now work with exactly the same
4+
file specification which can be a snapshot name, a file name, or a directory
5+
(#1546).
6+
37
* `expect_snapshot()` no longer inadvertently trims trailing new lines off
48
of errors and messages (#1509).
59

R/snapshot-file.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ expect_snapshot_file <- function(path,
127127

128128
lab <- quo_label(enquo(path))
129129
equal <- snapshotter$take_file_snapshot(name, path, compare, variant = variant)
130-
hint <- snapshot_hint(snapshotter$file, name)
130+
hint <- snapshot_review_hint(snapshotter$file, name)
131131

132132
expect(
133133
equal,
@@ -150,15 +150,15 @@ announce_snapshot_file <- function(path, name = basename(path)) {
150150
}
151151
}
152152

153-
snapshot_hint <- function(test, name, ci = on_ci(), check = in_rcmd_check()) {
153+
snapshot_review_hint <- function(test, name, ci = on_ci(), check = in_rcmd_check()) {
154154
path <- paste0("tests/testthat/_snaps/", test, "/", new_name(name))
155155

156156
paste0(
157157
if (check && ci) "* Download and unzip run artifact\n",
158158
if (check && !ci) "* Locate check directory\n",
159159
if (check) paste0("* Copy '", path, "' to local test directory\n"),
160160
if (check) "* ",
161-
paste0("Run `testthat::snapshot_review('", test, "')` to review changes")
161+
paste0("Run `testthat::snapshot_review('", test, "/')` to review changes")
162162
)
163163
}
164164

R/snapshot-manage.R

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
#' modified snapshot. This is particularly useful for whole file snapshots
66
#' created by `expect_snapshot_file()`.
77
#'
8-
#' @param files Optionally, filter affects to snapshots from specified test
9-
#' files. Can be full path to test (`tests/testthat/test-foo.R`), file name
10-
#' (`test-foo.R`), or test name (`foo`).
8+
#' @param files Optionally, filter effects to snapshots from specified files.
9+
#' This can be a snapshot name (e.g. `foo` or `foo.md`), a snapshot file name
10+
#' (e.g. `testfile/foo.txt`), or a snapshot file directory (e.g. `testfile/`).
11+
#'
1112
#' @param path Path to tests.
1213
#' @export
1314
snapshot_accept <- function(files = NULL, path = "tests/testthat") {
@@ -156,7 +157,14 @@ snapshot_meta <- function(files = NULL, path = "tests/testthat") {
156157
rownames(out) <- NULL
157158

158159
if (!is.null(files)) {
159-
out <- out[out$name %in% paste0(files, ".md"), , drop = FALSE]
160+
is_dir <- substr(files, nchar(files), nchar(files)) == "/"
161+
dirs <- files[is_dir]
162+
files <- files[!is_dir]
163+
164+
dirs <- substr(dirs, 1, nchar(dirs) - 1)
165+
files <- ifelse(tools::file_ext(files) == "", paste0(files, ".md"), files)
166+
167+
out <- out[out$name %in% files | out$test %in% dirs, , drop = FALSE]
160168
}
161169

162170
out

man/snapshot_accept.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
# snapshot_hint output differs in R CMD check
22

33
Code
4-
cat(snapshot_hint("lala", "foo.r", check = FALSE, ci = FALSE))
4+
cat(snapshot_review_hint("lala", "foo.r", check = FALSE, ci = FALSE))
55
Output
6-
Run `testthat::snapshot_review('lala')` to review changes
6+
Run `testthat::snapshot_review('lala/')` to review changes
77

88
---
99

1010
Code
11-
cat(snapshot_hint("lala", "foo.r", check = TRUE, ci = FALSE))
11+
cat(snapshot_review_hint("lala", "foo.r", check = TRUE, ci = FALSE))
1212
Output
1313
* Locate check directory
1414
* Copy 'tests/testthat/_snaps/lala/foo.new.r' to local test directory
15-
* Run `testthat::snapshot_review('lala')` to review changes
15+
* Run `testthat::snapshot_review('lala/')` to review changes
1616

1717
---
1818

1919
Code
20-
cat(snapshot_hint("lala", "foo.r", check = TRUE, ci = TRUE))
20+
cat(snapshot_review_hint("lala", "foo.r", check = TRUE, ci = TRUE))
2121
Output
2222
* Download and unzip run artifact
2323
* Copy 'tests/testthat/_snaps/lala/foo.new.r' to local test directory
24-
* Run `testthat::snapshot_review('lala')` to review changes
24+
* Run `testthat::snapshot_review('lala/')` to review changes
2525

tests/testthat/_snaps/snapshot-manage.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@
2222
Updating snapshots:
2323
* a.md
2424

25+
---
26+
27+
Code
28+
snapshot_accept("test/a.txt", path = path)
29+
Message <message>
30+
Updating snapshots:
31+
* test/a.txt
32+
33+
---
34+
35+
Code
36+
snapshot_accept("test/", path = path)
37+
Message <message>
38+
Updating snapshots:
39+
* test/a.txt
40+
2541
# can work with variants
2642

2743
Code

tests/testthat/test-snapshot-file.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ test_that("split_path handles edge cases", {
154154
})
155155

156156
test_that("snapshot_hint output differs in R CMD check", {
157-
expect_snapshot(cat(snapshot_hint("lala", "foo.r", check = FALSE, ci = FALSE)))
158-
expect_snapshot(cat(snapshot_hint("lala", "foo.r", check = TRUE, ci = FALSE)))
159-
expect_snapshot(cat(snapshot_hint("lala", "foo.r", check = TRUE, ci = TRUE)))
157+
expect_snapshot(cat(snapshot_review_hint("lala", "foo.r", check = FALSE, ci = FALSE)))
158+
expect_snapshot(cat(snapshot_review_hint("lala", "foo.r", check = TRUE, ci = FALSE)))
159+
expect_snapshot(cat(snapshot_review_hint("lala", "foo.r", check = TRUE, ci = TRUE)))
160160
})

tests/testthat/test-snapshot-manage.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ test_that("can accept specific files", {
1111
path <- local_snapshot_dir(c("a.md", "a.new.md", "b.md", "b.new.md"))
1212
expect_snapshot(snapshot_accept("a", path = path))
1313
expect_equal(dir(file.path(path, "_snaps")), c("a.md", "b.md", "b.new.md"))
14+
15+
path <- local_snapshot_dir(c("test/a.txt", "test/a.new.txt"))
16+
expect_snapshot(snapshot_accept("test/a.txt", path = path))
17+
expect_equal(dir(file.path(path, "_snaps"), recursive = TRUE), "test/a.txt")
18+
19+
# or whole directory
20+
path <- local_snapshot_dir(c("test/a.txt", "test/a.new.txt"))
21+
expect_snapshot(snapshot_accept("test/", path = path))
22+
expect_equal(dir(file.path(path, "_snaps"), recursive = TRUE), "test/a.txt")
23+
1424
})
1525

1626
test_that("can work with variants", {

0 commit comments

Comments
 (0)