Skip to content

Commit 420f25d

Browse files
authored
Correctly accept snapshots generated by dotted test names (#2204)
Fixes #1669
1 parent 8f16f26 commit 420f25d

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

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+
* `snapshot_accept(test)` now works when the test file name contains `.` (#1669).
34
* `local_mock()` and `with_mock()` have been deprecated because they are no longer permitted in R 4.5.
45
* `snapshot_review()` now passes `...` on to `shiny::runApp()` (#1928).
56
* `expect_named()` now gives more informative errors (#2091).

R/snapshot-manage.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ snapshot_meta <- function(files = NULL, path = "tests/testthat") {
196196
files <- files[!is_dir]
197197

198198
dirs <- substr(dirs, 1, nchar(dirs) - 1)
199-
files <- ifelse(tools::file_ext(files) == "", paste0(files, ".md"), files)
199+
# Match regardless of whether user include .md or not
200+
files <- c(files, paste0(files, ".md"))
200201

201202
out <- out[out$name %in% files | out$test %in% dirs, , drop = FALSE]
202203
}

tests/testthat/test-snapshot-manage.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ test_that("can accept files created by expect_snapshot()", {
3535
expect_equal(dir(file.path(path, "_snaps")), c("a.md", "b.md"))
3636
})
3737

38+
test_that("can accept files created by dotted tests in name", {
39+
# e.g. test-data.frame.R will create _snaps/data.frame.md
40+
41+
# without extension
42+
path <- local_snapshot_dir(c("data.frame.md", "data.frame.new.md"))
43+
suppressMessages(snapshot_accept("data.frame", path = path))
44+
expect_equal(dir(file.path(path, "_snaps")), "data.frame.md")
45+
46+
# with extension
47+
path <- local_snapshot_dir(c("data.frame.md", "data.frame.new.md"))
48+
suppressMessages(snapshot_accept("data.frame.md", path = path))
49+
expect_equal(dir(file.path(path, "_snaps")), "data.frame.md")
50+
})
51+
3852
test_that("can accept files created by expect_snapshot_file()", {
3953
path <- local_snapshot_dir(c("test/a.txt", "test/a.new.txt"))
4054
suppressMessages(snapshot_accept("test/a.txt", path = path))

0 commit comments

Comments
 (0)