From 947d4aa89328ca3de223e23c6ed064660ff03347 Mon Sep 17 00:00:00 2001 From: Ian Cottrell Date: Mon, 18 Nov 2019 10:30:03 -0500 Subject: [PATCH] internal/lsp: make the ocagent test external They now call a public encode function and have no knowledge of the private conversion functions or any of the contents of the wire package. Change-Id: I4364a4d9d1efe4bc872627556e537936b5880231 Reviewed-on: https://go-review.googlesource.com/c/tools/+/207903 Run-TryBot: Ian Cottrell TryBot-Result: Gobot Gobot Reviewed-by: Emmanuel Odeke --- .../telemetry/export/ocagent/metrics_test.go | 6 +++++ internal/telemetry/export/ocagent/ocagent.go | 4 +++ .../telemetry/export/ocagent/ocagent_test.go | 25 +++++++++++++------ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/internal/telemetry/export/ocagent/metrics_test.go b/internal/telemetry/export/ocagent/metrics_test.go index 033a60ed9b4..a12c66abb0d 100644 --- a/internal/telemetry/export/ocagent/metrics_test.go +++ b/internal/telemetry/export/ocagent/metrics_test.go @@ -1,6 +1,7 @@ package ocagent import ( + "encoding/json" "reflect" "testing" "time" @@ -991,3 +992,8 @@ func TestInfoKeysToLabelKeys(t *testing.T) { }) } } + +func marshaled(v interface{}) string { + blob, _ := json.MarshalIndent(v, "", " ") + return string(blob) +} diff --git a/internal/telemetry/export/ocagent/ocagent.go b/internal/telemetry/export/ocagent/ocagent.go index eb28d0e6d3e..b8825007ccd 100644 --- a/internal/telemetry/export/ocagent/ocagent.go +++ b/internal/telemetry/export/ocagent/ocagent.go @@ -144,6 +144,10 @@ func (e *exporter) Flush() { } } +func EncodeAnnotation(a telemetry.Event) ([]byte, error) { + return json.Marshal(convertAnnotation(a)) +} + func (e *exporter) send(endpoint string, message interface{}) { blob, err := json.Marshal(message) if err != nil { diff --git a/internal/telemetry/export/ocagent/ocagent_test.go b/internal/telemetry/export/ocagent/ocagent_test.go index a5d0e96db60..534c54c316c 100644 --- a/internal/telemetry/export/ocagent/ocagent_test.go +++ b/internal/telemetry/export/ocagent/ocagent_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package ocagent +package ocagent_test import ( "context" @@ -12,6 +12,7 @@ import ( "testing" "golang.org/x/tools/internal/telemetry" + "golang.org/x/tools/internal/telemetry/export/ocagent" "golang.org/x/tools/internal/telemetry/tag" ) @@ -199,15 +200,25 @@ func TestConvert_annotation(t *testing.T) { ctx := context.TODO() for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := marshaled(convertAnnotation(tt.event(ctx))) - if !reflect.DeepEqual(got, tt.want) { - t.Fatalf("Got:\n%s\nWant:\n%s", got, tt.want) + got, err := ocagent.EncodeAnnotation(tt.event(ctx)) + if err != nil { + t.Fatal(err) } + checkJSON(t, got, []byte(tt.want)) }) } } -func marshaled(v interface{}) string { - blob, _ := json.MarshalIndent(v, "", " ") - return string(blob) +func checkJSON(t *testing.T, got, want []byte) { + // compare the decoded form, to allow for formatting differences + var g, w map[string]interface{} + if err := json.Unmarshal(got, &g); err != nil { + t.Fatal(err) + } + if err := json.Unmarshal(want, &w); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(g, w) { + t.Fatalf("Got:\n%s\nWant:\n%s", got, want) + } }