From 5d9198ff97c2fc600743d57719dd6b1d77bc6d3c Mon Sep 17 00:00:00 2001 From: Joseph Richey Date: Sat, 27 Aug 2022 00:44:21 -0700 Subject: Ignore JSON whitespace in tests (#364) Follow up to #362 Protojson randomly inserts whitespace to indicate that the output is unstable, breaking out tests. To fix this, compact the output before comparison. Signed-off-by: Joe Richey Signed-off-by: Joe Richey --- metadata/config_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/metadata/config_test.go b/metadata/config_test.go index c68dc2e..f8cb7fb 100644 --- a/metadata/config_test.go +++ b/metadata/config_test.go @@ -21,6 +21,7 @@ package metadata import ( "bytes" + "encoding/json" "testing" "google.golang.org/protobuf/proto" @@ -54,6 +55,15 @@ var testConfigString = `{ } ` +// Used for JSON string comparison while ignoring whitespace +func compact(t testing.TB, s string) string { + var b bytes.Buffer + if err := json.Compact(&b, []byte(s)); err != nil { + t.Fatalf("failed to compact json: %v", err) + } + return b.String() +} + // Makes sure that writing a config and reading it back gives the same thing. func TestWrite(t *testing.T) { var b bytes.Buffer @@ -62,7 +72,7 @@ func TestWrite(t *testing.T) { t.Fatal(err) } t.Logf("json encoded config:\n%s", b.String()) - if b.String() != testConfigString { + if compact(t, b.String()) != compact(t, testConfigString) { t.Errorf("did not match: %s", testConfigString) } } -- cgit v1.2.3