diff --git a/NEWS.md b/NEWS.md index fca47541c..35859f5fc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # testthat (development version) +* `snapshot_accept()`, `snapshot_reject()`, and `snapshot_review()` now correctly + find changed snapshots in variant subdirectories when filtering by test file + name (#2325). * `run_cpp_tests()` no longer accidentally reports that a test has been skipped (#2315). * `expect_setequal()` uses better wording in the results (@mcol, #2310). diff --git a/R/snapshot-manage.R b/R/snapshot-manage.R index 0c40b9741..f27f437eb 100644 --- a/R/snapshot-manage.R +++ b/R/snapshot-manage.R @@ -207,7 +207,12 @@ snapshot_meta <- function(files = NULL, path = "tests/testthat") { # Match regardless of whether user include .md or not files <- c(files, paste0(files, ".md")) - out <- out[out$name %in% files | out$test %in% dirs, , drop = FALSE] + # Also match basename to handle variant snapshots (e.g. "variant/html.md") + out <- out[ + out$name %in% files | basename(out$name) %in% files | out$test %in% dirs, + , + drop = FALSE + ] } out diff --git a/tests/testthat/test-snapshot-manage.R b/tests/testthat/test-snapshot-manage.R index 56df6d378..c66543460 100644 --- a/tests/testthat/test-snapshot-manage.R +++ b/tests/testthat/test-snapshot-manage.R @@ -83,6 +83,37 @@ test_that("can work with variants", { path <- local_snapshot_dir(c("foo/a.md", "foo/a.new.md")) expect_snapshot(snapshot_accept("foo/a", path = path)) expect_equal(dir(file.path(path, "_snaps", "foo")), "a.md") + + # Can accept by test name alone (#2325) + path <- local_snapshot_dir(c("foo/a.md", "foo/a.new.md")) + suppressMessages(snapshot_accept("a", path = path)) + expect_equal(dir(file.path(path, "_snaps", "foo")), "a.md") + + # Can accept across multiple variants (#2325) + path <- local_snapshot_dir(c( + "variant1/a.md", + "variant1/a.new.md", + "variant2/a.md", + "variant2/a.new.md" + )) + suppressMessages(snapshot_accept("a", path = path)) + expect_equal( + dir(file.path(path, "_snaps"), recursive = TRUE), + c("variant1/a.md", "variant2/a.md") + ) + + # Only accepts matching test name (#2325) + path <- local_snapshot_dir(c( + "foo/a.md", + "foo/a.new.md", + "foo/b.md", + "foo/b.new.md" + )) + suppressMessages(snapshot_accept("a", path = path)) + expect_equal( + dir(file.path(path, "_snaps"), recursive = TRUE), + c("foo/a.md", "foo/b.md", "foo/b.new.md") + ) }) test_that("snapshot_reject deletes .new files", {