10BC0 feat: snaps.Clean now returns if snaps dirty and err (#131) · gkampitakis/go-snaps@e303e41 · GitHub
[go: up one dir, main page]

Skip to content

Commit e303e41

Browse files
authored
feat: snaps.Clean now returns if snaps dirty and err (#131)
1 parent e88f212 commit e303e41

File tree

4 files changed

+63
-30
lines changed

4 files changed

+63
-30
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@ func TestMain(m *testing.M) {
362362

363363
// After all tests have run `go-snaps` can check for unused snapshots
364364
snaps.Clean(m)
365+
// dirty, err := snaps.Clean(m)
366+
// _ = err
367+
// if dirty {
368+
// fmt.Println("Some snapshots were outdated.")
369+
// os.Exit(1)
370+
// }
365371

366372
os.Exit(v)
367373
}

snaps/clean.go

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ type CleanOpts struct {
5757
// v := m.Run()
5858
//
5959
// // After all tests have run `go-snaps` can check for unused snapshots
60-
// snaps.Clean(m)
60+
// dirty, err := snaps.Clean(m)
61+
// if err != nil {
62+
// fmt.Println("Error cleaning snaps:", err)
63+
// os.Exit(1)
64+
// }
65+
// if dirty {
66+
// fmt.Println("Some snapshots were outdated.")
67+
// os.Exit(1)
68+
// }
6169
//
6270
// os.Exit(v)
6371
// }
@@ -68,11 +76,19 @@ type CleanOpts struct {
6876
// v := m.Run()
6977
//
7078
// // After all tests have run `go-snaps` will sort snapshots
71-
// snaps.Clean(m, snaps.CleanOpts{Sort: true})
79+
// dirty, err := snaps.Clean(m, snaps.CleanOpts{Sort: true})
80+
// if err != nil {
81+
// fmt.Println("Error cleaning snaps:", err)
82+
// os.Exit(1)
83+
// }
84+
// if dirty {
85+
// fmt.Println("Some snapshots were outdated.")
86+
// os.Exit(1)
87+
// }
7288
//
7389
// os.Exit(v)
7490
// }
75-
func Clean(m *testing.M, opts ...CleanOpts) {
91+
func Clean(m *testing.M, opts ...CleanOpts) (bool, error) {
7692
var opt CleanOpts
7793
if len(opts) != 0 {
7894
opt = opts[0]
@@ -87,34 +103,35 @@ func Clean(m *testing.M, opts ...CleanOpts) {
87103
standaloneOccurrenceFMT,
88104
)
89105

90-
obsoleteFiles, usedFiles := examineFiles(
106+
obsoleteFiles, usedFiles, filesDirty := examineFiles(
91107
testsRegistry.cleanup,
92108
registeredStandaloneTests,
93109
runOnly,
94-
shouldClean && !isCI,
110+
shouldClean,
95111
)
96-
obsoleteTests, err := examineSnaps(
112+
obsoleteTests, snapsDirty, err := examineSnaps(
97113
testsRegistry.cleanup,
98114
usedFiles,
99115
runOnly,
100116
count,
101-
shouldClean && !isCI,
102-
opt.Sort && !isCI,
117+
shouldClean,
118+
opt.Sort,
103119
)
104120
if err != nil {
105-
fmt.Println(err)
106-
return
121+
return snapsDirty || filesDirty, err
107122
}
108123

109124
if s := summary(
110125
obsoleteFiles,
111126
obsoleteTests,
112127
len(skippedTests.values),
113128
testEvents.items,
114-
shouldClean && !isCI,
129+
shouldClean,
115130
); s != "" {
116131
fmt.Println(s)
117132
}
133+
134+
return filesDirty || snapsDirty, nil
118135
}
119136

120137
// getTestID will return the testID if the line is in the form of [Test... - number]
@@ -161,7 +178,7 @@ func examineFiles(
161178
registeredStandaloneTests set,
162179
runOnly string,
163180
shouldUpdate bool,
164-
) (obsolete, used []string) {
181+
) (obsolete, used []string, dirtyFiles bool) {
165182
uniqueDirs := set{}
166183

167184
for snapPaths := range registry {
@@ -211,7 +228,7 @@ func examineFiles(
211228
}
212229
}
213230

214-
return obsolete, used
231+
return obsolete, used, len(obsolete) > 0
215232
}
216233

217234
func examineSnaps(
@@ -221,16 +238,17 @@ func examineSnaps(
221238
count int,
222239
update,
223240
sort bool,
224-
) ([]string, error) {
241+
) ([]string, bool, error) {
225242
obsoleteTests := []string{}
226243
tests := map[string]string{}
227244
data := bytes.Buffer{}
228245
testIDs := []string{}
246+
var isDirty bool
229247

230248
for _, snapPath := range used {
231249
f, err := os.OpenFile(snapPath, os.O_RDWR, os.ModePerm)
232250
if err != nil {
233-
return nil, err
251+
return nil, isDirty, err
234252
}
235253

236254
var hasDiffs bool
@@ -271,14 +289,16 @@ func examineSnaps(
271289
}
272290

273291
if err := s.Err(); err != nil {
274-
return nil, err
292+
return nil, isDirty, err
275293
}
276294

277-
shouldSort := sort && !slices.IsSortedFunc(testIDs, naturalSort)
278-
shouldUpdate := update && hasDiffs
295+
shouldSort := sort && !slices.IsSortedFunc(testIDs, naturalSort) && update
296+
shouldDelete := hasDiffs && update
297+
298+
isDirty = isDirty || (sort && !slices.IsSortedFunc(testIDs, naturalSort)) || hasDiffs
279299

280300
// if we don't have to "write" anything on the snap we skip
281-
if !shouldUpdate && !shouldSort {
301+
if !shouldDelete && !shouldSort {
282302
f.Close()
283303

284304
clear(tests)
@@ -294,7 +314,7 @@ func examineSnaps(
294314
}
295315

296316
if err := overwriteFile(f, nil); err != nil {
297-
return nil, err
317+
return nil, isDirty, err
298318
}
299319

300320
for _, id := range testIDs {
@@ -312,7 +332,7 @@ func examineSnaps(
312332
data.Reset()
313333
}
314334

315-
return obsoleteTests, nil
335+
return obsoleteTests, isDirty, nil
316336
}
317337

318338
func summary(

snaps/clean_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestExamineFiles(t *testing.T) {
116116
loadMockSnap(t, "mock-snap-1"),
117117
loadMockSnap(t, "mock-snap-2"),
118118
)
119-
obsolete, used := examineFiles(tests, set{
119+
obsolete, used, isDirty := examineFiles(tests, set{
120120
dir1 + "TestSomething_my_test_1.snap": struct{}{},
121121
dir2 + "TestAnotherThing_my_simple_test_1.snap": struct{}{},
122122
}, "", false)
@@ -142,6 +142,7 @@ func TestExamineFiles(t *testing.T) {
142142

143143
test.Equal(t, obsoleteExpected, obsolete)
144144
test.Equal(t, usedExpected, used)
145+
test.True(t, isDirty)
145146
})
146147

147148
t.Run("should remove outdated files", func(t *testing.T) {
@@ -187,10 +188,11 @@ func TestExamineSnaps(t *testing.T) {
187188
filepath.FromSlash(dir2 + "/test2.snap"),
188189
}
189190

190-
obsolete, err := examineSnaps(tests, used, "", 1, shouldUpdate, sort)
191+
obsolete, isDirty, err := examineSnaps(tests, used, "", 1, shouldUpdate, sort)
191192

192193
test.Equal(t, []string{}, obsolete)
193194
test.NoError(t, err)
195+
test.False(t, isDirty)
194196
})
195197

196198
t.Run("should report two obsolete snapshots and not change content", func(t *testing.T) {
@@ -208,7 +210,7 @@ func TestExamineSnaps(t *testing.T) {
208210
// Removing the test entirely
209211
delete(tests[used[1]], "TestDir2_2/TestSimple")
210212

211-
obsolete, err := examineSnaps(tests, used, "", 1, shouldUpdate, sort)
213+
obsolete, isDirty, err := examineSnaps(tests, used, "", 1, shouldUpdate, sort)
212214
content1 := test.GetFileContent(t, used[0])
213215
content2 := test.GetFileContent(t, used[1])
214216

@@ -218,6 +220,7 @@ func TestExamineSnaps(t *testing.T) {
218220
// Content of snaps is not changed
219221
test.Equal(t, mockSnap1, []byte(content1))
220222
test.Equal(t, mockSnap2, []byte(content2))
223+
test.True(t, isDirty)
221224
})
222225

223226
t.Run("should update the obsolete snap files", func(t *testing.T) {
@@ -236,7 +239,7 @@ func TestExamineSnaps(t *testing.T) {
236239
delete(tests[used[0]], "TestDir1_3/TestSimple")
237240
delete(tests[used[1]], "TestDir2_1/TestSimple")
238241

239-
obsolete, err := examineSnaps(tests, used, "", 1, shouldUpdate, sort)
242+
obsolete, isDirty, err := examineSnaps(tests, used, "", 1, shouldUpdate, sort)
240243
content1 := test.GetFileContent(t, used[0])
241244
content2 := test.GetFileContent(t, used[1])
242245

@@ -276,10 +279,11 @@ string hello world 2 2 1
276279
// Content of snaps is not changed
277280
test.Equal(t, expected1, content1)
278281
test.Equal(t, expected2, content2)
282+
test.True(t, isDirty)
279283
})
280284

281285
t.Run("should sort all tests", func(t *testing.T) {
282-
shouldUpdate, sort := false, true
286+
shouldUpdate, sort := true, true
283287
mockSnap1 := loadMockSnap(t, "mock-snap-sort-1")
284288
mockSnap2 := loadMockSnap(t, "mock-snap-sort-2")
285289
expectedMockSnap1 := loadMockSnap(t, "mock-snap-sort-1-sorted")
@@ -294,7 +298,7 @@ string hello world 2 2 1
294298
filepath.FromSlash(dir2 + "/test2.snap"),
295299
}
296300

297-
obsolete, err := examineSnaps(tests, used, "", 1, shouldUpdate, sort)
301+
obsolete, isDirty, err := examineSnaps(tests, used, "", 1, shouldUpdate, sort)
298302

299303
test.NoError(t, err)
300304
test.Equal(t, 0, len(obsolete))
@@ -304,6 +308,7 @@ string hello world 2 2 1
304308

305309
test.Equal(t, string(expectedMockSnap1), content1)
306310
test.Equal(t, string(expectedMockSnap2), content2)
311+
test.True(t, isDirty)
307312
})
308313

309314
t.Run(
@@ -326,7 +331,7 @@ string hello world 2 2 1
326331
delete(tests[used[0]], "TestDir1_3/TestSimple")
327332
delete(tests[used[1]], "TestDir2_1/TestSimple")
328333

329-
obsolete, err := examineSnaps(tests, used, "", 1, shouldUpdate, sort)
334+
obsolete, isDirty, err := examineSnaps(tests, used, "", 1, shouldUpdate, sort)
330335

331336
test.NoError(t, err)
332337
test.Equal(t, []string{
@@ -344,6 +349,7 @@ string hello world 2 2 1
344349

345350
test.Equal(t, string(mockSnap1), content1)
346351
test.Equal(t, string(mockSnap2), content2)
352+
test.True(t, isDirty)
347353
},
348354
)
349355
}

snaps/utils.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ var (
2020
errSnapNotFound = errors.New("snapshot not found")
2121
isCI = ciinfo.IsCI
2222
updateVAR = os.Getenv("UPDATE_SNAPS")
23-
shouldClean = updateVAR == "always" || updateVAR == "true" || updateVAR == "clean"
24-
defaultConfig = Config{
23+
shouldClean = updateVAR == "always" || (updateVAR == "true" && !isCI) ||
24+
(updateVAR == "clean" && !isCI)
25+
defaultConfig = Config{
2526
snapsDir: "__snapshots__",
2627
}
2728
isTrimBathBuild = trimPathBuild()

0 commit comments

Comments
 (0)
0