From fefc8d187781328d218f957bacb7632909acab8f Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Tue, 25 Feb 2020 11:00:37 -0500 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- internal/lsp/fake/editor.go | 4 +++- internal/lsp/regtest/diagnostics_test.go | 5 ++--- internal/lsp/regtest/env.go | 10 +++++++++- internal/lsp/regtest/reg_test.go | 18 +----------------- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/internal/lsp/fake/editor.go b/internal/lsp/fake/editor.go index 4ef7c8f23fd..5cb0a57027d 100644 --- a/internal/lsp/fake/editor.go +++ b/internal/lsp/fake/editor.go @@ -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 } diff --git a/internal/lsp/regtest/diagnostics_test.go b/internal/lsp/regtest/diagnostics_test.go index cbf4a0ed191..1006b5e4264 100644 --- a/internal/lsp/regtest/diagnostics_test.go +++ b/internal/lsp/regtest/diagnostics_test.go @@ -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")) }) } diff --git a/internal/lsp/regtest/env.go b/internal/lsp/regtest/env.go index 305e770ea2f..034deaaba92 100644 --- a/internal/lsp/regtest/env.go +++ b/internal/lsp/regtest/env.go @@ -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() diff --git a/internal/lsp/regtest/reg_test.go b/internal/lsp/regtest/reg_test.go index 2c57119f89a..9246ccb1bff 100644 --- a/internal/lsp/regtest/reg_test.go +++ b/internal/lsp/regtest/reg_test.go @@ -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 -}