| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 31 |
|
| 32 |
"diagnosticsDelay": "10ms", |
| 33 |
}, |
| 34 |
}, |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
|
| 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 |
|
| 50 |
func ProxyFiles(txt string) RunOption { |
| 51 |
return optionSetter(func(opts *runConfig) { |
| 52 |
opts.sandbox.ProxyFiles = fake.UnpackTxt(txt) |
| 53 |
}) |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
func WriteGoSum(dirs ...string) RunOption { |
| 62 |
return optionSetter(func(opts *runConfig) { |
| 63 |
opts.writeGoSum = dirs |
| 64 |
}) |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 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 |
|
| 82 |
func NoLogsOnError() RunOption { |
| 83 |
return optionSetter(func(opts *runConfig) { |
| 84 |
opts.noLogsOnError = true |
| 85 |
}) |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
func WindowsLineEndings() RunOption { |
| 90 |
return optionSetter(func(opts *runConfig) { |
| 91 |
opts.editor.WindowsLineEndings = true |
| 92 |
}) |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
func ClientName(name string) RunOption { |
| 97 |
return optionSetter(func(opts *runConfig) { |
| 98 |
opts.editor.ClientName = name |
| 99 |
}) |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
func CapabilitiesJSON(capabilities []byte) RunOption { |
| 104 |
return optionSetter(func(opts *runConfig) { |
| 105 |
opts.editor.CapabilitiesJSON = capabilities |
| 106 |
}) |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 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 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
func WorkspaceFolders(relFolders ...string) RunOption { |
| 127 |
if len(relFolders) == 0 { |
| 128 |
|
| 129 |
relFolders = []string{} |
| 130 |
} |
| 131 |
|
| 132 |
return optionSetter(func(opts *runConfig) { |
| 133 |
opts.editor.WorkspaceFolders = relFolders |
| 134 |
}) |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
func NoDefaultWorkspaceFiles() RunOption { |
| 141 |
return optionSetter(func(opts *runConfig) { |
| 142 |
opts.editor.NoDefaultWorkspaceFiles = true |
| 143 |
}) |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
func RootPath(relpath string) RunOption { |
| 148 |
return optionSetter(func(opts *runConfig) { |
| 149 |
opts.editor.RelRootPath = relpath |
| 150 |
}) |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
type FolderSettings map[string]Settings |
| 159 |
|
| 160 |
func (fs FolderSettings) set(opts *runConfig) { |
| 161 |
|
| 162 |
|
| 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 |
|
| 171 |
|
| 172 |
|
| 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 |
|
| 183 |
|
| 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 |
|
| 195 |
|
| 196 |
func InGOPATH() RunOption { |
| 197 |
return optionSetter(func(opts *runConfig) { |
| 198 |
opts.sandbox.InGoPath = true |
| 199 |
}) |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 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 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
func DelayMessages(upto time.Duration) RunOption { |
| 216 |
return optionSetter(func(opts *runConfig) { |
| 217 |
opts.editor.MaxMessageDelay = upto |
| 218 |
}) |
| 219 |
} |