Skip to content

Commit bc5d676

Browse files
committed
Make accept_snapshot() work on filenames containing a dot (#1669)
This changes slightly the logic for matching file names: - before if a file name was considered to be without extension, we would add the `.md` and then compare it with the actual file names in the `_snaps` directory. However, that would fail if the file contained dots, as they would be considered to be a file extension: for them no `.md` was added to their name, and therefore they would never match with the actual files in the `_snaps` directory, so no update could ever succeed - with this patch, we strip any `.md` or `.txt` extension from the file names received by `snapshot_manage()`, and compare those with the actual file names from the `_snaps` directory also without extension
1 parent 886527d commit bc5d676

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
* `with_mock()` and `local_mock()` have been unconditionally deprecated as
4444
they will no longer work in future versions of R (#1999).
4545

46+
* `snapshot_accept()` now works also when the tested file contains dots in
47+
the filename (@mcol #1669).
48+
4649
# testthat 3.2.1
4750

4851
* Fix incorrect format string detected by latest R-devel. Fix thanks to

R/snapshot-manage.R

+4-2
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,11 @@ snapshot_meta <- function(files = NULL, path = "tests/testthat") {
161161
files <- files[!is_dir]
162162

163163
dirs <- substr(dirs, 1, nchar(dirs) - 1)
164-
files <- ifelse(tools::file_ext(files) == "", paste0(files, ".md"), files)
164+
files <- gsub("\\.md|\\.txt$", "", files)
165165

166-
out <- out[out$name %in% files | out$test %in% dirs, , drop = FALSE]
166+
out_name_sans_ext <- tools::file_path_sans_ext(out$name)
167+
out <- out[out_name_sans_ext %in% files |
168+
out$test %in% dirs, , drop = FALSE]
167169
}
168170

169171
out

tests/testthat/_snaps/snapshot-manage.md

+16
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@
3838
Updating snapshots:
3939
* test/a.txt
4040

41+
---
42+
43+
Code
44+
snapshot_accept("test/c.d.md", path = path)
45+
Message
46+
Updating snapshots:
47+
* test/c.d.md
48+
49+
---
50+
51+
Code
52+
snapshot_accept("test/c.d", path = path)
53+
Message
54+
Updating snapshots:
55+
* test/c.d.md
56+
4157
# can work with variants
4258

4359
Code

tests/testthat/test-snapshot-manage.R

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ test_that("can accept specific files", {
2121
expect_snapshot(snapshot_accept("test/", path = path))
2222
expect_equal(dir(file.path(path, "_snaps"), recursive = TRUE), "test/a.txt")
2323

24+
## file names with dots
25+
path <- local_snapshot_dir(c("test/c.d.md", "test/c.d.new.md"))
26+
expect_snapshot(snapshot_accept("test/c.d.md", path = path))
27+
expect_equal(dir(file.path(path, "_snaps"), recursive = TRUE), "test/c.d.md")
28+
29+
path <- local_snapshot_dir(c("test/c.d.md", "test/c.d.new.md"))
30+
expect_snapshot(snapshot_accept("test/c.d", path = path))
31+
expect_equal(dir(file.path(path, "_snaps"), recursive = TRUE), "test/c.d.md")
32+
2433
})
2534

2635
test_that("can work with variants", {

0 commit comments

Comments
 (0)