golang.org/x/tools/gopls@v0.15.3/internal/test/integration/diagnostics/invalidation_test.go (about) 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 diagnostics 6 7 import ( 8 "fmt" 9 "testing" 10 11 "golang.org/x/tools/gopls/internal/protocol" 12 . "golang.org/x/tools/gopls/internal/test/integration" 13 ) 14 15 // Test for golang/go#50267: diagnostics should be re-sent after a file is 16 // opened. 17 func TestDiagnosticsAreResentAfterCloseOrOpen(t *testing.T) { 18 const files = ` 19 -- go.mod -- 20 module mod.com 21 22 go 1.16 23 -- main.go -- 24 package main 25 26 func _() { 27 x := 2 28 } 29 ` 30 Run(t, files, func(_ *testing.T, env *Env) { // Create a new workspace-level directory and empty file. 31 env.OpenFile("main.go") 32 var afterOpen protocol.PublishDiagnosticsParams 33 env.AfterChange( 34 ReadDiagnostics("main.go", &afterOpen), 35 ) 36 env.CloseBuffer("main.go") 37 var afterClose protocol.PublishDiagnosticsParams 38 env.AfterChange( 39 ReadDiagnostics("main.go", &afterClose), 40 ) 41 if afterOpen.Version == afterClose.Version { 42 t.Errorf("publishDiagnostics: got the same version after closing (%d) as after opening", afterOpen.Version) 43 } 44 env.OpenFile("main.go") 45 var afterReopen protocol.PublishDiagnosticsParams 46 env.AfterChange( 47 ReadDiagnostics("main.go", &afterReopen), 48 ) 49 if afterReopen.Version == afterClose.Version { 50 t.Errorf("pubslishDiagnostics: got the same version after reopening (%d) as after closing", afterClose.Version) 51 } 52 }) 53 } 54 55 // Test for the "chatty" diagnostics: gopls should re-send diagnostics for 56 // changed files after every file change, even if diagnostics did not change. 57 func TestChattyDiagnostics(t *testing.T) { 58 const files = ` 59 -- go.mod -- 60 module mod.com 61 62 go 1.16 63 -- main.go -- 64 package main 65 66 func _() { 67 x := 2 68 } 69 70 // Irrelevant comment #0 71 ` 72 73 Run(t, files, func(_ *testing.T, env *Env) { // Create a new workspace-level directory and empty file. 74 env.OpenFile("main.go") 75 var d protocol.PublishDiagnosticsParams 76 env.AfterChange( 77 ReadDiagnostics("main.go", &d), 78 ) 79 80 if len(d.Diagnostics) != 1 { 81 t.Fatalf("len(Diagnostics) = %d, want 1", len(d.Diagnostics)) 82 } 83 msg := d.Diagnostics[0].Message 84 85 for i := 0; i < 5; i++ { 86 before := d.Version 87 env.RegexpReplace("main.go", "Irrelevant comment #.", fmt.Sprintf("Irrelevant comment #%d", i)) 88 env.AfterChange( 89 ReadDiagnostics("main.go", &d), 90 ) 91 92 if d.Version == before { 93 t.Errorf("after change, got version %d, want new version", d.Version) 94 } 95 96 // As a sanity check, make sure we have the same diagnostic. 97 if len(d.Diagnostics) != 1 { 98 t.Fatalf("len(Diagnostics) = %d, want 1", len(d.Diagnostics)) 99 } 100 newMsg := d.Diagnostics[0].Message 101 if newMsg != msg { 102 t.Errorf("after change, got message %q, want %q", newMsg, msg) 103 } 104 } 105 }) 106 }