11test_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", {
5342test_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
9479test_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