Skip to content

Commit

Permalink
gopls/internal/regtest: use gopls hooks and add a test for staticcheck
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
findleyr committed Sep 3, 2020
1 parent e2cc5a1 commit af4cc2c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
28 changes: 28 additions & 0 deletions gopls/internal/regtest/diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}`))
})
}
5 changes: 3 additions & 2 deletions gopls/internal/regtest/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"
"time"

"golang.org/x/tools/gopls/internal/hooks"
"golang.org/x/tools/internal/jsonrpc2"
"golang.org/x/tools/internal/jsonrpc2/servertest"
"golang.org/x/tools/internal/lsp/cache"
Expand Down Expand Up @@ -315,7 +316,7 @@ func (s *loggingFramer) printBuffers(testname string, w io.Writer) {
}

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 {
Expand All @@ -331,7 +332,7 @@ func (r *Runner) getTestServer() *servertest.TCPServer {
if r.ts == nil {
ctx := context.Background()
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)
}
return r.ts
Expand Down
8 changes: 6 additions & 2 deletions internal/lsp/fake/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ type EditorConfig struct {
// EditorRootPath specifies the root path of the workspace folder used when
// initializing gopls in the sandbox. If empty, the Workdir is used.
EditorRootPath string

// EnableStaticcheck enables staticcheck analyzers.
EnableStaticcheck bool
}

// NewEditor Creates a new Editor.
Expand Down Expand Up @@ -180,14 +183,15 @@ func (e *Editor) configuration() map[string]interface{} {
if e.Config.CodeLens != nil {
config["codelens"] = e.Config.CodeLens
}

if e.Config.SymbolMatcher != nil {
config["symbolMatcher"] = *e.Config.SymbolMatcher
}

if e.Config.SymbolStyle != nil {
config["symbolStyle"] = *e.Config.SymbolStyle
}
if e.Config.EnableStaticcheck {
config["staticcheck"] = true
}

return config
}
Expand Down

0 comments on commit af4cc2c

Please sign in to comment.