gopls/internal/regtest: fix TestFailingDiagnosticClearingOnEdit

This test was asserting immediately on the non-presence of published
diagnostics, and therefore was racing with the diagnostics calculation
pass. Following https://go.dev/cl/420539 this became a flake, because
once gopls has calculated diagnostics for the open event, it will
actually publish empty diagnostics for the opened file.

Update the test to use a OnceMet so that we only evaluate the
expectation once we've finished the diagnostics pass. As a result, the
test deterministically failed, which was fixed by using the
EmptyOrNoDiagnostics expectation.

Fixes golang/go#54271

Change-Id: Id3f220ce44c7996132699a724b6c627f034e367f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/422136
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Peter Weinberger <pjw@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Robert Findley 2022-08-08 16:36:42 -04:00
parent 4ff08b4044
commit 96d05aa120
2 changed files with 15 additions and 5 deletions

View File

@ -39,9 +39,12 @@ func main() {
})
}
// badPackageDup contains a duplicate definition of the 'a' const.
// this is from diagnostics_test.go,
const badPackageDup = `
// This test demonstrates a case where gopls is confused by line directives,
// and fails to surface type checking errors.
func TestFailingDiagnosticClearingOnEdit(t *testing.T) {
// badPackageDup contains a duplicate definition of the 'a' const.
// this is from diagnostics_test.go,
const badPackageDup = `
-- go.mod --
module mod.com
@ -56,11 +59,17 @@ package consts
const a = 2
`
func TestFailingDiagnosticClearingOnEdit(t *testing.T) {
Run(t, badPackageDup, func(t *testing.T, env *Env) {
env.OpenFile("b.go")
// no diagnostics for any files, but there should be
env.Await(NoDiagnostics("a.go"), NoDiagnostics("b.go"))
env.Await(
OnceMet(
env.DoneWithOpen(),
EmptyOrNoDiagnostics("a.go"),
EmptyOrNoDiagnostics("b.go"),
),
)
// Fix the error by editing the const name in b.go to `b`.
env.RegexpReplace("b.go", "(a) = 2", "b")

View File

@ -282,6 +282,7 @@ func (a *Awaiter) DiagnosticsFor(name string) *protocol.PublishDiagnosticsParams
}
func (e *Env) Await(expectations ...Expectation) {
e.T.Helper()
if err := e.Awaiter.Await(e.Ctx, expectations...); err != nil {
e.T.Fatal(err)
}