// 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 ( "bytes" "context" "fmt" "io" "io/ioutil" "os" "os/exec" "path/filepath" "runtime/pprof" "strings" "sync" "testing" "time" "golang.org/x/tools/internal/jsonrpc2" "golang.org/x/tools/internal/jsonrpc2/servertest" "golang.org/x/tools/internal/lsp/cache" "golang.org/x/tools/internal/lsp/debug" "golang.org/x/tools/internal/lsp/fake" "golang.org/x/tools/internal/lsp/lsprpc" "golang.org/x/tools/internal/lsp/protocol" ) // Mode is a bitmask that defines for which execution modes a test should run. type Mode int const ( // Singleton mode uses a separate in-process gopls instance for each test, // and communicates over pipes to mimic the gopls sidecar execution mode, // which communicates over stdin/stderr. Singleton Mode = 1 << iota // Forwarded forwards connections to a shared in-process gopls instance. Forwarded // SeparateProcess forwards connection to a shared separate gopls process. SeparateProcess // NormalModes are the global default execution modes, when unmodified by // test flags or by individual test options. NormalModes = Singleton | Forwarded ) // 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 { DefaultModes Mode Timeout time.Duration GoplsPath string AlwaysPrintLogs bool PrintGoroutinesOnFailure bool mu sync.Mutex ts *servertest.TCPServer socketDir string // closers is a queue of clean-up functions to run at the end of the entire // test suite. closers []io.Closer } type runConfig struct { modes Mode proxyTxt string timeout time.Duration env []string skipCleanup bool gopath bool } func (r *Runner) defaultConfig() *runConfig { return &runConfig{ modes: r.DefaultModes, timeout: r.Timeout, } } // 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 Mode) RunOption { return optionSetter(func(opts *runConfig) { opts.modes = modes }) } // WithEnv overlays environment variables encoded by "= 0 { return fmt.Errorf("errors closing the test runner:\n\t%s", strings.Join(errmsgs, "\n\t")) } return nil }