Debian Codegraph @

gopls/internal/test/integration/watch/watch_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 watch
6
7 import (
8 "os"
9 "testing"
10
11 . "golang.org/x/tools/gopls/internal/test/integration"
12 "golang.org/x/tools/gopls/internal/util/bug"
13
14 "golang.org/x/tools/gopls/internal/protocol"
15 "golang.org/x/tools/gopls/internal/test/integration/fake"
16 )
17
18 func TestMain(m *testing.M) {
19 bug.PanicOnBugs = true
20 os.Exit(Main(m))
21 }
22
23 func TestEditFile(t *testing.T) {
24 const pkg = `
25 -- go.mod --
26 module mod.com
27
28 go 1.14
29 -- a/a.go --
30 package a
31
32 func _() {
33 var x int
34 }
35 `
36 // Edit the file when it's *not open* in the workspace, and check that
37 // diagnostics are updated.
38 t.Run("unopened", func(t *testing.T) {
39 Run(t, pkg, func(t *testing.T, env *Env) {
40 env.OnceMet(
41 InitialWorkspaceLoad,
42 Diagnostics(env.AtRegexp("a/a.go", "x")),
43 )
44 env.WriteWorkspaceFile("a/a.go", `package a; func _() {};`)
45 env.AfterChange(
46 NoDiagnostics(ForFile("a/a.go")),
47 )
48 })
49 })
50
51 // Edit the file when it *is open* in the workspace, and check that
52 // diagnostics are *not* updated.
53 t.Run("opened", func(t *testing.T) {
54 Run(t, pkg, func(t *testing.T, env *Env) {
55 env.OpenFile("a/a.go")
56 // Insert a trivial edit so that we don't automatically update the buffer
57 // (see CL 267577).
58 env.EditBuffer("a/a.go", fake.NewEdit(0, 0, 0, 0, " "))
59 env.AfterChange()
60 env.WriteWorkspaceFile("a/a.go", `package a; func _() {};`)
61 env.AfterChange(
62 Diagnostics(env.AtRegexp("a/a.go", "x")),
63 )
64 })
65 })
66 }
67
68 // Edit a dependency on disk and expect a new diagnostic.
69 func TestEditDependency(t *testing.T) {
70 const pkg = `
71 -- go.mod --
72 module mod.com
73
74 go 1.14
75 -- b/b.go --
76 package b
77
78 func B() int { return 0 }
79 -- a/a.go --
80 package a
81
82 import (
83 "mod.com/b"
84 )
85
86 func _() {
87 _ = b.B()
88 }
89 `
90 Run(t, pkg, func(t *testing.T, env *Env) {
91 env.OpenFile("a/a.go")
92 env.AfterChange()
93 env.WriteWorkspaceFile("b/b.go", `package b; func B() {};`)
94 env.AfterChange(
95 Diagnostics(env.AtRegexp("a/a.go", "b.B")),
96 )
97 })
98 }
99
100 // Edit both the current file and one of its dependencies on disk and
101 // expect diagnostic changes.
102 func TestEditFileAndDependency(t *testing.T) {
103 const pkg = `
104 -- go.mod --
105 module mod.com
106
107 go 1.14
108 -- b/b.go --
109 package b
110
111 func B() int { return 0 }
112 -- a/a.go --
113 package a
114
115 import (
116 "mod.com/b"
117 )
118
119 func _() {
120 var x int
121 _ = b.B()
122 }
123 `
124 Run(t, pkg, func(t *testing.T, env *Env) {
125 env.OnceMet(
126 InitialWorkspaceLoad,
127 Diagnostics(env.AtRegexp("a/a.go", "x")),
128 )
129 env.WriteWorkspaceFiles(map[string]string{
130 "b/b.go": `package b; func B() {};`,
131 "a/a.go": `package a
132
133 import "mod.com/b"
134
135 func _() {
136 b.B()
137 }`,
138 })
139 env.AfterChange(
140 NoDiagnostics(ForFile("a/a.go")),
141 NoDiagnostics(ForFile("b/b.go")),
142 )
143 })
144 }
145
146 // Delete a dependency and expect a new diagnostic.
147 func TestDeleteDependency(t *testing.T) {
148 const pkg = `
149 -- go.mod --
150 module mod.com
151
152 go 1.14
153 -- b/b.go --
154 package b
155
156 func B() int { return 0 }
157 -- a/a.go --
158 package a
159
160 import (
161 "mod.com/b"
162 )
163
164 func _() {
165 _ = b.B()
166 }
167 `
168 Run(t, pkg, func(t *testing.T, env *Env) {
169 env.OpenFile("a/a.go")
170 env.AfterChange()
171 env.RemoveWorkspaceFile("b/b.go")
172 env.AfterChange(
173 Diagnostics(env.AtRegexp("a/a.go", "\"mod.com/b\"")),
174 )
175 })
176 }
177
178 // Create a dependency on disk and expect the diagnostic to go away.
179 func TestCreateDependency(t *testing.T) {
180 const missing = `
181 -- go.mod --
182 module mod.com
183
184 go 1.14
185 -- b/b.go --
186 package b
187
188 func B() int { return 0 }
189 -- a/a.go --
190 package a
191
192 import (
193 "mod.com/c"
194 )
195
196 func _() {
197 c.C()
198 }
199 `
200 Run(t, missing, func(t *testing.T, env *Env) {
201 env.OnceMet(
202 InitialWorkspaceLoad,
203 Diagnostics(env.AtRegexp("a/a.go", "\"mod.com/c\"")),
204 )
205 env.WriteWorkspaceFile("c/c.go", `package c; func C() {};`)
206 env.AfterChange(
207 NoDiagnostics(ForFile("a/a.go")),
208 )
209 })
210 }
211
212 // Create a new dependency and add it to the file on disk.
213 // This is similar to what might happen if you switch branches.
214 func TestCreateAndAddDependency(t *testing.T) {
215 const original = `
216 -- go.mod --
217 module mod.com
218
219 go 1.14
220 -- a/a.go --
221 package a
222
223 func _() {}
224 `
225 Run(t, original, func(t *testing.T, env *Env) {
226 env.WriteWorkspaceFile("c/c.go", `package c; func C() {};`)
227 env.WriteWorkspaceFile("a/a.go", `package a; import "mod.com/c"; func _() { c.C() }`)
228 env.AfterChange(
229 NoDiagnostics(ForFile("a/a.go")),
230 )
231 })
232 }
233
234 // Create a new file that defines a new symbol, in the same package.
235 func TestCreateFile(t *testing.T) {
236 const pkg = `
237 -- go.mod --
238 module mod.com
239
240 go 1.14
241 -- a/a.go --
242 package a
243
244 func _() {
245 hello()
246 }
247 `
248 Run(t, pkg, func(t *testing.T, env *Env) {
249 env.OnceMet(
250 InitialWorkspaceLoad,
251 Diagnostics(env.AtRegexp("a/a.go", "hello")),
252 )
253 env.WriteWorkspaceFile("a/a2.go", `package a; func hello() {};`)
254 env.AfterChange(
255 NoDiagnostics(ForFile("a/a.go")),
256 )
257 })
258 }
259
260 // Add a new method to an interface and implement it.
261 // Inspired by the structure of internal/golang and internal/cache.
262 func TestCreateImplementation(t *testing.T) {
263 const pkg = `
264 -- go.mod --
265 module mod.com
266
267 go 1.14
268 -- b/b.go --
269 package b
270
271 type B interface{
272 Hello() string
273 }
274
275 func SayHello(bee B) {
276 println(bee.Hello())
277 }
278 -- a/a.go --
279 package a
280
281 import "mod.com/b"
282
283 type X struct {}
284
285 func (_ X) Hello() string {
286 return ""
287 }
288
289 func _() {
290 x := X{}
291 b.SayHello(x)
292 }
293 `
294 const newMethod = `package b
295 type B interface{
296 Hello() string
297 Bye() string
298 }
299
300 func SayHello(bee B) {
301 println(bee.Hello())
302 }`
303 const implementation = `package a
304
305 import "mod.com/b"
306
307 type X struct {}
308
309 func (_ X) Hello() string {
310 return ""
311 }
312
313 func (_ X) Bye() string {
314 return ""
315 }
316
317 func _() {
318 x := X{}
319 b.SayHello(x)
320 }`
321
322 // Add the new method before the implementation. Expect diagnostics.
323 t.Run("method before implementation", func(t *testing.T) {
324 Run(t, pkg, func(t *testing.T, env *Env) {
325 env.WriteWorkspaceFile("b/b.go", newMethod)
326 env.AfterChange(
327 Diagnostics(AtPosition("a/a.go", 12, 12)),
328 )
329 env.WriteWorkspaceFile("a/a.go", implementation)
330 env.AfterChange(
331 NoDiagnostics(ForFile("a/a.go")),
332 )
333 })
334 })
335 // Add the new implementation before the new method. Expect no diagnostics.
336 t.Run("implementation before method", func(t *testing.T) {
337 Run(t, pkg, func(t *testing.T, env *Env) {
338 env.WriteWorkspaceFile("a/a.go", implementation)
339 env.AfterChange(
340 NoDiagnostics(ForFile("a/a.go")),
341 )
342 env.WriteWorkspaceFile("b/b.go", newMethod)
343 env.AfterChange(
344 NoDiagnostics(ForFile("a/a.go")),
345 )
346 })
347 })
348 // Add both simultaneously. Expect no diagnostics.
349 t.Run("implementation and method simultaneously", func(t *testing.T) {
350 Run(t, pkg, func(t *testing.T, env *Env) {
351 env.WriteWorkspaceFiles(map[string]string{
352 "a/a.go": implementation,
353 "b/b.go": newMethod,
354 })
355 env.AfterChange(
356 NoDiagnostics(ForFile("a/a.go")),
357 NoDiagnostics(ForFile("b/b.go")),
358 )
359 })
360 })
361 }
362
363 // Tests golang/go#38498. Delete a file and then force a reload.
364 // Assert that we no longer try to load the file.
365 func TestDeleteFiles(t *testing.T) {
366 // TODO(rfindley): this test is brittle, because it depends on underspecified
367 // logging behavior around loads.
368 //
369 // We should have a robust way to test loads. It should be possible to assert
370 // on the specific loads that have occurred, and without the synchronization
371 // problems associated with logging.
372
373 const pkg = `
374 -- go.mod --
375 module mod.com
376
377 go 1.14
378 -- a/a.go --
379 package a
380
381 func _() {
382 var _ int
383 }
384 -- a/a_unneeded.go --
385 package a
386 `
387 t.Run("close then delete", func(t *testing.T) {
388 WithOptions(
389 // verboseOutput causes Snapshot.load to log package files.
390 // (see the TODO above: this is brittle)
391 Settings{"verboseOutput": true},
392 ).Run(t, pkg, func(t *testing.T, env *Env) {
393 env.OpenFile("a/a.go")
394 env.OpenFile("a/a_unneeded.go")
395 env.Await(
396 // Log messages are asynchronous to other events on the LSP stream, so we
397 // can't use OnceMet or AfterChange here.
398 LogMatching(protocol.Info, "a_unneeded.go", 1, false),
399 )
400
401 // Close and delete the open file, mimicking what an editor would do.
402 env.CloseBuffer("a/a_unneeded.go")
403 env.RemoveWorkspaceFile("a/a_unneeded.go")
404 env.RegexpReplace("a/a.go", "var _ int", "fmt.Println(\"\")")
405 env.AfterChange(
406 Diagnostics(env.AtRegexp("a/a.go", "fmt")),
407 )
408 env.SaveBuffer("a/a.go")
409 env.Await(
410 // There should only be one log message containing
411 // a_unneeded.go, from the initial workspace load, which we
412 // check for earlier. If there are more, there's a bug.
413 LogMatching(protocol.Info, "a_unneeded.go", 1, false),
414 NoDiagnostics(ForFile("a/a.go")),
415 )
416 })
417 })
418
419 t.Run("delete then close", func(t *testing.T) {
420 WithOptions(
421 Settings{"verboseOutput": true},
422 ).Run(t, pkg, func(t *testing.T, env *Env) {
423 env.OpenFile("a/a.go")
424 env.OpenFile("a/a_unneeded.go")
425 env.Await(
426 LogMatching(protocol.Info, "a_unneeded.go", 1, false),
427 )
428
429 // Delete and then close the file.
430 env.RemoveWorkspaceFile("a/a_unneeded.go")
431 env.CloseBuffer("a/a_unneeded.go")
432 env.RegexpReplace("a/a.go", "var _ int", "fmt.Println(\"\")")
433 env.AfterChange(
434 Diagnostics(env.AtRegexp("a/a.go", "fmt")),
435 )
436 env.SaveBuffer("a/a.go")
437 env.Await(
438 // There should only be one log message containing
439 // a_unneeded.go, from the initial workspace load, which we
440 // check for earlier. If there are more, there's a bug.
441 LogMatching(protocol.Info, "a_unneeded.go", 1, false),
442 NoDiagnostics(ForFile("a/a.go")),
443 )
444 })
445 })
446 }
447
448 // This change reproduces the behavior of switching branches, with multiple
449 // files being created and deleted. The key change here is the movement of a
450 // symbol from one file to another in a given package through a deletion and
451 // creation. To reproduce an issue with metadata invalidation in batched
452 // changes, the last change in the batch is an on-disk file change that doesn't
453 // require metadata invalidation.
454 func TestMoveSymbol(t *testing.T) {
455 const pkg = `
456 -- go.mod --
457 module mod.com
458
459 go 1.14
460 -- main.go --
461 package main
462
463 import "mod.com/a"
464
465 func main() {
466 var x int
467 x = a.Hello
468 println(x)
469 }
470 -- a/a1.go --
471 package a
472
473 var Hello int
474 -- a/a2.go --
475 package a
476
477 func _() {}
478 `
479 Run(t, pkg, func(t *testing.T, env *Env) {
480 env.WriteWorkspaceFile("a/a3.go", "package a\n\nvar Hello int\n")
481 env.RemoveWorkspaceFile("a/a1.go")
482 env.WriteWorkspaceFile("a/a2.go", "package a; func _() {};")
483 env.AfterChange(
484 NoDiagnostics(ForFile("main.go")),
485 )
486 })
487 }
488
489 // Reproduce golang/go#40456.
490 func TestChangeVersion(t *testing.T) {
491 const proxy = `
492 -- [email protected]/go.mod --
493 module example.com
494
495 go 1.12
496 -- [email protected]/blah/blah.go --
497 package blah
498
499 const Name = "Blah"
500
501 func X(x int) {}
502 -- [email protected]/go.mod --
503 module example.com
504
505 go 1.12
506 -- [email protected]/blah/blah.go --
507 package blah
508
509 const Name = "Blah"
510
511 func X() {}
512 -- [email protected]/go.mod --
513 module random.org
514
515 go 1.12
516 -- [email protected]/blah/blah.go --
517 package hello
518
519 const Name = "Hello"
520 `
521 const mod = `
522 -- go.mod --
523 module mod.com
524
525 go 1.12
526
527 require example.com v1.2.2
528 -- main.go --
529 package main
530
531 import "example.com/blah"
532
533 func main() {
534 blah.X()
535 }
536 `
537 WithOptions(
538 WriteGoSum("."),
539 ProxyFiles(proxy)).Run(t, mod, func(t *testing.T, env *Env) {
540 env.WriteWorkspaceFiles(map[string]string{
541 "go.mod": `module mod.com
542
543 go 1.12
544
545 require example.com v1.2.3
546 `,
547 "main.go": `package main
548
549 import (
550 "example.com/blah"
551 )
552
553 func main() {
554 blah.X(1)
555 }
556 `,
557 })
558 env.AfterChange(
559 env.DoneWithChangeWatchedFiles(),
560 NoDiagnostics(ForFile("main.go")),
561 )
562 })
563 }
564
565 // Reproduces golang/go#40340.
566 func TestSwitchFromGOPATHToModuleMode(t *testing.T) {
567 const files = `
568 -- foo/blah/blah.go --
569 package blah
570
571 const Name = ""
572 -- main.go --
573 package main
574
575 import "foo/blah"
576
577 func main() {
578 _ = blah.Name
579 }
580 `
581 WithOptions(
582 InGOPATH(),
583 Modes(Default), // golang/go#57521: this test is temporarily failing in 'experimental' mode
584 EnvVars{"GO111MODULE": "auto"},
585 ).Run(t, files, func(t *testing.T, env *Env) {
586 env.OpenFile("main.go")
587 env.AfterChange(
588 NoDiagnostics(ForFile("main.go")),
589 )
590 if _, err := env.Sandbox.RunGoCommand(env.Ctx, "", "mod", []string{"init", "mod.com"}, nil, true); err != nil {
591 t.Fatal(err)
592 }
593
594 // TODO(golang/go#57558, golang/go#57512): file watching is asynchronous,
595 // and we must wait for the view to be reconstructed before touching
596 // main.go, so that the new view "knows" about main.go. This is a bug, but
597 // awaiting the change here avoids it.
598 env.AfterChange()
599
600 env.RegexpReplace("main.go", `"foo/blah"`, `"mod.com/foo/blah"`)
601 env.AfterChange(
602 NoDiagnostics(ForFile("main.go")),
603 )
604 })
605 }
606
607 // Reproduces golang/go#40487.
608 func TestSwitchFromModulesToGOPATH(t *testing.T) {
609 const files = `
610 -- foo/go.mod --
611 module mod.com
612
613 go 1.14
614 -- foo/blah/blah.go --
615 package blah
616
617 const Name = ""
618 -- foo/main.go --
619 package main
620
621 import "mod.com/blah"
622
623 func main() {
624 _ = blah.Name
625 }
626 `
627 WithOptions(
628 InGOPATH(),
629 EnvVars{"GO111MODULE": "auto"},
630 ).Run(t, files, func(t *testing.T, env *Env) {
631 env.OpenFile("foo/main.go")
632 env.RemoveWorkspaceFile("foo/go.mod")
633 env.AfterChange(
634 Diagnostics(env.AtRegexp("foo/main.go", `"mod.com/blah"`)),
635 )
636 env.RegexpReplace("foo/main.go", `"mod.com/blah"`, `"foo/blah"`)
637 env.AfterChange(
638 NoDiagnostics(ForFile("foo/main.go")),
639 )
640 })
641 }
642
643 func TestNewSymbolInTestVariant(t *testing.T) {
644 const files = `
645 -- go.mod --
646 module mod.com
647
648 go 1.12
649 -- a/a.go --
650 package a
651
652 func bob() {}
653 -- a/a_test.go --
654 package a
655
656 import "testing"
657
658 func TestBob(t *testing.T) {
659 bob()
660 }
661 `
662 Run(t, files, func(t *testing.T, env *Env) {
663 // Add a new symbol to the package under test and use it in the test
664 // variant. Expect no diagnostics.
665 env.WriteWorkspaceFiles(map[string]string{
666 "a/a.go": `package a
667
668 func bob() {}
669 func george() {}
670 `,
671 "a/a_test.go": `package a
672
673 import "testing"
674
675 func TestAll(t *testing.T) {
676 bob()
677 george()
678 }
679 `,
680 })
681 env.AfterChange(
682 NoDiagnostics(ForFile("a/a.go")),
683 NoDiagnostics(ForFile("a/a_test.go")),
684 )
685 // Now, add a new file to the test variant and use its symbol in the
686 // original test file. Expect no diagnostics.
687 env.WriteWorkspaceFiles(map[string]string{
688 "a/a_test.go": `package a
689
690 import "testing"
691
692 func TestAll(t *testing.T) {
693 bob()
694 george()
695 hi()
696 }
697 `,
698 "a/a2_test.go": `package a
699
700 import "testing"
701
702 func hi() {}
703
704 func TestSomething(t *testing.T) {}
705 `,
706 })
707 env.AfterChange(
708 NoDiagnostics(ForFile("a/a_test.go")),
709 NoDiagnostics(ForFile("a/a2_test.go")),
710 )
711 })
712 }