Skip to content

Commit fefc8d1

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

File tree

4 files changed

+15
-22
lines changed

4 files changed

+15
-22
lines changed

Diff for: internal/lsp/fake/editor.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,9 @@ func (e *Editor) GoToDefinition(ctx context.Context, path string, pos Pos) (stri
348348
}
349349
newPath := e.ws.URIToPath(resp[0].URI)
350350
newPos := fromProtocolPosition(resp[0].Range.Start)
351-
e.OpenFile(ctx, newPath)
351+
if err := e.OpenFile(ctx, newPath); err != nil {
352+
return "", Pos{}, fmt.Errorf("OpenFile: %v", err)
353+
}
352354
return newPath, newPos, nil
353355
}
354356

Diff for: internal/lsp/regtest/diagnostics_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,11 @@ func TestDiagnosticClearingOnDelete(t *testing.T) {
9393
func TestDiagnosticClearingOnClose(t *testing.T) {
9494
t.Parallel()
9595
runner.Run(t, badPackage, func(ctx context.Context, t *testing.T, env *Env) {
96-
env.E.CreateBuffer(env.ctx, "c.go", `package consts
96+
env.CreateBuffer("c.go", `package consts
9797
9898
const a = 3`)
9999
env.Await(DiagnosticAt("a.go", 2, 6), DiagnosticAt("b.go", 2, 6), DiagnosticAt("c.go", 2, 6))
100-
env.E.CloseBuffer(env.ctx, "c.go")
101-
100+
env.CloseBuffer("c.go")
102101
env.Await(DiagnosticAt("a.go", 2, 6), DiagnosticAt("b.go", 2, 6), EmptyDiagnostics("c.go"))
103102
})
104103
}

Diff for: internal/lsp/regtest/env.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (r *Runner) getRemoteSocket(t *testing.T) string {
110110
t.Fatalf("creating tempdir: %v", err)
111111
}
112112
socket := filepath.Join(r.socketDir, daemonFile)
113-
args := []string{"serve", "-listen", "unix;" + socket}
113+
args := []string{"serve", "-listen", "unix;" + socket, "-listen.timeout", "10s"}
114114
cmd := exec.Command(r.goplsPath, args...)
115115
cmd.Env = append(os.Environ(), runTestAsGoplsEnvvar+"=true")
116116
var stderr bytes.Buffer
@@ -298,6 +298,14 @@ func (e *Env) CreateBuffer(name string, content string) {
298298
}
299299
}
300300

301+
// CloseBuffer closes an editor buffer, calling t.Fatal on any error.
302+
func (e *Env) CloseBuffer(name string) {
303+
e.t.Helper()
304+
if err := e.E.CloseBuffer(e.ctx, name); err != nil {
305+
e.t.Fatal(err)
306+
}
307+
}
308+
301309
// EditBuffer applies edits to an editor buffer, calling t.Fatal on any error.
302310
func (e *Env) EditBuffer(name string, edits ...fake.Edit) {
303311
e.t.Helper()

Diff for: internal/lsp/regtest/reg_test.go

+1-17
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"flag"
1010
"fmt"
1111
"os"
12-
"path/filepath"
1312
"testing"
1413
"time"
1514

@@ -39,7 +38,7 @@ func TestMain(m *testing.M) {
3938
goplsPath := *goplsBinaryPath
4039
if goplsPath == "" {
4140
var err error
42-
goplsPath, err = testBinaryPath()
41+
goplsPath, err = os.Executable()
4342
if err != nil {
4443
panic(fmt.Sprintf("finding test binary path: %v", err))
4544
}
@@ -52,18 +51,3 @@ func TestMain(m *testing.M) {
5251
runner.Close()
5352
os.Exit(code)
5453
}
55-
56-
func testBinaryPath() (string, error) {
57-
pth := os.Args[0]
58-
if !filepath.IsAbs(pth) {
59-
cwd, err := os.Getwd()
60-
if err == nil {
61-
return "", fmt.Errorf("os.Getwd: %v", err)
62-
}
63-
pth = filepath.Join(cwd, pth)
64-
}
65-
if _, err := os.Stat(pth); err != nil {
66-
return "", fmt.Errorf("os.Stat: %v", err)
67-
}
68-
return pth, nil
69-
}

0 commit comments

Comments
 (0)