Skip to content

Commit

Permalink
chore: call t.Helper in helper methods, and use require.NoError inste…
Browse files Browse the repository at this point in the history
…ad of panicking
  • Loading branch information
thomasschafer committed Feb 7, 2025
1 parent 85eb8ad commit eca7b53
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions lib/ecosystems/enrich_spdx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package ecosystems
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"testing"

Expand All @@ -33,32 +32,29 @@ import (
"github.com/snyk/parlay/lib/sbom"
)

func parseJson(jsonStr string) map[string]any {
func parseJson(t *testing.T, jsonStr string) map[string]any {
t.Helper()
var result map[string]any

err := json.Unmarshal([]byte(jsonStr), &result)
if err != nil {
panic(fmt.Errorf("failed to parse JSON: %w", err))
}

require.NoError(t, json.Unmarshal([]byte(jsonStr), &result))
return result
}

func setupHttpmock(packageVersionsResponse, packageResponse *string) {
func setupHttpmock(t *testing.T, packageVersionsResponse, packageResponse *string) {
t.Helper()
httpmock.Activate()

if packageVersionsResponse != nil {
httpmock.RegisterResponder("GET", `=~^https://packages.ecosyste.ms/api/v1/registries/.*/packages/.*/versions`,
func(r *http.Request) (*http.Response, error) {
return httpmock.NewJsonResponse(200, parseJson(*packageVersionsResponse))
return httpmock.NewJsonResponse(200, parseJson(t, *packageVersionsResponse))
},
)
}

if packageResponse != nil {
httpmock.RegisterResponder("GET", `=~^https://packages.ecosyste.ms/api/v1/registries`,
func(req *http.Request) (*http.Response, error) {
return httpmock.NewJsonResponse(200, parseJson(*packageResponse))
return httpmock.NewJsonResponse(200, parseJson(t, *packageResponse))
})
}
}
Expand Down

0 comments on commit eca7b53

Please sign in to comment.