8000 fix: only update snapshots when actually allowed (#135) · gkampitakis/go-snaps@037cb97 · GitHub
[go: up one dir, main page]

Skip to content

Commit 037cb97

Browse files
G-Rathgkampitakis
andauthored
fix: only update snapshots when actually allowed (#135)
* fix: regression on reporting isDirty and sorting snaps * add the writing enabled guard * fix: only update snapshots when actually allowed, but always report if they are dirty * refactor: rename variables --------- Co-authored-by: gkampitakis <gkabitakis@gmail.com> Co-authored-by: Georgios Kampitakis <50364739+gkampitakis@users.noreply.github.com>
1 parent 0071987 commit 037cb97

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

snaps/clean.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func examineSnaps(
236236
used []string,
237237
runOnly string,
238238
count int,
239-
update,
239+
shouldUpdate,
240240
sort bool,
241241
) ([]string, bool, error) {
242242
obsoleteTests := []string{}
@@ -251,7 +251,7 @@ func examineSnaps(
251251
return nil, isDirty, err
252252
}
253253

254-
var hasDiffs bool
254+
var needsUpdating bool
255255

256256
registeredTests := occurrences(registry[snapPath], count, snapshotOccurrenceFMT)
257257
s := snapshotScanner(f)
@@ -267,7 +267,7 @@ func examineSnaps(
267267

268268
if !registeredTests.Has(testID) && !testSkipped(testID, runOnly) {
269269
obsoleteTests = append(obsoleteTests, testID)
270-
hasDiffs = true
270+
needsUpdating = true
271271

272272
removeSnapshot(s)
273273
continue
@@ -292,13 +292,15 @@ func examineSnaps(
292292
return nil, isDirty, err
293293
}
294294

295-
shouldSort := sort && !slices.IsSortedFunc(testIDs, naturalSort) && update
296-
shouldDelete := hasDiffs && update
295+
needsSorting := sort && !slices.IsSortedFunc(testIDs, naturalSort)
297296

298-
isDirty = isDirty || (sort && !slices.IsSortedFunc(testIDs, naturalSort)) || hasDiffs
297+
// if we're not allowed to update anything, just capture if the snapshot
298+
// needs cleaning, and then continue to the next snapshot
299+
if !shouldUpdate {
300+
if needsUpdating || needsSorting {
301+
isDirty = true
302+
}
299303

300-
// if we don't have to "write" anything on the snap we skip
301-
if !shouldDelete && !shouldSort {
302304
f.Close()
303305

304306
clear(tests)
@@ -308,7 +310,7 @@ func examineSnaps(
308310
continue
309311
}
310312

311-
if shouldSort {
313+
if needsSorting {
312314
// sort testIDs
313315
slices.SortFunc(testIDs, naturalSort)
314316
}

snaps/clean_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ func TestExamineSnaps(t *testing.T) {
220220
// Content of snaps is not changed
221221
test.Equal(t, mockSnap1, []byte(content1))
222222
test.Equal(t, mockSnap2, []byte(content2))
223+
224+
// And thus we are dirty since the contents do need changing
223225
test.True(t, isDirty)
224226
})
225227

@@ -276,13 +278,15 @@ string hello world 2 2 1
276278
)
277279
test.NoError(t, err)
278280

279-
// Content of snaps is not changed
281+
// Content of snaps have been updated
280282
test.Equal(t, expected1, content1)
281283
test.Equal(t, expected2, content2)
282-
test.True(t, isDirty)
284+
285+
// And thus we are not dirty
286+
test.False(t, isDirty)
283287
})
284288

285-
t.Run("should sort all tests", func(t *testing.T) {
289+
t.Run("should sort all tests when allowed to update", func(t *testing.T) {
286290
shouldUpdate, sort := true, true
287291
mockSnap1 := loadMockSnap(t, "mock-snap-sort-1")
288292
mockSnap2 := loadMockSnap(t, "mock-snap-sort-2")
@@ -306,9 +310,12 @@ string hello world 2 2 1
306310
content1 := test.GetFileContent(t, filepath.FromSlash(dir1+"/test1.snap"))
307311
content2 := test.GetFileContent(t, filepath.FromSlash(dir2+"/test2.snap"))
308312

313+
// Content of snaps are now sorted
309314
test.Equal(t, string(expectedMockSnap1), content1)
310315
test.Equal(t, string(expectedMockSnap2), content2)
311-
test.True(t, isDirty)
316+
317+
// And thus we are not dirty
318+
test.False(t, isDirty)
312319
})
313320

314321
t.Run(
@@ -347,8 +354,11 @@ string hello world 2 2 1
347354
content1 := test.GetFileContent(t, filepath.FromSlash(dir1+"/test1.snap"))
348355
content2 := test.GetFileContent(t, filepath.FromSlash(dir2+"/test2.snap"))
349356

357+
// Content of snaps is not changed
350358
test.Equal(t, string(mockSnap1), content1)
351359
test.Equal(t, string(mockSnap2), content2)
360+
361+
// And thus we are dirty, since there are obsolete snapshots that should be removed
352362
test.True(t, isDirty)
353363
},
354364
)

0 commit comments

Comments
 (0)
0