]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/archive: don't rely on an erroneous install target in tests
authorBryan C. Mills <bcmills@google.com>
Fri, 24 Jun 2022 21:02:24 +0000 (17:02 -0400)
committerGopher Robot <gobot@golang.org>
Fri, 24 Jun 2022 21:22:43 +0000 (21:22 +0000)
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>

src/cmd/internal/archive/archive_test.go

index 9573495dec1bf9f0ae54e667cc99f498e5c63c6f..bbaa72cbf860c548d174805a9f5a2d61252d2845 100644 (file)
@@ -18,32 +18,23 @@ import (
        "os/exec"
        "path/filepath"
        "runtime"
+       "sync"
        "testing"
        "unicode/utf8"
 )
 
-var (
-       buildDir   string
-       go1obj     string
-       go2obj     string
-       goarchive  string
-       cgoarchive string
-)
+var buildDir string
 
 func TestMain(m *testing.M) {
        if !testenv.HasGoBuild() {
                return
        }
 
-       if err := buildGoobj(); err != nil {
-               fmt.Println(err)
-               os.RemoveAll(buildDir)
-               os.Exit(1)
-       }
-
        exit := m.Run()
 
-       os.RemoveAll(buildDir)
+       if buildDir != "" {
+               os.RemoveAll(buildDir)
+       }
        os.Exit(exit)
 }
 
@@ -89,71 +80,91 @@ func copyFile(dst, src string) (err error) {
        return nil
 }
 
-func buildGoobj() error {
-       var err error
+var (
+       buildOnce   sync.Once
+       builtGoobjs goobjPaths
+       buildErr    error
+)
 
-       buildDir, err = ioutil.TempDir("", "TestGoobj")
-       if err != nil {
-               return err
-       }
+type goobjPaths struct {
+       go1obj     string
+       go2obj     string
+       goarchive  string
+       cgoarchive string
+}
 
-       go1obj = filepath.Join(buildDir, "go1.o")
-       go2obj = filepath.Join(buildDir, "go2.o")
-       goarchive = filepath.Join(buildDir, "go.a")
+func buildGoobj(t *testing.T) goobjPaths {
+       buildOnce.Do(func() {
+               buildErr = func() (err error) {
+                       buildDir, err = ioutil.TempDir("", "TestGoobj")
+                       if err != nil {
+                               return err
+                       }
 
-       gotool, err := testenv.GoTool()
-       if err != nil {
-               return err
-       }
+                       go1obj := filepath.Join(buildDir, "go1.o")
+                       go2obj := filepath.Join(buildDir, "go2.o")
+                       goarchive := filepath.Join(buildDir, "go.a")
+                       cgoarchive := ""
 
-       go1src := filepath.Join("testdata", "go1.go")
-       go2src := filepath.Join("testdata", "go2.go")
+                       gotool, err := testenv.GoTool()
+                       if err != nil {
+                               return err
+                       }
 
-       out, err := exec.Command(gotool, "tool", "compile", "-p=p", "-o", go1obj, go1src).CombinedOutput()
-       if err != nil {
-               return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go1obj, go1src, err, out)
-       }
-       out, err = exec.Command(gotool, "tool", "compile", "-p=p", "-o", go2obj, go2src).CombinedOutput()
-       if err != nil {
-               return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go2obj, go2src, err, out)
-       }
-       out, err = exec.Command(gotool, "tool", "pack", "c", goarchive, go1obj, go2obj).CombinedOutput()
-       if err != nil {
-               return fmt.Errorf("go tool pack c %s %s %s: %v\n%s", goarchive, go1obj, go2obj, err, out)
-       }
+                       go1src := filepath.Join("testdata", "go1.go")
+                       go2src := filepath.Join("testdata", "go2.go")
 
-       if testenv.HasCGO() {
-               gopath := filepath.Join(buildDir, "gopath")
-               err = copyDir(filepath.Join(gopath, "src", "mycgo"), filepath.Join("testdata", "mycgo"))
-               if err == nil {
-                       err = ioutil.WriteFile(filepath.Join(gopath, "src", "mycgo", "go.mod"), []byte("module mycgo\n"), 0666)
-               }
-               if err != nil {
-                       return err
-               }
-               cmd := exec.Command(gotool, "install", "-gcflags=all="+os.Getenv("GO_GCFLAGS"), "mycgo")
-               cmd.Dir = filepath.Join(gopath, "src", "mycgo")
-               cmd.Env = append(os.Environ(), "GOPATH="+gopath)
-               out, err = cmd.CombinedOutput()
-               if err != nil {
-                       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]
-       }
+                       out, err := exec.Command(gotool, "tool", "compile", "-p=p", "-o", go1obj, go1src).CombinedOutput()
+                       if err != nil {
+                               return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go1obj, go1src, err, out)
+                       }
+                       out, err = exec.Command(gotool, "tool", "compile", "-p=p", "-o", go2obj, go2src).CombinedOutput()
+                       if err != nil {
+                               return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go2obj, go2src, err, out)
+                       }
+                       out, err = exec.Command(gotool, "tool", "pack", "c", goarchive, go1obj, go2obj).CombinedOutput()
+                       if err != nil {
+                               return fmt.Errorf("go tool pack c %s %s %s: %v\n%s", goarchive, go1obj, go2obj, err, out)
+                       }
 
-       return nil
+                       if testenv.HasCGO() {
+                               cgoarchive = filepath.Join(buildDir, "mycgo.a")
+                               gopath := filepath.Join(buildDir, "gopath")
+                               err = copyDir(filepath.Join(gopath, "src", "mycgo"), filepath.Join("testdata", "mycgo"))
+                               if err == nil {
+                                       err = ioutil.WriteFile(filepath.Join(gopath, "src", "mycgo", "go.mod"), []byte("module mycgo\n"), 0666)
+                               }
+                               if err != nil {
+                                       return err
+                               }
+                               cmd := exec.Command(gotool, "build", "-buildmode=archive", "-o", cgoarchive, "-gcflags=all="+os.Getenv("GO_GCFLAGS"), "mycgo")
+                               cmd.Dir = filepath.Join(gopath, "src", "mycgo")
+                               cmd.Env = append(os.Environ(), "GOPATH="+gopath)
+                               out, err = cmd.CombinedOutput()
+                               if err != nil {
+                                       return fmt.Errorf("go install mycgo: %v\n%s", err, out)
+                               }
+                       }
+
+                       builtGoobjs = goobjPaths{
+                               go1obj:     go1obj,
+                               go2obj:     go2obj,
+                               goarchive:  goarchive,
+                               cgoarchive: cgoarchive,
+                       }
+                       return nil
+               }()
+       })
+
+       if buildErr != nil {
+               t.Helper()
+               t.Fatal(buildErr)
+       }
+       return builtGoobjs
 }
 
 func TestParseGoobj(t *testing.T) {
-       path := go1obj
+       path := buildGoobj(t).go1obj
 
        f, err := os.Open(path)
        if err != nil {
@@ -182,7 +193,7 @@ func TestParseGoobj(t *testing.T) {
 }
 
 func TestParseArchive(t *testing.T) {
-       path := goarchive
+       path := buildGoobj(t).goarchive
 
        f, err := os.Open(path)
        if err != nil {
@@ -227,7 +238,7 @@ func TestParseArchive(t *testing.T) {
 func TestParseCGOArchive(t *testing.T) {
        testenv.MustHaveCGO(t)
 
-       path := cgoarchive
+       path := buildGoobj(t).cgoarchive
 
        f, err := os.Open(path)
        if err != nil {