From 797bd0f00cede9b8cebef232cfe961c67466e3d7 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Tue, 15 Sep 2020 22:04:47 -0400 Subject: [PATCH] gopls/internal/regtest: simplify regtest EditorConfig WithEditorConfig(fake.EditorConfig{...}) seemed like unnecessary boilerplate. Simplify the option to just 'EditorConfig'. Change-Id: I11a3045388f8e642caff78a3f0caa13374b4713a Reviewed-on: https://go-review.googlesource.com/c/tools/+/255124 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 | 5 ++--- gopls/internal/regtest/codelens_test.go | 7 +++--- gopls/internal/regtest/diagnostics_test.go | 26 +++++++++++++--------- gopls/internal/regtest/imports_test.go | 5 ++--- gopls/internal/regtest/runner.go | 10 ++++----- gopls/internal/regtest/symbol_test.go | 10 ++++----- gopls/internal/regtest/unix_test.go | 10 +++------ 7 files changed, 35 insertions(+), 38 deletions(-) diff --git a/gopls/internal/regtest/bench_test.go b/gopls/internal/regtest/bench_test.go index 9cb8ffd687..54ed988ee6 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/fake" "golang.org/x/tools/internal/lsp/protocol" ) @@ -56,14 +55,14 @@ func TestBenchmarkSymbols(t *testing.T) { t.Skip("-symbol_workdir not configured") } opts := stressTestOptions(symbolBench.workdir) - conf := fake.EditorConfig{} + conf := EditorConfig{} if symbolBench.matcher != "" { conf.SymbolMatcher = &symbolBench.matcher } if symbolBench.style != "" { conf.SymbolStyle = &symbolBench.style } - opts = append(opts, WithEditorConfig(conf)) + opts = append(opts, conf) withOptions(opts...).run(t, "", func(t *testing.T, env *Env) { // We can't Await in this test, since we have disabled hooks. Instead, run // one symbol request to completion to ensure all necessary cache entries diff --git a/gopls/internal/regtest/codelens_test.go b/gopls/internal/regtest/codelens_test.go index 8402e9b84c..3fcfdcebca 100644 --- a/gopls/internal/regtest/codelens_test.go +++ b/gopls/internal/regtest/codelens_test.go @@ -7,7 +7,6 @@ package regtest import ( "testing" - "golang.org/x/tools/internal/lsp/fake" "golang.org/x/tools/internal/lsp/protocol" "golang.org/x/tools/internal/lsp/source" "golang.org/x/tools/internal/lsp/tests" @@ -48,13 +47,15 @@ const ( } for _, test := range tests { t.Run(test.label, func(t *testing.T) { - runner.Run(t, workspace, func(t *testing.T, env *Env) { + withOptions( + EditorConfig{CodeLens: test.enabled}, + ).run(t, workspace, func(t *testing.T, env *Env) { env.OpenFile("lib.go") lens := env.CodeLens("lib.go") if gotCodeLens := len(lens) > 0; gotCodeLens != test.wantCodeLens { t.Errorf("got codeLens: %t, want %t", gotCodeLens, test.wantCodeLens) } - }, WithEditorConfig(fake.EditorConfig{CodeLens: test.enabled})) + }) }) } } diff --git a/gopls/internal/regtest/diagnostics_test.go b/gopls/internal/regtest/diagnostics_test.go index 44b30033f2..d86ff44e58 100644 --- a/gopls/internal/regtest/diagnostics_test.go +++ b/gopls/internal/regtest/diagnostics_test.go @@ -438,8 +438,8 @@ func _() { fmt.Println("Hello World") } ` - editorConfig := fake.EditorConfig{Env: map[string]string{"GOPATH": ""}} - withOptions(WithEditorConfig(editorConfig)).run(t, files, func(t *testing.T, env *Env) { + editorConfig := EditorConfig{Env: map[string]string{"GOPATH": ""}} + withOptions(editorConfig).run(t, files, func(t *testing.T, env *Env) { env.OpenFile("main.go") env.Await(env.DiagnosticAtRegexp("main.go", "fmt")) env.SaveBuffer("main.go") @@ -462,8 +462,8 @@ package x var X = 0 ` - editorConfig := fake.EditorConfig{Env: map[string]string{"GOFLAGS": "-tags=foo"}} - withOptions(WithEditorConfig(editorConfig)).run(t, files, func(t *testing.T, env *Env) { + editorConfig := EditorConfig{Env: map[string]string{"GOFLAGS": "-tags=foo"}} + withOptions(editorConfig).run(t, files, func(t *testing.T, env *Env) { env.OpenFile("main.go") env.OrganizeImports("main.go") env.Await(EmptyDiagnostics("main.go")) @@ -556,9 +556,9 @@ hi mom ` for _, go111module := range []string{"on", "off", ""} { t.Run(fmt.Sprintf("GO111MODULE_%v", go111module), func(t *testing.T) { - withOptions(WithEditorConfig(fake.EditorConfig{ + withOptions(EditorConfig{ Env: map[string]string{"GO111MODULE": go111module}, - })).run(t, files, func(t *testing.T, env *Env) { + }).run(t, files, func(t *testing.T, env *Env) { env.OpenFile("hello.txt") env.Await( OnceMet( @@ -642,7 +642,9 @@ func main() { _ = conf.ErrHelpWanted } ` - runner.Run(t, ardanLabs, func(t *testing.T, env *Env) { + withOptions( + WithProxyFiles(ardanLabsProxy), + ).run(t, ardanLabs, func(t *testing.T, env *Env) { // Expect a diagnostic with a suggested fix to add // "github.com/ardanlabs/conf" to the go.mod file. env.OpenFile("go.mod") @@ -686,7 +688,7 @@ func main() { env.Await( env.DiagnosticAtRegexp("main.go", `"github.com/ardanlabs/conf"`), ) - }, WithProxyFiles(ardanLabsProxy)) + }) } // Test for golang/go#38207. @@ -699,7 +701,9 @@ module mod.com go 1.12 -- main.go -- ` - runner.Run(t, emptyFile, func(t *testing.T, env *Env) { + withOptions( + WithProxyFiles(ardanLabsProxy), + ).run(t, emptyFile, func(t *testing.T, env *Env) { env.OpenFile("main.go") env.OpenFile("go.mod") env.EditBuffer("main.go", fake.NewEdit(0, 0, 0, 0, `package main @@ -722,7 +726,7 @@ func main() { env.Await( EmptyDiagnostics("main.go"), ) - }, WithProxyFiles(ardanLabsProxy)) + }) } // Test for golang/go#36960. @@ -1263,7 +1267,7 @@ func main() { ` withOptions( - WithEditorConfig(fake.EditorConfig{EnableStaticcheck: true}), + EditorConfig{EnableStaticcheck: true}, ).run(t, files, func(t *testing.T, env *Env) { env.OpenFile("main.go") // Staticcheck should generate a diagnostic to simplify this literal. diff --git a/gopls/internal/regtest/imports_test.go b/gopls/internal/regtest/imports_test.go index 79e70d4a8e..f570fa1f02 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/fake" "golang.org/x/tools/internal/lsp/protocol" "golang.org/x/tools/internal/testenv" ) @@ -143,9 +142,9 @@ var _, _ = x.X, y.Y t.Fatal(err) } defer os.RemoveAll(modcache) - editorConfig := fake.EditorConfig{Env: map[string]string{"GOMODCACHE": modcache}} + editorConfig := EditorConfig{Env: map[string]string{"GOMODCACHE": modcache}} withOptions( - WithEditorConfig(editorConfig), + editorConfig, WithProxyFiles(proxy), ).run(t, files, func(t *testing.T, env *Env) { env.OpenFile("main.go") diff --git a/gopls/internal/regtest/runner.go b/gopls/internal/regtest/runner.go index ad38463193..10d4bc4c62 100644 --- a/gopls/internal/regtest/runner.go +++ b/gopls/internal/regtest/runner.go @@ -117,11 +117,11 @@ func WithModes(modes Mode) RunOption { }) } -// WithEditorConfig configures the editor's LSP session. -func WithEditorConfig(config fake.EditorConfig) RunOption { - return optionSetter(func(opts *runConfig) { - opts.editor = config - }) +// EditorConfig is a RunOption option that configured the regtest editor. +type EditorConfig fake.EditorConfig + +func (c EditorConfig) set(opts *runConfig) { + opts.editor = fake.EditorConfig(c) } // WithoutWorkspaceFolders prevents workspace folders from being sent as part diff --git a/gopls/internal/regtest/symbol_test.go b/gopls/internal/regtest/symbol_test.go index 68f3de4115..af60488653 100644 --- a/gopls/internal/regtest/symbol_test.go +++ b/gopls/internal/regtest/symbol_test.go @@ -7,7 +7,6 @@ package regtest import ( "testing" - "golang.org/x/tools/internal/lsp/fake" "golang.org/x/tools/internal/lsp/protocol" ) @@ -193,10 +192,9 @@ func TestSymbolPos(t *testing.T) { func checkChecks(t *testing.T, matcher string, checks map[string]*expSymbolInformation) { t.Helper() - opts := []RunOption{ - WithEditorConfig(fake.EditorConfig{SymbolMatcher: &matcher}), - } - runner.Run(t, symbolSetup, func(t *testing.T, env *Env) { + withOptions( + EditorConfig{SymbolMatcher: &matcher}, + ).run(t, symbolSetup, func(t *testing.T, env *Env) { t.Run(matcher, func(t *testing.T) { for query, exp := range checks { t.Run(query, func(t *testing.T) { @@ -207,5 +205,5 @@ func checkChecks(t *testing.T, matcher string, checks map[string]*expSymbolInfor }) } }) - }, opts...) + }) } diff --git a/gopls/internal/regtest/unix_test.go b/gopls/internal/regtest/unix_test.go index e67de38516..cfdf094e50 100644 --- a/gopls/internal/regtest/unix_test.go +++ b/gopls/internal/regtest/unix_test.go @@ -8,8 +8,6 @@ package regtest import ( "testing" - - "golang.org/x/tools/internal/lsp/fake" ) func TestBadGOPATH(t *testing.T) { @@ -21,12 +19,10 @@ func _() { fmt.Println("Hello World") } ` - editorConfig := fake.EditorConfig{ - Env: map[string]string{"GOPATH": ":/path/to/gopath"}, - } - // Test the case given in // https://github.com/fatih/vim-go/issues/2673#issuecomment-622307211. - withOptions(WithEditorConfig(editorConfig)).run(t, files, func(t *testing.T, env *Env) { + withOptions( + EditorConfig{Env: map[string]string{"GOPATH": ":/path/to/gopath"}}, + ).run(t, files, func(t *testing.T, env *Env) { env.OpenFile("main.go") env.Await(env.DiagnosticAtRegexp("main.go", "fmt")) if err := env.Editor.OrganizeImports(env.Ctx, "main.go"); err != nil {