-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathinvalidation_test.go
More file actions
214 lines (185 loc) · 5.91 KB
/
invalidation_test.go
File metadata and controls
214 lines (185 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package diagnostics
import (
"fmt"
"testing"
"golang.org/x/tools/gopls/internal/protocol"
. "golang.org/x/tools/gopls/internal/test/integration"
)
// Test for golang/go#50267: diagnostics should be re-sent after a file is
// opened.
func TestDiagnosticsAreResentAfterCloseOrOpen(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.16
-- main.go --
package main
func _() {
x := 2
}
`
Run(t, files, func(t *testing.T, env *Env) { // Create a new workspace-level directory and empty file.
env.OpenFile("main.go")
var afterOpen protocol.PublishDiagnosticsParams
env.AfterChange(
ReadDiagnostics("main.go", &afterOpen),
)
env.CloseBuffer("main.go")
var afterClose protocol.PublishDiagnosticsParams
env.AfterChange(
ReadDiagnostics("main.go", &afterClose),
)
if afterOpen.Version == afterClose.Version {
t.Errorf("publishDiagnostics: got the same version after closing (%d) as after opening", afterOpen.Version)
}
env.OpenFile("main.go")
var afterReopen protocol.PublishDiagnosticsParams
env.AfterChange(
ReadDiagnostics("main.go", &afterReopen),
)
if afterReopen.Version == afterClose.Version {
t.Errorf("pubslishDiagnostics: got the same version after reopening (%d) as after closing", afterClose.Version)
}
})
}
// Test for the "chatty" diagnostics: gopls should re-send diagnostics for
// changed files after every file change, even if diagnostics did not change.
func TestChattyDiagnostics(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.16
-- main.go --
package main
func _() {
x := 2
}
// Irrelevant comment #0
`
Run(t, files, func(t *testing.T, env *Env) { // Create a new workspace-level directory and empty file.
env.OpenFile("main.go")
var d protocol.PublishDiagnosticsParams
env.AfterChange(
ReadDiagnostics("main.go", &d),
)
if len(d.Diagnostics) != 1 {
t.Fatalf("len(Diagnostics) = %d, want 1", len(d.Diagnostics))
}
msg := d.Diagnostics[0].Message
for i := range 5 {
before := d.Version
env.RegexpReplace("main.go", "Irrelevant comment #.", fmt.Sprintf("Irrelevant comment #%d", i))
env.AfterChange(
ReadDiagnostics("main.go", &d),
)
if d.Version == before {
t.Errorf("after change, got version %d, want new version", d.Version)
}
// As a sanity check, make sure we have the same diagnostic.
if len(d.Diagnostics) != 1 {
t.Fatalf("len(Diagnostics) = %d, want 1", len(d.Diagnostics))
}
newMsg := d.Diagnostics[0].Message
if newMsg != msg {
t.Errorf("after change, got message %q, want %q", newMsg, msg)
}
}
})
}
// TestEagerDiagnosticInvalidation verifies that when eagerDiagnosticsClear is
// enabled, the first publishDiagnostics notification after an edit is an empty
// clear of stale diagnostics, sent before reanalysis completes.
func TestEagerDiagnosticInvalidation(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.16
-- main.go --
package main
func main() {
x := 2
}
`
// With eagerDiagnosticsClear enabled: editing a file that has diagnostics
// should first publish an empty clear, then the real diagnostics.
WithOptions(
Settings{"eagerDiagnosticsClear": true},
).Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
env.AfterChange(
Diagnostics(env.AtRegexp("main.go", "x")),
)
// Start collecting all diagnostic notifications before editing.
getDiagHistory := env.Awaiter.ListenToDiagnostics("main.go")
// Fix the error by using the variable.
env.RegexpReplace("main.go", "x := 2", "_ = 2")
env.AfterChange(
NoDiagnostics(ForFile("main.go")),
)
history := getDiagHistory()
if len(history) == 0 {
t.Fatal("expected at least one diagnostic notification after edit")
}
if len(history[0].Diagnostics) != 0 {
t.Errorf("first notification after edit should be empty (eager clear), got %d diagnostics", len(history[0].Diagnostics))
}
})
// With eagerDiagnosticsClear disabled: no eager empty clear before
// reanalysis. Use a no-op edit (comment change) so the error persists
// and we can verify the first notification still carries diagnostics.
WithOptions(
Settings{"eagerDiagnosticsClear": false},
).Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
env.AfterChange(
Diagnostics(env.AtRegexp("main.go", "x")),
)
getDiagHistory := env.Awaiter.ListenToDiagnostics("main.go")
// Add a comment - the unused-variable error should persist.
env.RegexpReplace("main.go", "x := 2", "x := 2 // edited")
env.AfterChange(
Diagnostics(env.AtRegexp("main.go", "x")),
)
history := getDiagHistory()
if len(history) == 0 {
t.Fatal("expected at least one diagnostic notification after edit")
}
if len(history[0].Diagnostics) == 0 {
t.Errorf("without eagerDiagnosticsClear, first notification should have diagnostics, got empty")
}
})
}
func TestCreatingPackageInvalidatesDiagnostics_Issue66384(t *testing.T) {
const files = `
-- go.mod --
module example.com
go 1.15
-- main.go --
package main
import "example.com/pkg"
func main() {
var _ pkg.Thing
}
`
Run(t, files, func(t *testing.T, env *Env) {
env.OnceMet(
InitialWorkspaceLoad,
Diagnostics(env.AtRegexp("main.go", `"example.com/pkg"`)),
)
// In order for this test to reproduce golang/go#66384, we have to create
// the buffer, wait for loads, and *then* "type out" the contents. Doing so
// reproduces the conditions of the bug report, that typing the package
// name itself doesn't invalidate the broken import.
env.CreateBuffer("pkg/pkg.go", "")
env.AfterChange()
env.EditBuffer("pkg/pkg.go", protocol.TextEdit{NewText: "package pkg\ntype Thing struct{}\n"})
env.AfterChange()
env.SaveBuffer("pkg/pkg.go")
env.AfterChange(NoDiagnostics())
env.SetBufferContent("pkg/pkg.go", "package pkg")
env.AfterChange(Diagnostics(env.AtRegexp("main.go", "Thing")))
})
}