go/packages: make tests pass with custom GOCACHE
Before this change, a test would fail:
$ GOCACHE=$HOME/go/cache go test
--- FAIL: TestLoadImportsGraph (0.36s)
--- FAIL: TestLoadImportsGraph/GOPATH (0.19s)
packages_test.go:191: golang.org/fake/subdir/d.test.Srcs = [4302876da86a8aae0c1669924daa223cafca60ef49ccaa060ae37e778d18f218-d], want [0.go]
--- FAIL: TestLoadImportsGraph/Modules (0.17s)
packages_test.go:191: golang.org/fake/subdir/d.test.Srcs = [4302876da86a8aae0c1669924daa223cafca60ef49ccaa060ae37e778d18f218-d], want [0.go]
FAIL
This is because it assumed that the user hadn't set their own GOCACHE,
and thus that all source files in the cache would be under the default
"go-build" cache directory.
We could fix this via os.Getenv("GOCACHE"), but a simpler mechanism is
to see if the source file has an extension. Source files don't have an
extension in GOCACHE, so that's much simpler to detect.
After this change:
$ GOCACHE=$HOME/go/cache go test
PASS
While at it, gofmt.
Fixes #29445.
Change-Id: I83c0afc20a527bb50a03f9946e555db36cc85efd
Reviewed-on: https://go-review.googlesource.com/c/155897
Reviewed-by: Michael Matloob <matloob@golang.org>
diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go
index b5d451f..229abcd 100644
--- a/go/packages/packages_test.go
+++ b/go/packages/packages_test.go
@@ -1389,19 +1389,19 @@
ID: "golang.org/fake/b",
Name: "b",
Imports: map[string]*packages.Package{
- "golang.org/fake/a": &packages.Package{ID: "golang.org/fake/a"},
+ "golang.org/fake/a": {ID: "golang.org/fake/a"},
},
}, {
ID: "golang.org/fake/c",
Name: "c",
Imports: map[string]*packages.Package{
- "golang.org/fake/b": &packages.Package{ID: "golang.org/fake/b"},
+ "golang.org/fake/b": {ID: "golang.org/fake/b"},
},
}, {
ID: "golang.org/fake/d",
Name: "d",
Imports: map[string]*packages.Package{
- "golang.org/fake/b": &packages.Package{ID: "golang.org/fake/b"},
+ "golang.org/fake/b": {ID: "golang.org/fake/b"},
},
}} {
got := decoded[i]
@@ -1622,12 +1622,13 @@
func cleanPaths(paths []string) []string {
result := make([]string, len(paths))
for i, src := range paths {
- // The default location for cache data is a subdirectory named go-build
- // in the standard user cache directory for the current operating system.
- if strings.Contains(filepath.ToSlash(src), "/go-build/") {
+ // If the source file doesn't have an extension like .go or .s,
+ // it comes from GOCACHE. The names there aren't predictable.
+ name := filepath.Base(src)
+ if !strings.Contains(name, ".") {
result[i] = fmt.Sprintf("%d.go", i) // make cache names predictable
} else {
- result[i] = filepath.Base(src)
+ result[i] = name
}
}
return result