Skip to content

Commit 1f6724e

Browse files
committed
Show correct path in expect_snapshot_file()
Was previously missing the test name, which is part of the path for snapshot files Fixes #2090
1 parent edfd684 commit 1f6724e

File tree

5 files changed

+61
-62
lines changed

5 files changed

+61
-62
lines changed

R/snapshot-file.R

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,13 @@ snapshot_review_hint <- function(
188188
)
189189
}
190190

191+
191192
snapshot_file_equal <- function(
192-
snap_test_dir,
193-
snap_name,
194-
snap_variant,
195-
path,
193+
snap_dir, # _snaps/
194+
snap_test, # test file name
195+
snap_name, # snapshot file name
196+
snap_variant, # variant (optional)
197+
path, # path to new file
196198
file_equal = compare_file_binary,
197199
fail_on_new = FALSE,
198200
trace_env = caller_env()
@@ -201,8 +203,14 @@ snapshot_file_equal <- function(
201203
abort(paste0("`", path, "` not found"))
202204
}
203205

206+
if (is.null(snap_variant)) {
207+
snap_test_dir <- file.path(snap_dir, snap_test)
208+
} else {
209+
snap_test_dir <- file.path(snap_dir, snap_variant, snap_test)
210+
}
211+
204212
cur_path <- file.path(snap_test_dir, snap_name)
205-
new_path <- new_name(cur_path)
213+
new_path <- file.path(snap_test_dir, new_name(snap_name))
206214

207215
if (file.exists(cur_path)) {
208216
eq <- file_equal(cur_path, path)
@@ -217,10 +225,10 @@ snapshot_file_equal <- function(
217225
dir.create(snap_test_dir, showWarnings = FALSE, recursive = TRUE)
218226
file.copy(path, cur_path)
219227

220-
message <- paste0(
228+
message <- paste_c(
221229
"Adding new file snapshot: 'tests/testthat/_snaps/",
222-
snap_variant,
223-
if (!is.null(snap_variant)) "/",
230+
c(snap_variant, if (!is.null(snap_variant)) "/"),
231+
c(snap_test, "/"),
224232
snap_name,
225233
"'"
226234
)

R/snapshot-reporter.R

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,9 @@ SnapshotReporter <- R6::R6Class(
108108
) {
109109
self$announce_file_snapshot(name)
110110

111-
if (is.null(variant)) {
112-
snap_dir <- file.path(self$snap_dir, self$file)
113-
} else {
114-
snap_dir <- file.path(self$snap_dir, variant, self$file)
115-
}
116111
snapshot_file_equal(
117-
snap_test_dir = snap_dir,
112+
snap_dir = self$snap_dir,
113+
snap_test = self$file,
118114
snap_name = name,
119115
snap_variant = variant,
120116
path = path,

R/utils.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@ in_rcmd_check <- function() {
5656
}
5757

5858
r_version <- function() paste0("R", getRversion()[, 1:2])
59+
60+
paste_c <- function(...) {
61+
paste0(c(...), collapse = "")
62+
}

tests/testthat/_snaps/snapshot-file.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# warns on first creation
22

33
Code
4-
out <- snapshot_file_equal(tempdir(), "test.txt", NULL, path)
4+
out <- snapshot_file_equal_(path)
55
Condition
66
Warning:
7-
Adding new file snapshot: 'tests/testthat/_snaps/test.txt'
7+
Adding new file snapshot: 'tests/testthat/_snaps/my-test/test.txt'
88

99
---
1010

1111
Code
12-
expect_true(snapshot_file_equal(tempdir(), "test.txt", NULL, "doesnt-exist.txt"))
12+
snapshot_file_equal_("doesnt-exist.txt")
1313
Condition
1414
Error in `snapshot_file_equal()`:
1515
! `doesnt-exist.txt` not found

tests/testthat/test-snapshot-file.R

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
test_that("expect_snapshot_file works", {
2-
expect_snapshot_file(
3-
write_tmp_lines(letters),
4-
"foo.r",
5-
compare = compare_file_text
6-
)
2+
path <- write_tmp_lines(letters)
3+
expect_snapshot_file(path, "foo.r", compare = compare_file_text)
74

85
path <- withr::local_tempfile()
96
png(path, width = 300, height = 300, type = "cairo")
@@ -15,19 +12,11 @@ test_that("expect_snapshot_file works", {
1512
mtcars2 <- mtcars
1613
# mtcars2$wt[10] <- NA
1714
write.csv(mtcars2, path)
18-
expect_snapshot_file(
19-
path,
20-
"foo.csv",
21-
compare = compare_file_text
22-
)
15+
expect_snapshot_file(path, "foo.csv", compare = compare_file_text)
2316

2417
# Deprecated `binary` argument still works
2518
withr::local_options(lifecycle_verbosity = "quiet")
26-
expect_snapshot_file(
27-
path,
28-
"foo.csv",
29-
binary = FALSE
30-
)
19+
expect_snapshot_file(path, "foo.csv", binary = FALSE)
3120
})
3221

3322

@@ -53,25 +42,21 @@ test_that("expect_snapshot_file works with variant", {
5342
test_that("basic workflow", {
5443
snapper <- local_snapshotter(fail_on_new = FALSE)
5544

45+
path <- write_tmp_lines(letters)
5646
# warns on first run
5747
snapper$start_file("snapshot-6", "test")
58-
expect_warning(
59-
expect_snapshot_file(write_tmp_lines(letters), "letters.txt"),
60-
"Adding new"
61-
)
48+
expect_warning(expect_snapshot_file(path, "letters.txt"), "Adding new")
6249
snapper$end_file()
6350

6451
# succeeds if unchanged
6552
snapper$start_file("snapshot-6", "test")
66-
expect_success(expect_snapshot_file(write_tmp_lines(letters), "letters.txt"))
53+
expect_success(expect_snapshot_file(path, "letters.txt"))
6754
snapper$end_file()
6855

6956
# fails if changed
7057
snapper$start_file("snapshot-6", "test")
71-
expect_failure(expect_snapshot_file(
72-
write_tmp_lines(letters[-1]),
73-
"letters.txt"
74-
))
58+
path2 <- write_tmp_lines(letters[-1])
59+
expect_failure(expect_snapshot_file(path2, "letters.txt"))
7560
snapper$end_file()
7661
})
7762

@@ -93,42 +78,48 @@ test_that("can transform snapshot contents", {
9378

9479
test_that("warns on first creation", {
9580
path <- write_tmp_lines("a")
96-
withr::defer(unlink(file.path(tempdir(), "test.txt")))
81+
snap_dir <- withr::local_tempdir()
82+
83+
snapshot_file_equal_ <- function(path) {
84+
snapshot_file_equal(
85+
snap_dir = snap_dir,
86+
snap_test = "my-test",
87+
snap_name = "test.txt",
88+
snap_variant = NULL,
89+
path = path
90+
)
91+
}
9792

9893
# Warns on first run
99-
expect_snapshot(out <- snapshot_file_equal(tempdir(), "test.txt", NULL, path))
94+
expect_snapshot(out <- snapshot_file_equal_(path))
10095
expect_true(out)
10196

10297
# Errors on non-existing file
103-
expect_snapshot(error = TRUE, {
104-
expect_true(snapshot_file_equal(
105-
tempdir(),
106-
"test.txt",
107-
NULL,
108-
"doesnt-exist.txt"
109-
))
110-
})
98+
expect_snapshot(snapshot_file_equal_("doesnt-exist.txt"), error = TRUE)
11199

112100
# Unchanged returns TRUE
113-
expect_true(snapshot_file_equal(tempdir(), "test.txt", NULL, path))
114-
expect_true(file.exists(file.path(tempdir(), "test.txt")))
115-
expect_false(file.exists(file.path(tempdir(), "test.new.txt")))
101+
expect_true(snapshot_file_equal_(path))
102+
expect_true(file.exists(file.path(snap_dir, "my-test/test.txt")))
103+
expect_false(file.exists(file.path(snap_dir, "my-test/test.new.txt")))
116104

117105
# Changed returns FALSE
118106
path2 <- write_tmp_lines("b")
119-
expect_false(snapshot_file_equal(tempdir(), "test.txt", NULL, path2))
120-
expect_true(file.exists(file.path(tempdir(), "test.txt")))
121-
expect_true(file.exists(file.path(tempdir(), "test.new.txt")))
107+
expect_false(snapshot_file_equal_(path2))
108+
expect_true(file.exists(file.path(snap_dir, "my-test/test.txt")))
109+
expect_true(file.exists(file.path(snap_dir, "my-test/test.new.txt")))
122110

123111
# Changing again overwrites
124112
path2 <- write_tmp_lines("c")
125-
expect_false(snapshot_file_equal(tempdir(), "test.txt", NULL, path2))
126-
expect_equal(brio::read_lines(file.path(tempdir(), "test.new.txt")), "c")
113+
expect_false(snapshot_file_equal_(path2))
114+
expect_equal(
115+
brio::read_lines(file.path(snap_dir, "my-test/test.new.txt")),
116+
"c"
117+
)
127118

128119
# Unchanged cleans up
129-
expect_true(snapshot_file_equal(tempdir(), "test.txt", NULL, path))
130-
expect_true(file.exists(file.path(tempdir(), "test.txt")))
131-
expect_false(file.exists(file.path(tempdir(), "test.new.txt")))
120+
expect_true(snapshot_file_equal_(path))
121+
expect_true(file.exists(file.path(snap_dir, "my-test/test.txt")))
122+
expect_false(file.exists(file.path(snap_dir, "my-test/test.new.txt")))
132123
})
133124

134125
# helpers -----------------------------------------------------------------

0 commit comments

Comments
 (0)