Debian Codegraph @

gopls/internal/cache/session_test.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright 2023 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 cache
6
7 import (
8 "context"
9 "os"
10 "path"
11 "path/filepath"
12 "testing"
13
14 "github.com/google/go-cmp/cmp"
15 "golang.org/x/tools/gopls/internal/protocol"
16 "golang.org/x/tools/gopls/internal/settings"
17 "golang.org/x/tools/gopls/internal/test/integration/fake"
18 "golang.org/x/tools/internal/testenv"
19 )
20
21 func TestZeroConfigAlgorithm(t *testing.T) {
22 testenv.NeedsExec(t) // executes the Go command
23 t.Setenv("GOPACKAGESDRIVER", "off")
24
25 type viewSummary struct {
26 // fields exported for cmp.Diff
27 Type ViewType
28 Root string
29 Env []string
30 }
31
32 type folderSummary struct {
33 dir string
34 options func(dir string) map[string]any // options may refer to the temp dir
35 }
36
37 includeReplaceInWorkspace := func(string) map[string]any {
38 return map[string]any{
39 "includeReplaceInWorkspace": true,
40 }
41 }
42
43 type test struct {
44 name string
45 files map[string]string // use a map rather than txtar as file content is tiny
46 folders []folderSummary
47 open []string // open files
48 want []viewSummary
49 }
50
51 tests := []test{
52 // TODO(rfindley): add a test for GOPACKAGESDRIVER.
53 // Doing so doesn't yet work using options alone (user env is not honored)
54
55 // TODO(rfindley): add a test for degenerate cases, such as missing
56 // workspace folders (once we decide on the correct behavior).
57 {
58 "basic go.work workspace",
59 map[string]string{
60 "go.work": "go 1.18\nuse (\n\t./a\n\t./b\n)\n",
61 "a/go.mod": "module golang.org/a\ngo 1.18\n",
62 "b/go.mod": "module golang.org/b\ngo 1.18\n",
63 },
64 []folderSummary{{dir: "."}},
65 nil,
66 []viewSummary{{GoWorkView, ".", nil}},
67 },
68 {
69 "basic go.mod workspace",
70 map[string]string{
71 "go.mod": "module golang.org/a\ngo 1.18\n",
72 },
73 []folderSummary{{dir: "."}},
74 nil,
75 []viewSummary{{GoModView, ".", nil}},
76 },
77 {
78 "basic GOPATH workspace",
79 map[string]string{
80 "src/golang.org/a/a.go": "package a",
81 "src/golang.org/b/b.go": "package b",
82 },
83 []folderSummary{{
84 dir: "src",
85 options: func(dir string) map[string]any {
86 return map[string]any{
87 "env": map[string]any{
88 "GO111MODULE": "", // golang/go#70196: must be unset
89 "GOPATH": dir,
90 },
91 }
92 },
93 }},
94 []string{"src/golang.org/a//a.go", "src/golang.org/b/b.go"},
95 []viewSummary{{GOPATHView, "src", nil}},
96 },
97 {
98 "basic AdHoc workspace",
99 map[string]string{
100 "foo.go": "package foo",
101 },
102 []folderSummary{{dir: "."}},
103 nil,
104 []viewSummary{{AdHocView, ".", nil}},
105 },
106 {
107 "multi-folder workspace",
108 map[string]string{
109 "a/go.mod": "module golang.org/a\ngo 1.18\n",
110 "b/go.mod": "module golang.org/b\ngo 1.18\n",
111 },
112 []folderSummary{{dir: "a"}, {dir: "b"}},
113 nil,
114 []viewSummary{{GoModView, "a", nil}, {GoModView, "b", nil}},
115 },
116 {
117 "multi-module workspace",
118 map[string]string{
119 "a/go.mod": "module golang.org/a\ngo 1.18\n",
120 "b/go.mod": "module golang.org/b\ngo 1.18\n",
121 },
122 []folderSummary{{dir: "."}},
123 nil,
124 []viewSummary{{AdHocView, ".", nil}},
125 },
126 {
127 "zero-config open module",
128 map[string]string{
129 "a/go.mod": "module golang.org/a\ngo 1.18\n",
130 "a/a.go": "package a",
131 "b/go.mod": "module golang.org/b\ngo 1.18\n",
132 "b/b.go": "package b",
133 },
134 []folderSummary{{dir: "."}},
135 []string{"a/a.go"},
136 []viewSummary{
137 {AdHocView, ".", nil},
138 {GoModView, "a", nil},
139 },
140 },
141 {
142 "zero-config open modules",
143 map[string]string{
144 "a/go.mod": "module golang.org/a\ngo 1.18\n",
145 "a/a.go": "package a",
146 "b/go.mod": "module golang.org/b\ngo 1.18\n",
147 "b/b.go": "package b",
148 },
149 []folderSummary{{dir: "."}},
150 []string{"a/a.go", "b/b.go"},
151 []viewSummary{
152 {AdHocView, ".", nil},
153 {GoModView, "a", nil},
154 {GoModView, "b", nil},
155 },
156 },
157 {
158 "unified workspace",
159 map[string]string{
160 "go.work": "go 1.18\nuse (\n\t./a\n\t./b\n)\n",
161 "a/go.mod": "module golang.org/a\ngo 1.18\n",
162 "a/a.go": "package a",
163 "b/go.mod": "module golang.org/b\ngo 1.18\n",
164 "b/b.go": "package b",
165 },
166 []folderSummary{{dir: "."}},
167 []string{"a/a.go", "b/b.go"},
168 []viewSummary{{GoWorkView, ".", nil}},
169 },
170 {
171 "go.work from env",
172 map[string]string{
173 "nested/go.work": "go 1.18\nuse (\n\t../a\n\t../b\n)\n",
174 "a/go.mod": "module golang.org/a\ngo 1.18\n",
175 "a/a.go": "package a",
176 "b/go.mod": "module golang.org/b\ngo 1.18\n",
177 "b/b.go": "package b",
178 },
179 []folderSummary{{
180 dir: ".",
181 options: func(dir string) map[string]any {
182 return map[string]any{
183 "env": map[string]any{
184 "GOWORK": filepath.Join(dir, "nested", "go.work"),
185 },
186 }
187 },
188 }},
189 []string{"a/a.go", "b/b.go"},
190 []viewSummary{{GoWorkView, ".", nil}},
191 },
192 {
193 "independent module view",
194 map[string]string{
195 "go.work": "go 1.18\nuse (\n\t./a\n)\n", // not using b
196 "a/go.mod": "module golang.org/a\ngo 1.18\n",
197 "a/a.go": "package a",
198 "b/go.mod": "module golang.org/a\ngo 1.18\n",
199 "b/b.go": "package b",
200 },
201 []folderSummary{{dir: "."}},
202 []string{"a/a.go", "b/b.go"},
203 []viewSummary{
204 {GoWorkView, ".", nil},
205 {GoModView, "b", []string{"GOWORK=off"}},
206 },
207 },
208 {
209 "multiple go.work",
210 map[string]string{
211 "go.work": "go 1.18\nuse (\n\t./a\n\t./b\n)\n",
212 "a/go.mod": "module golang.org/a\ngo 1.18\n",
213 "a/a.go": "package a",
214 "b/go.work": "go 1.18\nuse (\n\t.\n\t./c\n)\n",
215 "b/go.mod": "module golang.org/b\ngo 1.18\n",
216 "b/b.go": "package b",
217 "b/c/go.mod": "module golang.org/c\ngo 1.18\n",
218 },
219 []folderSummary{{dir: "."}},
220 []string{"a/a.go", "b/b.go", "b/c/c.go"},
221 []viewSummary{{GoWorkView, ".", nil}, {GoWorkView, "b", nil}},
222 },
223 {
224 "multiple go.work, c unused",
225 map[string]string{
226 "go.work": "go 1.18\nuse (\n\t./a\n\t./b\n)\n",
227 "a/go.mod": "module golang.org/a\ngo 1.18\n",
228 "a/a.go": "package a",
229 "b/go.work": "go 1.18\nuse (\n\t.\n)\n",
230 "b/go.mod": "module golang.org/b\ngo 1.18\n",
231 "b/b.go": "package b",
232 "b/c/go.mod": "module golang.org/c\ngo 1.18\n",
233 },
234 []folderSummary{{dir: "."}},
235 []string{"a/a.go", "b/b.go", "b/c/c.go"},
236 []viewSummary{{GoWorkView, ".", nil}, {GoModView, "b/c", []string{"GOWORK=off"}}},
237 },
238 {
239 "go.mod with nested replace",
240 map[string]string{
241 "go.mod": "module golang.org/a\n require golang.org/b v1.2.3\nreplace example.com/b => ./b",
242 "a.go": "package a",
243 "b/go.mod": "module golang.org/b\ngo 1.18\n",
244 "b/b.go": "package b",
245 },
246 []folderSummary{{dir: ".", options: includeReplaceInWorkspace}},
247 []string{"a/a.go", "b/b.go"},
248 []viewSummary{{GoModView, ".", nil}},
249 },
250 {
251 "go.mod with parent replace, parent folder",
252 map[string]string{
253 "go.mod": "module golang.org/a",
254 "a.go": "package a",
255 "b/go.mod": "module golang.org/b\ngo 1.18\nrequire golang.org/a v1.2.3\nreplace golang.org/a => ../",
256 "b/b.go": "package b",
257 },
258 []folderSummary{{dir: ".", options: includeReplaceInWorkspace}},
259 []string{"a/a.go", "b/b.go"},
260 []viewSummary{{GoModView, ".", nil}, {GoModView, "b", nil}},
261 },
262 {
263 "go.mod with multiple replace",
264 map[string]string{
265 "go.mod": `
266 module golang.org/root
267
268 require (
269 golang.org/a v1.2.3
270 golang.org/b v1.2.3
271 golang.org/c v1.2.3
272 )
273
274 replace (
275 golang.org/b => ./b
276 golang.org/c => ./c
277 // Note: d is not replaced
278 )
279 `,
280 "a.go": "package a",
281 "b/go.mod": "module golang.org/b\ngo 1.18",
282 "b/b.go": "package b",
283 "c/go.mod": "module golang.org/c\ngo 1.18",
284 "c/c.go": "package c",
285 "d/go.mod": "module golang.org/d\ngo 1.18",
286 "d/d.go": "package d",
287 },
288 []folderSummary{{dir: ".", options: includeReplaceInWorkspace}},
289 []string{"b/b.go", "c/c.go", "d/d.go"},
290 []viewSummary{{GoModView, ".", nil}, {GoModView, "d", nil}},
291 },
292 {
293 "go.mod with replace outside the workspace",
294 map[string]string{
295 "go.mod": "module golang.org/a\ngo 1.18",
296 "a.go": "package a",
297 "b/go.mod": "module golang.org/b\ngo 1.18\nrequire golang.org/a v1.2.3\nreplace golang.org/a => ../",
298 "b/b.go": "package b",
299 },
300 []folderSummary{{dir: "b"}},
301 []string{"a.go", "b/b.go"},
302 []viewSummary{{GoModView, "b", nil}},
303 },
304 {
305 "go.mod with replace directive; workspace replace off",
306 map[string]string{
307 "go.mod": "module golang.org/a\n require golang.org/b v1.2.3\nreplace example.com/b => ./b",
308 "a.go": "package a",
309 "b/go.mod": "module golang.org/b\ngo 1.18\n",
310 "b/b.go": "package b",
311 },
312 []folderSummary{{
313 dir: ".",
314 options: func(string) map[string]any {
315 return map[string]any{
316 "includeReplaceInWorkspace": false,
317 }
318 },
319 }},
320 []string{"a/a.go", "b/b.go"},
321 []viewSummary{{GoModView, ".", nil}, {GoModView, "b", nil}},
322 },
323 }
324
325 for _, test := range tests {
326 ctx := context.Background()
327 t.Run(test.name, func(t *testing.T) {
328 dir := writeFiles(t, test.files)
329 rel := fake.RelativeTo(dir)
330 fs := newMemoizedFS()
331
332 toURI := func(path string) protocol.DocumentURI {
333 return protocol.URIFromPath(rel.AbsPath(path))
334 }
335
336 var folders []*Folder
337 for _, f := range test.folders {
338 opts := settings.DefaultOptions()
339 if f.options != nil {
340 _, errs := opts.Set(f.options(dir))
341 for _, err := range errs {
342 t.Fatal(err)
343 }
344 }
345 uri := toURI(f.dir)
346 env, err := FetchGoEnv(ctx, uri, opts)
347 if err != nil {
348 t.Fatalf("FetchGoEnv failed: %v", err)
349 }
350 t.Logf("FetchGoEnv(%q) = %+v", uri, env)
351 folders = append(folders, &Folder{
352 Dir: uri,
353 Name: path.Base(f.dir),
354 Options: opts,
355 Env: *env,
356 })
357 }
358
359 var openFiles []protocol.DocumentURI
360 for _, path := range test.open {
361 openFiles = append(openFiles, toURI(path))
362 }
363
364 defs, err := selectViewDefs(ctx, fs, folders, openFiles)
365 if err != nil {
366 t.Fatal(err)
367 }
368 var got []viewSummary
369 for _, def := range defs {
370 got = append(got, viewSummary{
371 Type: def.Type(),
372 Root: rel.RelPath(def.root.Path()),
373 Env: def.EnvOverlay(),
374 })
375 }
376 if diff := cmp.Diff(test.want, got); diff != "" {
377 t.Errorf("selectViews() mismatch (-want +got):\n%s", diff)
378 }
379 })
380 }
381 }
382
383 // TODO(rfindley): this function could be meaningfully factored with the
384 // various other test helpers of this nature.
385 func writeFiles(t *testing.T, files map[string]string) string {
386 root := t.TempDir()
387
388 // This unfortunate step is required because gopls output
389 // expands symbolic links in its input file names (arguably it
390 // should not), and on macOS the temp dir is in /var -> private/var.
391 root, err := filepath.EvalSymlinks(root)
392 if err != nil {
393 t.Fatal(err)
394 }
395
396 for name, content := range files {
397 filename := filepath.Join(root, name)
398 if err := os.MkdirAll(filepath.Dir(filename), 0777); err != nil {
399 t.Fatal(err)
400 }
401 if err := os.WriteFile(filename, []byte(content), 0666); err != nil {
402 t.Fatal(err)
403 }
404 }
405 return root
406 }