Skip to content

Commit af4cc2c

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent e2cc5a1 commit af4cc2c

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

gopls/internal/regtest/diagnostics_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -1246,3 +1246,31 @@ func main() {
12461246
)
12471247
})
12481248
}
1249+
1250+
func TestStaticcheckDiagnostic(t *testing.T) {
1251+
const files = `
1252+
-- go.mod --
1253+
module mod.com
1254+
-- main.go --
1255+
package main
1256+
1257+
import "fmt"
1258+
1259+
type t struct {
1260+
msg string
1261+
}
1262+
1263+
func main() {
1264+
x := []t{t{"msg"}}
1265+
fmt.Println(x)
1266+
}
1267+
`
1268+
1269+
withOptions(
1270+
WithEditorConfig(fake.EditorConfig{EnableStaticcheck: true}),
1271+
).run(t, files, func(t *testing.T, env *Env) {
1272+
env.OpenFile("main.go")
1273+
// Staticcheck should generate a diagnostic to simplify this literal.
1274+
env.Await(env.DiagnosticAtRegexp("main.go", `t{"msg"}`))
1275+
})
1276+
}

gopls/internal/regtest/runner.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121
"time"
2222

23+
"golang.org/x/tools/gopls/internal/hooks"
2324
"golang.org/x/tools/internal/jsonrpc2"
2425
"golang.org/x/tools/internal/jsonrpc2/servertest"
2526
"golang.org/x/tools/internal/lsp/cache"
@@ -315,7 +316,7 @@ func (s *loggingFramer) printBuffers(testname string, w io.Writer) {
315316
}
316317

317318
func singletonServer(ctx context.Context, t *testing.T) jsonrpc2.StreamServer {
318-
return lsprpc.NewStreamServer(cache.New(ctx, nil), false)
319+
return lsprpc.NewStreamServer(cache.New(ctx, hooks.Options), false)
319320
}
320321

321322
func (r *Runner) forwardedServer(ctx context.Context, t *testing.T) jsonrpc2.StreamServer {
@@ -331,7 +332,7 @@ func (r *Runner) getTestServer() *servertest.TCPServer {
331332
if r.ts == nil {
332333
ctx := context.Background()
333334
ctx = debug.WithInstance(ctx, "", "off")
334-
ss := lsprpc.NewStreamServer(cache.New(ctx, nil), false)
335+
ss := lsprpc.NewStreamServer(cache.New(ctx, hooks.Options), false)
335336
r.ts = servertest.NewTCPServer(ctx, ss, nil)
336337
}
337338
return r.ts

internal/lsp/fake/editor.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ type EditorConfig struct {
8181
// EditorRootPath specifies the root path of the workspace folder used when
8282
// initializing gopls in the sandbox. If empty, the Workdir is used.
8383
EditorRootPath string
84+
85+
// EnableStaticcheck enables staticcheck analyzers.
86+
EnableStaticcheck bool
8487
}
8588

8689
// NewEditor Creates a new Editor.
@@ -180,14 +183,15 @@ func (e *Editor) configuration() map[string]interface{} {
180183
if e.Config.CodeLens != nil {
181184
config["codelens"] = e.Config.CodeLens
182185
}
183-
184186
if e.Config.SymbolMatcher != nil {
185187
config["symbolMatcher"] = *e.Config.SymbolMatcher
186188
}
187-
188189
if e.Config.SymbolStyle != nil {
189190
config["symbolStyle"] = *e.Config.SymbolStyle
190191
}
192+
if e.Config.EnableStaticcheck {
193+
config["staticcheck"] = true
194+
}
191195

192196
return config
193197
}

0 commit comments

Comments
 (0)