-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathconfiguration_test.go
253 lines (218 loc) · 5.76 KB
/
configuration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package misc
import (
"testing"
. "golang.org/x/tools/gopls/internal/test/integration"
"golang.org/x/tools/internal/testenv"
)
// Test that enabling and disabling produces the expected results of showing
// and hiding staticcheck analysis results.
func TestChangeConfiguration(t *testing.T) {
// Staticcheck only supports Go versions >= 1.20.
// Note: keep this in sync with TestStaticcheckWarning. Below this version we
// should get an error when setting staticcheck configuration.
testenv.NeedsGo1Point(t, 20)
const files = `
-- go.mod --
module mod.com
go 1.12
-- a/a.go --
package a
import "errors"
// FooErr should be called ErrFoo (ST1012)
var FooErr = errors.New("foo")
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("a/a.go")
env.AfterChange(
NoDiagnostics(ForFile("a/a.go")),
)
cfg := env.Editor.Config()
cfg.Settings = map[string]any{
"staticcheck": true,
}
env.ChangeConfiguration(cfg)
env.AfterChange(
Diagnostics(env.AtRegexp("a/a.go", "var (FooErr)")),
)
})
}
func TestIdenticalConfiguration(t *testing.T) {
// This test checks that changing configuration does not cause views to be
// recreated if there is no configuration change.
const files = `
-- a.go --
package p
func _() {
var x *int
y := *x
_ = y
}
`
Run(t, files, func(t *testing.T, env *Env) {
// Sanity check: before disabling the nilness analyzer, we should have a
// diagnostic for the nil dereference.
env.OpenFile("a.go")
env.AfterChange(
Diagnostics(
ForFile("a.go"),
WithMessage("nil dereference"),
),
)
// Collect the view ID before changing configuration.
viewID := func() string {
t.Helper()
views := env.Views()
if len(views) != 1 {
t.Fatalf("got %d views, want 1", len(views))
}
return views[0].ID
}
before := viewID()
// Now disable the nilness analyzer.
cfg := env.Editor.Config()
cfg.Settings = map[string]any{
"analyses": map[string]any{
"nilness": false,
},
}
// This should cause the diagnostic to disappear...
env.ChangeConfiguration(cfg)
env.AfterChange(
NoDiagnostics(),
)
// ...and we should be on the second view.
after := viewID()
if after == before {
t.Errorf("after configuration change, got view %q (same as before), want new view", after)
}
// Now change configuration again, this time with the same configuration as
// before. We should still have no diagnostics...
env.ChangeConfiguration(cfg)
env.AfterChange(
NoDiagnostics(),
)
// ...and we should still be on the second view.
if got := viewID(); got != after {
t.Errorf("after second configuration change, got view %q, want %q", got, after)
}
})
}
// Test that clients can configure per-workspace configuration, which is
// queried via the scopeURI of a workspace/configuration request.
// (this was broken in golang/go#65519).
func TestWorkspaceConfiguration(t *testing.T) {
const files = `
-- go.mod --
module example.com/config
go 1.18
-- a/a.go --
package a
import "example.com/config/b"
func _() {
_ = b.B{2}
}
-- b/b.go --
package b
type B struct {
F int
}
`
WithOptions(
WorkspaceFolders("a"),
FolderSettings{
"a": {
"analyses": map[string]bool{
"composites": false,
},
},
},
).Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("a/a.go")
env.AfterChange(NoDiagnostics())
})
}
// TestMajorOptionsChange is like TestChangeConfiguration, but modifies an
// an open buffer before making a major (but inconsequential) change that
// causes gopls to recreate the view.
//
// Gopls should not get confused about buffer content when recreating the view.
func TestMajorOptionsChange(t *testing.T) {
testenv.NeedsGo1Point(t, 20) // needs staticcheck
const files = `
-- go.mod --
module mod.com
go 1.12
-- a/a.go --
package a
import "errors"
var ErrFoo = errors.New("foo")
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("a/a.go")
// Introduce a staticcheck diagnostic. It should be detected when we enable
// staticcheck later.
env.RegexpReplace("a/a.go", "ErrFoo", "FooErr")
env.AfterChange(
NoDiagnostics(ForFile("a/a.go")),
)
cfg := env.Editor.Config()
// Any change to environment recreates the view, but this should not cause
// gopls to get confused about the content of a/a.go: we should get the
// staticcheck diagnostic below.
cfg.Env = map[string]string{
"AN_ARBITRARY_VAR": "FOO",
}
cfg.Settings = map[string]interface{}{
"staticcheck": true,
}
env.ChangeConfiguration(cfg)
env.AfterChange(
Diagnostics(env.AtRegexp("a/a.go", "var (FooErr)")),
)
})
}
func TestStaticcheckWarning(t *testing.T) {
// Note: keep this in sync with TestChangeConfiguration.
testenv.SkipAfterGo1Point(t, 19)
const files = `
-- go.mod --
module mod.com
go 1.12
-- a/a.go --
package a
import "errors"
// FooErr should be called ErrFoo (ST1012)
var FooErr = errors.New("foo")
`
WithOptions(
Settings{"staticcheck": true},
).Run(t, files, func(t *testing.T, env *Env) {
env.OnceMet(
InitialWorkspaceLoad,
ShownMessage("staticcheck is not supported"),
)
})
}
func TestDeprecatedSettings(t *testing.T) {
WithOptions(
Settings{
"experimentalUseInvalidMetadata": true,
"experimentalWatchedFileDelay": "1s",
"experimentalWorkspaceModule": true,
"tempModfile": true,
"allowModfileModifications": true,
},
).Run(t, "", func(t *testing.T, env *Env) {
env.OnceMet(
InitialWorkspaceLoad,
ShownMessage("experimentalWorkspaceModule"),
ShownMessage("experimentalUseInvalidMetadata"),
ShownMessage("experimentalWatchedFileDelay"),
ShownMessage("tempModfile"),
ShownMessage("allowModfileModifications"),
)
})
}