1
0
mirror of https://github.com/golang/go synced 2024-11-16 23:44:42 -07:00

cmd/internal/archive: don't rely on an erroneous install target in tests

Non-main packages in module mode should not be installed to
GOPATH/pkg, but due to #37015 they were installed there anyway.
This change switches the 'go install' command to instead use
'go build -buildmode=archive' with an explicit archive path.

For #37015.

Change-Id: Ib0c8f213100b6473a7657af96f31395703e28493
Reviewed-on: https://go-review.googlesource.com/c/go/+/414055
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
This commit is contained in:
Bryan C. Mills 2022-06-24 17:02:24 -04:00 committed by Gopher Robot
parent 5a1c5b8ae7
commit 6b6c64b1cc

View File

@ -18,32 +18,23 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"sync"
"testing" "testing"
"unicode/utf8" "unicode/utf8"
) )
var ( var buildDir string
buildDir string
go1obj string
go2obj string
goarchive string
cgoarchive string
)
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
if !testenv.HasGoBuild() { if !testenv.HasGoBuild() {
return return
} }
if err := buildGoobj(); err != nil {
fmt.Println(err)
os.RemoveAll(buildDir)
os.Exit(1)
}
exit := m.Run() exit := m.Run()
if buildDir != "" {
os.RemoveAll(buildDir) os.RemoveAll(buildDir)
}
os.Exit(exit) os.Exit(exit)
} }
@ -89,17 +80,31 @@ func copyFile(dst, src string) (err error) {
return nil return nil
} }
func buildGoobj() error { var (
var err error buildOnce sync.Once
builtGoobjs goobjPaths
buildErr error
)
type goobjPaths struct {
go1obj string
go2obj string
goarchive string
cgoarchive string
}
func buildGoobj(t *testing.T) goobjPaths {
buildOnce.Do(func() {
buildErr = func() (err error) {
buildDir, err = ioutil.TempDir("", "TestGoobj") buildDir, err = ioutil.TempDir("", "TestGoobj")
if err != nil { if err != nil {
return err return err
} }
go1obj = filepath.Join(buildDir, "go1.o") go1obj := filepath.Join(buildDir, "go1.o")
go2obj = filepath.Join(buildDir, "go2.o") go2obj := filepath.Join(buildDir, "go2.o")
goarchive = filepath.Join(buildDir, "go.a") goarchive := filepath.Join(buildDir, "go.a")
cgoarchive := ""
gotool, err := testenv.GoTool() gotool, err := testenv.GoTool()
if err != nil { if err != nil {
@ -123,6 +128,7 @@ func buildGoobj() error {
} }
if testenv.HasCGO() { if testenv.HasCGO() {
cgoarchive = filepath.Join(buildDir, "mycgo.a")
gopath := filepath.Join(buildDir, "gopath") gopath := filepath.Join(buildDir, "gopath")
err = copyDir(filepath.Join(gopath, "src", "mycgo"), filepath.Join("testdata", "mycgo")) err = copyDir(filepath.Join(gopath, "src", "mycgo"), filepath.Join("testdata", "mycgo"))
if err == nil { if err == nil {
@ -131,29 +137,34 @@ func buildGoobj() error {
if err != nil { if err != nil {
return err return err
} }
cmd := exec.Command(gotool, "install", "-gcflags=all="+os.Getenv("GO_GCFLAGS"), "mycgo") cmd := exec.Command(gotool, "build", "-buildmode=archive", "-o", cgoarchive, "-gcflags=all="+os.Getenv("GO_GCFLAGS"), "mycgo")
cmd.Dir = filepath.Join(gopath, "src", "mycgo") cmd.Dir = filepath.Join(gopath, "src", "mycgo")
cmd.Env = append(os.Environ(), "GOPATH="+gopath) cmd.Env = append(os.Environ(), "GOPATH="+gopath)
out, err = cmd.CombinedOutput() out, err = cmd.CombinedOutput()
if err != nil { if err != nil {
return fmt.Errorf("go install mycgo: %v\n%s", err, out) return fmt.Errorf("go install mycgo: %v\n%s", err, out)
} }
pat := filepath.Join(gopath, "pkg", "*", "mycgo.a")
ms, err := filepath.Glob(pat)
if err != nil {
return err
}
if len(ms) == 0 {
return fmt.Errorf("cannot found paths for pattern %s", pat)
}
cgoarchive = ms[0]
} }
builtGoobjs = goobjPaths{
go1obj: go1obj,
go2obj: go2obj,
goarchive: goarchive,
cgoarchive: cgoarchive,
}
return nil return nil
}()
})
if buildErr != nil {
t.Helper()
t.Fatal(buildErr)
}
return builtGoobjs
} }
func TestParseGoobj(t *testing.T) { func TestParseGoobj(t *testing.T) {
path := go1obj path := buildGoobj(t).go1obj
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
@ -182,7 +193,7 @@ func TestParseGoobj(t *testing.T) {
} }
func TestParseArchive(t *testing.T) { func TestParseArchive(t *testing.T) {
path := goarchive path := buildGoobj(t).goarchive
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
@ -227,7 +238,7 @@ func TestParseArchive(t *testing.T) {
func TestParseCGOArchive(t *testing.T) { func TestParseCGOArchive(t *testing.T) {
testenv.MustHaveCGO(t) testenv.MustHaveCGO(t)
path := cgoarchive path := buildGoobj(t).cgoarchive
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {