From 42d590c9cf8d4c10cb82c05081a3b6a8f55b9bf0 Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Tue, 2 Apr 2024 17:37:35 -0400 Subject: [PATCH] gopls/internal/test/integration: add a WriteGoSum run option Working with go.sum files in integration tests was cumbersome, involving temporarily modifying the test to call env.DumpGoSum(...), then copying its output into the static go.sum file. For some tests, we may actually want to check in a fixed go.sum file, but the majority of the tests just want to avoid go.sum errors. To support this use case, add a new RunOption that writes the go.sum file before starting the test. Use this option in a test to illustrate its usage. Also, update DumpGoSum to use the faster "./..." pattern to create the go.sum output. "..." matches all modules, but we only care about the main module. Change-Id: I32cca005e10411033422cf8fee64cbd56e83f64c Reviewed-on: https://go-review.googlesource.com/c/tools/+/575700 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/test/integration/misc/link_test.go | 4 +--- gopls/internal/test/integration/options.go | 12 ++++++++++++ gopls/internal/test/integration/runner.go | 7 +++++++ gopls/internal/test/integration/wrappers.go | 4 ++-- 4 files changed, 22 insertions(+), 5 deletions(-) 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") }