Skip to content

Commit

Permalink
Make accept_snapshot() work on filenames containing a dot (#1669)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mcol committed Dec 21, 2024
1 parent 24ff6c0 commit 18efb9e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* Fixed an issue where calling `skip()` outside of an active test could
cause an unexpected error (@kevinushey, #2039).

* `snapshot_accept()` now works also when the tested file contains dots in
the filename (@mcol #1669).

# testthat 3.2.2

## New expectations
Expand Down
6 changes: 4 additions & 2 deletions R/snapshot-manage.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,11 @@ snapshot_meta <- function(files = NULL, path = "tests/testthat") {
files <- files[!is_dir]

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

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

out
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/snapshot-manage.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@
Updating snapshots:
* test/a.txt

---

Code
snapshot_accept("test/c.d.md", path = path)
Message
Updating snapshots:
* test/c.d.md

---

Code
snapshot_accept("test/c.d", path = path)
Message
Updating snapshots:
* test/c.d.md

# can work with variants

Code
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-snapshot-manage.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ test_that("can accept specific files", {
expect_snapshot(snapshot_accept("test/", path = path))
expect_equal(dir(file.path(path, "_snaps"), recursive = TRUE), "test/a.txt")

## file names with dots
path <- local_snapshot_dir(c("test/c.d.md", "test/c.d.new.md"))
expect_snapshot(snapshot_accept("test/c.d.md", path = path))
expect_equal(dir(file.path(path, "_snaps"), recursive = TRUE), "test/c.d.md")

path <- local_snapshot_dir(c("test/c.d.md", "test/c.d.new.md"))
expect_snapshot(snapshot_accept("test/c.d", path = path))
expect_equal(dir(file.path(path, "_snaps"), recursive = TRUE), "test/c.d.md")

})

test_that("can work with variants", {
Expand Down

0 comments on commit 18efb9e

Please sign in to comment.