diff options
| author | Joseph Richey <joerichey@google.com> | 2022-08-27 00:44:21 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-27 00:44:21 -0700 |
| commit | 5d9198ff97c2fc600743d57719dd6b1d77bc6d3c (patch) | |
| tree | 1813394b35f95217cbadf057fa9734da09c0db38 /metadata | |
| parent | 75cf58070a87aecfdad295ee50d048603d1916ed (diff) | |
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 <joerichey@google.com>
Signed-off-by: Joe Richey <joerichey@google.com>
Diffstat (limited to 'metadata')
| -rw-r--r-- | metadata/config_test.go | 12 |
1 files changed, 11 insertions, 1 deletions
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) } } |