Skip to content

Commit

Permalink
Sort JSON objects before comparing to prevent rc error
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasslash committed Oct 21, 2024
1 parent 4fa830b commit b48dca3
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion internal/command/modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"encoding/json"
"os"
"reflect"
"sort"
"strings"
"testing"

"github.com/hashicorp/cli"
"github.com/hashicorp/terraform/internal/moduleref"
)

func TestModules_noJsonFlag(t *testing.T) {
Expand Down Expand Up @@ -130,7 +132,7 @@ func TestModules_uninstalledModules(t *testing.T) {
}

func compareJSONOutput(t *testing.T, got string, want string) {
var expected, actual map[string]interface{}
var expected, actual moduleref.Manifest

if err := json.Unmarshal([]byte(got), &actual); err != nil {
t.Fatalf("Failed to unmarshal actual JSON: %v", err)
Expand All @@ -140,6 +142,13 @@ func compareJSONOutput(t *testing.T, got string, want string) {
t.Fatalf("Failed to unmarshal expected JSON: %v", err)
}

sort.Slice(actual.Records, func(i, j int) bool {
return actual.Records[i].Key < actual.Records[j].Key
})
sort.Slice(expected.Records, func(i, j int) bool {
return expected.Records[i].Key < expected.Records[j].Key
})

if !reflect.DeepEqual(expected, actual) {
t.Fatalf("unexpected output, got: %s\n, want:%s\n", got, want)
}
Expand Down

0 comments on commit b48dca3

Please sign in to comment.