Skip to content

Commit 7d73002

Browse files
authored
Add transform argument to expect_snapshot_file() (#1530)
And tweak `compare` default so we don't imply a byte-by-byte comparison but a line-by-line transformation. Fixes #1474
1 parent 912edba commit 7d73002

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

NEWS.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# testthat (development version)
22

3+
* `expect_snapshot_file()` gains a `transform` argument to match
4+
`expect_snapshot()` (#1474). `compare` now defaults to `NULL`, automatically
5+
guessing the comparison type based on the extension.
6+
37
* Snapshot cleanup also removes all empty directories (#1457).
48

59
* `.new` file snapshots variants are no longer immediately deleted after

R/snapshot-file.R

+21-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121
#' @param name Snapshot name, taken from `path` by default.
2222
#' @param binary `r lifecycle::badge("deprecated")` Please use the
2323
#' `compare` argument instead.
24-
#' @param compare A function used for comparison taking `old` and
25-
#' `new` arguments. By default this is `compare_file_binary`. Set it
26-
#' to `compare_file_text` to compare files line-by-line, ignoring
24+
#' @param compare A function used to compare the snapshot files. It should take
25+
#' two inputs, the paths to the `old` and `new` snapshot, and return either
26+
#' `TRUE` or `FALSE`. This defaults to `compare_file_text` if `name` has
27+
#' extension `.r`, `.R`, `.Rmd`, `.md`, or `.txt`, and otherwise uses
28+
#' `compare_file_binary`.
29+
#'
30+
#' `compare_file_binary()` compares byte-by-byte and
31+
#' `compare_file_text()` compares lines-by-line, ignoring
2732
#' the difference between Windows and Mac/Linux line endings.
2833
#' @param variant If not-`NULL`, results will be saved in
2934
#' `_snaps/{variant}/{test}/{name}.{ext}`. This allows you to create
@@ -84,7 +89,8 @@ expect_snapshot_file <- function(path,
8489
name = basename(path),
8590
binary = lifecycle::deprecated(),
8691
cran = FALSE,
87-
compare = compare_file_binary,
92+
compare = NULL,
93+
transform = NULL,
8894
variant = NULL) {
8995
edition_require(3, "expect_snapshot_file()")
9096
if (!cran && !interactive() && on_cran()) {
@@ -107,6 +113,17 @@ expect_snapshot_file <- function(path,
107113
)
108114
compare <- if (binary) compare_file_binary else compare_file_text
109115
}
116+
if (is.null(compare)) {
117+
ext <- tools::file_ext(name)
118+
is_text <- ext %in% c("r", "R", "txt", "md", "Rmd")
119+
compare <- if (is_text) compare_file_text else compare_file_binary
120+
}
121+
122+
if (!is.null(transform)) {
123+
lines <- brio::read_lines(path)
124+
lines <- transform(lines)
125+
brio::write_lines(lines, path)
126+
}
110127

111128
lab <- quo_label(enquo(path))
112129
equal <- snapshotter$take_file_snapshot(name, path, compare, variant = variant)

man/expect_snapshot_file.Rd

+14-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<redacted>
2+
ssh <redacted> squirrel

tests/testthat/test-snapshot-file.R

+6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ test_that("can announce snapshot file", {
8181
expect_equal(snapper$snap_file_seen, "snapshot-announce/bar.svg")
8282
})
8383

84+
test_that("can transform snapshot contents", {
85+
path <- local_tempfile1(c("secret", "ssh secret squirrel"))
86+
87+
redact <- function(x) gsub("secret", "<redacted>", x)
88+
expect_snapshot_file(path, "secret.txt", transform = redact)
89+
})
8490

8591
# snapshot_file_equal -----------------------------------------------------
8692

0 commit comments

Comments
 (0)