@@ -74,12 +74,14 @@ func cacheWriteBad(t *testing.T, storedAt string, contents string) {
74
74
}
75
75
}
76
76
77
- func createZipServer (t * testing.T , handler http.HandlerFunc ) ( * httptest.Server , func ()) {
77
+ func createZipServer (t * testing.T , handler http.HandlerFunc ) * httptest.Server {
78
78
t .Helper ()
79
79
80
80
ts := httptest .NewServer (handler )
81
81
82
- return ts , ts .Close
82
+ t .Cleanup (ts .Close )
83
+
84
+ return ts
83
85
}
84
86
85
87
func computeCRC32CHash (t * testing.T , data []byte ) string {
@@ -139,10 +141,9 @@ func TestNewZippedDB_Offline_WithoutCache(t *testing.T) {
139
141
140
142
testDir := testutility .CreateTestDir (t )
141
143
142
- ts , cleanupTestServer := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
144
+ ts := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
143
145
t .Errorf ("a server request was made when running offline" )
144
146
})
145
- defer cleanupTestServer ()
146
147
147
148
_ , err := local .NewZippedDB (testDir , "my-db" , ts .URL , true )
148
149
@@ -164,10 +165,9 @@ func TestNewZippedDB_Offline_WithCache(t *testing.T) {
164
165
165
166
testDir := testutility .CreateTestDir (t )
166
167
167
- ts , cleanupTestServer := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
168
+ ts := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
168
169
t .Errorf ("a server request was made when running offline" )
169
170
})
170
- defer cleanupTestServer ()
171
171
172
172
cacheWrite (t , determineStoredAtPath (testDir , "my-db" ), zipOSVs (t , map [string ]models.Vulnerability {
173
173
"GHSA-1.json" : {ID : "GHSA-1" },
@@ -191,10 +191,9 @@ func TestNewZippedDB_BadZip(t *testing.T) {
191
191
192
192
testDir := testutility .CreateTestDir (t )
193
193
194
- ts , cleanupTestServer := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
194
+ ts := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
195
195
_ , _ = w .Write ([]byte ("this is not a zip" ))
196
196
})
197
- defer cleanupTestServer ()
198
197
199
198
_ , err := local .NewZippedDB (testDir , "my-db" , ts .URL , false )
200
199
@@ -228,7 +227,7 @@ func TestNewZippedDB_Online_WithoutCache(t *testing.T) {
228
227
229
228
testDir := testutility .CreateTestDir (t )
230
229
231
- ts , cleanupTestServer := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
230
+ ts := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
232
231
_ , _ = writeOSVsZip (t , w , map [string ]models.Vulnerability {
233
232
"GHSA-1.json" : {ID : "GHSA-1" },
234
233
"GHSA-2.json" : {ID : "GHSA-2" },
@@ -237,7 +236,6 @@ func TestNewZippedDB_Online_WithoutCache(t *testing.T) {
237
236
"GHSA-5.json" : {ID : "GHSA-5" },
238
237
})
239
238
})
240
- defer cleanupTestServer ()
241
239
242
240
db , err := local .NewZippedDB (testDir , "my-db" , ts .URL , false )
243
241
@@ -261,7 +259,7 @@ func TestNewZippedDB_Online_WithoutCacheAndNoHashHeader(t *testing.T) {
261
259
262
260
testDir := testutility .CreateTestDir (t )
263
261
264
- ts , cleanupTestServer := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
262
+ ts := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
265
263
_ , _ = w .Write (zipOSVs (t , map [string ]models.Vulnerability {
266
264
"GHSA-1.json" : {ID : "GHSA-1" },
267
265
"GHSA-2.json" : {ID : "GHSA-2" },
@@ -270,7 +268,6 @@ func TestNewZippedDB_Online_WithoutCacheAndNoHashHeader(t *testing.T) {
270
268
"GHSA-5.json" : {ID : "GHSA-5" },
271
269
}))
272
270
})
273
- defer cleanupTestServer ()
274
271
275
272
db , err := local .NewZippedDB (testDir , "my-db" , ts .URL , false )
276
273
@@ -298,7 +295,7 @@ func TestNewZippedDB_Online_WithSameCache(t *testing.T) {
298
295
"GHSA-3.json" : {ID : "GHSA-3" },
299
296
})
300
297
301
- ts , cleanupTestServer := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
298
+ ts := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
302
299
if r .Method != http .MethodHead {
303
300
t .Errorf ("unexpected %s request" , r .Method )
304
301
}
@@ -307,7 +304,6 @@ func TestNewZippedDB_Online_WithSameCache(t *testing.T) {
307
304
308
305
_ , _ = w .Write (cache )
309
306
})
310
- defer cleanupTestServer ()
311
307
312
308
cacheWrite (t , determineStoredAtPath (testDir , "my-db" ), cache )
313
309
@@ -333,7 +329,7 @@ func TestNewZippedDB_Online_WithDifferentCache(t *testing.T) {
333
329
334
330
testDir := testutility .CreateTestDir (t )
335
331
336
- ts , cleanupTestServer := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
332
+ ts := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
337
333
_ , _ = writeOSVsZip (t , w , map [string ]models.Vulnerability {
338
334
"GHSA-1.json" : {ID : "GHSA-1" },
339
335
"GHSA-2.json" : {ID : "GHSA-2" },
@@ -342,7 +338,6 @@ func TestNewZippedDB_Online_WithDifferentCache(t *testing.T) {
342
338
"GHSA-5.json" : {ID : "GHSA-5" },
343
339
})
344
340
})
345
- defer cleanupTestServer ()
346
341
347
342
cacheWrite (t , determineStoredAtPath (testDir , "my-db" ), zipOSVs (t , map [string ]models.Vulnerability {
348
343
"GHSA-1.json" : {ID : "GHSA-1" },
@@ -364,7 +359,7 @@ func TestNewZippedDB_Online_WithCacheButNoHashHeader(t *testing.T) {
364
359
365
360
testDir := testutility .CreateTestDir (t )
366
361
367
- ts , cleanupTestServer := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
362
+ ts := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
368
363
_ , _ = w .Write (zipOSVs (t , map [string ]models.Vulnerability {
369
364
"GHSA-1.json" : {ID : "GHSA-1" },
370
365
"GHSA-2.json" : {ID : "GHSA-2" },
@@ -373,7 +368,6 @@ func TestNewZippedDB_Online_WithCacheButNoHashHeader(t *testing.T) {
373
368
"GHSA-5.json" : {ID : "GHSA-5" },
374
369
}))
375
370
})
376
- defer cleanupTestServer ()
377
371
378
372
cacheWrite (t , determineStoredAtPath (testDir , "my-db" ), zipOSVs (t , map [string ]models.Vulnerability {
379
373
"GHSA-1.json" : {ID : "GHSA-1" },
@@ -399,14 +393,13 @@ func TestNewZippedDB_Online_WithBadCache(t *testing.T) {
399
393
400
394
testDir := testutility .CreateTestDir (t )
401
395
402
- ts , cleanupTestServer := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
396
+ ts := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
403
397
_ , _ = writeOSVsZip (t , w , map [string ]models.Vulnerability {
404
398
"GHSA-1.json" : {ID : "GHSA-1" },
405
399
"GHSA-2.json" : {ID : "GHSA-2" },
406
400
"GHSA-3.json" : {ID : "GHSA-3" },
407
401
})
408
402
})
409
- defer cleanupTestServer ()
410
403
411
404
cacheWriteBad (t , determineStoredAtPath (testDir , "my-db" ), "this is not json!" )
412
405
@@ -426,7 +419,7 @@ func TestNewZippedDB_FileChecks(t *testing.T) {
426
419
427
420
testDir := testutility .CreateTestDir (t )
428
421
429
- ts , cleanupTestServer := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
422
+ ts := createZipServer (t , func (w http.ResponseWriter , r * http.Request ) {
430
423
_ , _ = writeOSVsZip (t , w , map [string ]models.Vulnerability {
431
424
"file.json" : {ID : "GHSA-1234" },
432
425
// only files with .json suffix should be loaded
@@ -435,7 +428,6 @@ func TestNewZippedDB_FileChecks(t *testing.T) {
435
428
"advisory-database-main/advisories/unreviewed/file.json" : {ID : "GHSA-4321" },
436
429
})
437
430
})
438
- defer cleanupTestServer ()
439
431
440
432
db , err := local .NewZippedDB (testDir , "my-db" , ts .URL , false )
441
433
0 commit comments