1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:14:43 -07:00

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 (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

golist_fallback.go also had a bit of code to add "/go-build/" to the
added testmain.go path, to trick the tests on Go 1.10 to think the file
was in GOCACHE. Update that code too, to now not add ".go" to the path
instead.

While at it, gofmt.

Fixes golang/go#29445.

Change-Id: I21fc59f13f00bea1f9a8a80e0438825f1a36ac3e
Reviewed-on: https://go-review.googlesource.com/c/156977
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Daniel Martí 2019-01-08 23:48:41 +01:00 committed by Brad Fitzpatrick
parent f94e9803bf
commit c3e1567727
2 changed files with 14 additions and 13 deletions

View File

@ -89,11 +89,7 @@ func golistDriverFallback(cfg *Config, words ...string) (*driverResponse, error)
return "", err
}
}
// Add a "go-build" component to the path to make the tests think the files are in the cache.
// This allows the same test to test the pre- and post-Go 1.11 go list logic because the Go 1.11
// go list generates test mains in the cache, and the test code knows not to rely on paths in the
// cache to stay stable.
outdir = filepath.Join(tmpdir, "go-build", strings.Replace(p.ImportPath, "/", "_", -1))
outdir = filepath.Join(tmpdir, strings.Replace(p.ImportPath, "/", "_", -1))
if err := os.MkdirAll(outdir, 0755); err != nil {
outdir = ""
return "", err
@ -200,7 +196,11 @@ func golistDriverFallback(cfg *Config, words ...string) (*driverResponse, error)
})
return
}
testmain := filepath.Join(outdir, "testmain.go")
// Don't use a .go extension on the file, so that the tests think the file is inside GOCACHE.
// This allows the same test to test the pre- and post-Go 1.11 go list logic because the Go 1.11
// go list generates test mains in the cache, and the test code knows not to rely on paths in the
// cache to stay stable.
testmain := filepath.Join(outdir, "testmain-go")
extraimports, extradeps, err := generateTestmain(testmain, testPkg, xtestPkg)
if err != nil {
testmainPkg.Errors = append(testmainPkg.Errors, Error{

View File

@ -1413,19 +1413,19 @@ func testJSON(t *testing.T, exporter packagestest.Exporter) {
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]
@ -1646,12 +1646,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