From 0aba90eae1f5993262255912eb9ade9768fc9426 Mon Sep 17 00:00:00 2001 From: PhilipGough Date: Wed, 14 Nov 2018 11:55:09 +0000 Subject: [PATCH] Compare text strings directly in testutil --- prometheus/testutil/testutil.go | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/prometheus/testutil/testutil.go b/prometheus/testutil/testutil.go index e7af303e6..533fa86a5 100644 --- a/prometheus/testutil/testutil.go +++ b/prometheus/testutil/testutil.go @@ -138,20 +138,13 @@ func GatherAndCompare(g prometheus.Gatherer, expected io.Reader, metricNames ... } want := internal.NormalizeMetricFamilies(wantRaw) - if len(got) != len(want) { - return notMatchingError(got, want) - } - for i := range got { - if got[i].String() != want[i].String() { - return notMatchingError(got, want) - } - } - return nil + return compare(got, want) } -// notMatchingError encodes both provided slices of metric families into the -// text format and creates a readable error message from the result. -func notMatchingError(got, want []*dto.MetricFamily) error { +// compare encodes both provided slices of metric families into the +// text format compares their string message and returns an error if they do not match. +// In case of error, prints a readable error message from the result. +func compare(got, want []*dto.MetricFamily) error { var gotBuf, wantBuf bytes.Buffer enc := expfmt.NewEncoder(&gotBuf, expfmt.FmtText) for _, mf := range got { @@ -166,7 +159,8 @@ func notMatchingError(got, want []*dto.MetricFamily) error { } } - return fmt.Errorf(` + if wantBuf.String() != gotBuf.String() { + return fmt.Errorf(` metric output does not match expectation; want: %s @@ -175,6 +169,9 @@ got: %s `, wantBuf.String(), gotBuf.String()) + + } + return nil } func filterMetrics(metrics []*dto.MetricFamily, names []string) []*dto.MetricFamily {