Skip to content

Commit

Permalink
gopls/internal/regtest: eliminate DiagnosticAt
Browse files Browse the repository at this point in the history
Replace the DiagnosticAt expectation with a filter.

Updates golang/go#39384

Change-Id: I29e61a531beb2097a88266943917b0ae43630e3f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/461937
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
findleyr committed Jan 13, 2023
1 parent 27dfeb2 commit 5d65394
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
18 changes: 12 additions & 6 deletions gopls/internal/lsp/regtest/expectation.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,18 @@ func (e *Env) AtRegexp(name, pattern string) DiagnosticFilter {
}
}

// AtPosition filters to diagnostics at location name:line:character, for a
// sandbox-relative path name.
func AtPosition(name string, line, character uint32) DiagnosticFilter {
pos := protocol.Position{Line: line, Character: character}
return DiagnosticFilter{
desc: fmt.Sprintf("at %s:%d:%d", name, line, character),
check: func(diagName string, d protocol.Diagnostic) bool {
return diagName == name && d.Range.Start == pos
},
}
}

// WithMessageContaining filters to diagnostics whose message contains the
// given substring.
func WithMessageContaining(substring string) DiagnosticFilter {
Expand Down Expand Up @@ -846,9 +858,3 @@ func (e *Env) DiagnosticAtRegexpWithMessage(name, re, msg string) DiagnosticExpe
pos := e.RegexpSearch(name, re)
return DiagnosticExpectation{path: name, pos: &pos, re: re, present: true, message: msg}
}

// DiagnosticAt asserts that there is a diagnostic entry at the position
// specified by line and col, for the workdir-relative path name.
func DiagnosticAt(name string, line, col uint32) DiagnosticExpectation {
return DiagnosticExpectation{path: name, pos: &protocol.Position{Line: line, Character: col}, present: true}
}
4 changes: 2 additions & 2 deletions gopls/internal/regtest/codelens/gcdetails_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func main() {
env.ExecuteCodeLensCommand("main.go", command.GCDetails, nil)
d := &protocol.PublishDiagnosticsParams{}
env.OnceMet(
DiagnosticAt("main.go", 5, 13),
Diagnostics(AtPosition("main.go", 5, 13)),
ReadDiagnostics("main.go", d),
)
// Confirm that the diagnostics come from the gc details code lens.
Expand All @@ -71,7 +71,7 @@ func main() {
// Saving a buffer should re-format back to the original state, and
// re-enable the gc_details diagnostics.
env.SaveBuffer("main.go")
env.AfterChange(DiagnosticAt("main.go", 5, 13))
env.AfterChange(Diagnostics(AtPosition("main.go", 5, 13)))

// Toggle the GC details code lens again so now it should be off.
env.ExecuteCodeLensCommand("main.go", command.GCDetails, nil)
Expand Down
12 changes: 6 additions & 6 deletions gopls/internal/regtest/diagnostics/diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ func TestDeleteTestVariant(t *testing.T) {
func TestDeleteTestVariant_DiskOnly(t *testing.T) {
Run(t, test38878, func(t *testing.T, env *Env) {
env.OpenFile("a_test.go")
env.Await(DiagnosticAt("a_test.go", 5, 3))
env.Await(Diagnostics(AtPosition("a_test.go", 5, 3)))
env.Sandbox.Workdir.RemoveFile(context.Background(), "a_test.go")
env.AfterChange(DiagnosticAt("a_test.go", 5, 3))
env.AfterChange(Diagnostics(AtPosition("a_test.go", 5, 3)))
})
}

Expand Down Expand Up @@ -343,8 +343,8 @@ func TestHello(t *testing.T) {
Run(t, testPackage, func(t *testing.T, env *Env) {
env.OpenFile("lib_test.go")
env.Await(
DiagnosticAt("lib_test.go", 10, 2),
DiagnosticAt("lib_test.go", 11, 2),
Diagnostics(AtPosition("lib_test.go", 10, 2)),
Diagnostics(AtPosition("lib_test.go", 11, 2)),
)
env.OpenFile("lib.go")
env.RegexpReplace("lib.go", "_ = x", "var y int")
Expand Down Expand Up @@ -515,7 +515,7 @@ func _() {
env.OpenFile("main.go")
var d protocol.PublishDiagnosticsParams
env.AfterChange(
DiagnosticAt("main.go", 5, 8),
Diagnostics(AtPosition("main.go", 5, 8)),
ReadDiagnostics("main.go", &d),
)
if fixes := env.GetQuickFixes("main.go", d.Diagnostics); len(fixes) != 0 {
Expand Down Expand Up @@ -1286,7 +1286,7 @@ func _() {
)
env.OpenFile("a/a_exclude.go")
env.Await(
DiagnosticAt("a/a_exclude.go", 2, 8),
Diagnostics(env.AtRegexp("a/a_exclude.go", "package (a)")),
)
})
}
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/regtest/misc/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var FooErr = errors.New("foo")
// change.
env.ChangeConfiguration(cfg)
env.Await(
DiagnosticAt("a/a.go", 5, 4),
Diagnostics(env.AtRegexp("a/a.go", "var (FooErr)")),
)
})
}
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/regtest/modfile/modfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ func main() {}
env.OpenFile("go.mod")
pos := env.RegexpSearch("go.mod", "require hasdep.com v1.2.3")
env.AfterChange(
DiagnosticAt("go.mod", pos.Line, pos.Character),
Diagnostics(AtPosition("go.mod", pos.Line, pos.Character)),
ReadDiagnostics("go.mod", d),
)
const want = `module mod.com
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/regtest/watch/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func _() {
Run(t, pkg, func(t *testing.T, env *Env) {
env.WriteWorkspaceFile("b/b.go", newMethod)
env.AfterChange(
DiagnosticAt("a/a.go", 12, 12),
Diagnostics(AtPosition("a/a.go", 12, 12)),
)
env.WriteWorkspaceFile("a/a.go", implementation)
env.AfterChange(
Expand Down
6 changes: 3 additions & 3 deletions gopls/internal/regtest/workspace/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ func Hello() int {
).Run(t, multiModule, func(t *testing.T, env *Env) {
env.OpenFile("modb/go.mod")
env.AfterChange(
DiagnosticAt("modb/go.mod", 0, 0),
Diagnostics(AtPosition("modb/go.mod", 0, 0)),
)
env.RegexpReplace("modb/go.mod", "modul", "module")
env.SaveBufferWithoutActions("modb/go.mod")
Expand Down Expand Up @@ -1052,7 +1052,7 @@ func main() {}
// package declaration.
env.AfterChange(
NoDiagnostics(ForFile("main.go")),
DiagnosticAt("b/main.go", 0, 0),
Diagnostics(AtPosition("b/main.go", 0, 0)),
)
env.WriteWorkspaceFile("go.work", `go 1.16
Expand All @@ -1078,7 +1078,7 @@ use (

env.AfterChange(
NoDiagnostics(ForFile("main.go")),
DiagnosticAt("b/main.go", 0, 0),
Diagnostics(AtPosition("b/main.go", 0, 0)),
)
})
}
Expand Down

0 comments on commit 5d65394

Please sign in to comment.