Skip to content

Commit

Permalink
gopls/internal/regtest: fix TestFailingDiagnosticClearingOnEdit
Browse files Browse the repository at this point in the history
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 <[email protected]>
Run-TryBot: Robert Findley <[email protected]>
Reviewed-by: Peter Weinberger <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
findleyr committed Aug 9, 2022
1 parent 4ff08b4 commit 96d05aa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
19 changes: 14 additions & 5 deletions gopls/internal/regtest/misc/failures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
1 change: 1 addition & 0 deletions internal/lsp/regtest/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 96d05aa

Please sign in to comment.