From fd8950f6eb8fd2343e867ecac168a9f4875261dc Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Thu, 30 Jul 2020 01:47:04 -0400 Subject: [PATCH] internal/lsp: add a test that reproduces golang/go#37069 Also, add a directory parameter to RunGoCommand. To make sure that the parameters aren't misused, change args to a []string. Updates golang/go#40340 Change-Id: Ib5ce606a401a18c29c904b570ec9339f067a3961 Reviewed-on: https://go-review.googlesource.com/c/tools/+/245818 Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot Reviewed-by: Robert Findley --- internal/lsp/fake/sandbox.go | 9 +++-- internal/lsp/regtest/diagnostics_test.go | 2 +- internal/lsp/regtest/modfile_test.go | 2 +- internal/lsp/regtest/watch_test.go | 45 ++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/internal/lsp/fake/sandbox.go b/internal/lsp/fake/sandbox.go index 0f6213cead..e443fc3a05 100644 --- a/internal/lsp/fake/sandbox.go +++ b/internal/lsp/fake/sandbox.go @@ -179,7 +179,7 @@ func (sb *Sandbox) GoEnv() map[string]string { } // RunGoCommand executes a go command in the sandbox. -func (sb *Sandbox) RunGoCommand(ctx context.Context, verb string, args ...string) error { +func (sb *Sandbox) RunGoCommand(ctx context.Context, dir, verb string, args []string) error { var vars []string for k, v := range sb.GoEnv() { vars = append(vars, fmt.Sprintf("%s=%s", k, v)) @@ -189,10 +189,13 @@ func (sb *Sandbox) RunGoCommand(ctx context.Context, verb string, args ...string Args: args, Env: vars, } + // Use the provided directory for the working directory, if available. // sb.Workdir may be nil if we exited the constructor with errors (we call // Close to clean up any partial state from the constructor, which calls // RunGoCommand). - if sb.Workdir != nil { + if dir != "" { + inv.WorkingDir = sb.Workdir.filePath(dir) + } else if sb.Workdir != nil { inv.WorkingDir = sb.Workdir.workdir } gocmdRunner := &gocommand.Runner{} @@ -214,7 +217,7 @@ func (sb *Sandbox) RunGoCommand(ctx context.Context, verb string, args ...string func (sb *Sandbox) Close() error { var goCleanErr error if sb.gopath != "" { - goCleanErr = sb.RunGoCommand(context.Background(), "clean", "-modcache") + goCleanErr = sb.RunGoCommand(context.Background(), "", "clean", []string{"-modcache"}) } err := os.RemoveAll(sb.basedir) if err != nil || goCleanErr != nil { diff --git a/internal/lsp/regtest/diagnostics_test.go b/internal/lsp/regtest/diagnostics_test.go index 23ed1721b6..779d820f9d 100644 --- a/internal/lsp/regtest/diagnostics_test.go +++ b/internal/lsp/regtest/diagnostics_test.go @@ -276,7 +276,7 @@ func Hello() { env.Await( env.DiagnosticAtRegexp("main.go", `"mod.com/bob"`), ) - if err := env.Sandbox.RunGoCommand(env.Ctx, "mod", "init", "mod.com"); err != nil { + if err := env.Sandbox.RunGoCommand(env.Ctx, "", "mod", []string{"init", "mod.com"}); err != nil { t.Fatal(err) } env.Await( diff --git a/internal/lsp/regtest/modfile_test.go b/internal/lsp/regtest/modfile_test.go index 50e98aed3f..3069629c5d 100644 --- a/internal/lsp/regtest/modfile_test.go +++ b/internal/lsp/regtest/modfile_test.go @@ -238,7 +238,7 @@ go 1.12 CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1), env.DiagnosticAtRegexp("go.mod", "require"), ) - env.Sandbox.RunGoCommand(env.Ctx, "mod", "tidy") + env.Sandbox.RunGoCommand(env.Ctx, "", "mod", []string{"tidy"}) env.Await( EmptyDiagnostics("go.mod"), ) diff --git a/internal/lsp/regtest/watch_test.go b/internal/lsp/regtest/watch_test.go index 75cac13d16..cbea6ef20d 100644 --- a/internal/lsp/regtest/watch_test.go +++ b/internal/lsp/regtest/watch_test.go @@ -573,3 +573,48 @@ func main() { ) }) } + +// Reproduces golang/go#37069. +func TestSwitchFromGOPATHToModules(t *testing.T) { + t.Skipf("golang/go#37069 is not yet resolved.") + + const files = ` +-- foo/blah/blah.go -- +package blah + +const Name = "" +-- foo/main.go -- +package main + +import "blah" + +func main() { + _ = blah.Name +} +` + withOptions(InGOPATH()).run(t, files, func(t *testing.T, env *Env) { + env.OpenFile("foo/main.go") + env.Await( + OnceMet( + CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1), + env.DiagnosticAtRegexp("foo/main.go", `"blah"`), + ), + ) + if err := env.Sandbox.RunGoCommand(env.Ctx, "foo", "mod", []string{"init", "mod.com"}); err != nil { + t.Fatal(err) + } + env.Await( + OnceMet( + CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChangeWatchedFiles), 1), + env.DiagnosticAtRegexp("foo/main.go", `"blah`), + ), + ) + env.RegexpReplace("foo/main.go", `"blah"`, `"mod.com/blah"`) + env.Await( + OnceMet( + CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChange), 1), + NoDiagnostics("foo/main.go"), + ), + ) + }) +}