Skip to content

Commit fc95c03

Browse files
committed
internal/modindex: fix index directory for tests
The module cache index is written to a subdirectory of os.UserCacheDir(). In non-test circumstances this is the right choice, as it makes the index generally available. In tests this is the wrong choice, as it may leave test remnants behind. This change sets the directory to a subdir os.TempDir for tests. Tests are free to further change modindex.IndexDir. Change-Id: I046e0520adf3e3ffa71cfc8a0dfbdd060e41eb1a Reviewed-on: https://go-review.googlesource.com/c/tools/+/639176 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent b93274b commit fc95c03

File tree

4 files changed

+32
-41
lines changed

4 files changed

+32
-41
lines changed

internal/imports/sourcex_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ type dirs struct {
9393
func testDirs(t *testing.T) dirs {
9494
t.Helper()
9595
dir := t.TempDir()
96-
modindex.IndexDir = func() (string, error) { return dir, nil }
96+
modindex.IndexDir = dir
9797
x := dirs{
9898
tmpdir: dir,
9999
cachedir: filepath.Join(dir, "pkg", "mod"),

internal/modindex/dir_test.go

+4-14
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@ var idtests = []id{
4747
}
4848

4949
func testModCache(t *testing.T) string {
50-
t.Helper()
51-
dir := t.TempDir()
52-
IndexDir = func() (string, error) { return dir, nil }
53-
return dir
50+
IndexDir = t.TempDir()
51+
return IndexDir
5452
}
5553

5654
// add a trivial package to the test module cache
@@ -211,11 +209,7 @@ func TestMissingCachedir(t *testing.T) {
211209
if err := Create(dir); err != nil {
212210
t.Fatal(err)
213211
}
214-
ixd, err := IndexDir()
215-
if err != nil {
216-
t.Fatal(err)
217-
}
218-
des, err := os.ReadDir(ixd)
212+
des, err := os.ReadDir(IndexDir)
219213
if err != nil {
220214
t.Fatal(err)
221215
}
@@ -232,11 +226,7 @@ func TestMissingIndex(t *testing.T) {
232226
} else if !ok {
233227
t.Error("Update returned !ok")
234228
}
235-
ixd, err := IndexDir()
236-
if err != nil {
237-
t.Fatal(err)
238-
}
239-
des, err := os.ReadDir(ixd)
229+
des, err := os.ReadDir(IndexDir)
240230
if err != nil {
241231
t.Fatal(err)
242232
}

internal/modindex/gomodindex/cmd.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@ func query(dir string) {
9393
panic("implement")
9494
}
9595
func clean(_ string) {
96-
des, err := modindex.IndexDir()
97-
if err != nil {
98-
log.Fatal(err)
99-
}
96+
des := modindex.IndexDir
10097
// look at the files starting with 'index'
10198
// the current ones of each version are pointed to by
10299
// index-name-%d files. Any others more than an hour old

internal/modindex/index.go

+26-22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"path/filepath"
1818
"strconv"
1919
"strings"
20+
"testing"
2021
"time"
2122
)
2223

@@ -85,6 +86,28 @@ type Entry struct {
8586
Names []string // exported names and information
8687
}
8788

89+
// IndexDir is where the module index is stored.
90+
var IndexDir string
91+
92+
// Set IndexDir
93+
func init() {
94+
var dir string
95+
var err error
96+
if testing.Testing() {
97+
dir = os.TempDir()
98+
} else {
99+
dir, err = os.UserCacheDir()
100+
// shouldn't happen, but TempDir is better than
101+
// creating ./go/imports
102+
if err != nil {
103+
dir = os.TempDir()
104+
}
105+
}
106+
dir = filepath.Join(dir, "go", "imports")
107+
os.MkdirAll(dir, 0777)
108+
IndexDir = dir
109+
}
110+
88111
// ReadIndex reads the latest version of the on-disk index
89112
// for the cache directory cd.
90113
// It returns (nil, nil) if there is no index, but returns
@@ -95,10 +118,7 @@ func ReadIndex(cachedir string) (*Index, error) {
95118
return nil, err
96119
}
97120
cd := Abspath(cachedir)
98-
dir, err := IndexDir()
99-
if err != nil {
100-
return nil, err
101-
}
121+
dir := IndexDir
102122
base := indexNameBase(cd)
103123
iname := filepath.Join(dir, base)
104124
buf, err := os.ReadFile(iname)
@@ -185,12 +205,8 @@ func readIndexFrom(cd Abspath, bx io.Reader) (*Index, error) {
185205

186206
// write the index as a text file
187207
func writeIndex(cachedir Abspath, ix *Index) error {
188-
dir, err := IndexDir()
189-
if err != nil {
190-
return err
191-
}
192208
ipat := fmt.Sprintf("index-%d-*", CurrentVersion)
193-
fd, err := os.CreateTemp(dir, ipat)
209+
fd, err := os.CreateTemp(IndexDir, ipat)
194210
if err != nil {
195211
return err // can this happen?
196212
}
@@ -201,7 +217,7 @@ func writeIndex(cachedir Abspath, ix *Index) error {
201217
content := fd.Name()
202218
content = filepath.Base(content)
203219
base := indexNameBase(cachedir)
204-
nm := filepath.Join(dir, base)
220+
nm := filepath.Join(IndexDir, base)
205221
err = os.WriteFile(nm, []byte(content), 0666)
206222
if err != nil {
207223
return err
@@ -241,18 +257,6 @@ func writeIndexToFile(x *Index, fd *os.File) error {
241257
return nil
242258
}
243259

244-
// tests can override this
245-
var IndexDir = indexDir
246-
247-
// indexDir computes the directory containing the index
248-
func indexDir() (string, error) {
249-
dir, err := os.UserCacheDir()
250-
if err != nil {
251-
return "", fmt.Errorf("cannot open UserCacheDir, %w", err)
252-
}
253-
return filepath.Join(dir, "go", "imports"), nil
254-
}
255-
256260
// return the base name of the file containing the name of the current index
257261
func indexNameBase(cachedir Abspath) string {
258262
// crc64 is a way to convert path names into 16 hex digits.

0 commit comments

Comments
 (0)