From 080366ec8f8fb8f3f26ad5c233df0bd5eeaad3b6 Mon Sep 17 00:00:00 2001 From: Andrew Peabody Date: Wed, 27 Nov 2024 18:31:53 +0000 Subject: [PATCH] fix: JSONEq handle json doc whitespace --- infra/blueprint-test/pkg/golden/golden.go | 7 ++++--- infra/blueprint-test/pkg/golden/golden_test.go | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/infra/blueprint-test/pkg/golden/golden.go b/infra/blueprint-test/pkg/golden/golden.go index fd79d4dba8a..d2b3a5d58da 100644 --- a/infra/blueprint-test/pkg/golden/golden.go +++ b/infra/blueprint-test/pkg/golden/golden.go @@ -154,9 +154,10 @@ func (g *GoldenFile) GetJSON() gjson.Result { // JSONEq asserts that json content in jsonPath for got and goldenfile is the same func (g *GoldenFile) JSONEq(a *assert.Assertions, got gjson.Result, jsonPath string) { gf := g.GetJSON() - gotData := g.ApplySanitizers(got.Get(jsonPath).String()) - gfData := gf.Get(jsonPath).String() - a.Equal(gfData, gotData, fmt.Sprintf("expected %s to match fixture %s", jsonPath, gfData)) + getPath := fmt.Sprintf("%s|@tostr", jsonPath) + gotData := g.ApplySanitizers(got.Get(getPath).String()) + gfData := gf.Get(getPath).String() + a.JSONEq(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 diff --git a/infra/blueprint-test/pkg/golden/golden_test.go b/infra/blueprint-test/pkg/golden/golden_test.go index e501a05a851..bfd0b56895a 100644 --- a/infra/blueprint-test/pkg/golden/golden_test.go +++ b/infra/blueprint-test/pkg/golden/golden_test.go @@ -118,6 +118,12 @@ func TestJSONEq(t *testing.T) { eqPath: "baz", want: "{\"qux\":\"REPLACED\",\"quux\":\"NEW\"}", }, + { + name: "diff_whitespace", + data: "{\"list\":[\n \"SYSTEM_COMPONENTS\",\n \"POD\",\n \"DAEMONSET\",\n \"DEPLOYMENT\",\n \"STATEFULSET\",\n \"STORAGE\",\n \"HPA\",\n \"CADVISOR\",\n \"KUBELET\"\n ]}", + eqPath: "list", + want: "[\n \"SYSTEM_COMPONENTS\",\n \"POD\",\n \"DAEMONSET\",\n \"DEPLOYMENT\",\n \"STATEFULSET\",\n \"STORAGE\",\n \"HPA\",\n \"CADVISOR\",\n \"KUBELET\"\n ]", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {