From e9f45831faab4562ce3f53f7196b67c08f948e86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Tue, 8 Jan 2019 23:48:41 +0100 Subject: [PATCH] [release-branch.go1.11] go/packages: make tests pass with custom GOCACHE This commit was merged earlier with some failing trybots, so it was reverted. This is a re-submission. Before this change, a test would fail: $ GOCACHE=$HOME/go/cache go test --- FAIL: TestLoadImportsGraph (1.05s) packages_test.go:225: subdir/d.test.Srcs = [cf570d60b25cde4f49bbe5f69d3ed407f2d7f1fbc500b8807da726fb19b8f588-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 On release-branch.go1.11, golist_fallback.go did not yet have the code added that would need to be fixed, so nothing is being backported to it in this change. While at it, gofmt. Updates golang/go#29445 Fixes golang/go#29944 Change-Id: I21fc59f13f00bea1f9a8a80e0438825f1a36ac3e Reviewed-on: https://go-review.googlesource.com/c/156977 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot Reviewed-by: Michael Matloob Reviewed-on: https://go-review.googlesource.com/c/163780 Run-TryBot: Dmitri Shuralyov Reviewed-by: Brad Fitzpatrick --- go/packages/packages_test.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index 3ee4e870..b3a78aa6 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -1207,19 +1207,19 @@ func TestJSON(t *testing.T) { ID: "b", Name: "b", Imports: map[string]*packages.Package{ - "a": &packages.Package{ID: "a"}, + "a": {ID: "a"}, }, }, { ID: "c", Name: "c", Imports: map[string]*packages.Package{ - "b": &packages.Package{ID: "b"}, + "b": {ID: "b"}, }, }, { ID: "d", Name: "d", Imports: map[string]*packages.Package{ - "b": &packages.Package{ID: "b"}, + "b": {ID: "b"}, }, }} { got := decoded[i] @@ -1267,12 +1267,13 @@ func srcs(p *packages.Package) []string { 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