21
21
# ' @param name Snapshot name, taken from `path` by default.
22
22
# ' @param binary `r lifecycle::badge("deprecated")` Please use the
23
23
# ' `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
27
32
# ' the difference between Windows and Mac/Linux line endings.
28
33
# ' @param variant If not-`NULL`, results will be saved in
29
34
# ' `_snaps/{variant}/{test}/{name}.{ext}`. This allows you to create
@@ -84,7 +89,8 @@ expect_snapshot_file <- function(path,
84
89
name = basename(path ),
85
90
binary = lifecycle :: deprecated(),
86
91
cran = FALSE ,
87
- compare = compare_file_binary ,
92
+ compare = NULL ,
93
+ transform = NULL ,
88
94
variant = NULL ) {
89
95
edition_require(3 , " expect_snapshot_file()" )
90
96
if (! cran && ! interactive() && on_cran()) {
@@ -107,6 +113,17 @@ expect_snapshot_file <- function(path,
107
113
)
108
114
compare <- if (binary ) compare_file_binary else compare_file_text
109
115
}
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
+ }
110
127
111
128
lab <- quo_label(enquo(path ))
112
129
equal <- snapshotter $ take_file_snapshot(name , path , compare , variant = variant )
0 commit comments