Debian Codegraph @

gopls/internal/test/integration/misc/configuration_test.go

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