diff --git a/pkg/test/utils.go b/pkg/test/utils.go index 566aa7ae7e..9ea644e373 100644 --- a/pkg/test/utils.go +++ b/pkg/test/utils.go @@ -15,6 +15,7 @@ package test import ( + "encoding/json" "os" "path/filepath" "regexp" @@ -242,3 +243,32 @@ func CompareGoldenObject(t *testing.T, p string, got []byte) { t.Logf("wrote updated golden output to %s", p) } } + +func PrettyPrintJSON[T any](t *testing.T, k T) string { + encoded, err := json.MarshalIndent(k, "", " ") + if err != nil { + t.Fatalf("error encoding to json: %v", err) + } + + return string(encoded) +} + +func PrettyPrintYAML[T any](t *testing.T, k T) string { + encoded, err := json.MarshalIndent(k, "", " ") + if err != nil { + t.Fatalf("error encoding to json: %v", err) + } + + otherK := new(T) + err = json.Unmarshal(encoded, &otherK) + if err != nil { + t.Fatalf("error decoding from json: %v", err) + } + + yEncoded, err := yaml.Marshal(otherK) + if err != nil { + t.Fatalf("error encoding to yaml: %v", err) + } + + return string(yEncoded) +}