Skip to content

Commit 7307b47

Browse files
authored
Simplify variant test (#1610)
And report variant name when adding file snapshot
1 parent fd648f7 commit 7307b47

File tree

10 files changed

+25
-20
lines changed

10 files changed

+25
-20
lines changed

R/snapshot-file.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ snapshot_review_hint <- function(test, name, ci = on_ci(), check = in_rcmd_check
166166
)
167167
}
168168

169-
snapshot_file_equal <- function(snap_test_dir, snap_name, path, file_equal = compare_file_binary, fail_on_new = FALSE, trace_env = NULL) {
169+
snapshot_file_equal <- function(snap_test_dir, snap_name, snap_variant, path, file_equal = compare_file_binary, fail_on_new = FALSE, trace_env = NULL) {
170170
if (!file.exists(path)) {
171171
abort(paste0("`", path, "` not found"))
172172
}
@@ -187,7 +187,11 @@ snapshot_file_equal <- function(snap_test_dir, snap_name, path, file_equal = com
187187
dir.create(snap_test_dir, showWarnings = FALSE, recursive = TRUE)
188188
file.copy(path, cur_path)
189189

190-
message <- paste0("Adding new file snapshot: 'tests/testhat/_snaps/", snap_name, "'")
190+
message <- paste0(
191+
"Adding new file snapshot: 'tests/testhat/_snaps/",
192+
snap_variant, if (!is.null(snap_variant)) "/",
193+
snap_name, "'"
194+
)
191195
if (fail_on_new) {
192196
fail(message, trace_env = trace_env)
193197
} else {

R/snapshot-reporter.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ SnapshotReporter <- R6::R6Class("SnapshotReporter",
101101
snapshot_file_equal(
102102
snap_test_dir = snap_dir,
103103
snap_name = name,
104+
snap_variant = variant,
104105
path = path,
105106
file_equal = file_equal,
106107
fail_on_new = self$fail_on_new,

tests/testthat/_snaps/R4.0/snapshot-file/nickname.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
R4.0
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# can access nickname
1+
# variants save different values
22

33
Code
4-
version$nickname
4+
r_version()
55
Output
6-
[1] "Shake and Throw"
6+
[1] "R4.0"
77

tests/testthat/_snaps/R4.1/snapshot-file/nickname.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
R4.1
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# can access nickname
1+
# variants save different values
22

33
Code
4-
version$nickname
4+
r_version()
55
Output
6-
[1] "Bird Hippie"
6+
[1] "R4.1"
77

tests/testthat/test-snapshot-file.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ test_that("expect_snapshot_file works in a different directory", {
4848

4949
test_that("expect_snapshot_file works with variant", {
5050
expect_snapshot_file(
51-
write_tmp_lines(version$nickname),
52-
"nickname.txt",
51+
write_tmp_lines(r_version()),
52+
"version.txt",
5353
compare = compare_file_text,
5454
variant = r_version()
5555
)
@@ -96,35 +96,35 @@ test_that("warns on first creation", {
9696

9797
# Warns on first run
9898
expect_warning(
99-
expect_true(snapshot_file_equal(tempdir(), "test.txt", path)),
99+
expect_true(snapshot_file_equal(tempdir(), "test.txt", NULL, path)),
100100
"new file snapshot"
101101
)
102102

103103
# Errors on non-existing file
104104
expect_error(
105-
expect_true(snapshot_file_equal(tempdir(), "test.txt", "doesnt-exist.txt")),
105+
expect_true(snapshot_file_equal(tempdir(), "test.txt", NULL, "doesnt-exist.txt")),
106106
"`doesnt-exist.txt` not found"
107107
)
108108

109109

110110
# Unchanged returns TRUE
111-
expect_true(snapshot_file_equal(tempdir(), "test.txt", path))
111+
expect_true(snapshot_file_equal(tempdir(), "test.txt", NULL, path))
112112
expect_true(file.exists(file.path(tempdir(), "test.txt")))
113113
expect_false(file.exists(file.path(tempdir(), "test.new.txt")))
114114

115115
# Changed returns FALSE
116116
path2 <- write_tmp_lines("b")
117-
expect_false(snapshot_file_equal(tempdir(), "test.txt", path2))
117+
expect_false(snapshot_file_equal(tempdir(), "test.txt", NULL, path2))
118118
expect_true(file.exists(file.path(tempdir(), "test.txt")))
119119
expect_true(file.exists(file.path(tempdir(), "test.new.txt")))
120120

121121
# Changing again overwrites
122122
path2 <- write_tmp_lines("c")
123-
expect_false(snapshot_file_equal(tempdir(), "test.txt", path2))
123+
expect_false(snapshot_file_equal(tempdir(), "test.txt", NULL, path2))
124124
expect_equal(brio::read_lines(file.path(tempdir(), "test.new.txt")), "c")
125125

126126
# Unchanged cleans up
127-
expect_true(snapshot_file_equal(tempdir(), "test.txt", path))
127+
expect_true(snapshot_file_equal(tempdir(), "test.txt", NULL, path))
128128
expect_true(file.exists(file.path(tempdir(), "test.txt")))
129129
expect_false(file.exists(file.path(tempdir(), "test.new.txt")))
130130
})

tests/testthat/test-snapshot.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
test_that("can access nickname", {
2-
expect_snapshot(version$nickname, variant = r_version())
1+
test_that("variants save different values", {
2+
expect_snapshot(r_version(), variant = r_version())
33
})
44

55
test_that("can snapshot output", {

0 commit comments

Comments
 (0)