@@ -10,14 +10,20 @@ SnapshotReporter <- R6::R6Class(
1010    snap_file_saved  =  character (),
1111    variants_changed  =  FALSE ,
1212    fail_on_new  =  NULL ,
13+     desc  =  NULL ,
1314
1415    old_snaps  =  NULL ,
1516    cur_snaps  =  NULL ,
1617    new_snaps  =  NULL ,
1718
18-     initialize  =  function (snap_dir  =  " _snaps"  , fail_on_new  =  NULL ) {
19+     initialize  =  function (
20+       snap_dir  =  " _snaps"  ,
21+       fail_on_new  =  NULL ,
22+       desc  =  NULL 
23+     ) {
1924      self $ snap_dir  <-  normalizePath(snap_dir , mustWork  =  FALSE )
2025      self $ fail_on_new  <-  fail_on_new 
26+       self $ desc  <-  desc 
2127    },
2228
2329    start_file  =  function (path , test  =  NULL ) {
@@ -31,6 +37,22 @@ SnapshotReporter <- R6::R6Class(
3137      self $ cur_snaps  <-  FileSnaps $ new(self $ snap_dir , self $ file , type  =  " cur"  )
3238      self $ new_snaps  <-  FileSnaps $ new(self $ snap_dir , self $ file , type  =  " new"  )
3339
40+       if  (! is.null(self $ desc )) {
41+         #  When filtering tests, we need to copy over all of the old snapshots,
42+         #  apart from the one that matches the test
43+         snaps  <-  self $ old_snaps $ snaps 
44+         test_name  <-  test_description(self $ desc )
45+         for  (variant  in  names(snaps )) {
46+           #  In the case of subtests, snaps are named a / b / c1, a / b / c2 etc.
47+           #  So if we run a / b, we want to remove a / b, a / b / c, a / b / c2
48+           #  Subtests that use / in their names are not currently supported.
49+           matches  <-  startsWith(names(snaps [[variant ]]), test_name )
50+           #  Can't just remove because we want to preserve order
51+           snaps [[variant ]][matches ] <-  rep(list (NULL ), sum(matches ))
52+         }
53+         self $ cur_snaps $ snaps  <-  snaps 
54+       }
55+ 
3456      if  (! is.null(test )) {
3557        self $ start_test(NULL , test )
3658      }
@@ -207,16 +229,30 @@ local_snapshotter <- function(
207229  reporter  =  SnapshotReporter ,
208230  snap_dir  =  " _snaps"  ,
209231  cleanup  =  FALSE ,
232+   desc  =  NULL ,
210233  fail_on_new  =  NULL ,
211234  frame  =  caller_env()
212235) {
213-   reporter  <-  reporter $ new(snap_dir  =  snap_dir , fail_on_new  =  fail_on_new )
236+   reporter  <-  reporter $ new(
237+     snap_dir  =  snap_dir ,
238+     fail_on_new  =  fail_on_new ,
239+     desc  =  desc 
240+   )
214241  withr :: local_options(" testthat.snapshotter"   =  reporter , .local_envir  =  frame )
215242
216243  reporter 
217244}
218245
219- local_test_snapshotter  <-  function (snap_dir  =  NULL , frame  =  caller_env()) {
246+ local_test_snapshotter  <-  function (
247+   snap_dir  =  NULL ,
248+   desc  =  NULL ,
249+   frame  =  caller_env()
250+ ) {
220251  snap_dir  <-  snap_dir  %|| % withr :: local_tempdir(.local_envir  =  frame )
221-   local_snapshotter(snap_dir  =  snap_dir , fail_on_new  =  FALSE , frame  =  frame )
252+   local_snapshotter(
253+     snap_dir  =  snap_dir ,
254+     desc  =  desc ,
255+     fail_on_new  =  FALSE ,
256+     frame  =  frame 
257+   )
222258}
0 commit comments