gopls/internal/regtest: add an InitialWorkspaceLoad expectation
The pattern of awaiting initial workspace load has become very common in
regtests. Factor out an expectation variable for this, to clean up.
Change-Id: I2fe0ad94e2584e447baa57b23d4fdf9f8550772e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/255123
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Trust: Rebecca Stambler <rstambler@golang.org>
Trust: Robert Findley <rfindley@google.com>
diff --git a/gopls/internal/regtest/bench_test.go b/gopls/internal/regtest/bench_test.go
index 763e547..9cb8ffd 100644
--- a/gopls/internal/regtest/bench_test.go
+++ b/gopls/internal/regtest/bench_test.go
@@ -9,7 +9,6 @@
"fmt"
"testing"
- "golang.org/x/tools/internal/lsp"
"golang.org/x/tools/internal/lsp/fake"
"golang.org/x/tools/internal/lsp/protocol"
)
@@ -32,9 +31,7 @@
b := testing.Benchmark(func(b *testing.B) {
for i := 0; i < b.N; i++ {
withOptions(opts...).run(t, "", func(t *testing.T, env *Env) {
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
})
}
})
@@ -125,9 +122,7 @@
// it first (and therefore need hooks).
opts = append(opts, SkipHooks(false))
withOptions(opts...).run(t, "", func(t *testing.T, env *Env) {
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
env.OpenFile(completionBench.fileName)
params := &protocol.CompletionParams{}
params.Context.TriggerCharacter = "s"
diff --git a/gopls/internal/regtest/configuration_test.go b/gopls/internal/regtest/configuration_test.go
index 47dfc42..3fab18f 100644
--- a/gopls/internal/regtest/configuration_test.go
+++ b/gopls/internal/regtest/configuration_test.go
@@ -26,9 +26,7 @@
const ThisVariable = 7
`
run(t, files, func(t *testing.T, env *Env) {
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
env.OpenFile("a/a.go")
env.Await(
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidOpen), 1),
diff --git a/gopls/internal/regtest/diagnostics_test.go b/gopls/internal/regtest/diagnostics_test.go
index 50ae251..44b3003 100644
--- a/gopls/internal/regtest/diagnostics_test.go
+++ b/gopls/internal/regtest/diagnostics_test.go
@@ -847,7 +847,7 @@
-- foo/bar_test.go --
`
run(t, mod, func(t *testing.T, env *Env) {
- env.Await(CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1))
+ env.Await(InitialWorkspaceLoad)
env.OpenFile("foo/bar_test.go")
env.EditBuffer("foo/bar_test.go", fake.NewEdit(0, 0, 0, 0, `package foo
`))
@@ -876,7 +876,7 @@
package foo_
`
run(t, mod, func(t *testing.T, env *Env) {
- env.Await(CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1))
+ env.Await(InitialWorkspaceLoad)
env.OpenFile("foo/bar_test.go")
env.RegexpReplace("foo/bar_test.go", "package foo_", "package foo_test")
env.SaveBuffer("foo/bar_test.go")
@@ -1101,9 +1101,7 @@
runner.Run(t, basic, func(t *testing.T, env *Env) {
testenv.NeedsGo1Point(t, 15)
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
env.WriteWorkspaceFile("foo/foo_test.go", `package main
func main() {
@@ -1132,9 +1130,7 @@
func main() {}
`
runner.Run(t, basic, func(t *testing.T, env *Env) {
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
env.Editor.OpenFileWithContent(env.Ctx, "foo.go", `package main`)
env.Await(
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidOpen), 1),
@@ -1293,7 +1289,7 @@
log.SetFlags(log.Lshortfile)
env.OpenFile("main.go")
env.OpenFile("other.go")
- env.Await(CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1))
+ env.Await(InitialWorkspaceLoad)
x := env.DiagnosticsFor("main.go")
if x == nil {
t.Fatalf("expected 1 diagnostic, got none")
@@ -1342,9 +1338,7 @@
}
`
run(t, files, func(t *testing.T, env *Env) {
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
env.OpenFile("a/a.go")
env.Await(
env.DiagnosticAtRegexp("a/a.go", "x"),
diff --git a/gopls/internal/regtest/expectation.go b/gopls/internal/regtest/expectation.go
index 19f5ff2..e8697f6 100644
--- a/gopls/internal/regtest/expectation.go
+++ b/gopls/internal/regtest/expectation.go
@@ -9,6 +9,7 @@
"regexp"
"strings"
+ "golang.org/x/tools/internal/lsp"
"golang.org/x/tools/internal/lsp/protocol"
)
@@ -23,6 +24,12 @@
Description() string
}
+var (
+ // InitialWorkspaceLoad is an expectation that the workspace initial load has
+ // completed. It is verified via workdone reporting.
+ InitialWorkspaceLoad = CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1)
+)
+
// A Verdict is the result of checking an expectation against the current
// editor state.
type Verdict int
diff --git a/gopls/internal/regtest/fix_test.go b/gopls/internal/regtest/fix_test.go
index 0900baa..423f008 100644
--- a/gopls/internal/regtest/fix_test.go
+++ b/gopls/internal/regtest/fix_test.go
@@ -7,7 +7,6 @@
import (
"testing"
- "golang.org/x/tools/internal/lsp"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/tests"
)
@@ -29,9 +28,7 @@
}
`
runner.Run(t, basic, func(t *testing.T, env *Env) {
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
env.OpenFile("main.go")
if err := env.Editor.RefactorRewrite(env.Ctx, "main.go", &protocol.Range{
Start: protocol.Position{
diff --git a/gopls/internal/regtest/imports_test.go b/gopls/internal/regtest/imports_test.go
index 63835cd..79e70d4 100644
--- a/gopls/internal/regtest/imports_test.go
+++ b/gopls/internal/regtest/imports_test.go
@@ -7,7 +7,6 @@
"strings"
"testing"
- "golang.org/x/tools/internal/lsp"
"golang.org/x/tools/internal/lsp/fake"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/testenv"
@@ -191,9 +190,7 @@
}
`
run(t, pkg, func(t *testing.T, env *Env) {
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
env.OpenFile("a/a.go")
metBy := env.Await(
env.DiagnosticAtRegexp("a/a.go", "os.Stat"),
diff --git a/gopls/internal/regtest/modfile_test.go b/gopls/internal/regtest/modfile_test.go
index 3069629..bfae9dc 100644
--- a/gopls/internal/regtest/modfile_test.go
+++ b/gopls/internal/regtest/modfile_test.go
@@ -235,7 +235,7 @@
`
runner.Run(t, mod, func(t *testing.T, env *Env) {
env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
+ InitialWorkspaceLoad,
env.DiagnosticAtRegexp("go.mod", "require"),
)
env.Sandbox.RunGoCommand(env.Ctx, "", "mod", []string{"tidy"})
@@ -354,9 +354,7 @@
// Start from a bad state/bad IWL, and confirm that we recover.
t.Run("bad", func(t *testing.T) {
runner.Run(t, unknown, func(t *testing.T, env *Env) {
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
env.OpenFile("go.mod")
env.Await(
env.DiagnosticAtRegexp("go.mod", "example.com v1.2.2"),
diff --git a/gopls/internal/regtest/watch_test.go b/gopls/internal/regtest/watch_test.go
index 6e6919b..50a628a 100644
--- a/gopls/internal/regtest/watch_test.go
+++ b/gopls/internal/regtest/watch_test.go
@@ -309,9 +309,7 @@
// Add the new method before the implementation. Expect diagnostics.
t.Run("method before implementation", func(t *testing.T) {
runner.Run(t, pkg, func(t *testing.T, env *Env) {
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
env.WriteWorkspaceFile("b/b.go", newMethod)
env.Await(
OnceMet(
@@ -328,9 +326,7 @@
// Add the new implementation before the new method. Expect no diagnostics.
t.Run("implementation before method", func(t *testing.T) {
runner.Run(t, pkg, func(t *testing.T, env *Env) {
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
env.WriteWorkspaceFile("a/a.go", implementation)
env.Await(
OnceMet(
@@ -347,9 +343,7 @@
// Add both simultaneously. Expect no diagnostics.
t.Run("implementation and method simultaneously", func(t *testing.T) {
runner.Run(t, pkg, func(t *testing.T, env *Env) {
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
env.WriteWorkspaceFiles(map[string]string{
"a/a.go": implementation,
"b/b.go": newMethod,
@@ -479,9 +473,7 @@
func _() {}
`
runner.Run(t, pkg, func(t *testing.T, env *Env) {
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
env.ChangeFilesOnDisk([]fake.FileEvent{
{
Path: "a/a3.go",
@@ -568,9 +560,7 @@
}
`
withOptions(WithProxyFiles(proxy)).run(t, mod, func(t *testing.T, env *Env) {
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
env.WriteWorkspaceFiles(map[string]string{
"go.mod": `module mod.com
@@ -618,7 +608,7 @@
env.OpenFile("foo/main.go")
env.Await(
OnceMet(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
+ InitialWorkspaceLoad,
env.DiagnosticAtRegexp("foo/main.go", `"blah"`),
),
)
@@ -661,9 +651,7 @@
}
`
run(t, files, func(t *testing.T, env *Env) {
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
// Add a new symbol to the package under test and use it in the test
// variant. Expect no diagnostics.
env.WriteWorkspaceFiles(map[string]string{
diff --git a/gopls/internal/regtest/workspace_test.go b/gopls/internal/regtest/workspace_test.go
index 63bcdea..d672316 100644
--- a/gopls/internal/regtest/workspace_test.go
+++ b/gopls/internal/regtest/workspace_test.go
@@ -139,9 +139,7 @@
// replace target is added to the go.mod.
func TestWatchReplaceTargets(t *testing.T) {
withOptions(WithProxyFiles(workspaceProxy), WithRootPath("pkg")).run(t, workspaceModule, func(t *testing.T, env *Env) {
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
// Add a replace directive and expect the files that gopls is watching
// to change.
dir := env.Sandbox.Workdir.URI("goodbye").SpanURI().Filename()
@@ -199,9 +197,7 @@
withOptions(
WithProxyFiles(workspaceModuleProxy),
).run(t, multiModule, func(t *testing.T, env *Env) {
- env.Await(
- CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
- )
+ env.Await(InitialWorkspaceLoad)
env.Await(
env.DiagnosticAtRegexp("moda/a/a.go", "x"),
env.DiagnosticAtRegexp("modb/b/b.go", "x"),