Skip to content

Commit 6e6f313

Browse files
committed
internal/lsp/regtest: simplify, consolidate, and document settings
Configuration of LSP settings within the regression test runner had become a bit of a grab-bag: some were configured via explicit fields on EditorConfig, some via the catch-all EditorConfig.Settings field, and others via custom RunOption implementations. Consolidate these fields as follows: - Add an EnvVars and Settings field, for configuring environment and LSP settings. - Eliminate the EditorConfig RunOption wrapper. RunOptions help build the config. - Remove RunOptions that just wrap a key-value settings pair. By definition settings are user-facing and cannot change without breaking compatibility. Therefore, our tests can and should set the exact string keys they are using. - Eliminate the unused SendPID option. Also clean up some logic to change configuration. For golang/go#39384 Change-Id: Id5d1614f139550cbc62db2bab1d1e1f545ad9393 Reviewed-on: https://go-review.googlesource.com/c/tools/+/416876 TryBot-Result: Gopher Robot <gobot@golang.org> gopls-CI: kokoro <noreply+kokoro@google.com> Run-TryBot: Robert Findley <rfindley@google.com> Reviewed-by: Alan Donovan <adonovan@google.com>
1 parent 3db2cdc commit 6e6f313

25 files changed

+201
-301
lines changed

gopls/internal/regtest/bench/bench_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ func TestBenchmarkSymbols(t *testing.T) {
9393
}
9494

9595
opts := benchmarkOptions(symbolOptions.workdir)
96-
conf := EditorConfig{}
96+
settings := make(Settings)
9797
if symbolOptions.matcher != "" {
98-
conf.SymbolMatcher = &symbolOptions.matcher
98+
settings["symbolMatcher"] = symbolOptions.matcher
9999
}
100100
if symbolOptions.style != "" {
101-
conf.SymbolStyle = &symbolOptions.style
101+
settings["symbolStyle"] = symbolOptions.style
102102
}
103-
opts = append(opts, conf)
103+
opts = append(opts, settings)
104104

105105
WithOptions(opts...).Run(t, "", func(t *testing.T, env *Env) {
106106
// We can't Await in this test, since we have disabled hooks. Instead, run
@@ -200,9 +200,10 @@ func TestBenchmarkDidChange(t *testing.T) {
200200
// Always run it in isolation since it measures global heap usage.
201201
//
202202
// Kubernetes example:
203-
// $ go test -run=TestPrintMemStats -didchange_dir=$HOME/w/kubernetes
204-
// TotalAlloc: 5766 MB
205-
// HeapAlloc: 1984 MB
203+
//
204+
// $ go test -run=TestPrintMemStats -didchange_dir=$HOME/w/kubernetes
205+
// TotalAlloc: 5766 MB
206+
// HeapAlloc: 1984 MB
206207
//
207208
// Both figures exhibit variance of less than 1%.
208209
func TestPrintMemStats(t *testing.T) {

gopls/internal/regtest/codelens/codelens_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ const (
6363
for _, test := range tests {
6464
t.Run(test.label, func(t *testing.T) {
6565
WithOptions(
66-
EditorConfig{
67-
CodeLenses: test.enabled,
68-
},
66+
Settings{"codelenses": test.enabled},
6967
).Run(t, workspace, func(t *testing.T, env *Env) {
7068
env.OpenFile("lib.go")
7169
lens := env.CodeLens("lib.go")
@@ -308,10 +306,11 @@ func main() {
308306
}
309307
`
310308
WithOptions(
311-
EditorConfig{
312-
CodeLenses: map[string]bool{
309+
Settings{
310+
"codelenses": map[string]bool{
313311
"gc_details": true,
314-
}},
312+
},
313+
},
315314
).Run(t, mod, func(t *testing.T, env *Env) {
316315
env.OpenFile("main.go")
317316
env.ExecuteCodeLensCommand("main.go", command.GCDetails)

gopls/internal/regtest/completion/completion_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ func main() {
529529
}
530530
`
531531
WithOptions(
532-
EditorConfig{WindowsLineEndings: true},
532+
WindowsLineEndings(),
533533
).Run(t, src, func(t *testing.T, env *Env) {
534534
// Trigger unimported completions for the example.com/blah package.
535535
env.OpenFile("main.go")

gopls/internal/regtest/debug/debug_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ func TestBugNotification(t *testing.T) {
2121
// server.
2222
WithOptions(
2323
Modes(Singleton), // must be in-process to receive the bug report below
24-
EditorConfig{
25-
Settings: map[string]interface{}{
26-
"showBugReports": true,
27-
},
28-
},
24+
Settings{"showBugReports": true},
2925
).Run(t, "", func(t *testing.T, env *Env) {
3026
const desc = "got a bug"
3127
bug.Report(desc, nil)

gopls/internal/regtest/diagnostics/diagnostics_test.go

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -471,12 +471,11 @@ func _() {
471471
}
472472
`
473473
WithOptions(
474-
EditorConfig{
475-
Env: map[string]string{
476-
"GOPATH": "",
477-
"GO111MODULE": "off",
478-
},
479-
}).Run(t, files, func(t *testing.T, env *Env) {
474+
EnvVars{
475+
"GOPATH": "",
476+
"GO111MODULE": "off",
477+
},
478+
).Run(t, files, func(t *testing.T, env *Env) {
480479
env.OpenFile("main.go")
481480
env.Await(env.DiagnosticAtRegexp("main.go", "fmt"))
482481
env.SaveBuffer("main.go")
@@ -500,8 +499,9 @@ package x
500499
501500
var X = 0
502501
`
503-
editorConfig := EditorConfig{Env: map[string]string{"GOFLAGS": "-tags=foo"}}
504-
WithOptions(editorConfig).Run(t, files, func(t *testing.T, env *Env) {
502+
WithOptions(
503+
EnvVars{"GOFLAGS": "-tags=foo"},
504+
).Run(t, files, func(t *testing.T, env *Env) {
505505
env.OpenFile("main.go")
506506
env.OrganizeImports("main.go")
507507
env.Await(EmptyDiagnostics("main.go"))
@@ -573,9 +573,9 @@ hi mom
573573
`
574574
for _, go111module := range []string{"on", "off", ""} {
575575
t.Run(fmt.Sprintf("GO111MODULE_%v", go111module), func(t *testing.T) {
576-
WithOptions(EditorConfig{
577-
Env: map[string]string{"GO111MODULE": go111module},
578-
}).Run(t, files, func(t *testing.T, env *Env) {
576+
WithOptions(
577+
EnvVars{"GO111MODULE": go111module},
578+
).Run(t, files, func(t *testing.T, env *Env) {
579579
env.Await(
580580
NoOutstandingWork(),
581581
)
@@ -605,11 +605,7 @@ func main() {
605605
`
606606
WithOptions(
607607
InGOPATH(),
608-
EditorConfig{
609-
Env: map[string]string{
610-
"GO111MODULE": "off",
611-
},
612-
},
608+
EnvVars{"GO111MODULE": "off"},
613609
).Run(t, collision, func(t *testing.T, env *Env) {
614610
env.OpenFile("x/x.go")
615611
env.Await(
@@ -1236,7 +1232,7 @@ func main() {
12361232
})
12371233
WithOptions(
12381234
WorkspaceFolders("a"),
1239-
LimitWorkspaceScope(),
1235+
Settings{"expandWorkspaceToModule": false},
12401236
).Run(t, mod, func(t *testing.T, env *Env) {
12411237
env.OpenFile("a/main.go")
12421238
env.Await(
@@ -1267,11 +1263,7 @@ func main() {
12671263
`
12681264

12691265
WithOptions(
1270-
EditorConfig{
1271-
Settings: map[string]interface{}{
1272-
"staticcheck": true,
1273-
},
1274-
},
1266+
Settings{"staticcheck": true},
12751267
).Run(t, files, func(t *testing.T, env *Env) {
12761268
env.OpenFile("main.go")
12771269
var d protocol.PublishDiagnosticsParams
@@ -1381,9 +1373,7 @@ func b(c bytes.Buffer) {
13811373
}
13821374
`
13831375
WithOptions(
1384-
EditorConfig{
1385-
AllExperiments: true,
1386-
},
1376+
Settings{"allExperiments": true},
13871377
).Run(t, mod, func(t *testing.T, env *Env) {
13881378
// Confirm that the setting doesn't cause any warnings.
13891379
env.Await(NoShowMessage())
@@ -1495,11 +1485,7 @@ package foo_
14951485
WithOptions(
14961486
ProxyFiles(proxy),
14971487
InGOPATH(),
1498-
EditorConfig{
1499-
Env: map[string]string{
1500-
"GO111MODULE": "off",
1501-
},
1502-
},
1488+
EnvVars{"GO111MODULE": "off"},
15031489
).Run(t, contents, func(t *testing.T, env *Env) {
15041490
// Simulate typing character by character.
15051491
env.OpenFile("foo/foo_test.go")
@@ -1698,9 +1684,7 @@ import (
16981684
t.Run("GOPATH", func(t *testing.T) {
16991685
WithOptions(
17001686
InGOPATH(),
1701-
EditorConfig{
1702-
Env: map[string]string{"GO111MODULE": "off"},
1703-
},
1687+
EnvVars{"GO111MODULE": "off"},
17041688
Modes(Singleton),
17051689
).Run(t, mod, func(t *testing.T, env *Env) {
17061690
env.Await(
@@ -1729,11 +1713,7 @@ package b
17291713
t.Run("GO111MODULE="+go111module, func(t *testing.T) {
17301714
WithOptions(
17311715
Modes(Singleton),
1732-
EditorConfig{
1733-
Env: map[string]string{
1734-
"GO111MODULE": go111module,
1735-
},
1736-
},
1716+
EnvVars{"GO111MODULE": go111module},
17371717
).Run(t, modules, func(t *testing.T, env *Env) {
17381718
env.OpenFile("a/a.go")
17391719
env.OpenFile("b/go.mod")
@@ -1750,11 +1730,7 @@ package b
17501730
t.Run("GOPATH_GO111MODULE_auto", func(t *testing.T) {
17511731
WithOptions(
17521732
Modes(Singleton),
1753-
EditorConfig{
1754-
Env: map[string]string{
1755-
"GO111MODULE": "auto",
1756-
},
1757-
},
1733+
EnvVars{"GO111MODULE": "auto"},
17581734
InGOPATH(),
17591735
).Run(t, modules, func(t *testing.T, env *Env) {
17601736
env.OpenFile("a/a.go")
@@ -2026,9 +2002,7 @@ package a
20262002
func Hello() {}
20272003
`
20282004
WithOptions(
2029-
EditorConfig{
2030-
ExperimentalUseInvalidMetadata: true,
2031-
},
2005+
Settings{"experimentalUseInvalidMetadata": true},
20322006
Modes(Singleton),
20332007
).Run(t, mod, func(t *testing.T, env *Env) {
20342008
env.OpenFile("go.mod")
@@ -2082,9 +2056,7 @@ package main
20822056
func _() {}
20832057
`
20842058
WithOptions(
2085-
EditorConfig{
2086-
ExperimentalUseInvalidMetadata: true,
2087-
},
2059+
Settings{"experimentalUseInvalidMetadata": true},
20882060
// ExperimentalWorkspaceModule has a different failure mode for this
20892061
// case.
20902062
Modes(Singleton),

gopls/internal/regtest/inlayhints/inlayhints_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ const (
5656
for _, test := range tests {
5757
t.Run(test.label, func(t *testing.T) {
5858
WithOptions(
59-
EditorConfig{
60-
Settings: map[string]interface{}{
61-
"hints": test.enabled,
62-
},
59+
Settings{
60+
"hints": test.enabled,
6361
},
6462
).Run(t, workspace, func(t *testing.T, env *Env) {
6563
env.OpenFile("lib.go")

gopls/internal/regtest/misc/configuration_test.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
. "golang.org/x/tools/internal/lsp/regtest"
1111

12-
"golang.org/x/tools/internal/lsp/fake"
1312
"golang.org/x/tools/internal/testenv"
1413
)
1514

@@ -40,12 +39,11 @@ var FooErr = errors.New("foo")
4039
env.DoneWithOpen(),
4140
NoDiagnostics("a/a.go"),
4241
)
43-
cfg := &fake.EditorConfig{}
44-
*cfg = env.Editor.Config
42+
cfg := env.Editor.Config()
4543
cfg.Settings = map[string]interface{}{
4644
"staticcheck": true,
4745
}
48-
env.ChangeConfiguration(t, cfg)
46+
env.ChangeConfiguration(cfg)
4947
env.Await(
5048
DiagnosticAt("a/a.go", 5, 4),
5149
)
@@ -70,11 +68,9 @@ import "errors"
7068
var FooErr = errors.New("foo")
7169
`
7270

73-
WithOptions(EditorConfig{
74-
Settings: map[string]interface{}{
75-
"staticcheck": true,
76-
},
77-
}).Run(t, files, func(t *testing.T, env *Env) {
71+
WithOptions(
72+
Settings{"staticcheck": true},
73+
).Run(t, files, func(t *testing.T, env *Env) {
7874
env.Await(ShownMessage("staticcheck is not supported"))
7975
})
8076
}

gopls/internal/regtest/misc/definition_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ func main() {}
162162
} {
163163
t.Run(tt.importShortcut, func(t *testing.T) {
164164
WithOptions(
165-
EditorConfig{
166-
ImportShortcut: tt.importShortcut,
167-
},
165+
Settings{"importShortcut": tt.importShortcut},
168166
).Run(t, mod, func(t *testing.T, env *Env) {
169167
env.OpenFile("main.go")
170168
file, pos := env.GoToDefinition("main.go", env.RegexpSearch("main.go", `"fmt"`))

gopls/internal/regtest/misc/formatting_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,8 @@ const Bar = 42
352352
`
353353

354354
WithOptions(
355-
EditorConfig{
356-
Settings: map[string]interface{}{
357-
"gofumpt": true,
358-
},
355+
Settings{
356+
"gofumpt": true,
359357
},
360358
).Run(t, input, func(t *testing.T, env *Env) {
361359
env.OpenFile("foo.go")

gopls/internal/regtest/misc/imports_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,8 @@ var _, _ = x.X, y.Y
153153
t.Fatal(err)
154154
}
155155
defer os.RemoveAll(modcache)
156-
editorConfig := EditorConfig{Env: map[string]string{"GOMODCACHE": modcache}}
157156
WithOptions(
158-
editorConfig,
157+
EnvVars{"GOMODCACHE": modcache},
159158
ProxyFiles(proxy),
160159
).Run(t, files, func(t *testing.T, env *Env) {
161160
env.OpenFile("main.go")

gopls/internal/regtest/misc/link_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ const Hello = "Hello"
7575
}
7676

7777
// Then change the environment to make these links private.
78-
env.ChangeEnv(map[string]string{"GOPRIVATE": "import.test"})
78+
cfg := env.Editor.Config()
79+
cfg.Env = map[string]string{"GOPRIVATE": "import.test"}
80+
env.ChangeConfiguration(cfg)
7981

8082
// Finally, verify that the links are gone.
8183
content, _ = env.Hover("main.go", env.RegexpSearch("main.go", "pkg.Hello"))

gopls/internal/regtest/misc/semantictokens_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ func main() {}
2626
`
2727
WithOptions(
2828
Modes(Singleton),
29-
EditorConfig{
30-
AllExperiments: true,
31-
},
29+
Settings{"allExperiments": true},
3230
).Run(t, src, func(t *testing.T, env *Env) {
3331
params := &protocol.SemanticTokensParams{}
3432
const badURI = "http://foo"

gopls/internal/regtest/misc/settings_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ func main() {
2424
`
2525

2626
WithOptions(
27-
EditorConfig{
28-
Settings: map[string]interface{}{
29-
"directoryFilters": []string{""},
30-
},
31-
},
27+
Settings{"directoryFilters": []string{""}},
3228
).Run(t, src, func(t *testing.T, env *Env) {
3329
// No need to do anything. Issue golang/go#51843 is triggered by the empty
3430
// directory filter above.

gopls/internal/regtest/misc/shared_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func runShared(t *testing.T, testFunc func(env1 *Env, env2 *Env)) {
3030
WithOptions(Modes(modes)).Run(t, sharedProgram, func(t *testing.T, env1 *Env) {
3131
// Create a second test session connected to the same workspace and server
3232
// as the first.
33-
env2, cleanup := NewEnv(env1.Ctx, t, env1.Sandbox, env1.Server, env1.Editor.Config, true)
33+
env2, cleanup := NewEnv(env1.Ctx, t, env1.Sandbox, env1.Server, env1.Editor.Config(), true)
3434
defer cleanup()
3535
env2.Await(InitialWorkspaceLoad)
3636
testFunc(env1, env2)

gopls/internal/regtest/misc/staticcheck_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,9 @@ func testGenerics[P *T, T any](p P) {
6060
var FooErr error = errors.New("foo")
6161
`
6262

63-
WithOptions(EditorConfig{
64-
Settings: map[string]interface{}{
65-
"staticcheck": true,
66-
},
67-
}).Run(t, files, func(t *testing.T, env *Env) {
63+
WithOptions(
64+
Settings{"staticcheck": true},
65+
).Run(t, files, func(t *testing.T, env *Env) {
6866
env.OpenFile("a/a.go")
6967
env.Await(
7068
env.DiagnosticAtRegexpFromSource("a/a.go", "sort.Slice", "sortslice"),

0 commit comments

Comments
 (0)