gopls/internal/test/integration/fake: integrate module cache This CL introduces CacheFiles() parallel to ProxyFiles() as an option to include a txtar archive as the module cache for tests. Several tests build a temporary module cache from a txtar and explicitly construct the module cache index. The new option moves all that work into the code that sets up the fake sandbox. Further, CleanModCache() is no longer necessary. Working on: golang.org/go#74595 Change-Id: Ie2a0abfb5f1c18175ed5bac6e8dedf8401410973 Reviewed-on: https://go-review.googlesource.com/c/tools/+/806040 Reviewed-by: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/gopls/internal/test/integration/completion/completion_test.go b/gopls/internal/test/integration/completion/completion_test.go index b110046..0fcd8a6 100644 --- a/gopls/internal/test/integration/completion/completion_test.go +++ b/gopls/internal/test/integration/completion/completion_test.go
@@ -1452,21 +1452,8 @@ package main var _ = blah. ` - modcache := t.TempDir() - defer CleanModCache(t, modcache) - mx := fake.UnpackTxt(cache) - for k, v := range mx { - fname := filepath.Join(modcache, k) - dir := filepath.Dir(fname) - os.MkdirAll(dir, 0777) // ignore error - if err := os.WriteFile(fname, v, 0644); err != nil { - t.Fatal(err) - } - } - modindex.Update(modcache) - WithOptions( - EnvVars{"GOMODCACHE": modcache}, + CacheFiles(cache), WriteGoSum("."), NoLogsOnError(), ).Run(t, files, func(t *testing.T, env *Env) {
diff --git a/gopls/internal/test/integration/fake/sandbox.go b/gopls/internal/test/integration/fake/sandbox.go index 12ce516..3446edb 100644 --- a/gopls/internal/test/integration/fake/sandbox.go +++ b/gopls/internal/test/integration/fake/sandbox.go
@@ -13,6 +13,7 @@ "strings" "golang.org/x/tools/internal/gocommand" + "golang.org/x/tools/internal/modindex" "golang.org/x/tools/internal/robustio" "golang.org/x/tools/txtar" ) @@ -54,6 +55,9 @@ // ProxyFiles holds a txtar-encoded archive of files to populate a file-based // Go proxy. ProxyFiles map[string][]byte + // CacheFiles holds a txtar-encoded archive of files to populate the sandbox's + // module cache. + CacheFiles map[string][]byte // GOPROXY is the explicit GOPROXY value that should be used for the sandbox. // // This option is incompatible with ProxyFiles. @@ -112,6 +116,25 @@ return nil, err } } + if len(config.CacheFiles) > 0 { + modcache := filepath.Join(sb.gopath, "pkg", "mod") + if err := os.MkdirAll(modcache, 0755); err != nil { + return nil, err + } + for name, content := range config.CacheFiles { + fname := filepath.Join(modcache, name) + dir := filepath.Dir(fname) + if err := os.MkdirAll(dir, 0755); err != nil { + return nil, err + } + if err := os.WriteFile(fname, content, 0644); err != nil { + return nil, err + } + } + if _, err := modindex.Update(modcache); err != nil { + return nil, fmt.Errorf("failed to create modindex: %w", err) + } + } // Short-circuit writing the workdir if we're given an absolute path, since // this is used for running in an existing directory. // TODO(findleyr): refactor this to be less of a workaround.
diff --git a/gopls/internal/test/integration/misc/imports_test.go b/gopls/internal/test/integration/misc/imports_test.go index 409fb18..52fde22 100644 --- a/gopls/internal/test/integration/misc/imports_test.go +++ b/gopls/internal/test/integration/misc/imports_test.go
@@ -300,27 +300,8 @@ return nil } ` - modcache := t.TempDir() - defer CleanModCache(t, modcache) - mx := fake.UnpackTxt(cache) - - for k, v := range mx { - fname := filepath.Join(modcache, k) - dir := filepath.Dir(fname) - os.MkdirAll(dir, 0777) // ignore error - if err := os.WriteFile(fname, v, 0644); err != nil { - t.Fatal(err) - } - if true { - // for diagnosing flakiness - t.Logf("wrote %s:%d", fname, len(v)) - } - } - // golang/go#77552 finds this test flaky, so create the index explicitly - // rather than hoping a background go routine finishes in time - modindex.Update(modcache) WithOptions( - EnvVars{"GOMODCACHE": modcache}, + CacheFiles(cache), WriteGoSum("."), NoLogsOnError(), ).Run(t, files, func(t *testing.T, env *Env) { @@ -358,26 +339,8 @@ return nil } ` - modcache := t.TempDir() - defer CleanModCache(t, modcache) - mx := fake.UnpackTxt(cache) - for k, v := range mx { - fname := filepath.Join(modcache, k) - dir := filepath.Dir(fname) - os.MkdirAll(dir, 0777) // ignore error - if err := os.WriteFile(fname, v, 0644); err != nil { - t.Fatal(err) - } - } - // create the index deterministically. When gopls is invoked - // interactively, the module index is created or updated in - // a goroutine, so we can't rely on it being present - // immediately. - if _, err := modindex.Update(modcache); err != nil { - t.Fatal(err) - } WithOptions( - EnvVars{"GOMODCACHE": modcache}, + CacheFiles(cache), WriteGoSum("."), NoLogsOnError(), ).Run(t, files, func(t *testing.T, env *Env) {
diff --git a/gopls/internal/test/integration/options.go b/gopls/internal/test/integration/options.go index 73c78cd..623cd01 100644 --- a/gopls/internal/test/integration/options.go +++ b/gopls/internal/test/integration/options.go
@@ -58,6 +58,13 @@ }) } +// CacheFiles configures a module cache using the given txtar-encoded string. +func CacheFiles(txt string) RunOption { + return optionSetter(func(opts *runConfig) { + opts.sandbox.CacheFiles = fake.UnpackTxt(txt) + }) +} + // WriteGoSum causes the environment to write a go.sum file for the requested // relative directories (via `go list -mod=mod`), before starting gopls. //