1
0
mirror of https://github.com/golang/go synced 2024-11-18 07:24:45 -07:00

gopls/internal/regtest: use gopls hooks and add a test for staticcheck

To match the actual gopls binary, use hooks.Options when creating the
regtest server.

Add a test for staticcheck diagnostics to leverage this.

For golang/go#39384

Change-Id: I52837c2b12bb586a2530343bdfae5172b08df49c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/252683
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Rob Findley 2020-09-02 18:43:45 -04:00 committed by Robert Findley
parent e2cc5a1191
commit af4cc2cd81
3 changed files with 37 additions and 4 deletions

View File

@ -1246,3 +1246,31 @@ func main() {
) )
}) })
} }
func TestStaticcheckDiagnostic(t *testing.T) {
const files = `
-- go.mod --
module mod.com
-- main.go --
package main
import "fmt"
type t struct {
msg string
}
func main() {
x := []t{t{"msg"}}
fmt.Println(x)
}
`
withOptions(
WithEditorConfig(fake.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.
env.Await(env.DiagnosticAtRegexp("main.go", `t{"msg"}`))
})
}

View File

@ -20,6 +20,7 @@ import (
"testing" "testing"
"time" "time"
"golang.org/x/tools/gopls/internal/hooks"
"golang.org/x/tools/internal/jsonrpc2" "golang.org/x/tools/internal/jsonrpc2"
"golang.org/x/tools/internal/jsonrpc2/servertest" "golang.org/x/tools/internal/jsonrpc2/servertest"
"golang.org/x/tools/internal/lsp/cache" "golang.org/x/tools/internal/lsp/cache"
@ -315,7 +316,7 @@ func (s *loggingFramer) printBuffers(testname string, w io.Writer) {
} }
func singletonServer(ctx context.Context, t *testing.T) jsonrpc2.StreamServer { func singletonServer(ctx context.Context, t *testing.T) jsonrpc2.StreamServer {
return lsprpc.NewStreamServer(cache.New(ctx, nil), false) return lsprpc.NewStreamServer(cache.New(ctx, hooks.Options), false)
} }
func (r *Runner) forwardedServer(ctx context.Context, t *testing.T) jsonrpc2.StreamServer { func (r *Runner) forwardedServer(ctx context.Context, t *testing.T) jsonrpc2.StreamServer {
@ -331,7 +332,7 @@ func (r *Runner) getTestServer() *servertest.TCPServer {
if r.ts == nil { if r.ts == nil {
ctx := context.Background() ctx := context.Background()
ctx = debug.WithInstance(ctx, "", "off") ctx = debug.WithInstance(ctx, "", "off")
ss := lsprpc.NewStreamServer(cache.New(ctx, nil), false) ss := lsprpc.NewStreamServer(cache.New(ctx, hooks.Options), false)
r.ts = servertest.NewTCPServer(ctx, ss, nil) r.ts = servertest.NewTCPServer(ctx, ss, nil)
} }
return r.ts return r.ts

View File

@ -81,6 +81,9 @@ type EditorConfig struct {
// EditorRootPath specifies the root path of the workspace folder used when // EditorRootPath specifies the root path of the workspace folder used when
// initializing gopls in the sandbox. If empty, the Workdir is used. // initializing gopls in the sandbox. If empty, the Workdir is used.
EditorRootPath string EditorRootPath string
// EnableStaticcheck enables staticcheck analyzers.
EnableStaticcheck bool
} }
// NewEditor Creates a new Editor. // NewEditor Creates a new Editor.
@ -180,14 +183,15 @@ func (e *Editor) configuration() map[string]interface{} {
if e.Config.CodeLens != nil { if e.Config.CodeLens != nil {
config["codelens"] = e.Config.CodeLens config["codelens"] = e.Config.CodeLens
} }
if e.Config.SymbolMatcher != nil { if e.Config.SymbolMatcher != nil {
config["symbolMatcher"] = *e.Config.SymbolMatcher config["symbolMatcher"] = *e.Config.SymbolMatcher
} }
if e.Config.SymbolStyle != nil { if e.Config.SymbolStyle != nil {
config["symbolStyle"] = *e.Config.SymbolStyle config["symbolStyle"] = *e.Config.SymbolStyle
} }
if e.Config.EnableStaticcheck {
config["staticcheck"] = true
}
return config return config
} }