1
0
mirror of https://github.com/golang/go synced 2024-10-01 01:38:33 -06:00

internal/lsp/regtest: clean-up and more error handling

Fix various things related to regtest execution:
 + Check the error from OpenFile in fake.Editor.GoToDefinition.
 + Add an error-checked wrapper to env for CloseBuffer.
 + Use env wrappers in TestDiagnosticClearingOnClose.
 + Use os.Executable to get the test binary path.
 + Add a -listen.timeout to the remote gopls process, so that it is
   cleaned up.

Updates golang/go#36879

Change-Id: I056ff50bdb611a78ad78e4f5e2a94a4f155cc9de
Reviewed-on: https://go-review.googlesource.com/c/tools/+/220902
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-02-25 11:00:37 -05:00 committed by Robert Findley
parent 042d10a150
commit fefc8d1877
4 changed files with 15 additions and 22 deletions

View File

@ -348,7 +348,9 @@ func (e *Editor) GoToDefinition(ctx context.Context, path string, pos Pos) (stri
}
newPath := e.ws.URIToPath(resp[0].URI)
newPos := fromProtocolPosition(resp[0].Range.Start)
e.OpenFile(ctx, newPath)
if err := e.OpenFile(ctx, newPath); err != nil {
return "", Pos{}, fmt.Errorf("OpenFile: %v", err)
}
return newPath, newPos, nil
}

View File

@ -93,12 +93,11 @@ func TestDiagnosticClearingOnDelete(t *testing.T) {
func TestDiagnosticClearingOnClose(t *testing.T) {
t.Parallel()
runner.Run(t, badPackage, func(ctx context.Context, t *testing.T, env *Env) {
env.E.CreateBuffer(env.ctx, "c.go", `package consts
env.CreateBuffer("c.go", `package consts
const a = 3`)
env.Await(DiagnosticAt("a.go", 2, 6), DiagnosticAt("b.go", 2, 6), DiagnosticAt("c.go", 2, 6))
env.E.CloseBuffer(env.ctx, "c.go")
env.CloseBuffer("c.go")
env.Await(DiagnosticAt("a.go", 2, 6), DiagnosticAt("b.go", 2, 6), EmptyDiagnostics("c.go"))
})
}

View File

@ -110,7 +110,7 @@ func (r *Runner) getRemoteSocket(t *testing.T) string {
t.Fatalf("creating tempdir: %v", err)
}
socket := filepath.Join(r.socketDir, daemonFile)
args := []string{"serve", "-listen", "unix;" + socket}
args := []string{"serve", "-listen", "unix;" + socket, "-listen.timeout", "10s"}
cmd := exec.Command(r.goplsPath, args...)
cmd.Env = append(os.Environ(), runTestAsGoplsEnvvar+"=true")
var stderr bytes.Buffer
@ -298,6 +298,14 @@ func (e *Env) CreateBuffer(name string, content string) {
}
}
// CloseBuffer closes an editor buffer, calling t.Fatal on any error.
func (e *Env) CloseBuffer(name string) {
e.t.Helper()
if err := e.E.CloseBuffer(e.ctx, name); err != nil {
e.t.Fatal(err)
}
}
// EditBuffer applies edits to an editor buffer, calling t.Fatal on any error.
func (e *Env) EditBuffer(name string, edits ...fake.Edit) {
e.t.Helper()

View File

@ -9,7 +9,6 @@ import (
"flag"
"fmt"
"os"
"path/filepath"
"testing"
"time"
@ -39,7 +38,7 @@ func TestMain(m *testing.M) {
goplsPath := *goplsBinaryPath
if goplsPath == "" {
var err error
goplsPath, err = testBinaryPath()
goplsPath, err = os.Executable()
if err != nil {
panic(fmt.Sprintf("finding test binary path: %v", err))
}
@ -52,18 +51,3 @@ func TestMain(m *testing.M) {
runner.Close()
os.Exit(code)
}
func testBinaryPath() (string, error) {
pth := os.Args[0]
if !filepath.IsAbs(pth) {
cwd, err := os.Getwd()
if err == nil {
return "", fmt.Errorf("os.Getwd: %v", err)
}
pth = filepath.Join(cwd, pth)
}
if _, err := os.Stat(pth); err != nil {
return "", fmt.Errorf("os.Stat: %v", err)
}
return pth, nil
}