From 3065ac7350c19e53ae082d6953407c5dd406e8c1 Mon Sep 17 00:00:00 2001 From: Andrew Peabody Date: Thu, 21 Nov 2024 12:23:06 -0800 Subject: [PATCH] feat(blueprint-test): add JSONPathEqs() (#2706) --- infra/blueprint-test/pkg/golden/golden.go | 14 +++++++ .../blueprint-test/pkg/golden/golden_test.go | 42 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/infra/blueprint-test/pkg/golden/golden.go b/infra/blueprint-test/pkg/golden/golden.go index 59f2bf11461..ee0590cd0af 100644 --- a/infra/blueprint-test/pkg/golden/golden.go +++ b/infra/blueprint-test/pkg/golden/golden.go @@ -22,6 +22,7 @@ import ( "os" "path" "strings" + "sync" "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud" "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils" @@ -156,3 +157,16 @@ func (g *GoldenFile) JSONEq(a *assert.Assertions, got gjson.Result, jsonPath str gfData := gf.Get(jsonPath).String() a.Equal(gfData, gotData, fmt.Sprintf("expected %s to match fixture %s", jsonPath, gfData)) } + +// JSONPathEqs asserts that json content in jsonPaths for got and goldenfile are the same +func (g *GoldenFile) JSONPathEqs(a *assert.Assertions, got gjson.Result, jsonPaths []string) { + var wg sync.WaitGroup + wg.Add(len(jsonPaths)) + for _, path := range jsonPaths { + go func(a *assert.Assertions, got gjson.Result, path string) { + defer wg.Done() + g.JSONEq(a, got, path) + }(a, got, path) + } + wg.Wait() +} diff --git a/infra/blueprint-test/pkg/golden/golden_test.go b/infra/blueprint-test/pkg/golden/golden_test.go index c72f22e4269..e501a05a851 100644 --- a/infra/blueprint-test/pkg/golden/golden_test.go +++ b/infra/blueprint-test/pkg/golden/golden_test.go @@ -25,6 +25,8 @@ import ( "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud" "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils" "github.com/stretchr/testify/assert" + + gotest "github.com/mitchellh/go-testing-interface" ) const testProjectID = "foo" @@ -133,3 +135,43 @@ func TestJSONEq(t *testing.T) { }) } } + +func TestJSONEqs(t *testing.T) { + tests := []struct { + name string + data string + eqPaths []string + opts []goldenFileOption + want string + hasError bool + }{ + { + name: "simple", + data: "{\"foo\":\"bar\",\"baz\":{\"qux\":\"quz\"},\"fizz\":\"pop\"}", + eqPaths: []string{"foo","baz"}, + want: "{\"foo\":\"bar\",\"baz\":{\"qux\":\"quz\"}}", + hasError: false, + }, + { + name: "false", + data: "{\"foo\":\"bar\",\"baz\":{\"qux\":\"quz\"},\"fizz\":\"pop\"}", + eqPaths: []string{"foo","baz"}, + want: "{\"foo\":\"bar\",\"baz\":{\"qux\":\"quz1\"}}", + hasError: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + innerT := &gotest.RuntimeT{} + innerAssert := assert.New(innerT) + os.Setenv(gfUpdateEnvVar, "true") + defer os.Unsetenv(gfUpdateEnvVar) + got := NewOrUpdate(t, tt.data, tt.opts...) + defer os.Remove(got.GetName()) + got.JSONPathEqs(innerAssert, utils.ParseJSONResult(t, tt.want), tt.eqPaths) + + assert := assert.New(t) + assert.True(innerT.Failed() == tt.hasError) + }) + } +}