@@ -11,6 +11,8 @@ import (
11
11
"regexp"
12
12
"testing"
13
13
14
+ "golang.org/x/telemetry/counter"
15
+ "golang.org/x/telemetry/counter/countertest"
14
16
"golang.org/x/tools/gopls/internal/protocol"
15
17
"golang.org/x/tools/gopls/internal/protocol/command"
16
18
"golang.org/x/tools/gopls/internal/server"
@@ -69,6 +71,10 @@ func main() {
69
71
70
72
// Test that responding to the telemetry prompt results in the expected state.
71
73
func TestTelemetryPrompt_Response (t * testing.T ) {
74
+ if ! countertest .SupportedPlatform {
75
+ t .Skip ("requires counter support" )
76
+ }
77
+
72
78
const src = `
73
79
-- go.mod --
74
80
module mod.com
@@ -81,18 +87,32 @@ func main() {
81
87
}
82
88
`
83
89
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
+
84
99
tests := []struct {
85
100
name string // subtest name
86
101
response string // response to choose for the telemetry dialog
87
102
wantMode string // resulting telemetry mode
88
103
wantMsg string // substring contained in the follow-up popup (if empty, no popup is expected)
104
+ wantInc uint64 // expected 'prompt accepted' counter increment
89
105
}{
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 },
93
109
}
94
110
for _ , test := range tests {
95
111
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
+ }
96
116
modeFile := filepath .Join (t .TempDir (), "mode" )
97
117
msgRE := regexp .MustCompile (".*Would you like to enable Go telemetry?" )
98
118
respond := func (m * protocol.ShowMessageRequestParams ) (* protocol.MessageActionItem , error ) {
@@ -136,6 +156,13 @@ func main() {
136
156
if gotMode != test .wantMode {
137
157
t .Errorf ("after prompt, mode=%s, want %s" , gotMode , test .wantMode )
138
158
}
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
+ }
139
166
})
140
167
})
141
168
}
0 commit comments