From cbbbe6237a2093dd6634949af55ef6e8d4d08da4 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Tue, 15 Sep 2020 21:49:37 -0400 Subject: [PATCH] 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 TryBot-Result: Go Bot gopls-CI: kokoro Reviewed-by: Rebecca Stambler Trust: Rebecca Stambler Trust: Robert Findley --- gopls/internal/regtest/bench_test.go | 9 ++----- gopls/internal/regtest/configuration_test.go | 4 +-- gopls/internal/regtest/diagnostics_test.go | 18 +++++--------- gopls/internal/regtest/expectation.go | 7 ++++++ gopls/internal/regtest/fix_test.go | 5 +--- gopls/internal/regtest/imports_test.go | 5 +--- gopls/internal/regtest/modfile_test.go | 6 ++--- gopls/internal/regtest/watch_test.go | 26 ++++++-------------- gopls/internal/regtest/workspace_test.go | 8 ++---- 9 files changed, 29 insertions(+), 59 deletions(-) diff --git a/gopls/internal/regtest/bench_test.go b/gopls/internal/regtest/bench_test.go index 763e547710..9cb8ffd687 100644 --- a/gopls/internal/regtest/bench_test.go +++ b/gopls/internal/regtest/bench_test.go @@ -9,7 +9,6 @@ import ( "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 @@ func TestBenchmarkIWL(t *testing.T) { 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 @@ func TestBenchmarkCompletion(t *testing.T) { // 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 47dfc421ea..3fab18fc41 100644 --- a/gopls/internal/regtest/configuration_test.go +++ b/gopls/internal/regtest/configuration_test.go @@ -26,9 +26,7 @@ package a 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 50ae251fba..44b30033f2 100644 --- a/gopls/internal/regtest/diagnostics_test.go +++ b/gopls/internal/regtest/diagnostics_test.go @@ -847,7 +847,7 @@ func TestCreateOnlyXTest(t *testing.T) { -- 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 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 @@ func Foo() { 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 @@ package main 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 @@ func main() {} 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 @@ func _() { } ` 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 19f5ff2c68..e8697f6b32 100644 --- a/gopls/internal/regtest/expectation.go +++ b/gopls/internal/regtest/expectation.go @@ -9,6 +9,7 @@ import ( "regexp" "strings" + "golang.org/x/tools/internal/lsp" "golang.org/x/tools/internal/lsp/protocol" ) @@ -23,6 +24,12 @@ type Expectation interface { 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 0900baaff8..423f008987 100644 --- a/gopls/internal/regtest/fix_test.go +++ b/gopls/internal/regtest/fix_test.go @@ -7,7 +7,6 @@ package regtest 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 @@ func Foo() { } ` 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 63835cd63c..79e70d4a8e 100644 --- a/gopls/internal/regtest/imports_test.go +++ b/gopls/internal/regtest/imports_test.go @@ -7,7 +7,6 @@ import ( "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 @@ func TestA(t *testing.T) { } ` 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 3069629c5d..bfae9dc325 100644 --- a/gopls/internal/regtest/modfile_test.go +++ b/gopls/internal/regtest/modfile_test.go @@ -235,7 +235,7 @@ go 1.12 ` 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 @@ func main() { // 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 6e6919bcd0..50a628a7ef 100644 --- a/gopls/internal/regtest/watch_test.go +++ b/gopls/internal/regtest/watch_test.go @@ -309,9 +309,7 @@ func _() { // 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 @@ func _() { // 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 @@ func _() { // 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 @@ package a 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 @@ func main() { } ` 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 @@ func main() { 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 @@ func TestBob(t *testing.T) { } ` 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 63bcdea40c..d672316a09 100644 --- a/gopls/internal/regtest/workspace_test.go +++ b/gopls/internal/regtest/workspace_test.go @@ -139,9 +139,7 @@ func TestClearAnalysisDiagnostics(t *testing.T) { // 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 @@ func Hello() int { 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"),