Debian Codegraph @

gopls/internal/test/integration/options.go

raw symbols SCIP
1 // Copyright 2022 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 integration
6
7 import (
8 "maps"
9 "strings"
10 "testing"
11 "time"
12
13 "golang.org/x/tools/gopls/internal/protocol"
14 "golang.org/x/tools/gopls/internal/test/integration/fake"
15 "golang.org/x/tools/internal/drivertest"
16 )
17
18 type runConfig struct {
19 editor fake.EditorConfig
20 sandbox fake.SandboxConfig
21 modes Mode
22 noLogsOnError bool
23 writeGoSum []string
24 }
25
26 func defaultConfig() runConfig {
27 return runConfig{
28 editor: fake.EditorConfig{
29 Settings: map[string]any{
30 // Shorten the diagnostic delay to speed up test execution (else we'd add
31 // the default delay to each assertion about diagnostics)
32 "diagnosticsDelay": "10ms",
33 },
34 },
35 }
36 }
37
38 // A RunOption augments the behavior of the test runner.
39 type RunOption interface {
40 set(*runConfig)
41 }
42
43 type optionSetter func(*runConfig)
44
45 func (f optionSetter) set(opts *runConfig) {
46 f(opts)
47 }
48
49 // ProxyFiles configures a file proxy using the given txtar-encoded string.
50 func ProxyFiles(txt string) RunOption {
51 return optionSetter(func(opts *runConfig) {
52 opts.sandbox.ProxyFiles = fake.UnpackTxt(txt)
53 })
54 }
55
56 // WriteGoSum causes the environment to write a go.sum file for the requested
57 // relative directories (via `go list -mod=mod`), before starting gopls.
58 //
59 // Useful for tests that use ProxyFiles, but don't care about crafting the
60 // go.sum content.
61 func WriteGoSum(dirs ...string) RunOption {
62 return optionSetter(func(opts *runConfig) {
63 opts.writeGoSum = dirs
64 })
65 }
66
67 // Modes configures the execution modes that the test should run in.
68 //
69 // By default, modes are configured by the test runner. If this option is set,
70 // it overrides the set of default modes and the test runs in exactly these
71 // modes.
72 func Modes(modes Mode) RunOption {
73 return optionSetter(func(opts *runConfig) {
74 if opts.modes != 0 {
75 panic("modes set more than once")
76 }
77 opts.modes = modes
78 })
79 }
80
81 // NoLogsOnError turns off dumping the LSP logs on test failures.
82 func NoLogsOnError() RunOption {
83 return optionSetter(func(opts *runConfig) {
84 opts.noLogsOnError = true
85 })
86 }
87
88 // WindowsLineEndings configures the editor to use windows line endings.
89 func WindowsLineEndings() RunOption {
90 return optionSetter(func(opts *runConfig) {
91 opts.editor.WindowsLineEndings = true
92 })
93 }
94
95 // ClientName sets the LSP client name.
96 func ClientName(name string) RunOption {
97 return optionSetter(func(opts *runConfig) {
98 opts.editor.ClientName = name
99 })
100 }
101
102 // CapabilitiesJSON sets the capabalities json.
103 func CapabilitiesJSON(capabilities []byte) RunOption {
104 return optionSetter(func(opts *runConfig) {
105 opts.editor.CapabilitiesJSON = capabilities
106 })
107 }
108
109 // Settings sets user-provided configuration for the LSP server.
110 //
111 // As a special case, the env setting must not be provided via Settings: use
112 // EnvVars instead.
113 type Settings map[string]any
114
115 func (s Settings) set(opts *runConfig) {
116 if opts.editor.Settings == nil {
117 opts.editor.Settings = make(map[string]any)
118 }
119 maps.Copy(opts.editor.Settings, s)
120 }
121
122 // WorkspaceFolders configures the workdir-relative workspace folders or uri
123 // to send to the LSP server. By default the editor sends a single workspace folder
124 // corresponding to the workdir root. To explicitly configure no workspace
125 // folders, use WorkspaceFolders with no arguments.
126 func WorkspaceFolders(relFolders ...string) RunOption {
127 if len(relFolders) == 0 {
128 // Use an empty non-nil slice to signal explicitly no folders.
129 relFolders = []string{}
130 }
131
132 return optionSetter(func(opts *runConfig) {
133 opts.editor.WorkspaceFolders = relFolders
134 })
135 }
136
137 // NoDefaultWorkspaceFiles is used to specify whether the fake editor
138 // should give a default workspace folder to the LSP server.
139 // When it's true, the editor will pass original WorkspaceFolders to the LSP server.
140 func NoDefaultWorkspaceFiles() RunOption {
141 return optionSetter(func(opts *runConfig) {
142 opts.editor.NoDefaultWorkspaceFiles = true
143 })
144 }
145
146 // RootPath configures the roo path which will be converted to rootUri and sent to the LSP server.
147 func RootPath(relpath string) RunOption {
148 return optionSetter(func(opts *runConfig) {
149 opts.editor.RelRootPath = relpath
150 })
151 }
152
153 // FolderSettings defines per-folder workspace settings, keyed by relative path
154 // to the folder.
155 //
156 // Use in conjunction with WorkspaceFolders to have different settings for
157 // different folders.
158 type FolderSettings map[string]Settings
159
160 func (fs FolderSettings) set(opts *runConfig) {
161 // Re-use the Settings type, for symmetry, but translate back into maps for
162 // the editor config.
163 folders := make(map[string]map[string]any)
164 for k, v := range fs {
165 folders[k] = v
166 }
167 opts.editor.FolderSettings = folders
168 }
169
170 // EnvVars sets environment variables for the LSP session. When applying these
171 // variables to the session, the special string $SANDBOX_WORKDIR is replaced by
172 // the absolute path to the sandbox working directory.
173 type EnvVars map[string]string
174
175 func (e EnvVars) set(opts *runConfig) {
176 if opts.editor.Env == nil {
177 opts.editor.Env = make(map[string]string)
178 }
179 maps.Copy(opts.editor.Env, e)
180 }
181
182 // FakeGoPackagesDriver configures gopls to run with a fake GOPACKAGESDRIVER
183 // environment variable.
184 func FakeGoPackagesDriver(t *testing.T) RunOption {
185 env := drivertest.Env(t)
186 vars := make(EnvVars)
187 for _, e := range env {
188 kv := strings.SplitN(e, "=", 2)
189 vars[kv[0]] = kv[1]
190 }
191 return vars
192 }
193
194 // InGOPATH configures the workspace working directory to be GOPATH, rather
195 // than a separate working directory for use with modules.
196 func InGOPATH() RunOption {
197 return optionSetter(func(opts *runConfig) {
198 opts.sandbox.InGoPath = true
199 })
200 }
201
202 // MessageResponder configures the editor to respond to
203 // window/showMessageRequest messages using the provided function.
204 func MessageResponder(f func(*protocol.ShowMessageRequestParams) (*protocol.MessageActionItem, error)) RunOption {
205 return optionSetter(func(opts *runConfig) {
206 opts.editor.MessageResponder = f
207 })
208 }
209
210 // DelayMessages can be used to fuzz message delivery delays for the purpose of
211 // reproducing test flakes.
212 //
213 // (Even though this option may be unused, keep it around to aid in debugging
214 // future flakes.)
215 func DelayMessages(upto time.Duration) RunOption {
216 return optionSetter(func(opts *runConfig) {
217 opts.editor.MaxMessageDelay = upto
218 })
219 }