remove go-spew package

Signed-off-by: dongjiang1989 <dongjiang1989@126.com>
This commit is contained in:
dongjiang1989 2024-04-12 17:28:01 +08:00
parent d038ab96c0
commit 3aeef40009
No known key found for this signature in database
4 changed files with 32 additions and 38 deletions

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.20
require ( require (
github.com/beorn7/perks v1.0.1 github.com/beorn7/perks v1.0.1
github.com/cespare/xxhash/v2 v2.2.0 github.com/cespare/xxhash/v2 v2.2.0
github.com/davecgh/go-spew v1.1.1 github.com/google/go-cmp v0.6.0
github.com/json-iterator/go v1.1.12 github.com/json-iterator/go v1.1.12
github.com/prometheus/client_model v0.6.0 github.com/prometheus/client_model v0.6.0
github.com/prometheus/common v0.52.3 github.com/prometheus/common v0.52.3

1
go.sum
View File

@ -12,6 +12,7 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=

View File

@ -44,7 +44,7 @@ import (
"net/http" "net/http"
"reflect" "reflect"
"github.com/davecgh/go-spew/spew" "github.com/google/go-cmp/cmp"
dto "github.com/prometheus/client_model/go" dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt" "github.com/prometheus/common/expfmt"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
@ -264,6 +264,7 @@ func compareMetricFamilies(got, expected []*dto.MetricFamily, metricNames ...str
// The error contains the encoded text of both the desired and the actual // The error contains the encoded text of both the desired and the actual
// result. // result.
func compare(got, want []*dto.MetricFamily) error { func compare(got, want []*dto.MetricFamily) error {
var gotBuf, wantBuf bytes.Buffer var gotBuf, wantBuf bytes.Buffer
enc := expfmt.NewEncoder(&gotBuf, expfmt.NewFormat(expfmt.TypeTextPlain)) enc := expfmt.NewEncoder(&gotBuf, expfmt.NewFormat(expfmt.TypeTextPlain))
for _, mf := range got { for _, mf := range got {
@ -277,7 +278,7 @@ func compare(got, want []*dto.MetricFamily) error {
return fmt.Errorf("encoding expected metrics failed: %w", err) return fmt.Errorf("encoding expected metrics failed: %w", err)
} }
} }
if diffErr := diff(wantBuf, gotBuf); diffErr != "" { if diffErr := diff(gotBuf.String(), wantBuf.String()); diffErr != "" {
return fmt.Errorf(diffErr) return fmt.Errorf(diffErr)
} }
return nil return nil
@ -300,31 +301,7 @@ func diff(expected, actual interface{}) string {
return "" return ""
} }
var e, a string diff := cmp.Diff(expected, actual)
c := spew.ConfigState{
Indent: " ",
DisablePointerAddresses: true,
DisableCapacities: true,
SortKeys: true,
}
if et != reflect.TypeOf("") {
e = c.Sdump(expected)
a = c.Sdump(actual)
} else {
e = reflect.ValueOf(expected).String()
a = reflect.ValueOf(actual).String()
}
diff, _ := internal.GetUnifiedDiffString(internal.UnifiedDiff{
A: internal.SplitLines(e),
B: internal.SplitLines(a),
FromFile: "metric output does not match expectation; want",
FromDate: "",
ToFile: "got:",
ToDate: "",
Context: 1,
})
if diff == "" { if diff == "" {
return "" return ""
} }

View File

@ -305,20 +305,36 @@ func TestMetricNotFound(t *testing.T) {
expected := ` expected := `
some_other_metric{label1="value1"} 1 some_other_metric{label1="value1"} 1
` `
/*
expectedError := `
Diff:
--- metric output does not match expectation; want
+++ got:
@@ -1,4 +1,4 @@
-(bytes.Buffer) # HELP some_other_metric A value that represents a counter.
-# TYPE some_other_metric counter
-some_other_metric{label1="value1"} 1
+(bytes.Buffer) # HELP some_total A value that represents a counter.
+# TYPE some_total counter
+some_total{label1="value1"} 1
`
*/
expectedError := ` expectedError := `
Diff: Diff:
--- metric output does not match expectation; want (
+++ got: """
@@ -1,4 +1,4 @@ - # HELP some_total A value that represents a counter.
-(bytes.Buffer) # HELP some_other_metric A value that represents a counter. - # TYPE some_total counter
-# TYPE some_other_metric counter - some_total{label1="value1"} 1
-some_other_metric{label1="value1"} 1 + # HELP some_other_metric A value that represents a counter.
+(bytes.Buffer) # HELP some_total A value that represents a counter. + # TYPE some_other_metric counter
+# TYPE some_total counter + some_other_metric{label1="value1"} 1
+some_total{label1="value1"} 1 """
)
` `
err := CollectAndCompare(c, strings.NewReader(metadata+expected)) err := CollectAndCompare(c, strings.NewReader(metadata+expected))