Skip to content

Commit 0aba90e

Browse files
committed
Compare text strings directly in testutil
1 parent d58049e commit 0aba90e

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

prometheus/testutil/testutil.go

+10-13
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,13 @@ func GatherAndCompare(g prometheus.Gatherer, expected io.Reader, metricNames ...
138138
}
139139
want := internal.NormalizeMetricFamilies(wantRaw)
140140

141-
if len(got) != len(want) {
142-
return notMatchingError(got, want)
143-
}
144-
for i := range got {
145-
if got[i].String() != want[i].String() {
146-
return notMatchingError(got, want)
147-
}
148-
}
149-
return nil
141+
return compare(got, want)
150142
}
151143

152-
// notMatchingError encodes both provided slices of metric families into the
153-
// text format and creates a readable error message from the result.
154-
func notMatchingError(got, want []*dto.MetricFamily) error {
144+
// compare encodes both provided slices of metric families into the
145+
// text format compares their string message and returns an error if they do not match.
146+
// In case of error, prints a readable error message from the result.
147+
func compare(got, want []*dto.MetricFamily) error {
155148
var gotBuf, wantBuf bytes.Buffer
156149
enc := expfmt.NewEncoder(&gotBuf, expfmt.FmtText)
157150
for _, mf := range got {
@@ -166,7 +159,8 @@ func notMatchingError(got, want []*dto.MetricFamily) error {
166159
}
167160
}
168161

169-
return fmt.Errorf(`
162+
if wantBuf.String() != gotBuf.String() {
163+
return fmt.Errorf(`
170164
metric output does not match expectation; want:
171165
172166
%s
@@ -175,6 +169,9 @@ got:
175169
176170
%s
177171
`, wantBuf.String(), gotBuf.String())
172+
173+
}
174+
return nil
178175
}
179176

180177
func filterMetrics(metrics []*dto.MetricFamily, names []string) []*dto.MetricFamily {

0 commit comments

Comments
 (0)