Skip to content

Commit 364a689

Browse files
authored
feat(testing): validate json goldens (#2513)
1 parent 22c0726 commit 364a689

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

internal/core/testing.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33
import (
44
"bytes"
55
"context"
6+
"encoding/json"
67
"flag"
78
"fmt"
89
"io"
@@ -650,6 +651,18 @@ func getHTTPRecoder(t *testing.T, update bool) (client *http.Client, cleanup fun
650651
}, nil
651652
}
652653

654+
func validateJSONGolden(t *testing.T, jsonStdout, jsonStderr *bytes.Buffer) {
655+
var jsonInterface interface{}
656+
if jsonStdout.Len() > 0 {
657+
err := json.Unmarshal(jsonStdout.Bytes(), &jsonInterface)
658+
require.NoError(t, err, "json stdout is invalid (%s)", getTestFilePath(t, ".cassette"))
659+
}
660+
if jsonStderr.Len() > 0 {
661+
err := json.Unmarshal(jsonStderr.Bytes(), &jsonInterface)
662+
require.NoError(t, err, "json stderr is invalid (%s)", getTestFilePath(t, ".cassette"))
663+
}
664+
}
665+
653666
func marshalGolden(t *testing.T, ctx *CheckFuncCtx) string {
654667
jsonStderr := &bytes.Buffer{}
655668
jsonStdout := &bytes.Buffer{}
@@ -670,6 +683,10 @@ func marshalGolden(t *testing.T, ctx *CheckFuncCtx) string {
670683
require.NoError(t, err)
671684
}
672685

686+
if _, isRawResult := ctx.Result.(RawResult); !isRawResult {
687+
validateJSONGolden(t, jsonStdout, jsonStderr)
688+
}
689+
673690
buffer := bytes.Buffer{}
674691
buffer.WriteString(fmt.Sprintf("\U0001F3B2\U0001F3B2\U0001F3B2 EXIT CODE: %d \U0001F3B2\U0001F3B2\U0001F3B2\n", ctx.ExitCode))
675692

0 commit comments

Comments
 (0)