Skip to content

Commit 42d590c

Browse files
committed
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 <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 53d35a5 commit 42d590c

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

gopls/internal/test/integration/misc/link_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ module mod.test
1919
go 1.12
2020
2121
require import.test v1.2.3
22-
-- go.sum --
23-
import.test v1.2.3 h1:Mu4N9BICLJFxwwn8YNg6T3frkFWW1O7evXvo0HiRjBc=
24-
import.test v1.2.3/go.mod h1:KooCN1g237upRg7irU7F+3oADn5tVClU8YYW4I1xhMk=
2522
-- main.go --
2623
package main
2724
@@ -45,6 +42,7 @@ const Hello = "Hello"
4542
`
4643
WithOptions(
4744
ProxyFiles(proxy),
45+
WriteGoSum("."),
4846
).Run(t, program, func(t *testing.T, env *Env) {
4947
env.OpenFile("main.go")
5048
env.OpenFile("go.mod")

gopls/internal/test/integration/options.go

+12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type runConfig struct {
1414
sandbox fake.SandboxConfig
1515
modes Mode
1616
noLogsOnError bool
17+
writeGoSum []string
1718
}
1819

1920
func defaultConfig() runConfig {
@@ -46,6 +47,17 @@ func ProxyFiles(txt string) RunOption {
4647
})
4748
}
4849

50+
// WriteGoSum causes the environment to write a go.sum file for the requested
51+
// relative directories (via `go list -mod=mod`), before starting gopls.
52+
//
53+
// Useful for tests that use ProxyFiles, but don't care about crafting the
54+
// go.sum content.
55+
func WriteGoSum(dirs ...string) RunOption {
56+
return optionSetter(func(opts *runConfig) {
57+
opts.writeGoSum = dirs
58+
})
59+
}
60+
4961
// Modes configures the execution modes that the test should run in.
5062
//
5163
// By default, modes are configured by the test runner. If this option is set,

gopls/internal/test/integration/runner.go

+7
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ func (r *Runner) Run(t *testing.T, files string, test TestFunc, opts ...RunOptio
210210
}
211211
}()
212212

213+
// Write the go.sum file for the requested directories, before starting the server.
214+
for _, dir := range config.writeGoSum {
215+
if err := sandbox.RunGoCommand(context.Background(), dir, "list", []string{"-mod=mod", "./..."}, []string{"GOWORK=off"}, true); err != nil {
216+
t.Fatal(err)
217+
}
218+
}
219+
213220
ss := tc.getServer(r.OptionsHook)
214221

215222
framer := jsonrpc2.NewRawStream

gopls/internal/test/integration/wrappers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,10 @@ func (e *Env) GoVersion() int {
318318
func (e *Env) DumpGoSum(dir string) {
319319
e.T.Helper()
320320

321-
if err := e.Sandbox.RunGoCommand(e.Ctx, dir, "list", []string{"-mod=mod", "..."}, nil, true); err != nil {
321+
if err := e.Sandbox.RunGoCommand(e.Ctx, dir, "list", []string{"-mod=mod", "./..."}, nil, true); err != nil {
322322
e.T.Fatal(err)
323323
}
324-
sumFile := path.Join(dir, "/go.sum")
324+
sumFile := path.Join(dir, "go.sum")
325325
e.T.Log("\n\n-- " + sumFile + " --\n" + e.ReadWorkspaceFile(sumFile))
326326
e.T.Fatal("see contents above")
327327
}

0 commit comments

Comments
 (0)