1
0
mirror of https://github.com/golang/go synced 2024-11-18 17:04:41 -07:00
go/internal/lsp/regtest/shared_test.go
Rob Findley 741f65b509 internal/lsp/lsprpc: add a forwarder handler
Add a forwarder handler that alters messages before forwarding, for now,
it just intercepts the "exit" message.

Also, make it easier to write regression tests for a shared gopls
instance, by adding a helper that instantiates two connected
environments, and only runs in the shared execution modes.

Updates golang/go#36879
Updates golang/go#34111

Change-Id: I7673f72ab71b5c7fd6ad65d274c15132a942e06a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/218778
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-02-19 15:38:09 +00:00

70 lines
1.9 KiB
Go

// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package regtest
import (
"context"
"testing"
"golang.org/x/tools/internal/lsp/fake"
)
const sharedProgram = `
-- go.mod --
module mod
go 1.12
-- main.go --
package main
import "fmt"
func main() {
fmt.Println("Hello World.")
}`
func runShared(t *testing.T, program string, testFunc func(ctx context.Context, t *testing.T, env1 *Env, env2 *Env)) {
runner.RunInMode(Forwarded, t, sharedProgram, func(ctx context.Context, t *testing.T, env1 *Env) {
// Create a second test session connected to the same workspace and server
// as the first.
env2 := NewEnv(ctx, t, env1.W, env1.Server)
testFunc(ctx, t, env1, env2)
})
}
func TestSimultaneousEdits(t *testing.T) {
t.Parallel()
runner.Run(t, exampleProgram, func(ctx context.Context, t *testing.T, env1 *Env) {
// Create a second test session connected to the same workspace and server
// as the first.
env2 := NewEnv(ctx, t, env1.W, env1.Server)
// In editor #1, break fmt.Println as before.
edit1 := fake.NewEdit(5, 11, 5, 12, "")
env1.OpenFile("main.go")
env1.EditBuffer("main.go", edit1)
// In editor #2 remove the closing brace.
edit2 := fake.NewEdit(6, 0, 6, 1, "")
env2.OpenFile("main.go")
env2.EditBuffer("main.go", edit2)
// Now check that we got different diagnostics in each environment.
env1.Await(DiagnosticAt("main.go", 5, 5))
env2.Await(DiagnosticAt("main.go", 7, 0))
})
}
func TestShutdown(t *testing.T) {
t.Parallel()
runShared(t, sharedProgram, func(ctx context.Context, t *testing.T, env1 *Env, env2 *Env) {
env1.CloseEditor()
// Now make an edit in editor #2 to trigger diagnostics.
edit2 := fake.NewEdit(6, 0, 6, 1, "")
env2.OpenFile("main.go")
env2.EditBuffer("main.go", edit2)
env2.Await(DiagnosticAt("main.go", 7, 0))
})
}