Skip to content

Commit b9b253a

Browse files
authored
Improve logic for variant cleanup (#1542)
1 parent 8866385 commit b9b253a

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

R/snapshot-file-snaps.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,15 @@ FileSnaps <- R6::R6Class("FileSnaps", public = list(
7676
invisible()
7777
},
7878

79-
delete = function() {
80-
unlink(self$path())
79+
delete = function(variant = "_default") {
80+
unlink(self$path(variant))
8181
invisible()
8282
},
8383

84+
variants = function() {
85+
names(self$snaps)
86+
},
87+
8488
filename = function() {
8589
paste0(self$file, if (self$type == "new") ".new", ".md")
8690
},

R/snapshot-reporter.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,12 @@ SnapshotReporter <- R6::R6Class("SnapshotReporter",
111111

112112
self$cur_snaps$write()
113113

114-
if (length(self$variants_changed) >= 1) {
115-
self$new_snaps$write(self$variants_changed)
116-
} else {
117-
self$new_snaps$delete()
114+
for (variant in self$new_snaps$variants()) {
115+
if (variant %in% self$variants_changed) {
116+
self$new_snaps$write(variant)
117+
} else {
118+
self$new_snaps$delete(variant)
119+
}
118120
}
119121
},
120122
end_reporter = function() {

tests/testthat/test-snapshot-reporter.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ test_that("only create new files for changed variants", {
7373
)
7474
})
7575

76+
test_that("only reverting change in variant deletes .new", {
77+
snapper <- local_snapshotter()
78+
snapper$start_file("v", "test")
79+
expect_warning(expect_snapshot_output("x", variant = "a"), "Adding new")
80+
expect_warning(expect_snapshot_output("x", variant = "b"), "Adding new")
81+
snapper$end_file()
82+
expect_setequal(snapper$snap_files(), c("a/v.md", "b/v.md"))
83+
84+
# failure
85+
snapper$start_file("v", "test")
86+
expect_failure(expect_snapshot_output("y", variant = "a"))
87+
snapper$end_file()
88+
expect_setequal(snapper$snap_files(), c("a/v.md", "b/v.md", "a/v.new.md"))
89+
90+
# success
91+
snapper$start_file("v", "test")
92+
expect_success(expect_snapshot_output("x", variant = "a"))
93+
snapper$end_file()
94+
expect_setequal(snapper$snap_files(), c("a/v.md", "b/v.md"))
95+
})
96+
7697

7798
test_that("removing tests removes snap file", {
7899
path <- withr::local_tempdir()

0 commit comments

Comments
 (0)