@@ -11,6 +11,8 @@ import (
1111 "regexp"
1212 "testing"
1313
14+ "golang.org/x/telemetry/counter"
15+ "golang.org/x/telemetry/counter/countertest"
1416 "golang.org/x/tools/gopls/internal/protocol"
1517 "golang.org/x/tools/gopls/internal/protocol/command"
1618 "golang.org/x/tools/gopls/internal/server"
@@ -69,6 +71,10 @@ func main() {
6971
7072// Test that responding to the telemetry prompt results in the expected state.
7173func TestTelemetryPrompt_Response (t * testing.T ) {
74+ if ! countertest .SupportedPlatform {
75+ t .Skip ("requires counter support" )
76+ }
77+
7278 const src = `
7379-- go.mod --
7480module mod.com
@@ -81,18 +87,32 @@ func main() {
8187}
8288`
8389
90+ acceptanceCounterName := "gopls/telemetryprompt/accepted"
91+ acceptanceCounter := counter .New (acceptanceCounterName )
92+ // We must increment the acceptance counter in order for the initial read
93+ // below to succeed.
94+ //
95+ // TODO(rfindley): ReadCounter should simply return 0 for uninitialized
96+ // counters.
97+ acceptanceCounter .Inc ()
98+
8499 tests := []struct {
85100 name string // subtest name
86101 response string // response to choose for the telemetry dialog
87102 wantMode string // resulting telemetry mode
88103 wantMsg string // substring contained in the follow-up popup (if empty, no popup is expected)
104+ wantInc uint64 // expected 'prompt accepted' counter increment
89105 }{
90- {"yes" , server .TelemetryYes , "on" , "uploading is now enabled" },
91- {"no" , server .TelemetryNo , "" , "" },
92- {"empty" , "" , "" , "" },
106+ {"yes" , server .TelemetryYes , "on" , "uploading is now enabled" , 1 },
107+ {"no" , server .TelemetryNo , "" , "" , 0 },
108+ {"empty" , "" , "" , "" , 0 },
93109 }
94110 for _ , test := range tests {
95111 t .Run (test .name , func (t * testing.T ) {
112+ initialCount , err := countertest .ReadCounter (acceptanceCounter )
113+ if err != nil {
114+ t .Fatalf ("ReadCounter(%q) failed: %v" , acceptanceCounterName , err )
115+ }
96116 modeFile := filepath .Join (t .TempDir (), "mode" )
97117 msgRE := regexp .MustCompile (".*Would you like to enable Go telemetry?" )
98118 respond := func (m * protocol.ShowMessageRequestParams ) (* protocol.MessageActionItem , error ) {
@@ -136,6 +156,13 @@ func main() {
136156 if gotMode != test .wantMode {
137157 t .Errorf ("after prompt, mode=%s, want %s" , gotMode , test .wantMode )
138158 }
159+ finalCount , err := countertest .ReadCounter (acceptanceCounter )
160+ if err != nil {
161+ t .Fatalf ("ReadCounter(%q) failed: %v" , acceptanceCounterName , err )
162+ }
163+ if gotInc := finalCount - initialCount ; gotInc != test .wantInc {
164+ t .Errorf ("%q mismatch: got %d, want %d" , acceptanceCounterName , gotInc , test .wantInc )
165+ }
139166 })
140167 })
141168 }
0 commit comments