Skip to content

Commit 8763934

Browse files
JustinKuliopenshift-merge-robot
authored andcommitted
Improve some testing utils
They now give more structured output to help identify where issues, and there is a new function to help compare YAML. Signed-off-by: Justin Kulikauskas <[email protected]>
1 parent 58d0263 commit 8763934

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

internal/expanders/expanders_utils_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ package expanders
33
import (
44
"reflect"
55
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
68
)
79

810
func assertEqual(t *testing.T, a interface{}, b interface{}) {
911
t.Helper()
1012

1113
if a != b {
12-
t.Fatalf("%s != %s", a, b)
14+
t.Fatalf(cmp.Diff(a, b))
1315
}
1416
}
1517

1618
func assertReflectEqual(t *testing.T, a interface{}, b interface{}) {
1719
t.Helper()
1820

1921
if !reflect.DeepEqual(a, b) {
20-
t.Fatalf("%s != %s", a, b)
22+
t.Fatalf(cmp.Diff(a, b))
2123
}
2224
}

internal/utils_test.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,59 @@
22
package internal
33

44
import (
5+
"bytes"
6+
"errors"
57
"fmt"
8+
"io"
69
"os"
710
"path"
811
"path/filepath"
912
"reflect"
1013
"testing"
1114

1215
"github.com/google/go-cmp/cmp"
16+
"github.com/google/go-cmp/cmp/cmpopts"
17+
"gopkg.in/yaml.v3"
1318
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1419
"open-cluster-management.io/ocm-kustomize-generator-plugins/internal/types"
1520
)
1621

1722
func assertEqual(t *testing.T, a interface{}, b interface{}) {
1823
t.Helper()
1924

20-
if a != b {
21-
t.Fatalf(cmp.Diff(a, b))
25+
diff := cmp.Diff(a, b, cmpopts.EquateErrors())
26+
27+
if diff != "" {
28+
t.Fatalf(diff)
29+
}
30+
}
31+
32+
func assertEqualYaml(t *testing.T, a, b []byte) {
33+
t.Helper()
34+
35+
aDec := yaml.NewDecoder(bytes.NewReader(a))
36+
bDec := yaml.NewDecoder(bytes.NewReader(b))
37+
38+
for {
39+
aOut := map[string]interface{}{}
40+
aErr := aDec.Decode(aOut)
41+
42+
bOut := map[string]interface{}{}
43+
bErr := bDec.Decode(bOut)
44+
45+
aDone := errors.Is(aErr, io.EOF)
46+
bDone := errors.Is(bErr, io.EOF)
47+
48+
if aDone && bDone {
49+
return // both inputs had the same number of documents
50+
} else if aDone && !bDone {
51+
t.Fatalf("extra yaml doc in second input")
52+
} else if !aDone && bDone {
53+
t.Fatalf("missing yaml doc in second input")
54+
}
55+
56+
assertEqual(t, aErr, bErr)
57+
assertEqual(t, aOut, bOut)
2258
}
2359
}
2460

0 commit comments

Comments
 (0)