Skip to content

Commit

Permalink
fix: JSONEq() whitespace backwards compatible (#2737)
Browse files Browse the repository at this point in the history
  • Loading branch information
apeabody authored Dec 9, 2024
1 parent a49b1f9 commit 97fe43b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions infra/blueprint-test/pkg/golden/golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +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()
getPath := fmt.Sprintf("%s|@tostr", jsonPath)
getPath := fmt.Sprintf("%s|@ugly", 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))
a.Equalf(gfData, gotData, "For path %q expected %q to match fixture %q", jsonPath, gotData, gfData)
}

// JSONPathEqs asserts that json content in jsonPaths for got and goldenfile are the same
Expand Down
24 changes: 22 additions & 2 deletions infra/blueprint-test/pkg/golden/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func TestJSONEq(t *testing.T) {
eqPath: "baz",
want: "{\"qux\":\"quz\"}",
},
{
name: "empty path",
data: "{\"foo\":\"bar\",\"baz\":{\"qux\":\"quz\"}}",
eqPath: "cookie",
want: "",
},
{
name: "sanitize quz",
data: "{\"foo\":\"bar\",\"baz\":{\"qux\":\"quz\"}}",
Expand Down Expand Up @@ -122,7 +128,7 @@ func TestJSONEq(t *testing.T) {
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 ]",
want: "[\"SYSTEM_COMPONENTS\",\"POD\",\"DAEMONSET\",\"DEPLOYMENT\",\"STATEFULSET\",\"STORAGE\",\"HPA\",\"CADVISOR\",\"KUBELET\"]",
},
}
for _, tt := range tests {
Expand All @@ -137,7 +143,7 @@ func TestJSONEq(t *testing.T) {
got := NewOrUpdate(t, tt.data, tt.opts...)
defer os.Remove(got.GetName())
got.JSONEq(assert, utils.ParseJSONResult(t, tt.data), tt.eqPath)
assert.JSONEq(tt.want, got.GetJSON().Get(tt.eqPath).String())
assert.Equal(tt.want, got.GetJSON().Get(tt.eqPath).Get("@ugly").String())
})
}
}
Expand All @@ -158,6 +164,20 @@ func TestJSONEqs(t *testing.T) {
want: "{\"foo\":\"bar\",\"baz\":{\"qux\":\"quz\"}}",
hasError: false,
},
{
name: "simple space diff",
data: "{\"foo\":\"bar\",\"baz\":{\"qux\":\"quz\"},\"fizz\":\"pop\"}",
eqPaths: []string{"foo","baz"},
want: "{ \"foo\":\"bar\",\"baz\":{\"qux\":\"quz\"}}",
hasError: false,
},
{
name: "simple order diff",
data: "{\"foo\":\"bar\",\"baz\":{\"qux\":\"quz\"},\"fizz\":\"pop\"}",
eqPaths: []string{"foo","baz"},
want: "{\"baz\":{\"qux\":\"quz\"},\"foo\":\"bar\",\"foo\":\"bar\"}",
hasError: false,
},
{
name: "false",
data: "{\"foo\":\"bar\",\"baz\":{\"qux\":\"quz\"},\"fizz\":\"pop\"}",
Expand Down

0 comments on commit 97fe43b

Please sign in to comment.