Skip to content

Commit 8e6f142

Browse files
authored
internal/testrunner/runners/pipeline: output documents with fields in a normalized order (#644)
1 parent 35c3beb commit 8e6f142

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

internal/testrunner/runners/pipeline/test_result.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,29 @@ func unmarshalTestResult(body []byte) (*testResult, error) {
146146
func marshalTestResultDefinition(result *testResult) ([]byte, error) {
147147
var trd testResultDefinition
148148
trd.Expected = result.events
149-
body, err := json.MarshalIndent(&trd, "", " ")
149+
body, err := marshalNormalizedJSON(trd)
150150
if err != nil {
151151
return nil, errors.Wrap(err, "marshalling test result definition failed")
152152
}
153153
return body, nil
154154
}
155155

156+
// marshalNormalizedJSON marshals test results ensuring that field
157+
// order remains consistent independent of field order returned by
158+
// ES to minimize diff noise during changes.
159+
func marshalNormalizedJSON(v testResultDefinition) ([]byte, error) {
160+
msg, err := json.Marshal(v)
161+
if err != nil {
162+
return msg, err
163+
}
164+
var obj interface{}
165+
err = json.Unmarshal(msg, &obj)
166+
if err != nil {
167+
return msg, err
168+
}
169+
return json.MarshalIndent(obj, "", " ")
170+
}
171+
156172
func expectedTestResultFile(testFile string) string {
157173
return fmt.Sprintf("%s%s", testFile, expectedTestResultSuffix)
158174
}

0 commit comments

Comments
 (0)