Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Commit 58d2c31

Browse files
authored
Use go-cmp to show the diff for test failures. (#513)
* Use go-cmp to show the diff for test failures. * Use go-cmp in one more test. * get before vet
1 parent 7a37c4a commit 58d2c31

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ test:
3838
$(GOTEST) $(GOTEST_OPT) $(ALL_PKGS)
3939

4040
.PHONY: travis-ci
41-
travis-ci: fmt vet lint test-with-cover
41+
travis-ci: fmt install-go-cmp vet lint test-with-cover
4242

4343
.PHONY: test-with-cover
44-
test-with-cover:
44+
test-with-cover:
4545
@echo Verifying that all packages have test files to count in coverage
4646
@scripts/check-test-files.sh $(subst github.com/census-instrumentation/opencensus-service/,./,$(ALL_PKGS))
4747
@echo pre-compiling tests
@@ -86,6 +86,10 @@ vet:
8686
install-tools:
8787
go get golang.org/x/lint/golint
8888

89+
.PHONY: install-go-cmp
90+
install-go-cmp:
91+
go get -u github.com/google/go-cmp/cmp
92+
8993
.PHONY: agent
9094
agent:
9195
GO111MODULE=on CGO_ENABLED=0 go build -o ./bin/ocagent_$(GOOS) $(BUILD_INFO) ./cmd/ocagent
@@ -112,7 +116,7 @@ docker-agent:
112116
COMPONENT=agent $(MAKE) docker-component
113117

114118
.PHONY: docker-collector
115-
docker-collector:
119+
docker-collector:
116120
COMPONENT=collector $(MAKE) docker-component
117121

118122

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
github.com/gogo/googleapis v1.1.0 // indirect
2323
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef // indirect
2424
github.com/golang/protobuf v1.3.1
25+
github.com/google/go-cmp v0.2.0
2526
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf // indirect
2627
github.com/googleapis/gnostic v0.2.0 // indirect
2728
github.com/gophercloud/gophercloud v0.0.0-20190206021053-df38e1611dbe // indirect

receiver/jaegerreceiver/jaeger_agent_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
package jaegerreceiver
1616

1717
import (
18-
"bytes"
1918
"context"
2019
"fmt"
21-
"reflect"
2220
"testing"
2321
"time"
2422

23+
"github.com/google/go-cmp/cmp"
24+
2525
"go.opencensus.io/exporter/jaeger"
2626
"go.opencensus.io/trace"
2727

@@ -228,10 +228,7 @@ func testJaegerAgent(t *testing.T, agentEndpoint string, receiverConfig *Configu
228228
},
229229
}
230230

231-
if !reflect.DeepEqual(got, want) {
232-
gj, wj := exportertest.ToJSON(got), exportertest.ToJSON(want)
233-
if !bytes.Equal(gj, wj) {
234-
t.Errorf("Mismatched responses\nGot:\n\t%v\n\t%s\nWant:\n\t%v\n\t%s", got, gj, want, wj)
235-
}
231+
if diff := cmp.Diff(got, want); diff != "" {
232+
t.Errorf("Mismatched responses\n-Got +Want:\n\t%s", diff)
236233
}
237234
}

receiver/jaegerreceiver/trace_receiver_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
package jaegerreceiver
1616

1717
import (
18-
"bytes"
1918
"context"
2019
"fmt"
21-
"reflect"
2220
"testing"
2321
"time"
2422

23+
"github.com/google/go-cmp/cmp"
24+
2525
"go.opencensus.io/exporter/jaeger"
2626
"go.opencensus.io/trace"
2727

@@ -212,10 +212,7 @@ func TestReception(t *testing.T) {
212212
},
213213
}
214214

215-
if !reflect.DeepEqual(got, want) {
216-
gj, wj := exportertest.ToJSON(got), exportertest.ToJSON(want)
217-
if !bytes.Equal(gj, wj) {
218-
t.Errorf("Mismatches responses\nGot:\n\t%v\n\t%s\nWant:\n\t%v\n\t%s", got, gj, want, wj)
219-
}
215+
if diff := cmp.Diff(got, want); diff != "" {
216+
t.Errorf("Mismatched responses\n-Got +Want:\n\t%s", diff)
220217
}
221218
}

0 commit comments

Comments
 (0)