aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrandon Lum <lumjjb@gmail.com>2023-05-26 13:33:17 -0400
committerGitHub <noreply@github.com>2023-05-26 13:33:17 -0400
commite939624e362e42cbd362b1d4ecaf1167a42d4901 (patch)
treeac3d4b230df14f86500e3dc782452b87c59eca67
parent00864c01536765d8491e81d284a615a4023902e0 (diff)
parentfb0f8d8b593f51a2a60014d48a02c1b0d20d3b34 (diff)
downloadspdx-tools-e939624e362e42cbd362b1d4ecaf1167a42d4901.tar.gz
Merge pull request #218 from spdx/fix-shorthand-dupls
Ensure no duplicates in relationships when shortcut fields are used.
-rw-r--r--spdx/v2/v2_2/document.go30
-rw-r--r--spdx/v2/v2_2/example/example.go10
-rw-r--r--spdx/v2/v2_2/json/json_test.go177
-rw-r--r--spdx/v2/v2_2/yaml/yaml_test.go44
-rw-r--r--spdx/v2/v2_3/document.go29
-rw-r--r--spdx/v2/v2_3/json/json_test.go147
-rw-r--r--spdx/v2/v2_3/yaml/yaml_test.go15
7 files changed, 365 insertions, 87 deletions
diff --git a/spdx/v2/v2_2/document.go b/spdx/v2/v2_2/document.go
index 3bd605f..d94f5b0 100644
--- a/spdx/v2/v2_2/document.go
+++ b/spdx/v2/v2_2/document.go
@@ -5,8 +5,9 @@ package v2_2
import (
"encoding/json"
+ "fmt"
- "github.com/anchore/go-struct-converter"
+ converter "github.com/anchore/go-struct-converter"
"github.com/spdx/tools-golang/spdx/v2/common"
)
@@ -98,27 +99,46 @@ func (d *Document) UnmarshalJSON(b []byte) error {
*d = Document(d2)
+ relationshipExists := map[string]bool{}
+ serializeRel := func(r *Relationship) string {
+ return fmt.Sprintf("%v-%v->%v", common.RenderDocElementID(r.RefA), r.Relationship, common.RenderDocElementID(r.RefB))
+ }
+
+ // index current list of relationships to ensure no duplication
+ for _, r := range d.Relationships {
+ relationshipExists[serializeRel(r)] = true
+ }
+
// build relationships for documentDescribes field
for _, id := range e.DocumentDescribes {
- d.Relationships = append(d.Relationships, &Relationship{
+ r := &Relationship{
RefA: common.DocElementID{
ElementRefID: d.SPDXIdentifier,
},
RefB: id,
Relationship: common.TypeRelationshipDescribe,
- })
+ }
+
+ if !relationshipExists[serializeRel(r)] {
+ d.Relationships = append(d.Relationships, r)
+ relationshipExists[serializeRel(r)] = true
+ }
}
// build relationships for package hasFiles field
for _, p := range d.Packages {
for _, f := range p.hasFiles {
- d.Relationships = append(d.Relationships, &Relationship{
+ r := &Relationship{
RefA: common.DocElementID{
ElementRefID: p.PackageSPDXIdentifier,
},
RefB: f,
Relationship: common.TypeRelationshipContains,
- })
+ }
+ if !relationshipExists[serializeRel(r)] {
+ d.Relationships = append(d.Relationships, r)
+ relationshipExists[serializeRel(r)] = true
+ }
}
p.hasFiles = nil
diff --git a/spdx/v2/v2_2/example/example.go b/spdx/v2/v2_2/example/example.go
index 010ed73..501eb77 100644
--- a/spdx/v2/v2_2/example/example.go
+++ b/spdx/v2/v2_2/example/example.go
@@ -374,6 +374,16 @@ var example = spdx.Document{
Relationship: "CONTAINS",
},
{
+ RefA: common.MakeDocElementID("", "Package"),
+ RefB: common.MakeDocElementID("", "CommonsLangSrc"),
+ Relationship: "CONTAINS",
+ },
+ {
+ RefA: common.MakeDocElementID("", "Package"),
+ RefB: common.MakeDocElementID("", "DoapSource"),
+ Relationship: "CONTAINS",
+ },
+ {
RefA: common.MakeDocElementID("", "DOCUMENT"),
RefB: common.MakeDocElementID("spdx-tool-1.2", "ToolsElement"),
Relationship: "COPY_OF",
diff --git a/spdx/v2/v2_2/json/json_test.go b/spdx/v2/v2_2/json/json_test.go
index d624627..a4a0275 100644
--- a/spdx/v2/v2_2/json/json_test.go
+++ b/spdx/v2/v2_2/json/json_test.go
@@ -4,6 +4,7 @@ package json
import (
"bytes"
+ jsonenc "encoding/json"
"fmt"
"os"
"strings"
@@ -21,35 +22,6 @@ import (
func TestLoad(t *testing.T) {
want := example.Copy()
-
- want.Relationships = append(want.Relationships, []*spdx.Relationship{
- {
- RefA: common.DocElementID{ElementRefID: "DOCUMENT"},
- RefB: common.DocElementID{ElementRefID: "File"},
- Relationship: "DESCRIBES",
- },
- {
- RefA: common.DocElementID{ElementRefID: "DOCUMENT"},
- RefB: common.DocElementID{ElementRefID: "Package"},
- Relationship: "DESCRIBES",
- },
- {
- RefA: common.DocElementID{ElementRefID: "Package"},
- RefB: common.DocElementID{ElementRefID: "CommonsLangSrc"},
- Relationship: "CONTAINS",
- },
- {
- RefA: common.DocElementID{ElementRefID: "Package"},
- RefB: common.DocElementID{ElementRefID: "JenaLib"},
- Relationship: "CONTAINS",
- },
- {
- RefA: common.DocElementID{ElementRefID: "Package"},
- RefB: common.DocElementID{ElementRefID: "DoapSource"},
- Relationship: "CONTAINS",
- },
- }...)
-
file, err := os.Open("../../../../examples/sample-docs/json/SPDXJSONExample-v2.2.spdx.json")
if err != nil {
panic(fmt.Errorf("error opening File: %s", err))
@@ -62,8 +34,8 @@ func TestLoad(t *testing.T) {
return
}
- if !cmp.Equal(want, got, cmpopts.IgnoreUnexported(spdx.Package{})) {
- t.Errorf("got incorrect struct after parsing YAML example: %s", cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{})))
+ if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{}), cmpopts.SortSlices(relationshipLess)); len(diff) > 0 {
+ t.Errorf("got incorrect struct after parsing JSON example: %s", diff)
return
}
}
@@ -91,8 +63,8 @@ func Test_Write(t *testing.T) {
return
}
- if !cmp.Equal(want, got, cmpopts.IgnoreUnexported(spdx.Package{})) {
- t.Errorf("got incorrect struct after writing and re-parsing JSON example: %s", cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{})))
+ if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{}), cmpopts.SortSlices(relationshipLess)); len(diff) > 0 {
+ t.Errorf("got incorrect struct after writing and re-parsing JSON example: %s", diff)
return
}
}
@@ -149,7 +121,7 @@ func Test_ShorthandFields(t *testing.T) {
}
}
- require.Equal(t, spdx.Document{
+ want := spdx.Document{
SPDXVersion: spdx.Version,
DataLicense: spdx.DataLicense,
SPDXIdentifier: "DOCUMENT",
@@ -200,7 +172,136 @@ func Test_ShorthandFields(t *testing.T) {
Relationship: common.TypeRelationshipContains,
},
},
- }, doc)
+ }
+
+ if diff := cmp.Diff(want, doc, cmpopts.IgnoreUnexported(spdx.Package{}), cmpopts.SortSlices(relationshipLess)); len(diff) > 0 {
+ t.Errorf("got incorrect struct after parsing JSON example: %s", diff)
+ return
+ }
+
+}
+
+func Test_ShorthandFieldsNoDuplicates(t *testing.T) {
+ contents := `{
+ "spdxVersion": "SPDX-2.2",
+ "dataLicense": "CC0-1.0",
+ "SPDXID": "SPDXRef-DOCUMENT",
+ "name": "SPDX-Tools-v2.0",
+ "documentDescribes": [
+ "SPDXRef-Container"
+ ],
+ "packages": [
+ {
+ "name": "Container",
+ "SPDXID": "SPDXRef-Container"
+ },
+ {
+ "name": "Package-1",
+ "SPDXID": "SPDXRef-Package-1",
+ "versionInfo": "1.1.1",
+ "hasFiles": [
+ "SPDXRef-File-1",
+ "SPDXRef-File-2"
+ ]
+ },
+ {
+ "name": "Package-2",
+ "SPDXID": "SPDXRef-Package-2",
+ "versionInfo": "2.2.2"
+ }
+ ],
+ "files": [
+ {
+ "fileName": "./f1",
+ "SPDXID": "SPDXRef-File-1"
+ },
+ {
+ "fileName": "./f2",
+ "SPDXID": "SPDXRef-File-2"
+ }
+ ],
+ "relationships": [
+ {
+ "spdxElementId": "SPDXRef-Package-1",
+ "relationshipType": "CONTAINS",
+ "relatedSpdxElement": "SPDXRef-File-1"
+ },
+ {
+ "spdxElementId": "SPDXRef-Package-1",
+ "relationshipType": "CONTAINS",
+ "relatedSpdxElement": "SPDXRef-File-2"
+ }
+ ]
+ }`
+
+ doc := spdx.Document{}
+ err := json.ReadInto(strings.NewReader(contents), &doc)
+
+ require.NoError(t, err)
+
+ id := func(s string) common.DocElementID {
+ return common.DocElementID{
+ ElementRefID: common.ElementID(s),
+ }
+ }
+
+ want := spdx.Document{
+ SPDXVersion: spdx.Version,
+ DataLicense: spdx.DataLicense,
+ SPDXIdentifier: "DOCUMENT",
+ DocumentName: "SPDX-Tools-v2.0",
+ Packages: []*spdx.Package{
+ {
+ PackageName: "Container",
+ PackageSPDXIdentifier: "Container",
+ FilesAnalyzed: true,
+ },
+ {
+ PackageName: "Package-1",
+ PackageSPDXIdentifier: "Package-1",
+ PackageVersion: "1.1.1",
+ FilesAnalyzed: true,
+ },
+ {
+ PackageName: "Package-2",
+ PackageSPDXIdentifier: "Package-2",
+ PackageVersion: "2.2.2",
+ FilesAnalyzed: true,
+ },
+ },
+ Files: []*spdx.File{
+ {
+ FileName: "./f1",
+ FileSPDXIdentifier: "File-1",
+ },
+ {
+ FileName: "./f2",
+ FileSPDXIdentifier: "File-2",
+ },
+ },
+ Relationships: []*spdx.Relationship{
+ {
+ RefA: id("DOCUMENT"),
+ RefB: id("Container"),
+ Relationship: common.TypeRelationshipDescribe,
+ },
+ {
+ RefA: id("Package-1"),
+ RefB: id("File-1"),
+ Relationship: common.TypeRelationshipContains,
+ },
+ {
+ RefA: id("Package-1"),
+ RefB: id("File-2"),
+ Relationship: common.TypeRelationshipContains,
+ },
+ },
+ }
+
+ if diff := cmp.Diff(want, doc, cmpopts.IgnoreUnexported(spdx.Package{}), cmpopts.SortSlices(relationshipLess)); len(diff) > 0 {
+ t.Errorf("got incorrect struct after parsing JSON example: %s", diff)
+ return
+ }
}
func Test_JsonEnums(t *testing.T) {
@@ -344,3 +445,9 @@ func Test_JsonEnums(t *testing.T) {
},
}, doc)
}
+
+func relationshipLess(a, b *spdx.Relationship) bool {
+ aStr, _ := jsonenc.Marshal(a)
+ bStr, _ := jsonenc.Marshal(b)
+ return string(aStr) < string(bStr)
+}
diff --git a/spdx/v2/v2_2/yaml/yaml_test.go b/spdx/v2/v2_2/yaml/yaml_test.go
index bacfc80..404a261 100644
--- a/spdx/v2/v2_2/yaml/yaml_test.go
+++ b/spdx/v2/v2_2/yaml/yaml_test.go
@@ -4,6 +4,7 @@ package yaml
import (
"bytes"
+ jsonenc "encoding/json"
"fmt"
"os"
"testing"
@@ -11,7 +12,6 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
- "github.com/spdx/tools-golang/spdx/v2/common"
spdx "github.com/spdx/tools-golang/spdx/v2/v2_2"
"github.com/spdx/tools-golang/spdx/v2/v2_2/example"
"github.com/spdx/tools-golang/yaml"
@@ -20,34 +20,6 @@ import (
func Test_Read(t *testing.T) {
want := example.Copy()
- want.Relationships = append(want.Relationships, []*spdx.Relationship{
- {
- RefA: common.DocElementID{ElementRefID: "DOCUMENT"},
- RefB: common.DocElementID{ElementRefID: "File"},
- Relationship: "DESCRIBES",
- },
- {
- RefA: common.DocElementID{ElementRefID: "DOCUMENT"},
- RefB: common.DocElementID{ElementRefID: "Package"},
- Relationship: "DESCRIBES",
- },
- {
- RefA: common.DocElementID{ElementRefID: "Package"},
- RefB: common.DocElementID{ElementRefID: "CommonsLangSrc"},
- Relationship: "CONTAINS",
- },
- {
- RefA: common.DocElementID{ElementRefID: "Package"},
- RefB: common.DocElementID{ElementRefID: "JenaLib"},
- Relationship: "CONTAINS",
- },
- {
- RefA: common.DocElementID{ElementRefID: "Package"},
- RefB: common.DocElementID{ElementRefID: "DoapSource"},
- Relationship: "CONTAINS",
- },
- }...)
-
file, err := os.Open("../../../../examples/sample-docs/yaml/SPDXYAMLExample-2.2.spdx.yaml")
if err != nil {
panic(fmt.Errorf("error opening File: %s", err))
@@ -60,8 +32,8 @@ func Test_Read(t *testing.T) {
return
}
- if !cmp.Equal(want, got, cmpopts.IgnoreUnexported(spdx.Package{})) {
- t.Errorf("got incorrect struct after parsing YAML example: %s", cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{})))
+ if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{}), cmpopts.SortSlices(relationshipLess)); len(diff) > 0 {
+ t.Errorf("got incorrect struct after parsing YAML example: %s", diff)
return
}
}
@@ -88,8 +60,14 @@ func Test_Write(t *testing.T) {
return
}
- if !cmp.Equal(want, got, cmpopts.IgnoreUnexported(spdx.Package{})) {
- t.Errorf("got incorrect struct after writing and re-parsing YAML example: %s", cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{})))
+ if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{}), cmpopts.SortSlices(relationshipLess)); len(diff) > 0 {
+ t.Errorf("got incorrect struct after writing and re-parsing YAML example: %s", diff)
return
}
}
+
+func relationshipLess(a, b *spdx.Relationship) bool {
+ aStr, _ := jsonenc.Marshal(a)
+ bStr, _ := jsonenc.Marshal(b)
+ return string(aStr) < string(bStr)
+}
diff --git a/spdx/v2/v2_3/document.go b/spdx/v2/v2_3/document.go
index 9dbb809..279e976 100644
--- a/spdx/v2/v2_3/document.go
+++ b/spdx/v2/v2_3/document.go
@@ -5,6 +5,7 @@ package v2_3
import (
"encoding/json"
+ "fmt"
converter "github.com/anchore/go-struct-converter"
@@ -97,27 +98,47 @@ func (d *Document) UnmarshalJSON(b []byte) error {
*d = Document(d2)
+ relationshipExists := map[string]bool{}
+ serializeRel := func(r *Relationship) string {
+ return fmt.Sprintf("%v-%v->%v", common.RenderDocElementID(r.RefA), r.Relationship, common.RenderDocElementID(r.RefB))
+ }
+
+ // index current list of relationships to ensure no duplication
+ for _, r := range d.Relationships {
+ relationshipExists[serializeRel(r)] = true
+ }
+
// build relationships for documentDescribes field
for _, id := range e.DocumentDescribes {
- d.Relationships = append(d.Relationships, &Relationship{
+ r := &Relationship{
RefA: common.DocElementID{
ElementRefID: d.SPDXIdentifier,
},
RefB: id,
Relationship: common.TypeRelationshipDescribe,
- })
+ }
+
+ if !relationshipExists[serializeRel(r)] {
+ d.Relationships = append(d.Relationships, r)
+ relationshipExists[serializeRel(r)] = true
+ }
}
// build relationships for package hasFiles field
+ // build relationships for package hasFiles field
for _, p := range d.Packages {
for _, f := range p.hasFiles {
- d.Relationships = append(d.Relationships, &Relationship{
+ r := &Relationship{
RefA: common.DocElementID{
ElementRefID: p.PackageSPDXIdentifier,
},
RefB: f,
Relationship: common.TypeRelationshipContains,
- })
+ }
+ if !relationshipExists[serializeRel(r)] {
+ d.Relationships = append(d.Relationships, r)
+ relationshipExists[serializeRel(r)] = true
+ }
}
p.hasFiles = nil
diff --git a/spdx/v2/v2_3/json/json_test.go b/spdx/v2/v2_3/json/json_test.go
index 6980143..ea648df 100644
--- a/spdx/v2/v2_3/json/json_test.go
+++ b/spdx/v2/v2_3/json/json_test.go
@@ -4,6 +4,7 @@ package json
import (
"bytes"
+ jsonenc "encoding/json"
"flag"
"fmt"
"os"
@@ -52,8 +53,8 @@ func Test_Read(t *testing.T) {
return
}
- if !cmp.Equal(want, got, cmpopts.IgnoreUnexported(spdx.Package{})) {
- t.Errorf("got incorrect struct after parsing YAML example: %s", cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{})))
+ if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{}), cmpopts.SortSlices(relationshipLess)); len(diff) > 0 {
+ t.Errorf("got incorrect struct after parsing JSON example: %s", diff)
return
}
}
@@ -81,8 +82,8 @@ func Test_Write(t *testing.T) {
return
}
- if !cmp.Equal(want, got, cmpopts.IgnoreUnexported(spdx.Package{})) {
- t.Errorf("got incorrect struct after writing and re-parsing JSON example: %s", cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{})))
+ if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{}), cmpopts.SortSlices(relationshipLess)); len(diff) > 0 {
+ t.Errorf("got incorrect struct after parsing JSON example: %s", diff)
return
}
}
@@ -139,7 +140,7 @@ func Test_ShorthandFields(t *testing.T) {
}
}
- require.Equal(t, spdx.Document{
+ want := spdx.Document{
SPDXVersion: spdx.Version,
DataLicense: spdx.DataLicense,
SPDXIdentifier: "DOCUMENT",
@@ -190,7 +191,135 @@ func Test_ShorthandFields(t *testing.T) {
Relationship: common.TypeRelationshipContains,
},
},
- }, doc)
+ }
+
+ if diff := cmp.Diff(want, doc, cmpopts.IgnoreUnexported(spdx.Package{}), cmpopts.SortSlices(relationshipLess)); len(diff) > 0 {
+ t.Errorf("got incorrect struct after parsing JSON example: %s", diff)
+ return
+ }
+}
+
+func Test_ShorthandFieldsNoDuplicates(t *testing.T) {
+ contents := `{
+ "spdxVersion": "SPDX-2.3",
+ "dataLicense": "CC0-1.0",
+ "SPDXID": "SPDXRef-DOCUMENT",
+ "name": "SPDX-Tools-v2.0",
+ "documentDescribes": [
+ "SPDXRef-Container"
+ ],
+ "packages": [
+ {
+ "name": "Container",
+ "SPDXID": "SPDXRef-Container"
+ },
+ {
+ "name": "Package-1",
+ "SPDXID": "SPDXRef-Package-1",
+ "versionInfo": "1.1.1",
+ "hasFiles": [
+ "SPDXRef-File-1",
+ "SPDXRef-File-2"
+ ]
+ },
+ {
+ "name": "Package-2",
+ "SPDXID": "SPDXRef-Package-2",
+ "versionInfo": "2.2.2"
+ }
+ ],
+ "files": [
+ {
+ "fileName": "./f1",
+ "SPDXID": "SPDXRef-File-1"
+ },
+ {
+ "fileName": "./f2",
+ "SPDXID": "SPDXRef-File-2"
+ }
+ ],
+ "relationships": [
+ {
+ "spdxElementId": "SPDXRef-Package-1",
+ "relationshipType": "CONTAINS",
+ "relatedSpdxElement": "SPDXRef-File-1"
+ },
+ {
+ "spdxElementId": "SPDXRef-Package-1",
+ "relationshipType": "CONTAINS",
+ "relatedSpdxElement": "SPDXRef-File-2"
+ }
+ ]
+ }`
+
+ doc := spdx.Document{}
+ err := json.ReadInto(strings.NewReader(contents), &doc)
+
+ require.NoError(t, err)
+
+ id := func(s string) common.DocElementID {
+ return common.DocElementID{
+ ElementRefID: common.ElementID(s),
+ }
+ }
+
+ want := spdx.Document{
+ SPDXVersion: spdx.Version,
+ DataLicense: spdx.DataLicense,
+ SPDXIdentifier: "DOCUMENT",
+ DocumentName: "SPDX-Tools-v2.0",
+ Packages: []*spdx.Package{
+ {
+ PackageName: "Container",
+ PackageSPDXIdentifier: "Container",
+ FilesAnalyzed: true,
+ },
+ {
+ PackageName: "Package-1",
+ PackageSPDXIdentifier: "Package-1",
+ PackageVersion: "1.1.1",
+ FilesAnalyzed: true,
+ },
+ {
+ PackageName: "Package-2",
+ PackageSPDXIdentifier: "Package-2",
+ PackageVersion: "2.2.2",
+ FilesAnalyzed: true,
+ },
+ },
+ Files: []*spdx.File{
+ {
+ FileName: "./f1",
+ FileSPDXIdentifier: "File-1",
+ },
+ {
+ FileName: "./f2",
+ FileSPDXIdentifier: "File-2",
+ },
+ },
+ Relationships: []*spdx.Relationship{
+ {
+ RefA: id("DOCUMENT"),
+ RefB: id("Container"),
+ Relationship: common.TypeRelationshipDescribe,
+ },
+ {
+ RefA: id("Package-1"),
+ RefB: id("File-1"),
+ Relationship: common.TypeRelationshipContains,
+ },
+ {
+ RefA: id("Package-1"),
+ RefB: id("File-2"),
+ Relationship: common.TypeRelationshipContains,
+ },
+ },
+ }
+
+ if diff := cmp.Diff(want, doc, cmpopts.IgnoreUnexported(spdx.Package{}), cmpopts.SortSlices(relationshipLess)); len(diff) > 0 {
+ t.Errorf("got incorrect struct after parsing JSON example: %s", diff)
+ return
+ }
}
func Test_JsonEnums(t *testing.T) {
@@ -334,3 +463,9 @@ func Test_JsonEnums(t *testing.T) {
},
}, doc)
}
+
+func relationshipLess(a, b *spdx.Relationship) bool {
+ aStr, _ := jsonenc.Marshal(a)
+ bStr, _ := jsonenc.Marshal(b)
+ return string(aStr) < string(bStr)
+}
diff --git a/spdx/v2/v2_3/yaml/yaml_test.go b/spdx/v2/v2_3/yaml/yaml_test.go
index 766a7c3..c0f787d 100644
--- a/spdx/v2/v2_3/yaml/yaml_test.go
+++ b/spdx/v2/v2_3/yaml/yaml_test.go
@@ -4,6 +4,7 @@ package yaml
import (
"bytes"
+ jsonenc "encoding/json"
"flag"
"fmt"
"os"
@@ -47,8 +48,8 @@ func Test_Read(t *testing.T) {
return
}
- if !cmp.Equal(want, got, cmpopts.IgnoreUnexported(spdx.Package{})) {
- t.Errorf("got incorrect struct after parsing YAML example: %s", cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{})))
+ if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{}), cmpopts.SortSlices(relationshipLess)); len(diff) > 0 {
+ t.Errorf("got incorrect struct after parsing YAML example: %s", diff)
return
}
}
@@ -76,8 +77,14 @@ func Test_Write(t *testing.T) {
return
}
- if !cmp.Equal(want, got, cmpopts.IgnoreUnexported(spdx.Package{})) {
- t.Errorf("got incorrect struct after writing and re-parsing YAML example: %s", cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{})))
+ if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{}), cmpopts.SortSlices(relationshipLess)); len(diff) > 0 {
+ t.Errorf("got incorrect struct after parsing YAML example: %s", diff)
return
}
}
+
+func relationshipLess(a, b *spdx.Relationship) bool {
+ aStr, _ := jsonenc.Marshal(a)
+ bStr, _ := jsonenc.Marshal(b)
+ return string(aStr) < string(bStr)
+}