1
0
mirror of https://github.com/golang/go synced 2024-09-30 16:18:35 -06:00

internal/lsp: add a reg test to test "go mod init"

This change adds a RunGoCommand function to the workspace, which will
allow us to test how gopls responds when a certain go command is
executed. Add a test that checks behavior after "go mod init" runs.

Change-Id: I679249c1654b136d44187397b4196b8a10b5615e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226478
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-03-30 16:26:04 -04:00
parent a7c0594f4e
commit e18c1c42de
3 changed files with 62 additions and 23 deletions

View File

@ -82,7 +82,7 @@ func (s *Server) diagnose(ctx context.Context, snapshot source.Snapshot, alwaysA
// Diagnose all of the packages in the workspace.
wsPackages, err := snapshot.WorkspacePackages(ctx)
if err != nil {
event.Error(ctx, "failed to load workspace packages, skipping diagnostics", err, tag.Snapshot.Of(snapshot.ID()), tag.Directory.Of(snapshot.View().Folder))
event.Error(ctx, "failed to load workspace packages, skipping diagnostics", err, tag.Snapshot.Of(snapshot.ID()), tag.Directory.Of(snapshot.View().Folder()))
return nil
}
for _, ph := range wsPackages {

View File

@ -13,6 +13,7 @@ import (
"strings"
"sync"
"golang.org/x/tools/internal/gocommand"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/txtar"
@ -149,6 +150,32 @@ func (w *Workspace) RemoveFile(ctx context.Context, path string) error {
return nil
}
// RunGoCommand executes a go command in the workspace.
func (w *Workspace) RunGoCommand(ctx context.Context, verb string, args ...string) error {
inv := gocommand.Invocation{
Verb: verb,
Args: args,
WorkingDir: w.workdir,
}
_, stderr, _, err := inv.RunRaw(ctx)
if err != nil {
return err
}
// Hardcoded "file watcher": If the command executed was "go mod init",
// send a file creation event for a go.mod in the working directory.
if strings.HasPrefix(stderr.String(), "go: creating new go.mod") {
modpath := filepath.Join(w.workdir, "go.mod")
w.sendEvents(ctx, []FileEvent{{
Path: modpath,
ProtocolEvent: protocol.FileEvent{
URI: toURI(modpath),
Type: protocol.Created,
},
}})
}
return nil
}
func (w *Workspace) sendEvents(ctx context.Context, evts []FileEvent) {
w.watcherMu.Lock()
watchers := make([]func(context.Context, []FileEvent), len(w.watchers))

View File

@ -152,34 +152,46 @@ import "mod.com/bob"
func main() {
bob.Hello()
}
-- bob/bob.go --
package bob
func Hello() {
var x int
}
`
// TestNoMod confirms that gopls continues to work when a user adds a go.mod
// file to their workspace.
func TestNoMod(t *testing.T) {
runner.Run(t, noMod, func(env *Env) {
env.Await(
env.DiagnosticAtRegexp("main.go", `"mod.com/bob"`),
)
env.CreateBuffer("bob/bob.go", `package bob
t.Run("manual", func(t *testing.T) {
runner.Run(t, noMod, func(env *Env) {
env.Await(
env.DiagnosticAtRegexp("main.go", `"mod.com/bob"`),
)
env.CreateBuffer("go.mod", `module mod.com
func Hello() {
var x int
}
go 1.12
`)
env.Await(
env.DiagnosticAtRegexp("bob/bob.go", "x"),
)
// Save this file because otherwise, the go command will not be able to
// resolve mod.com/bob as a package.
env.SaveBuffer("bob/bob.go")
env.CreateBuffer("go.mod", `module mod.com
go 1.12
`)
env.SaveBuffer("go.mod")
env.Await(
EmptyDiagnostics("main.go"),
)
env.SaveBuffer("go.mod")
env.Await(
EmptyDiagnostics("main.go"),
env.DiagnosticAtRegexp("bob/bob.go", "x"),
)
})
})
t.Run("initialized", func(t *testing.T) {
runner.Run(t, noMod, func(env *Env) {
env.Await(
env.DiagnosticAtRegexp("main.go", `"mod.com/bob"`),
)
if err := env.W.RunGoCommand(env.Ctx, "mod", "init", "mod.com"); err != nil {
t.Fatal(err)
}
env.Await(
EmptyDiagnostics("main.go"),
env.DiagnosticAtRegexp("bob/bob.go", "x"),
)
})
})
}