Skip to content

Commit

Permalink
testutil: Use %w verb wherever we're using an error in fmt.Errorf
Browse files Browse the repository at this point in the history
Signed-off-by: sazary <soroosh@azary.ir>
  • Loading branch information
sazary committed May 5, 2022
1 parent 6f41614 commit 86f0b14
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions prometheus/testutil/testutil.go
Expand Up @@ -124,7 +124,7 @@ func ToFloat64(c prometheus.Collector) float64 {
func CollectAndCount(c prometheus.Collector, metricNames ...string) int {
reg := prometheus.NewPedanticRegistry()
if err := reg.Register(c); err != nil {
panic(fmt.Errorf("registering collector failed: %s", err))
panic(fmt.Errorf("registering collector failed: %w", err))
}
result, err := GatherAndCount(reg, metricNames...)
if err != nil {
Expand All @@ -140,7 +140,7 @@ func CollectAndCount(c prometheus.Collector, metricNames ...string) int {
func GatherAndCount(g prometheus.Gatherer, metricNames ...string) (int, error) {
got, err := g.Gather()
if err != nil {
return 0, fmt.Errorf("gathering metrics failed: %s", err)
return 0, fmt.Errorf("gathering metrics failed: %w", err)
}
if metricNames != nil {
got = filterMetrics(got, metricNames)
Expand All @@ -159,7 +159,7 @@ func GatherAndCount(g prometheus.Gatherer, metricNames ...string) (int, error) {
func ScrapeAndCompare(url string, expected io.Reader, metricNames ...string) error {
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("scraping metrics failed: %s", err)
return fmt.Errorf("scraping metrics failed: %w", err)
}
defer resp.Body.Close()

Expand Down Expand Up @@ -187,7 +187,7 @@ func ScrapeAndCompare(url string, expected io.Reader, metricNames ...string) err
func CollectAndCompare(c prometheus.Collector, expected io.Reader, metricNames ...string) error {
reg := prometheus.NewPedanticRegistry()
if err := reg.Register(c); err != nil {
return fmt.Errorf("registering collector failed: %s", err)
return fmt.Errorf("registering collector failed: %w", err)
}
return GatherAndCompare(reg, expected, metricNames...)
}
Expand All @@ -208,7 +208,7 @@ func TransactionalGatherAndCompare(g prometheus.TransactionalGatherer, expected
got, done, err := g.Gather()
defer done()
if err != nil {
return fmt.Errorf("gathering metrics failed: %s", err)
return fmt.Errorf("gathering metrics failed: %w", err)
}

wanted, err := convertReaderToMetricFamily(expected)
Expand All @@ -225,7 +225,7 @@ func convertReaderToMetricFamily(reader io.Reader) ([]*dto.MetricFamily, error)
var tp expfmt.TextParser
notNormalized, err := tp.TextToMetricFamilies(reader)
if err != nil {
return nil, fmt.Errorf("converting reader to metric families failed: %s", err)
return nil, fmt.Errorf("converting reader to metric families failed: %w", err)
}

return internal.NormalizeMetricFamilies(notNormalized), nil
Expand All @@ -250,13 +250,13 @@ func compare(got, want []*dto.MetricFamily) error {
enc := expfmt.NewEncoder(&gotBuf, expfmt.FmtText)
for _, mf := range got {
if err := enc.Encode(mf); err != nil {
return fmt.Errorf("encoding gathered metrics failed: %s", err)
return fmt.Errorf("encoding gathered metrics failed: %w", err)
}
}
enc = expfmt.NewEncoder(&wantBuf, expfmt.FmtText)
for _, mf := range want {
if err := enc.Encode(mf); err != nil {
return fmt.Errorf("encoding expected metrics failed: %s", err)
return fmt.Errorf("encoding expected metrics failed: %w", err)
}
}
if diffErr := diff(wantBuf, gotBuf); diffErr != "" {
Expand Down

0 comments on commit 86f0b14

Please sign in to comment.