Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* `it()` now finds the correct evaluation environment in more cases (@averissimo, #2085).
* Fixed an issue preventing compilation from succeeding due to deprecation / removal of `std::uncaught_exception()` (@kevinushey, #2047).
* `snapshot_accept()` now works also when the tested file contains dots in the filename (@mcol, #1669).

# testthat 3.2.3

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your fix assumes that snapshots can only have file names ending in .md or .txt, but in fact they can have any extension because the user supplies the extension in expect_snapshot_file().

But overall I find this code (i.e. the code that existed before your PR) very hard to understand: I don't understand what it's try is trying to achieve, or how the logic flows. So that might imply it's time to take a step back and rewrite this code to make it more clear what the varies inputs are.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a partial fix for the hardcoded extensions, although it may not work if there are multiple snapshot extensions being used simultaneously (but also, there's no pre-existing test for that case, independently of dots in the filenames).

Having said that, I agree that this code is quite hacky as it is. Are you planning to do it or should I have a go?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd really appreciate it if you wanted to have a go 😄


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
Loading