Skip to content

Commit

Permalink
gopls/internal/test/integration: add a WriteGoSum run option
Browse files Browse the repository at this point in the history
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
findleyr committed Apr 3, 2024
1 parent 53d35a5 commit 42d590c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
4 changes: 1 addition & 3 deletions gopls/internal/test/integration/misc/link_test.go
Expand Up @@ -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
Expand All @@ -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")
Expand Down
12 changes: 12 additions & 0 deletions gopls/internal/test/integration/options.go
Expand Up @@ -14,6 +14,7 @@ type runConfig struct {
sandbox fake.SandboxConfig
modes Mode
noLogsOnError bool
writeGoSum []string
}

func defaultConfig() runConfig {
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions gopls/internal/test/integration/runner.go
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions gopls/internal/test/integration/wrappers.go
Expand Up @@ -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")
}
Expand Down

0 comments on commit 42d590c

Please sign in to comment.