2020-04-28 22:00:52 -06:00
|
|
|
// 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 fake
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/gocommand"
|
2020-05-14 21:15:12 -06:00
|
|
|
"golang.org/x/tools/internal/testenv"
|
2020-04-28 22:00:52 -06:00
|
|
|
"golang.org/x/tools/txtar"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Sandbox holds a collection of temporary resources to use for working with Go
|
|
|
|
// code in tests.
|
|
|
|
type Sandbox struct {
|
|
|
|
gopath string
|
|
|
|
basedir string
|
|
|
|
Proxy *Proxy
|
|
|
|
Workdir *Workdir
|
2020-06-25 23:34:55 -06:00
|
|
|
|
|
|
|
// withoutWorkspaceFolders is used to simulate opening a single file in the
|
|
|
|
// editor, without a workspace root. In that case, the client sends neither
|
|
|
|
// workspace folders nor a root URI.
|
|
|
|
withoutWorkspaceFolders bool
|
2020-07-20 23:34:22 -06:00
|
|
|
|
|
|
|
// editorRootPath specifies the root path of the workspace folder used when
|
|
|
|
// initializing gopls in the sandbox. If empty, the Workdir is used.
|
|
|
|
editorRootPath string
|
2020-04-28 22:00:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSandbox creates a collection of named temporary resources, with a
|
|
|
|
// working directory populated by the txtar-encoded content in srctxt, and a
|
|
|
|
// file-based module proxy populated with the txtar-encoded content in
|
|
|
|
// proxytxt.
|
2020-06-25 14:04:28 -06:00
|
|
|
//
|
|
|
|
// If rootDir is non-empty, it will be used as the root of temporary
|
|
|
|
// directories created for the sandbox. Otherwise, a new temporary directory
|
|
|
|
// will be used as root.
|
2020-07-20 23:34:22 -06:00
|
|
|
func NewSandbox(rootDir, srctxt, proxytxt string, inGopath bool, withoutWorkspaceFolders bool, editorRootPath string) (_ *Sandbox, err error) {
|
2020-06-25 14:04:28 -06:00
|
|
|
sb := &Sandbox{}
|
2020-04-28 22:00:52 -06:00
|
|
|
defer func() {
|
|
|
|
// Clean up if we fail at any point in this constructor.
|
|
|
|
if err != nil {
|
|
|
|
sb.Close()
|
|
|
|
}
|
|
|
|
}()
|
2020-06-25 14:04:28 -06:00
|
|
|
|
|
|
|
baseDir, err := ioutil.TempDir(rootDir, "gopls-sandbox-")
|
2020-04-28 22:00:52 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("creating temporary workdir: %v", err)
|
|
|
|
}
|
2020-06-25 14:04:28 -06:00
|
|
|
sb.basedir = baseDir
|
2020-04-28 22:00:52 -06:00
|
|
|
proxydir := filepath.Join(sb.basedir, "proxy")
|
2020-04-23 21:24:24 -06:00
|
|
|
sb.gopath = filepath.Join(sb.basedir, "gopath")
|
|
|
|
// Set the working directory as $GOPATH/src if inGopath is true.
|
|
|
|
workdir := filepath.Join(sb.gopath, "src")
|
|
|
|
dirs := []string{sb.gopath, proxydir}
|
|
|
|
if !inGopath {
|
|
|
|
workdir = filepath.Join(sb.basedir, "work")
|
|
|
|
dirs = append(dirs, workdir)
|
|
|
|
}
|
|
|
|
for _, subdir := range dirs {
|
2020-04-28 22:00:52 -06:00
|
|
|
if err := os.Mkdir(subdir, 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sb.Proxy, err = NewProxy(proxydir, proxytxt)
|
|
|
|
sb.Workdir, err = NewWorkdir(workdir, srctxt)
|
2020-06-25 23:34:55 -06:00
|
|
|
sb.withoutWorkspaceFolders = withoutWorkspaceFolders
|
2020-07-20 23:34:22 -06:00
|
|
|
sb.editorRootPath = editorRootPath
|
2020-04-23 21:24:24 -06:00
|
|
|
|
2020-04-28 22:00:52 -06:00
|
|
|
return sb, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unpackTxt(txt string) map[string][]byte {
|
|
|
|
dataMap := make(map[string][]byte)
|
|
|
|
archive := txtar.Parse([]byte(txt))
|
|
|
|
for _, f := range archive.Files {
|
|
|
|
dataMap[f.Name] = f.Data
|
|
|
|
}
|
|
|
|
return dataMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// splitModuleVersionPath extracts module information from files stored in the
|
|
|
|
// directory structure modulePath@version/suffix.
|
|
|
|
// For example:
|
|
|
|
// splitModuleVersionPath("mod.com@v1.2.3/package") = ("mod.com", "v1.2.3", "package")
|
|
|
|
func splitModuleVersionPath(path string) (modulePath, version, suffix string) {
|
|
|
|
parts := strings.Split(path, "/")
|
|
|
|
var modulePathParts []string
|
|
|
|
for i, p := range parts {
|
|
|
|
if strings.Contains(p, "@") {
|
|
|
|
mv := strings.SplitN(p, "@", 2)
|
|
|
|
modulePathParts = append(modulePathParts, mv[0])
|
|
|
|
return strings.Join(modulePathParts, "/"), mv[1], strings.Join(parts[i+1:], "/")
|
|
|
|
}
|
|
|
|
modulePathParts = append(modulePathParts, p)
|
|
|
|
}
|
|
|
|
// Default behavior: this is just a module path.
|
|
|
|
return path, "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// GOPATH returns the value of the Sandbox GOPATH.
|
|
|
|
func (sb *Sandbox) GOPATH() string {
|
|
|
|
return sb.gopath
|
|
|
|
}
|
|
|
|
|
|
|
|
// GoEnv returns the default environment variables that can be used for
|
|
|
|
// invoking Go commands in the sandbox.
|
2020-07-22 12:03:48 -06:00
|
|
|
func (sb *Sandbox) GoEnv() map[string]string {
|
|
|
|
vars := map[string]string{
|
|
|
|
"GOPATH": sb.GOPATH(),
|
|
|
|
"GOPROXY": sb.Proxy.GOPROXY(),
|
|
|
|
"GO111MODULE": "",
|
|
|
|
"GOSUMDB": "off",
|
|
|
|
"GOPACKAGESDRIVER": "off",
|
2020-05-06 20:54:50 -06:00
|
|
|
}
|
2020-05-14 21:15:12 -06:00
|
|
|
if testenv.Go1Point() >= 5 {
|
2020-07-22 12:03:48 -06:00
|
|
|
vars["GOMODCACHE"] = ""
|
2020-05-14 21:15:12 -06:00
|
|
|
}
|
|
|
|
return vars
|
2020-04-28 22:00:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunGoCommand executes a go command in the sandbox.
|
|
|
|
func (sb *Sandbox) RunGoCommand(ctx context.Context, verb string, args ...string) error {
|
2020-07-22 12:03:48 -06:00
|
|
|
var vars []string
|
|
|
|
for k, v := range sb.GoEnv() {
|
|
|
|
vars = append(vars, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
2020-04-28 22:00:52 -06:00
|
|
|
inv := gocommand.Invocation{
|
|
|
|
Verb: verb,
|
|
|
|
Args: args,
|
|
|
|
WorkingDir: sb.Workdir.workdir,
|
2020-07-22 12:03:48 -06:00
|
|
|
Env: vars,
|
2020-04-28 22:00:52 -06:00
|
|
|
}
|
|
|
|
gocmdRunner := &gocommand.Runner{}
|
2020-05-06 12:22:21 -06:00
|
|
|
_, _, _, err := gocmdRunner.RunRaw(ctx, inv)
|
2020-04-28 22:00:52 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-06 12:22:21 -06:00
|
|
|
// Since running a go command may result in changes to workspace files,
|
|
|
|
// check if we need to send any any "watched" file events.
|
|
|
|
if err := sb.Workdir.CheckForFileChanges(ctx); err != nil {
|
|
|
|
return fmt.Errorf("checking for file changes: %w", err)
|
2020-04-28 22:00:52 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close removes all state associated with the sandbox.
|
|
|
|
func (sb *Sandbox) Close() error {
|
|
|
|
var goCleanErr error
|
|
|
|
if sb.gopath != "" {
|
|
|
|
if err := sb.RunGoCommand(context.Background(), "clean", "-modcache"); err != nil {
|
|
|
|
goCleanErr = fmt.Errorf("cleaning modcache: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := os.RemoveAll(sb.basedir)
|
|
|
|
if err != nil || goCleanErr != nil {
|
|
|
|
return fmt.Errorf("error(s) cleaning sandbox: cleaning modcache: %v; removing files: %v", goCleanErr, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|