Skip to content

Commit 55e4e35

Browse files
authored
Merge pull request #44157 from tabito-hara/t-fix_flaky_TestCreatePatchFromStrings
[test fix] Fix flaky test `internal/json.TestCreatePatchFromStrings` by introducing transformer for `cmp.Diff` to sort elements during comparison
2 parents 7f6ff14 + 65acd1e commit 55e4e35

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

internal/json/patch_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
package json_test
55

66
import (
7+
"cmp"
8+
"slices"
79
"testing"
810

9-
"github.com/google/go-cmp/cmp"
11+
gocmp "github.com/google/go-cmp/cmp"
1012
tfjson "github.com/hashicorp/terraform-provider-aws/internal/json"
1113
mattbairdjsonpatch "github.com/mattbaird/jsonpatch"
1214
)
@@ -72,11 +74,20 @@ func TestCreatePatchFromStrings(t *testing.T) {
7274
t.Parallel()
7375

7476
got, err := tfjson.CreatePatchFromStrings(testCase.a, testCase.b)
75-
if got, want := err != nil, testCase.wantErr; !cmp.Equal(got, want) {
77+
if got, want := err != nil, testCase.wantErr; !gocmp.Equal(got, want) {
7678
t.Errorf("CreatePatchFromStrings(%s, %s) err %t, want %t", testCase.a, testCase.b, got, want)
7779
}
7880
if err == nil {
79-
if diff := cmp.Diff(got, testCase.wantPatch); diff != "" {
81+
sortTransformer := gocmp.Transformer("SortPatchOps", func(ops []mattbairdjsonpatch.JsonPatchOperation) []mattbairdjsonpatch.JsonPatchOperation {
82+
sorted := make([]mattbairdjsonpatch.JsonPatchOperation, len(ops))
83+
copy(sorted, ops)
84+
slices.SortFunc(sorted, func(a, b mattbairdjsonpatch.JsonPatchOperation) int {
85+
return cmp.Or(cmp.Compare(a.Operation, b.Operation), cmp.Compare(a.Path, b.Path))
86+
})
87+
return sorted
88+
})
89+
90+
if diff := gocmp.Diff(got, testCase.wantPatch, sortTransformer); diff != "" {
8091
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
8192
}
8293
}

0 commit comments

Comments
 (0)