Skip to content

Commit f999707

Browse files
authored
test: automatically cleanup test zip server (#834)
When doing #832 I leaned `t.Cleanup` is a thing so let's use that instead here
1 parent 0e3a11b commit f999707

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

internal/local/zip_test.go

+14-22
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ func cacheWriteBad(t *testing.T, storedAt string, contents string) {
7474
}
7575
}
7676

77-
func createZipServer(t *testing.T, handler http.HandlerFunc) (*httptest.Server, func()) {
77+
func createZipServer(t *testing.T, handler http.HandlerFunc) *httptest.Server {
7878
t.Helper()
7979

8080
ts := httptest.NewServer(handler)
8181

82-
return ts, ts.Close
82+
t.Cleanup(ts.Close)
83+
84+
return ts
8385
}
8486

8587
func computeCRC32CHash(t *testing.T, data []byte) string {
@@ -139,10 +141,9 @@ func TestNewZippedDB_Offline_WithoutCache(t *testing.T) {
139141

140142
testDir := testutility.CreateTestDir(t)
141143

142-
ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
144+
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
143145
t.Errorf("a server request was made when running offline")
144146
})
145-
defer cleanupTestServer()
146147

147148
_, err := local.NewZippedDB(testDir, "my-db", ts.URL, true)
148149

@@ -164,10 +165,9 @@ func TestNewZippedDB_Offline_WithCache(t *testing.T) {
164165

165166
testDir := testutility.CreateTestDir(t)
166167

167-
ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
168+
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
168169
t.Errorf("a server request was made when running offline")
169170
})
170-
defer cleanupTestServer()
171171

172172
cacheWrite(t, determineStoredAtPath(testDir, "my-db"), zipOSVs(t, map[string]models.Vulnerability{
173173
"GHSA-1.json": {ID: "GHSA-1"},
@@ -191,10 +191,9 @@ func TestNewZippedDB_BadZip(t *testing.T) {
191191

192192
testDir := testutility.CreateTestDir(t)
193193

194-
ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
194+
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
195195
_, _ = w.Write([]byte("this is not a zip"))
196196
})
197-
defer cleanupTestServer()
198197

199198
_, err := local.NewZippedDB(testDir, "my-db", ts.URL, false)
200199

@@ -228,7 +227,7 @@ func TestNewZippedDB_Online_WithoutCache(t *testing.T) {
228227

229228
testDir := testutility.CreateTestDir(t)
230229

231-
ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
230+
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
232231
_, _ = writeOSVsZip(t, w, map[string]models.Vulnerability{
233232
"GHSA-1.json": {ID: "GHSA-1"},
234233
"GHSA-2.json": {ID: "GHSA-2"},
@@ -237,7 +236,6 @@ func TestNewZippedDB_Online_WithoutCache(t *testing.T) {
237236
"GHSA-5.json": {ID: "GHSA-5"},
238237
})
239238
})
240-
defer cleanupTestServer()
241239

242240
db, err := local.NewZippedDB(testDir, "my-db", ts.URL, false)
243241

@@ -261,7 +259,7 @@ func TestNewZippedDB_Online_WithoutCacheAndNoHashHeader(t *testing.T) {
261259

262260
testDir := testutility.CreateTestDir(t)
263261

264-
ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
262+
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
265263
_, _ = w.Write(zipOSVs(t, map[string]models.Vulnerability{
266264
"GHSA-1.json": {ID: "GHSA-1"},
267265
"GHSA-2.json": {ID: "GHSA-2"},
@@ -270,7 +268,6 @@ func TestNewZippedDB_Online_WithoutCacheAndNoHashHeader(t *testing.T) {
270268
"GHSA-5.json": {ID: "GHSA-5"},
271269
}))
272270
})
273-
defer cleanupTestServer()
274271

275272
db, err := local.NewZippedDB(testDir, "my-db", ts.URL, false)
276273

@@ -298,7 +295,7 @@ func TestNewZippedDB_Online_WithSameCache(t *testing.T) {
298295
"GHSA-3.json": {ID: "GHSA-3"},
299296
})
300297

301-
ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
298+
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
302299
if r.Method != http.MethodHead {
303300
t.Errorf("unexpected %s request", r.Method)
304301
}
@@ -307,7 +304,6 @@ func TestNewZippedDB_Online_WithSameCache(t *testing.T) {
307304

308305
_, _ = w.Write(cache)
309306
})
310-
defer cleanupTestServer()
311307

312308
cacheWrite(t, determineStoredAtPath(testDir, "my-db"), cache)
313309

@@ -333,7 +329,7 @@ func TestNewZippedDB_Online_WithDifferentCache(t *testing.T) {
333329

334330
testDir := testutility.CreateTestDir(t)
335331

336-
ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
332+
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
337333
_, _ = writeOSVsZip(t, w, map[string]models.Vulnerability{
338334
"GHSA-1.json": {ID: "GHSA-1"},
339335
"GHSA-2.json": {ID: "GHSA-2"},
@@ -342,7 +338,6 @@ func TestNewZippedDB_Online_WithDifferentCache(t *testing.T) {
342338
"GHSA-5.json": {ID: "GHSA-5"},
343339
})
344340
})
345-
defer cleanupTestServer()
346341

347342
cacheWrite(t, determineStoredAtPath(testDir, "my-db"), zipOSVs(t, map[string]models.Vulnerability{
348343
"GHSA-1.json": {ID: "GHSA-1"},
@@ -364,7 +359,7 @@ func TestNewZippedDB_Online_WithCacheButNoHashHeader(t *testing.T) {
364359

365360
testDir := testutility.CreateTestDir(t)
366361

367-
ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
362+
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
368363
_, _ = w.Write(zipOSVs(t, map[string]models.Vulnerability{
369364
"GHSA-1.json": {ID: "GHSA-1"},
370365
"GHSA-2.json": {ID: "GHSA-2"},
@@ -373,7 +368,6 @@ func TestNewZippedDB_Online_WithCacheButNoHashHeader(t *testing.T) {
373368
"GHSA-5.json": {ID: "GHSA-5"},
374369
}))
375370
})
376-
defer cleanupTestServer()
377371

378372
cacheWrite(t, determineStoredAtPath(testDir, "my-db"), zipOSVs(t, map[string]models.Vulnerability{
379373
"GHSA-1.json": {ID: "GHSA-1"},
@@ -399,14 +393,13 @@ func TestNewZippedDB_Online_WithBadCache(t *testing.T) {
399393

400394
testDir := testutility.CreateTestDir(t)
401395

402-
ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
396+
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
403397
_, _ = writeOSVsZip(t, w, map[string]models.Vulnerability{
404398
"GHSA-1.json": {ID: "GHSA-1"},
405399
"GHSA-2.json": {ID: "GHSA-2"},
406400
"GHSA-3.json": {ID: "GHSA-3"},
407401
})
408402
})
409-
defer cleanupTestServer()
410403

411404
cacheWriteBad(t, determineStoredAtPath(testDir, "my-db"), "this is not json!")
412405

@@ -426,7 +419,7 @@ func TestNewZippedDB_FileChecks(t *testing.T) {
426419

427420
testDir := testutility.CreateTestDir(t)
428421

429-
ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
422+
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
430423
_, _ = writeOSVsZip(t, w, map[string]models.Vulnerability{
431424
"file.json": {ID: "GHSA-1234"},
432425
// only files with .json suffix should be loaded
@@ -435,7 +428,6 @@ func TestNewZippedDB_FileChecks(t *testing.T) {
435428
"advisory-database-main/advisories/unreviewed/file.json": {ID: "GHSA-4321"},
436429
})
437430
})
438-
defer cleanupTestServer()
439431

440432
db, err := local.NewZippedDB(testDir, "my-db", ts.URL, false)
441433

0 commit comments

Comments
 (0)