1
0
mirror of https://github.com/golang/go synced 2024-11-18 10:04:43 -07:00

internal/lsp: add regtest for golang/go#37984

This change also required the addition of a new run configuration -
WithEnv, which adds extra environment variables to the configuration.
Please let me know if this is the wrong approach.

Fixes golang/go#37984

Change-Id: Ied2a53a443dc74c7ed723b91765eeddc1a7c1c00
Reviewed-on: https://go-review.googlesource.com/c/tools/+/229128
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Rebecca Stambler 2020-04-20 20:32:11 -04:00
parent 4e4aced336
commit 3d57cf2e72
4 changed files with 37 additions and 5 deletions

View File

@ -388,6 +388,9 @@ func (v *view) buildProcessEnv(ctx context.Context) (*imports.ProcessEnv, error)
processEnv.GOSUMDB = split[1]
}
}
if processEnv.GOPATH == "" {
return nil, fmt.Errorf("no GOPATH for view %s", v.folder)
}
return processEnv, nil
}

View File

@ -34,6 +34,7 @@ type Workspace struct {
gopath string
workdir string
proxydir string
env []string
watcherMu sync.Mutex
watchers []func(context.Context, []FileEvent)
@ -42,8 +43,11 @@ type Workspace struct {
// NewWorkspace creates a named workspace populated by the txtar-encoded
// content given by txt. It creates temporary directories for the workspace
// content and for GOPATH.
func NewWorkspace(name string, srctxt string, proxytxt string) (_ *Workspace, err error) {
w := &Workspace{name: name}
func NewWorkspace(name, srctxt, proxytxt string, env ...string) (_ *Workspace, err error) {
w := &Workspace{
name: name,
env: env,
}
defer func() {
// Clean up if we fail at any point in this constructor.
if err != nil {
@ -219,12 +223,12 @@ func (w *Workspace) RemoveFile(ctx context.Context, path string) error {
// GoEnv returns the environment variables that should be used for invoking Go
// commands in the workspace.
func (w *Workspace) GoEnv() []string {
return []string{
return append([]string{
"GOPATH=" + w.GOPATH(),
"GOPROXY=" + w.GOPROXY(),
"GO111MODULE=",
"GOSUMDB=off",
}
}, w.env...)
}
// RunGoCommand executes a go command in the workspace.

View File

@ -344,3 +344,21 @@ func Hello() {
env.Await(env.DiagnosticAtRegexp("b/b.go", "x"))
})
}
func TestNoGOPATHIssue_37984(t *testing.T) {
const missingImport = `
-- main.go --
package main
func _() {
fmt.Println("Hello World")
}
`
runner.Run(t, missingImport, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
env.Await(env.DiagnosticAtRegexp("main.go", "fmt"))
if err := env.E.OrganizeImports(env.Ctx, "main.go"); err == nil {
t.Fatalf("organize imports should fail with an empty GOPATH")
}
}, WithEnv("GOPATH="))
}

View File

@ -161,6 +161,7 @@ type runConfig struct {
modes EnvMode
proxyTxt string
timeout time.Duration
env []string
}
func (r *Runner) defaultConfig() *runConfig {
@ -202,6 +203,12 @@ func WithModes(modes EnvMode) RunOption {
})
}
func WithEnv(env ...string) RunOption {
return optionSetter(func(opts *runConfig) {
opts.env = env
})
}
// Run executes the test function in the default configured gopls execution
// modes. For each a test run, a new workspace is created containing the
// un-txtared files specified by filedata.
@ -233,7 +240,7 @@ func (r *Runner) Run(t *testing.T, filedata string, test func(t *testing.T, e *E
defer cancel()
ctx = debug.WithInstance(ctx, "", "")
ws, err := fake.NewWorkspace("regtest", filedata, config.proxyTxt)
ws, err := fake.NewWorkspace("regtest", filedata, config.proxyTxt, config.env...)
if err != nil {
t.Fatal(err)
}