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 a0e16d0
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 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 All @@ -77,7 +73,7 @@ func TestEnrichSBOM_SPDX(t *testing.T) {
}
}
}`
setupHttpmock(&packageVersionResponse, &packageResponse)
setupHttpmock(t, &packageVersionResponse, &packageResponse)
defer httpmock.DeactivateAndReset()

doc, err := sbom.DecodeSBOMDocument([]byte(`{"spdxVersion":"SPDX-2.3","SPDXID":"SPDXRef-DOCUMENT"}`))
Expand Down Expand Up @@ -134,7 +130,7 @@ func TestEnrichSBOM_MissingVersionedLicense(t *testing.T) {
}
}
}`
setupHttpmock(&packageVersionResponse, &packageResponse)
setupHttpmock(t, &packageVersionResponse, &packageResponse)
defer httpmock.DeactivateAndReset()

doc, err := sbom.DecodeSBOMDocument([]byte(`{"spdxVersion":"SPDX-2.3","SPDXID":"SPDXRef-DOCUMENT"}`))
Expand Down Expand Up @@ -188,7 +184,7 @@ func TestEnrichSBOM_SPDX_NoSupplierName(t *testing.T) {
}
}
}`
setupHttpmock(nil, &packageResponse)
setupHttpmock(t, nil, &packageResponse)
defer httpmock.DeactivateAndReset()

doc, err := sbom.DecodeSBOMDocument([]byte(`{"spdxVersion":"SPDX-2.3","SPDXID":"SPDXRef-DOCUMENT"}`))
Expand Down

0 comments on commit a0e16d0

Please sign in to comment.