diff --git a/gopls/internal/test/integration/misc/link_test.go b/gopls/internal/test/integration/misc/link_test.go index 533b1186c61..53b0f0818f3 100644 --- a/gopls/internal/test/integration/misc/link_test.go +++ b/gopls/internal/test/integration/misc/link_test.go @@ -19,9 +19,6 @@ module mod.test go 1.12 require import.test v1.2.3 --- go.sum -- -import.test v1.2.3 h1:Mu4N9BICLJFxwwn8YNg6T3frkFWW1O7evXvo0HiRjBc= -import.test v1.2.3/go.mod h1:KooCN1g237upRg7irU7F+3oADn5tVClU8YYW4I1xhMk= -- main.go -- package main @@ -45,6 +42,7 @@ const Hello = "Hello" ` WithOptions( ProxyFiles(proxy), + WriteGoSum("."), ).Run(t, program, func(t *testing.T, env *Env) { env.OpenFile("main.go") env.OpenFile("go.mod") diff --git a/gopls/internal/test/integration/options.go b/gopls/internal/test/integration/options.go index b36ebc9c3f8..baa13d06ecd 100644 --- a/gopls/internal/test/integration/options.go +++ b/gopls/internal/test/integration/options.go @@ -14,6 +14,7 @@ type runConfig struct { sandbox fake.SandboxConfig modes Mode noLogsOnError bool + writeGoSum []string } func defaultConfig() runConfig { @@ -46,6 +47,17 @@ func ProxyFiles(txt string) RunOption { }) } +// WriteGoSum causes the environment to write a go.sum file for the requested +// relative directories (via `go list -mod=mod`), before starting gopls. +// +// Useful for tests that use ProxyFiles, but don't care about crafting the +// go.sum content. +func WriteGoSum(dirs ...string) RunOption { + return optionSetter(func(opts *runConfig) { + opts.writeGoSum = dirs + }) +} + // Modes configures the execution modes that the test should run in. // // By default, modes are configured by the test runner. If this option is set, diff --git a/gopls/internal/test/integration/runner.go b/gopls/internal/test/integration/runner.go index ed333585ddf..adb307f1901 100644 --- a/gopls/internal/test/integration/runner.go +++ b/gopls/internal/test/integration/runner.go @@ -210,6 +210,13 @@ func (r *Runner) Run(t *testing.T, files string, test TestFunc, opts ...RunOptio } }() + // Write the go.sum file for the requested directories, before starting the server. + for _, dir := range config.writeGoSum { + if err := sandbox.RunGoCommand(context.Background(), dir, "list", []string{"-mod=mod", "./..."}, []string{"GOWORK=off"}, true); err != nil { + t.Fatal(err) + } + } + ss := tc.getServer(r.OptionsHook) framer := jsonrpc2.NewRawStream diff --git a/gopls/internal/test/integration/wrappers.go b/gopls/internal/test/integration/wrappers.go index cc4a66d79fd..ce51208d0a3 100644 --- a/gopls/internal/test/integration/wrappers.go +++ b/gopls/internal/test/integration/wrappers.go @@ -318,10 +318,10 @@ func (e *Env) GoVersion() int { func (e *Env) DumpGoSum(dir string) { e.T.Helper() - if err := e.Sandbox.RunGoCommand(e.Ctx, dir, "list", []string{"-mod=mod", "..."}, nil, true); err != nil { + if err := e.Sandbox.RunGoCommand(e.Ctx, dir, "list", []string{"-mod=mod", "./..."}, nil, true); err != nil { e.T.Fatal(err) } - sumFile := path.Join(dir, "/go.sum") + sumFile := path.Join(dir, "go.sum") e.T.Log("\n\n-- " + sumFile + " --\n" + e.ReadWorkspaceFile(sumFile)) e.T.Fatal("see contents above") }