Skip to content

Commit

Permalink
update yaml tests to ignore order of relationships
Browse files Browse the repository at this point in the history
Signed-off-by: Brandon Lum <lumjjb@gmail.com>
  • Loading branch information
lumjjb committed May 24, 2023
1 parent 3f743e1 commit 6b2ab7a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 37 deletions.
44 changes: 11 additions & 33 deletions spdx/v2/v2_2/yaml/yaml_test.go
Expand Up @@ -4,14 +4,14 @@ package yaml

import (
"bytes"
jsonenc "encoding/json"
"fmt"
"os"
"testing"

"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"
Expand All @@ -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))
Expand All @@ -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
}
}
Expand All @@ -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)
}
15 changes: 11 additions & 4 deletions spdx/v2/v2_3/yaml/yaml_test.go
Expand Up @@ -4,6 +4,7 @@ package yaml

import (
"bytes"
jsonenc "encoding/json"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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)
}

0 comments on commit 6b2ab7a

Please sign in to comment.