|
2 | 2 | package internal |
3 | 3 |
|
4 | 4 | import ( |
| 5 | + "bytes" |
| 6 | + "errors" |
5 | 7 | "fmt" |
| 8 | + "io" |
6 | 9 | "os" |
7 | 10 | "path" |
8 | 11 | "path/filepath" |
9 | 12 | "reflect" |
10 | 13 | "testing" |
11 | 14 |
|
12 | 15 | "github.com/google/go-cmp/cmp" |
| 16 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 17 | + "gopkg.in/yaml.v3" |
13 | 18 | "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
14 | 19 | "open-cluster-management.io/ocm-kustomize-generator-plugins/internal/types" |
15 | 20 | ) |
16 | 21 |
|
17 | 22 | func assertEqual(t *testing.T, a interface{}, b interface{}) { |
18 | 23 | t.Helper() |
19 | 24 |
|
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) |
22 | 58 | } |
23 | 59 | } |
24 | 60 |
|
|
0 commit comments