mirror of
https://github.com/golang/go
synced 2024-11-19 01:04:40 -07:00
5fb17a1e7b
For tests (and perhaps later, for daemon discovery), unix domain sockets offer advantages over TCP: we can know the exact socket address that will be used when starting a server subprocess. They also offer performance and security advantages over TCP, and were specifically requested on golang.org/issues/34111. This CL adds support for listening on UDS, and uses this to implement an additional regtest environment mode that starts up an external process. This mode is disabled by default, but may be enabled by the -enable_gopls_subprocess_tests. The regtest TestMain may be hijacked to instead run as gopls, if a special environment variable is set. This allows the the test runner to start a separate process by using os.Argv[0]. The -gopls_test_binary flag may be used to point tests at a separate gopls binary. Updates golang/go#36879 Updates golang/go#34111 Change-Id: I1cfdf55040e81ffa69a6726878a96529e5522e82 Reviewed-on: https://go-review.googlesource.com/c/tools/+/218839 Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
79 lines
2.1 KiB
Go
79 lines
2.1 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"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/tools/internal/lsp"
|
|
"golang.org/x/tools/internal/lsp/cmd"
|
|
"golang.org/x/tools/internal/lsp/lsprpc"
|
|
"golang.org/x/tools/internal/tool"
|
|
)
|
|
|
|
var (
|
|
runSubprocessTests = flag.Bool("enable_gopls_subprocess_tests", false, "run regtests against a gopls subprocess")
|
|
goplsBinaryPath = flag.String("gopls_test_binary", "", "path to the gopls binary for use as a remote, for use with the -gopls_subprocess_testmode flag")
|
|
)
|
|
|
|
var runner *Runner
|
|
|
|
func TestMain(m *testing.M) {
|
|
flag.Parse()
|
|
if os.Getenv("_GOPLS_TEST_BINARY_RUN_AS_GOPLS") == "true" {
|
|
tool.Main(context.Background(), cmd.New("gopls", "", nil, nil), os.Args[1:])
|
|
os.Exit(0)
|
|
}
|
|
// Override functions that would shut down the test process
|
|
defer func(lspExit, forwarderExit func(code int)) {
|
|
lsp.ServerExitFunc = lspExit
|
|
lsprpc.ForwarderExitFunc = forwarderExit
|
|
}(lsp.ServerExitFunc, lsprpc.ForwarderExitFunc)
|
|
// None of these regtests should be able to shut down a server process.
|
|
lsp.ServerExitFunc = func(code int) {
|
|
panic(fmt.Sprintf("LSP server exited with code %d", code))
|
|
}
|
|
// We don't want our forwarders to exit, but it's OK if they would have.
|
|
lsprpc.ForwarderExitFunc = func(code int) {}
|
|
|
|
if *runSubprocessTests {
|
|
goplsPath := *goplsBinaryPath
|
|
if goplsPath == "" {
|
|
var err error
|
|
goplsPath, err = testBinaryPath()
|
|
if err != nil {
|
|
panic(fmt.Sprintf("finding test binary path: %v", err))
|
|
}
|
|
}
|
|
runner = NewTestRunner(NormalModes|SeparateProcess, 30*time.Second, goplsPath)
|
|
} else {
|
|
runner = NewTestRunner(NormalModes, 30*time.Second, "")
|
|
}
|
|
code := m.Run()
|
|
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
|
|
}
|