1
0
mirror of https://github.com/golang/go synced 2024-10-01 09:28:37 -06:00

go/packages: run name query tests in a temporary directory

Some of the tests on the name query operate on test modules or module cache
trees that are checked in as testdata. When the tests run, they can modify
the go.mod files and the cache tree directories. Copy the directories
to a temporary directory to avoid getting spurious git diffs showing up.

Change-Id: I991a4510201988d596833faea88425a335d3228b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/167859
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Michael Matloob 2019-03-15 14:25:54 -04:00
parent 2f43c6d1a2
commit e6df0c1bb3

View File

@ -13,6 +13,7 @@ import (
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"os"
"path/filepath"
"reflect"
@ -1211,12 +1212,20 @@ func TestName_Modules(t *testing.T) {
if err != nil {
t.Fatal(err)
}
gopath, err := ioutil.TempDir("", "TestName_Modules")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(gopath)
if err := copyAll(filepath.Join(wd, "testdata", "TestName_Modules"), gopath); err != nil {
t.Fatal(err)
}
// testdata/TestNamed_Modules contains:
// - pkg/mod/github.com/heschik/tools-testrepo@v1.0.0/pkg
// - pkg/mod/github.com/heschik/tools-testrepo/v2@v2.0.0/pkg
// - src/b/pkg
exported.Config.Mode = packages.LoadImports
exported.Config.Env = append(exported.Config.Env, "GOPATH="+wd+"/testdata/TestName_Modules")
exported.Config.Env = append(exported.Config.Env, "GOPATH="+gopath)
initial, err := packages.Load(exported.Config, "iamashamedtousethedisabledqueryname=pkg")
if err != nil {
t.Fatal(err)
@ -1248,13 +1257,21 @@ func TestName_ModulesDedup(t *testing.T) {
if err != nil {
t.Fatal(err)
}
gopath, err := ioutil.TempDir("", "TestName_ModulesDedup")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(gopath)
if err := copyAll(filepath.Join(wd, "testdata", "TestName_ModulesDedup"), gopath); err != nil {
t.Fatal(err)
}
// testdata/TestNamed_ModulesDedup contains:
// - pkg/mod/github.com/heschik/tools-testrepo/v2@v2.0.2/pkg/pkg.go
// - pkg/mod/github.com/heschik/tools-testrepo/v2@v2.0.1/pkg/pkg.go
// - pkg/mod/github.com/heschik/tools-testrepo@v1.0.0/pkg/pkg.go
// but, inexplicably, not v2.0.0. Nobody knows why.
exported.Config.Mode = packages.LoadImports
exported.Config.Env = append(exported.Config.Env, "GOPATH="+wd+"/testdata/TestName_ModulesDedup")
exported.Config.Env = append(exported.Config.Env, "GOPATH="+gopath)
initial, err := packages.Load(exported.Config, "iamashamedtousethedisabledqueryname=pkg")
if err != nil {
t.Fatal(err)
@ -1757,3 +1774,27 @@ func constant(p *packages.Package, name string) *types.Const {
}
return c.(*types.Const)
}
func copyAll(srcPath, dstPath string) error {
return filepath.Walk(srcPath, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
contents, err := ioutil.ReadFile(path)
if err != nil {
return err
}
rel, err := filepath.Rel(srcPath, path)
if err != nil {
return err
}
dstFilePath := filepath.Join(dstPath, rel)
if err := os.MkdirAll(filepath.Dir(dstFilePath), 0755); err != nil {
return err
}
if err := ioutil.WriteFile(dstFilePath, contents, 0644); err != nil {
return err
}
return nil
})
}