| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 16 |
|
| 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(t *testing.T, env *Env) { |
| 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 |
|
| 56 |
|
| 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(t *testing.T, env *Env) { |
| 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 := range 5 { |
| 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 |
|
| 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 |
} |
| 107 |
|
| 108 |
func TestCreatingPackageInvalidatesDiagnostics_Issue66384(t *testing.T) { |
| 109 |
const files = ` |
| 110 |
-- go.mod -- |
| 111 |
module example.com |
| 112 |
|
| 113 |
go 1.15 |
| 114 |
-- main.go -- |
| 115 |
package main |
| 116 |
|
| 117 |
import "example.com/pkg" |
| 118 |
|
| 119 |
func main() { |
| 120 |
var _ pkg.Thing |
| 121 |
} |
| 122 |
` |
| 123 |
Run(t, files, func(t *testing.T, env *Env) { |
| 124 |
env.OnceMet( |
| 125 |
InitialWorkspaceLoad, |
| 126 |
Diagnostics(env.AtRegexp("main.go", `"example.com/pkg"`)), |
| 127 |
) |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
env.CreateBuffer("pkg/pkg.go", "") |
| 133 |
env.AfterChange() |
| 134 |
env.EditBuffer("pkg/pkg.go", protocol.TextEdit{NewText: "package pkg\ntype Thing struct{}\n"}) |
| 135 |
env.AfterChange() |
| 136 |
env.SaveBuffer("pkg/pkg.go") |
| 137 |
env.AfterChange(NoDiagnostics()) |
| 138 |
env.SetBufferContent("pkg/pkg.go", "package pkg") |
| 139 |
env.AfterChange(Diagnostics(env.AtRegexp("main.go", "Thing"))) |
| 140 |
}) |
| 141 |
} |