Skip to content

Commit 6b6c64b

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
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 <[email protected]> Reviewed-by: Cherry Mui <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Run-TryBot: Bryan Mills <[email protected]>
1 parent 5a1c5b8 commit 6b6c64b

File tree

1 file changed

+82
-71
lines changed

1 file changed

+82
-71
lines changed

src/cmd/internal/archive/archive_test.go

Lines changed: 82 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,23 @@ import (
1818
"os/exec"
1919
"path/filepath"
2020
"runtime"
21+
"sync"
2122
"testing"
2223
"unicode/utf8"
2324
)
2425

25-
var (
26-
buildDir string
27-
go1obj string
28-
go2obj string
29-
goarchive string
30-
cgoarchive string
31-
)
26+
var buildDir string
3227

3328
func TestMain(m *testing.M) {
3429
if !testenv.HasGoBuild() {
3530
return
3631
}
3732

38-
if err := buildGoobj(); err != nil {
39-
fmt.Println(err)
40-
os.RemoveAll(buildDir)
41-
os.Exit(1)
42-
}
43-
4433
exit := m.Run()
4534

46-
os.RemoveAll(buildDir)
35+
if buildDir != "" {
36+
os.RemoveAll(buildDir)
37+
}
4738
os.Exit(exit)
4839
}
4940

@@ -89,71 +80,91 @@ func copyFile(dst, src string) (err error) {
8980
return nil
9081
}
9182

92-
func buildGoobj() error {
93-
var err error
83+
var (
84+
buildOnce sync.Once
85+
builtGoobjs goobjPaths
86+
buildErr error
87+
)
9488

95-
buildDir, err = ioutil.TempDir("", "TestGoobj")
96-
if err != nil {
97-
return err
98-
}
89+
type goobjPaths struct {
90+
go1obj string
91+
go2obj string
92+
goarchive string
93+
cgoarchive string
94+
}
9995

100-
go1obj = filepath.Join(buildDir, "go1.o")
101-
go2obj = filepath.Join(buildDir, "go2.o")
102-
goarchive = filepath.Join(buildDir, "go.a")
96+
func buildGoobj(t *testing.T) goobjPaths {
97+
buildOnce.Do(func() {
98+
buildErr = func() (err error) {
99+
buildDir, err = ioutil.TempDir("", "TestGoobj")
100+
if err != nil {
101+
return err
102+
}
103103

104-
gotool, err := testenv.GoTool()
105-
if err != nil {
106-
return err
107-
}
104+
go1obj := filepath.Join(buildDir, "go1.o")
105+
go2obj := filepath.Join(buildDir, "go2.o")
106+
goarchive := filepath.Join(buildDir, "go.a")
107+
cgoarchive := ""
108108

109-
go1src := filepath.Join("testdata", "go1.go")
110-
go2src := filepath.Join("testdata", "go2.go")
109+
gotool, err := testenv.GoTool()
110+
if err != nil {
111+
return err
112+
}
111113

112-
out, err := exec.Command(gotool, "tool", "compile", "-p=p", "-o", go1obj, go1src).CombinedOutput()
113-
if err != nil {
114-
return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go1obj, go1src, err, out)
115-
}
116-
out, err = exec.Command(gotool, "tool", "compile", "-p=p", "-o", go2obj, go2src).CombinedOutput()
117-
if err != nil {
118-
return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go2obj, go2src, err, out)
119-
}
120-
out, err = exec.Command(gotool, "tool", "pack", "c", goarchive, go1obj, go2obj).CombinedOutput()
121-
if err != nil {
122-
return fmt.Errorf("go tool pack c %s %s %s: %v\n%s", goarchive, go1obj, go2obj, err, out)
123-
}
114+
go1src := filepath.Join("testdata", "go1.go")
115+
go2src := filepath.Join("testdata", "go2.go")
124116

125-
if testenv.HasCGO() {
126-
gopath := filepath.Join(buildDir, "gopath")
127-
err = copyDir(filepath.Join(gopath, "src", "mycgo"), filepath.Join("testdata", "mycgo"))
128-
if err == nil {
129-
err = ioutil.WriteFile(filepath.Join(gopath, "src", "mycgo", "go.mod"), []byte("module mycgo\n"), 0666)
130-
}
131-
if err != nil {
132-
return err
133-
}
134-
cmd := exec.Command(gotool, "install", "-gcflags=all="+os.Getenv("GO_GCFLAGS"), "mycgo")
135-
cmd.Dir = filepath.Join(gopath, "src", "mycgo")
136-
cmd.Env = append(os.Environ(), "GOPATH="+gopath)
137-
out, err = cmd.CombinedOutput()
138-
if err != nil {
139-
return fmt.Errorf("go install mycgo: %v\n%s", err, out)
140-
}
141-
pat := filepath.Join(gopath, "pkg", "*", "mycgo.a")
142-
ms, err := filepath.Glob(pat)
143-
if err != nil {
144-
return err
145-
}
146-
if len(ms) == 0 {
147-
return fmt.Errorf("cannot found paths for pattern %s", pat)
148-
}
149-
cgoarchive = ms[0]
150-
}
117+
out, err := exec.Command(gotool, "tool", "compile", "-p=p", "-o", go1obj, go1src).CombinedOutput()
118+
if err != nil {
119+
return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go1obj, go1src, err, out)
120+
}
121+
out, err = exec.Command(gotool, "tool", "compile", "-p=p", "-o", go2obj, go2src).CombinedOutput()
122+
if err != nil {
123+
return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go2obj, go2src, err, out)
124+
}
125+
out, err = exec.Command(gotool, "tool", "pack", "c", goarchive, go1obj, go2obj).CombinedOutput()
126+
if err != nil {
127+
return fmt.Errorf("go tool pack c %s %s %s: %v\n%s", goarchive, go1obj, go2obj, err, out)
128+
}
151129

152-
return nil
130+
if testenv.HasCGO() {
131+
cgoarchive = filepath.Join(buildDir, "mycgo.a")
132+
gopath := filepath.Join(buildDir, "gopath")
133+
err = copyDir(filepath.Join(gopath, "src", "mycgo"), filepath.Join("testdata", "mycgo"))
134+
if err == nil {
135+
err = ioutil.WriteFile(filepath.Join(gopath, "src", "mycgo", "go.mod"), []byte("module mycgo\n"), 0666)
136+
}
137+
if err != nil {
138+
return err
139+
}
140+
cmd := exec.Command(gotool, "build", "-buildmode=archive", "-o", cgoarchive, "-gcflags=all="+os.Getenv("GO_GCFLAGS"), "mycgo")
141+
cmd.Dir = filepath.Join(gopath, "src", "mycgo")
142+
cmd.Env = append(os.Environ(), "GOPATH="+gopath)
143+
out, err = cmd.CombinedOutput()
144+
if err != nil {
145+
return fmt.Errorf("go install mycgo: %v\n%s", err, out)
146+
}
147+
}
148+
149+
builtGoobjs = goobjPaths{
150+
go1obj: go1obj,
151+
go2obj: go2obj,
152+
goarchive: goarchive,
153+
cgoarchive: cgoarchive,
154+
}
155+
return nil
156+
}()
157+
})
158+
159+
if buildErr != nil {
160+
t.Helper()
161+
t.Fatal(buildErr)
162+
}
163+
return builtGoobjs
153164
}
154165

155166
func TestParseGoobj(t *testing.T) {
156-
path := go1obj
167+
path := buildGoobj(t).go1obj
157168

158169
f, err := os.Open(path)
159170
if err != nil {
@@ -182,7 +193,7 @@ func TestParseGoobj(t *testing.T) {
182193
}
183194

184195
func TestParseArchive(t *testing.T) {
185-
path := goarchive
196+
path := buildGoobj(t).goarchive
186197

187198
f, err := os.Open(path)
188199
if err != nil {
@@ -227,7 +238,7 @@ func TestParseArchive(t *testing.T) {
227238
func TestParseCGOArchive(t *testing.T) {
228239
testenv.MustHaveCGO(t)
229240

230-
path := cgoarchive
241+
path := buildGoobj(t).cgoarchive
231242

232243
f, err := os.Open(path)
233244
if err != nil {

0 commit comments

Comments
 (0)