Skip to content

Commit 0c50fa3

Browse files
committed
Address review comments
Signed-off-by: Keshav Priyadarshi <[email protected]>
1 parent fd0a06d commit 0c50fa3

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

packageurl_test.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -139,22 +139,27 @@ func (cop *ComponentsOrPurl) UnmarshalJSON(data []byte) error {
139139
}
140140

141141
type TestFixture struct {
142-
Description string `json:"description"`
143-
TestGroup string `json:"test_group"`
144-
TestType string `json:"test_type"`
145-
Input ComponentsOrPurl `json:"input"`
146-
ExpectedFailure bool `json:"expected_failure"`
147-
ExpectedOutput ComponentsOrPurl `json:"expected_output"`
148-
ExpectedFailureMsg *string `json:"expected_failure_reason"`
142+
Description string `json:"description"`
143+
TestGroup string `json:"test_group"`
144+
TestType string `json:"test_type"`
145+
Input ComponentsOrPurl `json:"input"`
146+
ExpectedFailure bool `json:"expected_failure"`
147+
ExpectedOutput ComponentsOrPurl `json:"expected_output"`
148+
ExpectedFailureReason *string `json:"expected_failure_reason"`
149149
}
150150

151151
type TestSuite struct {
152152
Schema string `json:"$schema"`
153153
Tests []TestFixture `json:"tests"`
154154
}
155155

156-
func readJSONFilesFromDir(dirPath string) ([][]byte, error) {
157-
var result [][]byte
156+
type jsonFile struct {
157+
name string
158+
content []byte
159+
}
160+
161+
func readJSONFilesFromDir(dirPath string) ([]jsonFile, error) {
162+
var result []jsonFile
158163

159164
entries, err := os.ReadDir(dirPath)
160165
if err != nil {
@@ -172,7 +177,7 @@ func readJSONFilesFromDir(dirPath string) ([][]byte, error) {
172177
return nil, fmt.Errorf("reading file %s: %w", fullPath, err)
173178
}
174179

175-
result = append(result, data)
180+
result = append(result, jsonFile{name: entry.Name(), content: data})
176181
}
177182

178183
return result, nil
@@ -266,14 +271,18 @@ func buildTest(tc TestFixture, t *testing.T) {
266271
result := instance.ToString()
267272
canonicalExpectedPurl := tc.ExpectedOutput.Purl
268273

269-
if tc.ExpectedFailure == false {
274+
if tc.ExpectedFailure {
275+
// String()/ToString() signature won't error so the only reasonable thing is to check this here.
276+
err := instance.Normalize()
277+
if err == nil {
278+
t.Logf("'%s' did not fail for %#v", tc.Description, instance)
279+
t.Fail()
280+
}
281+
} else {
270282
if result != *canonicalExpectedPurl {
271283
t.Logf("%s: '%s' test failed: wanted: '%s', got '%s'", tc.Description, tc.TestType, *canonicalExpectedPurl, result)
272284
t.Fail()
273285
}
274-
} else {
275-
t.Logf("%s did not fail and returned %#v", tc.Description, instance)
276-
t.Fail()
277286
}
278287

279288
}
@@ -284,15 +293,16 @@ func TestPurlSpecFixtures(t *testing.T) {
284293
t.Fatal(err)
285294
}
286295

287-
for _, data := range testFiles {
296+
for _, file := range testFiles {
288297
var suite TestSuite
289-
err := json.Unmarshal(data, &suite)
298+
err := json.Unmarshal(file.content, &suite)
290299
if err != nil {
291300
t.Fatal(err)
292301
}
293302

294-
for _, tc := range suite.Tests {
295-
t.Run(tc.TestType, func(t *testing.T) {
303+
for idx, tc := range suite.Tests {
304+
testName := fmt.Sprintf("%s[%d]%s", file.name, (idx + 1), tc.TestType)
305+
t.Run(testName, func(t *testing.T) {
296306
testType := tc.TestType
297307

298308
switch testType {

0 commit comments

Comments
 (0)