Skip to content

Commit

Permalink
test: use t.Cleanup instead of defer (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Mar 1, 2024
1 parent 62e1939 commit ac33f16
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 34 deletions.
18 changes: 9 additions & 9 deletions pkg/database/api-check_test.go
Expand Up @@ -93,7 +93,7 @@ func TestAPIDB_Check_NoPackages(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Errorf("an API request was made even though there are no packages to check")
}))
defer ts.Close()
t.Cleanup(ts.Close)

db, err := database.NewAPIDB(database.Config{URL: ts.URL}, false, 1)

Expand Down Expand Up @@ -151,7 +151,7 @@ func TestAPIDB_Check_NotOK(t *testing.T) {
})

ts := httptest.NewServer(mux)
defer ts.Close()
t.Cleanup(ts.Close)

db, err := database.NewAPIDB(database.Config{URL: ts.URL}, false, 1)

Expand Down Expand Up @@ -189,7 +189,7 @@ func TestAPIDB_Check_InvalidBody(t *testing.T) {
})

ts := httptest.NewServer(mux)
defer ts.Close()
t.Cleanup(ts.Close)

db, err := database.NewAPIDB(database.Config{URL: ts.URL}, false, 1)

Expand Down Expand Up @@ -233,7 +233,7 @@ func TestAPIDB_Check_UnbalancedResponse(t *testing.T) {
})

ts := httptest.NewServer(mux)
defer ts.Close()
t.Cleanup(ts.Close)

db, err := database.NewAPIDB(database.Config{URL: ts.URL}, false, 2)

Expand Down Expand Up @@ -284,7 +284,7 @@ func TestAPIDB_Check_FetchSuccessful(t *testing.T) {
})

ts := httptest.NewServer(mux)
defer ts.Close()
t.Cleanup(ts.Close)

db, err := database.NewAPIDB(database.Config{URL: ts.URL}, false, 1)

Expand Down Expand Up @@ -340,7 +340,7 @@ func TestAPIDB_Check_FetchFails(t *testing.T) {
})

ts := httptest.NewServer(mux)
defer ts.Close()
t.Cleanup(ts.Close)

db, err := database.NewAPIDB(database.Config{URL: ts.URL}, false, 1)

Expand Down Expand Up @@ -397,7 +397,7 @@ func TestAPIDB_Check_FetchMixed(t *testing.T) {
})

ts := httptest.NewServer(mux)
defer ts.Close()
t.Cleanup(ts.Close)

db, err := database.NewAPIDB(database.Config{URL: ts.URL}, false, 1)

Expand Down Expand Up @@ -439,7 +439,7 @@ func TestAPIDB_Check_WithCommit(t *testing.T) {
})

ts := httptest.NewServer(mux)
defer ts.Close()
t.Cleanup(ts.Close)

db, err := database.NewAPIDB(database.Config{URL: ts.URL}, false, 1)

Expand Down Expand Up @@ -509,7 +509,7 @@ func TestAPIDB_Check_Batches(t *testing.T) {
})

ts := httptest.NewServer(mux)
defer ts.Close()
t.Cleanup(ts.Close)

db, err := database.NewAPIDB(database.Config{URL: ts.URL}, false, 2)

Expand Down
40 changes: 15 additions & 25 deletions pkg/database/zip_test.go
Expand Up @@ -57,8 +57,6 @@ func expectDBToHaveOSVs(
}
}

type CleanUpZipServerFn = func()

func cachePath(url string) string {
hash := sha256.Sum256([]byte(url))
fileName := fmt.Sprintf("osv-detector-%x-db.json", hash)
Expand Down Expand Up @@ -92,16 +90,18 @@ func cacheWriteBad(t *testing.T, url string, contents string) {
}
}

func createZipServer(t *testing.T, handler http.HandlerFunc) (*httptest.Server, CleanUpZipServerFn) {
func createZipServer(t *testing.T, handler http.HandlerFunc) *httptest.Server {
t.Helper()

ts := httptest.NewServer(handler)

return ts, func() {
t.Cleanup(func() {
ts.Close()

_ = os.Remove(cachePath(ts.URL))
}
})

return ts
}

func zipOSVs(t *testing.T, osvs map[string]database.OSV) []byte {
Expand Down Expand Up @@ -136,10 +136,9 @@ func zipOSVs(t *testing.T, osvs map[string]database.OSV) []byte {
func TestNewZippedDB_Offline_WithoutCache(t *testing.T) {
t.Parallel()

ts, cleanup := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
t.Errorf("a server request was made when running offline")
})
defer cleanup()

_, err := database.NewZippedDB(database.Config{URL: ts.URL}, true)

Expand All @@ -160,10 +159,9 @@ func TestNewZippedDB_Offline_WithCache(t *testing.T) {
withDefaultAffected("GHSA-5"),
}

ts, cleanup := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
t.Errorf("a server request was made when running offline")
})
defer cleanup()

cacheWrite(t, database.Cache{
URL: ts.URL,
Expand Down Expand Up @@ -194,10 +192,9 @@ func TestNewZippedDB_Offline_WithCache(t *testing.T) {
func TestNewZippedDB_BadZip(t *testing.T) {
t.Parallel()

ts, cleanup := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("this is not a zip"))
})
defer cleanup()

_, err := database.NewZippedDB(database.Config{URL: ts.URL}, false)

Expand Down Expand Up @@ -227,7 +224,7 @@ func TestNewZippedDB_Online_WithoutCache(t *testing.T) {
withDefaultAffected("GHSA-5"),
}

ts, cleanup := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(zipOSVs(t, map[string]database.OSV{
"GHSA-1.json": withDefaultAffected("GHSA-1"),
"GHSA-2.json": withDefaultAffected("GHSA-2"),
Expand All @@ -236,7 +233,6 @@ func TestNewZippedDB_Online_WithoutCache(t *testing.T) {
"GHSA-5.json": withDefaultAffected("GHSA-5"),
}))
})
defer cleanup()

db, err := database.NewZippedDB(database.Config{URL: ts.URL}, false)

Expand All @@ -250,11 +246,10 @@ func TestNewZippedDB_Online_WithoutCache(t *testing.T) {
func TestNewZippedDB_Online_WithoutCache_NotFound(t *testing.T) {
t.Parallel()

ts, cleanup := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write(zipOSVs(t, map[string]database.OSV{}))
})
defer cleanup()

_, err := database.NewZippedDB(database.Config{URL: ts.URL}, false)

Expand All @@ -275,14 +270,13 @@ func TestNewZippedDB_Online_WithCache(t *testing.T) {
withDefaultAffected("GHSA-3"),
}

ts, cleanup := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
if dateHeader := r.Header.Get("If-Modified-Since"); dateHeader != date {
t.Errorf("incorrect Date header: got = \"%s\", want = \"%s\"", dateHeader, date)
}

w.WriteHeader(http.StatusNotModified)
})
defer cleanup()

cacheWrite(t, database.Cache{
URL: ts.URL,
Expand Down Expand Up @@ -320,7 +314,7 @@ func TestNewZippedDB_Online_WithOldCache(t *testing.T) {
withDefaultAffected("GHSA-5"),
}

ts, cleanup := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
if dateHeader := r.Header.Get("If-Modified-Since"); dateHeader != date {
t.Errorf("incorrect Date header: got = \"%s\", want = \"%s\"", dateHeader, date)
}
Expand All @@ -334,7 +328,6 @@ func TestNewZippedDB_Online_WithOldCache(t *testing.T) {
"GHSA-5.json": withDefaultAffected("GHSA-5"),
}))
})
defer cleanup()

cacheWrite(t, database.Cache{
URL: ts.URL,
Expand Down Expand Up @@ -369,14 +362,13 @@ func TestNewZippedDB_Online_WithBadCache(t *testing.T) {
withDefaultAffected("GHSA-3"),
}

ts, cleanup := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(zipOSVs(t, map[string]database.OSV{
"GHSA-1.json": withDefaultAffected("GHSA-1"),
"GHSA-2.json": withDefaultAffected("GHSA-2"),
"GHSA-3.json": withDefaultAffected("GHSA-3"),
}))
})
defer cleanup()

cacheWriteBad(t, ts.URL, "this is not json!")

Expand All @@ -394,7 +386,7 @@ func TestNewZippedDB_FileChecks(t *testing.T) {

osvs := []database.OSV{withDefaultAffected("GHSA-1234"), withDefaultAffected("GHSA-4321")}

ts, cleanup := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(zipOSVs(t, map[string]database.OSV{
"file.json": withDefaultAffected("GHSA-1234"),
// only files with .json suffix should be loaded
Expand All @@ -403,7 +395,6 @@ func TestNewZippedDB_FileChecks(t *testing.T) {
"advisory-database-main/advisories/unreviewed/file.json": withDefaultAffected("GHSA-4321"),
}))
})
defer cleanup()

db, err := database.NewZippedDB(database.Config{URL: ts.URL}, false)

Expand All @@ -419,14 +410,13 @@ func TestNewZippedDB_WorkingDirectory(t *testing.T) {

osvs := []database.OSV{withDefaultAffected("GHSA-1234"), withDefaultAffected("GHSA-5678")}

ts, cleanup := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(zipOSVs(t, map[string]database.OSV{
"reviewed/file.json": withDefaultAffected("GHSA-1234"),
"reviewed/nested/file.json": withDefaultAffected("GHSA-5678"),
"unreviewed/file.json": withDefaultAffected("GHSA-4321"),
}))
})
defer cleanup()

db, err := database.NewZippedDB(database.Config{URL: ts.URL, WorkingDirectory: "reviewed"}, false)

Expand Down

0 comments on commit ac33f16

Please sign in to comment.