2020-02-06 17:50:37 -07:00
|
|
|
// 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 provides an environment for writing regression tests.
|
|
|
|
package regtest
|
|
|
|
|
|
|
|
import (
|
2020-02-10 09:34:13 -07:00
|
|
|
"bytes"
|
2020-02-06 17:50:37 -07:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-04-01 19:08:59 -06:00
|
|
|
"io"
|
2020-02-10 09:34:13 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2020-04-15 15:14:53 -06:00
|
|
|
"regexp"
|
|
|
|
"runtime/pprof"
|
2020-02-06 17:50:37 -07:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-03-04 11:29:23 -07:00
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
2020-02-06 17:50:37 -07:00
|
|
|
"golang.org/x/tools/internal/jsonrpc2/servertest"
|
|
|
|
"golang.org/x/tools/internal/lsp/cache"
|
2020-02-19 08:18:21 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/debug"
|
2020-02-06 17:50:37 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/fake"
|
|
|
|
"golang.org/x/tools/internal/lsp/lsprpc"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EnvMode is a bitmask that defines in which execution environments a test
|
|
|
|
// should run.
|
|
|
|
type EnvMode int
|
|
|
|
|
|
|
|
const (
|
2020-02-10 09:34:13 -07:00
|
|
|
// Singleton mode uses a separate cache for each test.
|
2020-02-06 17:50:37 -07:00
|
|
|
Singleton EnvMode = 1 << iota
|
2020-04-01 19:08:59 -06:00
|
|
|
|
2020-02-10 09:34:13 -07:00
|
|
|
// Forwarded forwards connections to an in-process gopls instance.
|
2020-02-06 17:50:37 -07:00
|
|
|
Forwarded
|
2020-02-10 09:34:13 -07:00
|
|
|
// SeparateProcess runs a separate gopls process, and forwards connections to
|
|
|
|
// it.
|
|
|
|
SeparateProcess
|
|
|
|
// NormalModes runs tests in all modes.
|
2020-02-27 10:31:02 -07:00
|
|
|
NormalModes = Singleton | Forwarded
|
2020-02-06 17:50:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// A Runner runs tests in gopls execution environments, as specified by its
|
|
|
|
// modes. For modes that share state (for example, a shared cache or common
|
|
|
|
// remote), any tests that execute on the same Runner will share the same
|
|
|
|
// state.
|
|
|
|
type Runner struct {
|
2020-04-15 15:14:53 -06:00
|
|
|
DefaultModes EnvMode
|
|
|
|
Timeout time.Duration
|
|
|
|
GoplsPath string
|
|
|
|
AlwaysPrintLogs bool
|
|
|
|
PrintGoroutinesOnFailure bool
|
2020-02-10 09:34:13 -07:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
ts *servertest.TCPServer
|
|
|
|
socketDir string
|
2020-04-14 14:59:58 -06:00
|
|
|
// closers is a queue of clean-up functions to run at the end of the entire
|
|
|
|
// test suite.
|
|
|
|
closers []io.Closer
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
|
|
|
|
2020-02-10 09:34:13 -07:00
|
|
|
// Modes returns the bitmask of environment modes this runner is configured to
|
|
|
|
// test.
|
|
|
|
func (r *Runner) Modes() EnvMode {
|
2020-04-15 15:14:53 -06:00
|
|
|
return r.DefaultModes
|
2020-02-10 09:34:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// getTestServer gets the test server instance to connect to, or creates one if
|
|
|
|
// it doesn't exist.
|
|
|
|
func (r *Runner) getTestServer() *servertest.TCPServer {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
if r.ts == nil {
|
2020-02-28 08:30:03 -07:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx = debug.WithInstance(ctx, "", "")
|
2020-03-09 11:22:56 -06:00
|
|
|
ss := lsprpc.NewStreamServer(cache.New(ctx, nil))
|
2020-02-10 09:34:13 -07:00
|
|
|
r.ts = servertest.NewTCPServer(context.Background(), ss)
|
|
|
|
}
|
|
|
|
return r.ts
|
|
|
|
}
|
|
|
|
|
|
|
|
// runTestAsGoplsEnvvar triggers TestMain to run gopls instead of running
|
|
|
|
// tests. It's a trick to allow tests to find a binary to use to start a gopls
|
|
|
|
// subprocess.
|
|
|
|
const runTestAsGoplsEnvvar = "_GOPLS_TEST_BINARY_RUN_AS_GOPLS"
|
|
|
|
|
|
|
|
func (r *Runner) getRemoteSocket(t *testing.T) string {
|
|
|
|
t.Helper()
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
const daemonFile = "gopls-test-daemon"
|
|
|
|
if r.socketDir != "" {
|
|
|
|
return filepath.Join(r.socketDir, daemonFile)
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
2020-02-10 09:34:13 -07:00
|
|
|
|
2020-04-15 15:14:53 -06:00
|
|
|
if r.GoplsPath == "" {
|
2020-02-10 09:34:13 -07:00
|
|
|
t.Fatal("cannot run tests with a separate process unless a path to a gopls binary is configured")
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
r.socketDir, err = ioutil.TempDir("", "gopls-regtests")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating tempdir: %v", err)
|
|
|
|
}
|
|
|
|
socket := filepath.Join(r.socketDir, daemonFile)
|
2020-02-25 09:00:37 -07:00
|
|
|
args := []string{"serve", "-listen", "unix;" + socket, "-listen.timeout", "10s"}
|
2020-04-15 15:14:53 -06:00
|
|
|
cmd := exec.Command(r.GoplsPath, args...)
|
2020-02-10 09:34:13 -07:00
|
|
|
cmd.Env = append(os.Environ(), runTestAsGoplsEnvvar+"=true")
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
go func() {
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
panic(fmt.Sprintf("error running external gopls: %v\nstderr:\n%s", err, stderr.String()))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return socket
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
|
|
|
|
2020-04-14 14:59:58 -06:00
|
|
|
// AddCloser schedules a closer to be closed at the end of the test run. This
|
|
|
|
// is useful for Windows in particular, as
|
|
|
|
func (r *Runner) AddCloser(closer io.Closer) {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
r.closers = append(r.closers, closer)
|
|
|
|
}
|
|
|
|
|
2020-02-06 17:50:37 -07:00
|
|
|
// Close cleans up resource that have been allocated to this workspace.
|
|
|
|
func (r *Runner) Close() error {
|
2020-02-10 09:34:13 -07:00
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
2020-04-14 14:59:58 -06:00
|
|
|
|
|
|
|
var errmsgs []string
|
2020-02-10 09:34:13 -07:00
|
|
|
if r.ts != nil {
|
2020-04-14 14:59:58 -06:00
|
|
|
if err := r.ts.Close(); err != nil {
|
|
|
|
errmsgs = append(errmsgs, err.Error())
|
|
|
|
}
|
2020-02-10 09:34:13 -07:00
|
|
|
}
|
|
|
|
if r.socketDir != "" {
|
2020-04-14 14:59:58 -06:00
|
|
|
if err := os.RemoveAll(r.socketDir); err != nil {
|
|
|
|
errmsgs = append(errmsgs, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, closer := range r.closers {
|
|
|
|
if err := closer.Close(); err != nil {
|
|
|
|
errmsgs = append(errmsgs, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errmsgs) > 0 {
|
|
|
|
return fmt.Errorf("errors closing the test runner:\n\t%s", strings.Join(errmsgs, "\n\t"))
|
2020-02-10 09:34:13 -07:00
|
|
|
}
|
|
|
|
return nil
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
|
|
|
|
2020-04-14 15:28:37 -06:00
|
|
|
type runConfig struct {
|
|
|
|
modes EnvMode
|
|
|
|
proxyTxt string
|
|
|
|
timeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) defaultConfig() *runConfig {
|
|
|
|
return &runConfig{
|
2020-04-15 15:14:53 -06:00
|
|
|
modes: r.DefaultModes,
|
|
|
|
timeout: r.Timeout,
|
2020-04-14 15:28:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A RunOption augments the behavior of the test runner.
|
|
|
|
type RunOption interface {
|
|
|
|
set(*runConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
type optionSetter func(*runConfig)
|
|
|
|
|
|
|
|
func (f optionSetter) set(opts *runConfig) {
|
|
|
|
f(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTimeout configures a custom timeout for this test run.
|
|
|
|
func WithTimeout(d time.Duration) RunOption {
|
|
|
|
return optionSetter(func(opts *runConfig) {
|
|
|
|
opts.timeout = d
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithProxy configures a file proxy using the given txtar-encoded string.
|
|
|
|
func WithProxy(txt string) RunOption {
|
|
|
|
return optionSetter(func(opts *runConfig) {
|
|
|
|
opts.proxyTxt = txt
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithModes configures the execution modes that the test should run in.
|
|
|
|
func WithModes(modes EnvMode) RunOption {
|
|
|
|
return optionSetter(func(opts *runConfig) {
|
|
|
|
opts.modes = modes
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-09 12:44:03 -07:00
|
|
|
// Run executes the test function in the default configured gopls execution
|
|
|
|
// modes. For each a test run, a new workspace is created containing the
|
|
|
|
// un-txtared files specified by filedata.
|
2020-04-14 15:28:37 -06:00
|
|
|
func (r *Runner) Run(t *testing.T, filedata string, test func(t *testing.T, e *Env), opts ...RunOption) {
|
2020-02-06 17:50:37 -07:00
|
|
|
t.Helper()
|
2020-04-14 15:28:37 -06:00
|
|
|
config := r.defaultConfig()
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.set(config)
|
|
|
|
}
|
2020-02-06 17:50:37 -07:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-04-01 19:08:59 -06:00
|
|
|
name string
|
|
|
|
mode EnvMode
|
|
|
|
getServer func(context.Context, *testing.T) jsonrpc2.StreamServer
|
2020-02-06 17:50:37 -07:00
|
|
|
}{
|
2020-04-01 19:08:59 -06:00
|
|
|
{"singleton", Singleton, singletonEnv},
|
2020-02-06 17:50:37 -07:00
|
|
|
{"forwarded", Forwarded, r.forwardedEnv},
|
2020-02-10 09:34:13 -07:00
|
|
|
{"separate_process", SeparateProcess, r.separateProcessEnv},
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
2020-04-14 15:28:37 -06:00
|
|
|
if config.modes&tc.mode == 0 {
|
2020-02-06 17:50:37 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Helper()
|
2020-04-14 15:28:37 -06:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), config.timeout)
|
2020-02-06 17:50:37 -07:00
|
|
|
defer cancel()
|
2020-04-01 19:08:59 -06:00
|
|
|
ctx = debug.WithInstance(ctx, "", "")
|
|
|
|
|
2020-04-14 15:28:37 -06:00
|
|
|
ws, err := fake.NewWorkspace("regtest", filedata, config.proxyTxt)
|
2020-02-06 17:50:37 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-04-14 14:59:58 -06:00
|
|
|
// Deferring the closure of ws until the end of the entire test suite
|
|
|
|
// has, in testing, given the LSP server time to properly shutdown and
|
|
|
|
// release any file locks held in workspace, which is a problem on
|
|
|
|
// Windows. This may still be flaky however, and in the future we need a
|
|
|
|
// better solution to ensure that all Go processes started by gopls have
|
|
|
|
// exited before we clean up.
|
|
|
|
r.AddCloser(ws)
|
2020-04-01 19:08:59 -06:00
|
|
|
ss := tc.getServer(ctx, t)
|
|
|
|
ls := &loggingServer{delegate: ss}
|
|
|
|
ts := servertest.NewPipeServer(ctx, ls)
|
|
|
|
defer func() {
|
|
|
|
ts.Close()
|
|
|
|
}()
|
2020-02-06 17:50:37 -07:00
|
|
|
env := NewEnv(ctx, t, ws, ts)
|
2020-02-18 18:53:06 -07:00
|
|
|
defer func() {
|
2020-04-15 15:14:53 -06:00
|
|
|
if t.Failed() && r.PrintGoroutinesOnFailure {
|
|
|
|
pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
|
|
|
|
}
|
|
|
|
if t.Failed() || r.AlwaysPrintLogs {
|
2020-04-01 19:08:59 -06:00
|
|
|
ls.printBuffers(t.Name(), os.Stderr)
|
|
|
|
}
|
2020-02-18 18:53:06 -07:00
|
|
|
if err := env.E.Shutdown(ctx); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
2020-04-14 11:40:37 -06:00
|
|
|
test(t, env)
|
2020-02-06 17:50:37 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:08:59 -06:00
|
|
|
type loggingServer struct {
|
|
|
|
delegate jsonrpc2.StreamServer
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
buffers []*bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *loggingServer) ServeStream(ctx context.Context, stream jsonrpc2.Stream) error {
|
|
|
|
s.mu.Lock()
|
|
|
|
var buf bytes.Buffer
|
|
|
|
s.buffers = append(s.buffers, &buf)
|
|
|
|
s.mu.Unlock()
|
|
|
|
logStream := protocol.LoggingStream(stream, &buf)
|
|
|
|
return s.delegate.ServeStream(ctx, logStream)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *loggingServer) printBuffers(testname string, w io.Writer) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
for i, buf := range s.buffers {
|
|
|
|
fmt.Fprintf(os.Stderr, "#### Start Gopls Test Logs %d of %d for %q\n", i+1, len(s.buffers), testname)
|
|
|
|
io.Copy(w, buf)
|
|
|
|
fmt.Fprintf(os.Stderr, "#### End Gopls Test Logs %d of %d for %q\n", i+1, len(s.buffers), testname)
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:08:59 -06:00
|
|
|
func singletonEnv(ctx context.Context, t *testing.T) jsonrpc2.StreamServer {
|
|
|
|
return lsprpc.NewStreamServer(cache.New(ctx, nil))
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
|
|
|
|
2020-04-01 19:08:59 -06:00
|
|
|
func (r *Runner) forwardedEnv(ctx context.Context, t *testing.T) jsonrpc2.StreamServer {
|
2020-02-10 09:34:13 -07:00
|
|
|
ts := r.getTestServer()
|
2020-04-01 19:08:59 -06:00
|
|
|
return lsprpc.NewForwarder("tcp", ts.Addr)
|
2020-02-10 09:34:13 -07:00
|
|
|
}
|
|
|
|
|
2020-04-01 19:08:59 -06:00
|
|
|
func (r *Runner) separateProcessEnv(ctx context.Context, t *testing.T) jsonrpc2.StreamServer {
|
2020-02-18 10:47:19 -07:00
|
|
|
// TODO(rfindley): can we use the autostart behavior here, instead of
|
|
|
|
// pre-starting the remote?
|
2020-04-01 19:08:59 -06:00
|
|
|
socket := r.getRemoteSocket(t)
|
|
|
|
return lsprpc.NewForwarder("unix", socket)
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Env holds an initialized fake Editor, Workspace, and Server, which may be
|
|
|
|
// used for writing tests. It also provides adapter methods that call t.Fatal
|
|
|
|
// on any error, so that tests for the happy path may be written without
|
|
|
|
// checking errors.
|
|
|
|
type Env struct {
|
2020-03-23 15:26:05 -06:00
|
|
|
T *testing.T
|
|
|
|
Ctx context.Context
|
2020-02-06 17:50:37 -07:00
|
|
|
|
2020-03-04 11:29:23 -07:00
|
|
|
// Most tests should not need to access the workspace, editor, server, or
|
|
|
|
// connection, but they are available if needed.
|
2020-02-06 17:50:37 -07:00
|
|
|
W *fake.Workspace
|
|
|
|
E *fake.Editor
|
2020-02-09 11:38:49 -07:00
|
|
|
Server servertest.Connector
|
2020-03-04 11:29:23 -07:00
|
|
|
Conn *jsonrpc2.Conn
|
2020-02-06 17:50:37 -07:00
|
|
|
|
|
|
|
// mu guards the fields below, for the purpose of checking conditions on
|
|
|
|
// every change to diagnostics.
|
|
|
|
mu sync.Mutex
|
|
|
|
// For simplicity, each waiter gets a unique ID.
|
2020-04-15 15:14:53 -06:00
|
|
|
nextWaiterID int
|
|
|
|
state State
|
|
|
|
waiters map[int]*condition
|
|
|
|
}
|
|
|
|
|
|
|
|
// State encapsulates the server state TODO: explain more
|
|
|
|
type State struct {
|
|
|
|
// diagnostics are a map of relative path->diagnostics params
|
|
|
|
diagnostics map[string]*protocol.PublishDiagnosticsParams
|
|
|
|
logs []*protocol.LogMessageParams
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s State) String() string {
|
|
|
|
var b strings.Builder
|
|
|
|
b.WriteString("#### log messages (see RPC logs for full text):\n")
|
|
|
|
for _, msg := range s.logs {
|
|
|
|
summary := fmt.Sprintf("%v: %q", msg.Type, msg.Message)
|
|
|
|
if len(summary) > 60 {
|
|
|
|
summary = summary[:57] + "..."
|
|
|
|
}
|
|
|
|
// Some logs are quite long, and since they should be reproduced in the RPC
|
|
|
|
// logs on any failure we include here just a short summary.
|
|
|
|
fmt.Fprint(&b, "\t"+summary+"\n")
|
|
|
|
}
|
|
|
|
b.WriteString("\n")
|
|
|
|
b.WriteString("#### diagnostics:\n")
|
|
|
|
for name, params := range s.diagnostics {
|
|
|
|
fmt.Fprintf(&b, "\t%s (version %d):\n", name, int(params.Version))
|
|
|
|
for _, d := range params.Diagnostics {
|
|
|
|
fmt.Fprintf(&b, "\t\t(%d, %d): %s\n", int(d.Range.Start.Line), int(d.Range.Start.Character), d.Message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b.String()
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
|
|
|
|
2020-04-15 15:14:53 -06:00
|
|
|
// A condition is satisfied when all expectations are simultaneously
|
internal/lsp/regtest: add functions to make diagnostic assertions easier
One of the tricky things about asserting on conditions in regtests is
the asynchronous nature of LSP. For example, as the LSP client we cannot
be sure when we've received all diagnostics for a given file.
Currently, regtests are implemented by awaiting specific diagnostic
expectations. This means that if gopls generates diagnostics that do
not match those expectations, we can only time out the test.
Ideally, we would want to know that gopls is done generating all diagnostics
for the current file state. This is not possible without knowing the
status of diagnostics for. Barring this, we would want to know that
diagnostics are done for the current file version. Unfortunately, that
also is not possible, because a new version of file B can affect
diagnostics in file A.
So in lieu of this information, this CL exposes a few tools that can be
used to improve the experience of writing new regtests.
- A new expectation is added: AnyDiagnosticAtCurrentVersion, that is
satisfied if any diagnostics have been received for the current
buffer version.
- ExpectDiagnostics is added to Env, to help check whether the current
diagnostics matches expectations.
Updates golang/go#38113
Change-Id: I48d2c3db87c13ac3ab424d01d9444cbc285af9e1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226842
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-04-01 12:56:48 -06:00
|
|
|
// met. At that point, the 'met' channel is closed. On any failure, err is set
|
|
|
|
// and the failed channel is closed.
|
2020-04-15 15:14:53 -06:00
|
|
|
type condition struct {
|
|
|
|
expectations []Expectation
|
|
|
|
verdict chan Verdict
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewEnv creates a new test environment using the given workspace and gopls
|
|
|
|
// server.
|
2020-02-09 11:38:49 -07:00
|
|
|
func NewEnv(ctx context.Context, t *testing.T, ws *fake.Workspace, ts servertest.Connector) *Env {
|
2020-02-06 17:50:37 -07:00
|
|
|
t.Helper()
|
|
|
|
conn := ts.Connect(ctx)
|
|
|
|
editor, err := fake.NewConnectedEditor(ctx, ws, conn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
env := &Env{
|
2020-04-15 15:14:53 -06:00
|
|
|
T: t,
|
|
|
|
Ctx: ctx,
|
|
|
|
W: ws,
|
|
|
|
E: editor,
|
|
|
|
Server: ts,
|
|
|
|
Conn: conn,
|
|
|
|
state: State{
|
|
|
|
diagnostics: make(map[string]*protocol.PublishDiagnosticsParams),
|
|
|
|
},
|
|
|
|
waiters: make(map[int]*condition),
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
|
|
|
env.E.Client().OnDiagnostics(env.onDiagnostics)
|
2020-04-15 15:14:53 -06:00
|
|
|
env.E.Client().OnLogMessage(env.onLogMessage)
|
2020-02-06 17:50:37 -07:00
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Env) onDiagnostics(_ context.Context, d *protocol.PublishDiagnosticsParams) error {
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
|
|
|
pth := e.W.URIToPath(d.URI)
|
2020-04-15 15:14:53 -06:00
|
|
|
e.state.diagnostics[pth] = d
|
|
|
|
e.checkConditionsLocked()
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-06 17:50:37 -07:00
|
|
|
|
2020-04-15 15:14:53 -06:00
|
|
|
func (e *Env) onLogMessage(_ context.Context, m *protocol.LogMessageParams) error {
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
e.state.logs = append(e.state.logs, m)
|
|
|
|
e.checkConditionsLocked()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Env) checkConditionsLocked() {
|
2020-02-06 17:50:37 -07:00
|
|
|
for id, condition := range e.waiters {
|
2020-04-15 15:14:53 -06:00
|
|
|
if v, _ := checkExpectations(e.state, condition.expectations); v != Unmet {
|
2020-02-06 17:50:37 -07:00
|
|
|
delete(e.waiters, id)
|
2020-04-15 15:14:53 -06:00
|
|
|
condition.verdict <- v
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:14:53 -06:00
|
|
|
// ExpectNow asserts that the current state of the editor matches the given
|
|
|
|
// expectations.
|
|
|
|
//
|
|
|
|
// It can be used together with Env.Await to allow waiting on
|
|
|
|
// simple expectations, followed by more detailed expectations tested by
|
|
|
|
// ExpectNow. For example:
|
internal/lsp/regtest: add functions to make diagnostic assertions easier
One of the tricky things about asserting on conditions in regtests is
the asynchronous nature of LSP. For example, as the LSP client we cannot
be sure when we've received all diagnostics for a given file.
Currently, regtests are implemented by awaiting specific diagnostic
expectations. This means that if gopls generates diagnostics that do
not match those expectations, we can only time out the test.
Ideally, we would want to know that gopls is done generating all diagnostics
for the current file state. This is not possible without knowing the
status of diagnostics for. Barring this, we would want to know that
diagnostics are done for the current file version. Unfortunately, that
also is not possible, because a new version of file B can affect
diagnostics in file A.
So in lieu of this information, this CL exposes a few tools that can be
used to improve the experience of writing new regtests.
- A new expectation is added: AnyDiagnosticAtCurrentVersion, that is
satisfied if any diagnostics have been received for the current
buffer version.
- ExpectDiagnostics is added to Env, to help check whether the current
diagnostics matches expectations.
Updates golang/go#38113
Change-Id: I48d2c3db87c13ac3ab424d01d9444cbc285af9e1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226842
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-04-01 12:56:48 -06:00
|
|
|
//
|
|
|
|
// env.RegexpReplace("foo.go", "a", "x")
|
|
|
|
// env.Await(env.AnyDiagnosticAtCurrentVersion("foo.go"))
|
2020-04-15 15:14:53 -06:00
|
|
|
// env.ExpectNow(env.DiagnosticAtRegexp("foo.go", "x"))
|
internal/lsp/regtest: add functions to make diagnostic assertions easier
One of the tricky things about asserting on conditions in regtests is
the asynchronous nature of LSP. For example, as the LSP client we cannot
be sure when we've received all diagnostics for a given file.
Currently, regtests are implemented by awaiting specific diagnostic
expectations. This means that if gopls generates diagnostics that do
not match those expectations, we can only time out the test.
Ideally, we would want to know that gopls is done generating all diagnostics
for the current file state. This is not possible without knowing the
status of diagnostics for. Barring this, we would want to know that
diagnostics are done for the current file version. Unfortunately, that
also is not possible, because a new version of file B can affect
diagnostics in file A.
So in lieu of this information, this CL exposes a few tools that can be
used to improve the experience of writing new regtests.
- A new expectation is added: AnyDiagnosticAtCurrentVersion, that is
satisfied if any diagnostics have been received for the current
buffer version.
- ExpectDiagnostics is added to Env, to help check whether the current
diagnostics matches expectations.
Updates golang/go#38113
Change-Id: I48d2c3db87c13ac3ab424d01d9444cbc285af9e1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226842
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-04-01 12:56:48 -06:00
|
|
|
//
|
|
|
|
// This has the advantage of not timing out if the diagnostic received for
|
|
|
|
// "foo.go" does not match the expectation: instead it fails early.
|
2020-04-15 15:14:53 -06:00
|
|
|
func (e *Env) ExpectNow(expectations ...Expectation) {
|
internal/lsp/regtest: add functions to make diagnostic assertions easier
One of the tricky things about asserting on conditions in regtests is
the asynchronous nature of LSP. For example, as the LSP client we cannot
be sure when we've received all diagnostics for a given file.
Currently, regtests are implemented by awaiting specific diagnostic
expectations. This means that if gopls generates diagnostics that do
not match those expectations, we can only time out the test.
Ideally, we would want to know that gopls is done generating all diagnostics
for the current file state. This is not possible without knowing the
status of diagnostics for. Barring this, we would want to know that
diagnostics are done for the current file version. Unfortunately, that
also is not possible, because a new version of file B can affect
diagnostics in file A.
So in lieu of this information, this CL exposes a few tools that can be
used to improve the experience of writing new regtests.
- A new expectation is added: AnyDiagnosticAtCurrentVersion, that is
satisfied if any diagnostics have been received for the current
buffer version.
- ExpectDiagnostics is added to Env, to help check whether the current
diagnostics matches expectations.
Updates golang/go#38113
Change-Id: I48d2c3db87c13ac3ab424d01d9444cbc285af9e1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226842
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-04-01 12:56:48 -06:00
|
|
|
e.T.Helper()
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
2020-04-15 15:14:53 -06:00
|
|
|
if verdict, summary := checkExpectations(e.state, expectations); verdict != Met {
|
|
|
|
e.T.Fatalf("expectations unmet:\n%s\ncurrent state:\n%v", summary, e.state)
|
internal/lsp/regtest: add functions to make diagnostic assertions easier
One of the tricky things about asserting on conditions in regtests is
the asynchronous nature of LSP. For example, as the LSP client we cannot
be sure when we've received all diagnostics for a given file.
Currently, regtests are implemented by awaiting specific diagnostic
expectations. This means that if gopls generates diagnostics that do
not match those expectations, we can only time out the test.
Ideally, we would want to know that gopls is done generating all diagnostics
for the current file state. This is not possible without knowing the
status of diagnostics for. Barring this, we would want to know that
diagnostics are done for the current file version. Unfortunately, that
also is not possible, because a new version of file B can affect
diagnostics in file A.
So in lieu of this information, this CL exposes a few tools that can be
used to improve the experience of writing new regtests.
- A new expectation is added: AnyDiagnosticAtCurrentVersion, that is
satisfied if any diagnostics have been received for the current
buffer version.
- ExpectDiagnostics is added to Env, to help check whether the current
diagnostics matches expectations.
Updates golang/go#38113
Change-Id: I48d2c3db87c13ac3ab424d01d9444cbc285af9e1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226842
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-04-01 12:56:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:14:53 -06:00
|
|
|
// checkExpectations reports whether s meets all expectations.
|
|
|
|
func checkExpectations(s State, expectations []Expectation) (Verdict, string) {
|
|
|
|
finalVerdict := Met
|
|
|
|
var summary strings.Builder
|
2020-02-06 17:50:37 -07:00
|
|
|
for _, e := range expectations {
|
2020-04-15 15:14:53 -06:00
|
|
|
v := e.Check(s)
|
|
|
|
if v > finalVerdict {
|
|
|
|
finalVerdict = v
|
|
|
|
}
|
|
|
|
summary.WriteString(fmt.Sprintf("\t%v: %s\n", v, e.Description()))
|
|
|
|
}
|
|
|
|
return finalVerdict, summary.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// An Expectation asserts that the state of the editor at a point in time
|
|
|
|
// matches an expected condition. This is used for signaling in tests when
|
|
|
|
// certain conditions in the editor are met.
|
|
|
|
type Expectation interface {
|
|
|
|
// Check determines whether the state of the editor satisfies the
|
|
|
|
// expectation.
|
|
|
|
Check(State) Verdict
|
|
|
|
// Description is a human-readable description of the expectation.
|
|
|
|
Description() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Verdict is the result of checking an expectation against the current
|
|
|
|
// editor state.
|
|
|
|
type Verdict int
|
|
|
|
|
|
|
|
// Order matters for the following constants: verdicts are sorted in order of
|
|
|
|
// decisiveness.
|
|
|
|
const (
|
|
|
|
// Met indicates that an expectation is satisfied by the current state.
|
|
|
|
Met Verdict = iota
|
|
|
|
// Unmet indicates that an expectation is not currently met, but could be met
|
|
|
|
// in the future.
|
|
|
|
Unmet
|
|
|
|
// Unmeetable indicates that an expectation cannot be satisfied in the
|
|
|
|
// future.
|
|
|
|
Unmeetable
|
|
|
|
)
|
|
|
|
|
|
|
|
func (v Verdict) String() string {
|
|
|
|
switch v {
|
|
|
|
case Met:
|
|
|
|
return "Met"
|
|
|
|
case Unmet:
|
|
|
|
return "Unmet"
|
|
|
|
case Unmeetable:
|
|
|
|
return "Unmeetable"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("unrecognized verdict %d", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogExpectation is an expectation on the log messages received by the editor
|
|
|
|
// from gopls.
|
|
|
|
type LogExpectation struct {
|
|
|
|
check func([]*protocol.LogMessageParams) Verdict
|
|
|
|
description string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check implements the Expectation interface.
|
|
|
|
func (e LogExpectation) Check(s State) Verdict {
|
|
|
|
return e.check(s.logs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Description implements the Expectation interface.
|
|
|
|
func (e LogExpectation) Description() string {
|
|
|
|
return e.description
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoErrorLogs asserts that the client has not received any log messages of
|
|
|
|
// error severity.
|
|
|
|
func NoErrorLogs() LogExpectation {
|
|
|
|
check := func(msgs []*protocol.LogMessageParams) Verdict {
|
|
|
|
for _, msg := range msgs {
|
|
|
|
if msg.Type == protocol.Error {
|
|
|
|
return Unmeetable
|
|
|
|
}
|
internal/lsp/regtest: add functions to make diagnostic assertions easier
One of the tricky things about asserting on conditions in regtests is
the asynchronous nature of LSP. For example, as the LSP client we cannot
be sure when we've received all diagnostics for a given file.
Currently, regtests are implemented by awaiting specific diagnostic
expectations. This means that if gopls generates diagnostics that do
not match those expectations, we can only time out the test.
Ideally, we would want to know that gopls is done generating all diagnostics
for the current file state. This is not possible without knowing the
status of diagnostics for. Barring this, we would want to know that
diagnostics are done for the current file version. Unfortunately, that
also is not possible, because a new version of file B can affect
diagnostics in file A.
So in lieu of this information, this CL exposes a few tools that can be
used to improve the experience of writing new regtests.
- A new expectation is added: AnyDiagnosticAtCurrentVersion, that is
satisfied if any diagnostics have been received for the current
buffer version.
- ExpectDiagnostics is added to Env, to help check whether the current
diagnostics matches expectations.
Updates golang/go#38113
Change-Id: I48d2c3db87c13ac3ab424d01d9444cbc285af9e1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226842
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-04-01 12:56:48 -06:00
|
|
|
}
|
2020-04-15 15:14:53 -06:00
|
|
|
return Met
|
|
|
|
}
|
|
|
|
return LogExpectation{
|
|
|
|
check: check,
|
|
|
|
description: "no errors have been logged",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogMatching asserts that the client has received a log message
|
|
|
|
// matching of type typ matching the regexp re.
|
|
|
|
func LogMatching(typ protocol.MessageType, re string) LogExpectation {
|
|
|
|
rec, err := regexp.Compile(re)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
check := func(msgs []*protocol.LogMessageParams) Verdict {
|
|
|
|
for _, msg := range msgs {
|
|
|
|
if msg.Type == typ && rec.Match([]byte(msg.Message)) {
|
|
|
|
return Met
|
|
|
|
}
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
2020-04-15 15:14:53 -06:00
|
|
|
return Unmet
|
|
|
|
}
|
|
|
|
return LogExpectation{
|
|
|
|
check: check,
|
|
|
|
description: fmt.Sprintf("log message matching %q", re),
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A DiagnosticExpectation is a condition that must be met by the current set
|
2020-04-15 15:14:53 -06:00
|
|
|
// of diagnostics for a file.
|
2020-02-06 17:50:37 -07:00
|
|
|
type DiagnosticExpectation struct {
|
internal/lsp/regtest: add functions to make diagnostic assertions easier
One of the tricky things about asserting on conditions in regtests is
the asynchronous nature of LSP. For example, as the LSP client we cannot
be sure when we've received all diagnostics for a given file.
Currently, regtests are implemented by awaiting specific diagnostic
expectations. This means that if gopls generates diagnostics that do
not match those expectations, we can only time out the test.
Ideally, we would want to know that gopls is done generating all diagnostics
for the current file state. This is not possible without knowing the
status of diagnostics for. Barring this, we would want to know that
diagnostics are done for the current file version. Unfortunately, that
also is not possible, because a new version of file B can affect
diagnostics in file A.
So in lieu of this information, this CL exposes a few tools that can be
used to improve the experience of writing new regtests.
- A new expectation is added: AnyDiagnosticAtCurrentVersion, that is
satisfied if any diagnostics have been received for the current
buffer version.
- ExpectDiagnostics is added to Env, to help check whether the current
diagnostics matches expectations.
Updates golang/go#38113
Change-Id: I48d2c3db87c13ac3ab424d01d9444cbc285af9e1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226842
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-04-01 12:56:48 -06:00
|
|
|
// IsMet determines whether the diagnostics for this file version satisfy our
|
|
|
|
// expectation.
|
2020-04-15 15:14:53 -06:00
|
|
|
isMet func(*protocol.PublishDiagnosticsParams) bool
|
internal/lsp/regtest: add functions to make diagnostic assertions easier
One of the tricky things about asserting on conditions in regtests is
the asynchronous nature of LSP. For example, as the LSP client we cannot
be sure when we've received all diagnostics for a given file.
Currently, regtests are implemented by awaiting specific diagnostic
expectations. This means that if gopls generates diagnostics that do
not match those expectations, we can only time out the test.
Ideally, we would want to know that gopls is done generating all diagnostics
for the current file state. This is not possible without knowing the
status of diagnostics for. Barring this, we would want to know that
diagnostics are done for the current file version. Unfortunately, that
also is not possible, because a new version of file B can affect
diagnostics in file A.
So in lieu of this information, this CL exposes a few tools that can be
used to improve the experience of writing new regtests.
- A new expectation is added: AnyDiagnosticAtCurrentVersion, that is
satisfied if any diagnostics have been received for the current
buffer version.
- ExpectDiagnostics is added to Env, to help check whether the current
diagnostics matches expectations.
Updates golang/go#38113
Change-Id: I48d2c3db87c13ac3ab424d01d9444cbc285af9e1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226842
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-04-01 12:56:48 -06:00
|
|
|
// Description is a human-readable description of the diagnostic expectation.
|
2020-04-15 15:14:53 -06:00
|
|
|
description string
|
internal/lsp/regtest: add functions to make diagnostic assertions easier
One of the tricky things about asserting on conditions in regtests is
the asynchronous nature of LSP. For example, as the LSP client we cannot
be sure when we've received all diagnostics for a given file.
Currently, regtests are implemented by awaiting specific diagnostic
expectations. This means that if gopls generates diagnostics that do
not match those expectations, we can only time out the test.
Ideally, we would want to know that gopls is done generating all diagnostics
for the current file state. This is not possible without knowing the
status of diagnostics for. Barring this, we would want to know that
diagnostics are done for the current file version. Unfortunately, that
also is not possible, because a new version of file B can affect
diagnostics in file A.
So in lieu of this information, this CL exposes a few tools that can be
used to improve the experience of writing new regtests.
- A new expectation is added: AnyDiagnosticAtCurrentVersion, that is
satisfied if any diagnostics have been received for the current
buffer version.
- ExpectDiagnostics is added to Env, to help check whether the current
diagnostics matches expectations.
Updates golang/go#38113
Change-Id: I48d2c3db87c13ac3ab424d01d9444cbc285af9e1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226842
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-04-01 12:56:48 -06:00
|
|
|
// Path is the workspace-relative path to the file being asserted on.
|
2020-04-15 15:14:53 -06:00
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check implements the Expectation interface.
|
|
|
|
func (e DiagnosticExpectation) Check(s State) Verdict {
|
|
|
|
if diags, ok := s.diagnostics[e.path]; ok && e.isMet(diags) {
|
|
|
|
return Met
|
|
|
|
}
|
|
|
|
return Unmet
|
|
|
|
}
|
|
|
|
|
|
|
|
// Description implements the Expectation interface.
|
|
|
|
func (e DiagnosticExpectation) Description() string {
|
|
|
|
return fmt.Sprintf("%s: %s", e.path, e.description)
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// EmptyDiagnostics asserts that diagnostics are empty for the
|
|
|
|
// workspace-relative path name.
|
|
|
|
func EmptyDiagnostics(name string) DiagnosticExpectation {
|
internal/lsp/regtest: add functions to make diagnostic assertions easier
One of the tricky things about asserting on conditions in regtests is
the asynchronous nature of LSP. For example, as the LSP client we cannot
be sure when we've received all diagnostics for a given file.
Currently, regtests are implemented by awaiting specific diagnostic
expectations. This means that if gopls generates diagnostics that do
not match those expectations, we can only time out the test.
Ideally, we would want to know that gopls is done generating all diagnostics
for the current file state. This is not possible without knowing the
status of diagnostics for. Barring this, we would want to know that
diagnostics are done for the current file version. Unfortunately, that
also is not possible, because a new version of file B can affect
diagnostics in file A.
So in lieu of this information, this CL exposes a few tools that can be
used to improve the experience of writing new regtests.
- A new expectation is added: AnyDiagnosticAtCurrentVersion, that is
satisfied if any diagnostics have been received for the current
buffer version.
- ExpectDiagnostics is added to Env, to help check whether the current
diagnostics matches expectations.
Updates golang/go#38113
Change-Id: I48d2c3db87c13ac3ab424d01d9444cbc285af9e1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226842
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-04-01 12:56:48 -06:00
|
|
|
isMet := func(diags *protocol.PublishDiagnosticsParams) bool {
|
|
|
|
return len(diags.Diagnostics) == 0
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
|
|
|
return DiagnosticExpectation{
|
2020-04-15 15:14:53 -06:00
|
|
|
isMet: isMet,
|
|
|
|
description: "empty diagnostics",
|
|
|
|
path: name,
|
internal/lsp/regtest: add functions to make diagnostic assertions easier
One of the tricky things about asserting on conditions in regtests is
the asynchronous nature of LSP. For example, as the LSP client we cannot
be sure when we've received all diagnostics for a given file.
Currently, regtests are implemented by awaiting specific diagnostic
expectations. This means that if gopls generates diagnostics that do
not match those expectations, we can only time out the test.
Ideally, we would want to know that gopls is done generating all diagnostics
for the current file state. This is not possible without knowing the
status of diagnostics for. Barring this, we would want to know that
diagnostics are done for the current file version. Unfortunately, that
also is not possible, because a new version of file B can affect
diagnostics in file A.
So in lieu of this information, this CL exposes a few tools that can be
used to improve the experience of writing new regtests.
- A new expectation is added: AnyDiagnosticAtCurrentVersion, that is
satisfied if any diagnostics have been received for the current
buffer version.
- ExpectDiagnostics is added to Env, to help check whether the current
diagnostics matches expectations.
Updates golang/go#38113
Change-Id: I48d2c3db87c13ac3ab424d01d9444cbc285af9e1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226842
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-04-01 12:56:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AnyDiagnosticAtCurrentVersion asserts that there is a diagnostic report for
|
|
|
|
// the current edited version of the buffer corresponding to the given
|
|
|
|
// workspace-relative pathname.
|
|
|
|
func (e *Env) AnyDiagnosticAtCurrentVersion(name string) DiagnosticExpectation {
|
|
|
|
version := e.E.BufferVersion(name)
|
|
|
|
isMet := func(diags *protocol.PublishDiagnosticsParams) bool {
|
|
|
|
return int(diags.Version) == version
|
|
|
|
}
|
|
|
|
return DiagnosticExpectation{
|
2020-04-15 15:14:53 -06:00
|
|
|
isMet: isMet,
|
|
|
|
description: fmt.Sprintf("any diagnostics at version %d", version),
|
|
|
|
path: name,
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 12:56:20 -07:00
|
|
|
// DiagnosticAtRegexp expects that there is a diagnostic entry at the start
|
|
|
|
// position matching the regexp search string re in the buffer specified by
|
|
|
|
// name. Note that this currently ignores the end position.
|
|
|
|
func (e *Env) DiagnosticAtRegexp(name, re string) DiagnosticExpectation {
|
|
|
|
pos := e.RegexpSearch(name, re)
|
|
|
|
expectation := DiagnosticAt(name, pos.Line, pos.Column)
|
2020-04-15 15:14:53 -06:00
|
|
|
expectation.description += fmt.Sprintf(" (location of %q)", re)
|
2020-02-28 12:56:20 -07:00
|
|
|
return expectation
|
|
|
|
}
|
|
|
|
|
2020-02-06 17:50:37 -07:00
|
|
|
// DiagnosticAt asserts that there is a diagnostic entry at the position
|
|
|
|
// specified by line and col, for the workspace-relative path name.
|
|
|
|
func DiagnosticAt(name string, line, col int) DiagnosticExpectation {
|
internal/lsp/regtest: add functions to make diagnostic assertions easier
One of the tricky things about asserting on conditions in regtests is
the asynchronous nature of LSP. For example, as the LSP client we cannot
be sure when we've received all diagnostics for a given file.
Currently, regtests are implemented by awaiting specific diagnostic
expectations. This means that if gopls generates diagnostics that do
not match those expectations, we can only time out the test.
Ideally, we would want to know that gopls is done generating all diagnostics
for the current file state. This is not possible without knowing the
status of diagnostics for. Barring this, we would want to know that
diagnostics are done for the current file version. Unfortunately, that
also is not possible, because a new version of file B can affect
diagnostics in file A.
So in lieu of this information, this CL exposes a few tools that can be
used to improve the experience of writing new regtests.
- A new expectation is added: AnyDiagnosticAtCurrentVersion, that is
satisfied if any diagnostics have been received for the current
buffer version.
- ExpectDiagnostics is added to Env, to help check whether the current
diagnostics matches expectations.
Updates golang/go#38113
Change-Id: I48d2c3db87c13ac3ab424d01d9444cbc285af9e1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226842
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-04-01 12:56:48 -06:00
|
|
|
isMet := func(diags *protocol.PublishDiagnosticsParams) bool {
|
|
|
|
for _, d := range diags.Diagnostics {
|
2020-02-06 17:50:37 -07:00
|
|
|
if d.Range.Start.Line == float64(line) && d.Range.Start.Character == float64(col) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return DiagnosticExpectation{
|
2020-04-15 15:14:53 -06:00
|
|
|
isMet: isMet,
|
|
|
|
description: fmt.Sprintf("diagnostic at {line:%d, column:%d}", line, col),
|
|
|
|
path: name,
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:14:53 -06:00
|
|
|
// Await waits for all expectations to simultaneously be met. It should only be
|
|
|
|
// called from the main test goroutine.
|
|
|
|
func (e *Env) Await(expectations ...Expectation) {
|
2020-03-23 15:26:05 -06:00
|
|
|
e.T.Helper()
|
2020-02-06 17:50:37 -07:00
|
|
|
e.mu.Lock()
|
internal/lsp/regtest: add functions to make diagnostic assertions easier
One of the tricky things about asserting on conditions in regtests is
the asynchronous nature of LSP. For example, as the LSP client we cannot
be sure when we've received all diagnostics for a given file.
Currently, regtests are implemented by awaiting specific diagnostic
expectations. This means that if gopls generates diagnostics that do
not match those expectations, we can only time out the test.
Ideally, we would want to know that gopls is done generating all diagnostics
for the current file state. This is not possible without knowing the
status of diagnostics for. Barring this, we would want to know that
diagnostics are done for the current file version. Unfortunately, that
also is not possible, because a new version of file B can affect
diagnostics in file A.
So in lieu of this information, this CL exposes a few tools that can be
used to improve the experience of writing new regtests.
- A new expectation is added: AnyDiagnosticAtCurrentVersion, that is
satisfied if any diagnostics have been received for the current
buffer version.
- ExpectDiagnostics is added to Env, to help check whether the current
diagnostics matches expectations.
Updates golang/go#38113
Change-Id: I48d2c3db87c13ac3ab424d01d9444cbc285af9e1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226842
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-04-01 12:56:48 -06:00
|
|
|
// Before adding the waiter, we check if the condition is currently met or
|
|
|
|
// failed to avoid a race where the condition was realized before Await was
|
|
|
|
// called.
|
2020-04-15 15:14:53 -06:00
|
|
|
switch verdict, summary := checkExpectations(e.state, expectations); verdict {
|
|
|
|
case Met:
|
2020-02-06 17:50:37 -07:00
|
|
|
return
|
2020-04-15 15:14:53 -06:00
|
|
|
case Unmeetable:
|
|
|
|
e.mu.Unlock()
|
|
|
|
e.T.Fatalf("unmeetable expectations:\n%s\nstate:\n%v", summary, e.state)
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
2020-04-15 15:14:53 -06:00
|
|
|
cond := &condition{
|
2020-02-06 17:50:37 -07:00
|
|
|
expectations: expectations,
|
2020-04-15 15:14:53 -06:00
|
|
|
verdict: make(chan Verdict),
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|
internal/lsp/regtest: add functions to make diagnostic assertions easier
One of the tricky things about asserting on conditions in regtests is
the asynchronous nature of LSP. For example, as the LSP client we cannot
be sure when we've received all diagnostics for a given file.
Currently, regtests are implemented by awaiting specific diagnostic
expectations. This means that if gopls generates diagnostics that do
not match those expectations, we can only time out the test.
Ideally, we would want to know that gopls is done generating all diagnostics
for the current file state. This is not possible without knowing the
status of diagnostics for. Barring this, we would want to know that
diagnostics are done for the current file version. Unfortunately, that
also is not possible, because a new version of file B can affect
diagnostics in file A.
So in lieu of this information, this CL exposes a few tools that can be
used to improve the experience of writing new regtests.
- A new expectation is added: AnyDiagnosticAtCurrentVersion, that is
satisfied if any diagnostics have been received for the current
buffer version.
- ExpectDiagnostics is added to Env, to help check whether the current
diagnostics matches expectations.
Updates golang/go#38113
Change-Id: I48d2c3db87c13ac3ab424d01d9444cbc285af9e1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226842
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-04-01 12:56:48 -06:00
|
|
|
e.waiters[e.nextWaiterID] = cond
|
2020-02-06 17:50:37 -07:00
|
|
|
e.nextWaiterID++
|
|
|
|
e.mu.Unlock()
|
|
|
|
|
2020-04-15 15:14:53 -06:00
|
|
|
var err error
|
2020-02-06 17:50:37 -07:00
|
|
|
select {
|
2020-03-23 15:26:05 -06:00
|
|
|
case <-e.Ctx.Done():
|
2020-04-15 15:14:53 -06:00
|
|
|
err = e.Ctx.Err()
|
|
|
|
case v := <-cond.verdict:
|
|
|
|
if v != Met {
|
|
|
|
err = fmt.Errorf("condition has final verdict %v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2020-02-06 17:50:37 -07:00
|
|
|
// Debugging an unmet expectation can be tricky, so we put some effort into
|
|
|
|
// nicely formatting the failure.
|
|
|
|
e.mu.Lock()
|
2020-04-15 15:14:53 -06:00
|
|
|
defer e.mu.Unlock()
|
|
|
|
_, summary := checkExpectations(e.state, expectations)
|
|
|
|
e.T.Fatalf("waiting on:\n%s\nerr:%v\nstate:\n%v", err, summary, e.state)
|
internal/lsp/regtest: add functions to make diagnostic assertions easier
One of the tricky things about asserting on conditions in regtests is
the asynchronous nature of LSP. For example, as the LSP client we cannot
be sure when we've received all diagnostics for a given file.
Currently, regtests are implemented by awaiting specific diagnostic
expectations. This means that if gopls generates diagnostics that do
not match those expectations, we can only time out the test.
Ideally, we would want to know that gopls is done generating all diagnostics
for the current file state. This is not possible without knowing the
status of diagnostics for. Barring this, we would want to know that
diagnostics are done for the current file version. Unfortunately, that
also is not possible, because a new version of file B can affect
diagnostics in file A.
So in lieu of this information, this CL exposes a few tools that can be
used to improve the experience of writing new regtests.
- A new expectation is added: AnyDiagnosticAtCurrentVersion, that is
satisfied if any diagnostics have been received for the current
buffer version.
- ExpectDiagnostics is added to Env, to help check whether the current
diagnostics matches expectations.
Updates golang/go#38113
Change-Id: I48d2c3db87c13ac3ab424d01d9444cbc285af9e1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226842
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-04-01 12:56:48 -06:00
|
|
|
}
|
|
|
|
}
|