]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/script/scripttest: use GOHOSTARCH to find tool directory
authorIan Lance Taylor <iant@golang.org>
Tue, 11 Mar 2025 22:00:23 +0000 (15:00 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 12 Mar 2025 01:16:13 +0000 (18:16 -0700)
Fixes #72800

Change-Id: Idde7eae13d1c0098e5314935cf8ca823cbc7a7cc
Reviewed-on: https://go-review.googlesource.com/c/go/+/656855
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/internal/script/scripttest/setup.go

index d430367c12a613319ec690a41779a06126c9b814..2826b56e87edb249aa5be47d6a1728ec27f73fd0 100644 (file)
@@ -6,10 +6,13 @@
 package scripttest
 
 import (
+       "internal/testenv"
        "io"
        "os"
        "path/filepath"
        "runtime"
+       "strings"
+       "sync"
        "testing"
 )
 
@@ -36,13 +39,17 @@ func SetupTestGoRoot(t *testing.T, tmpdir string, goroot string) string {
        }
 
        // Create various dirs in testgoroot.
-       toolsub := filepath.Join("tool", runtime.GOOS+"_"+runtime.GOARCH)
+       findToolOnce.Do(func() { findToolSub(t) })
+       if toolsub == "" {
+               t.Fatal("failed to find toolsub")
+       }
+
        tomake := []string{
                "bin",
                "src",
                "pkg",
                filepath.Join("pkg", "include"),
-               filepath.Join("pkg", toolsub),
+               toolsub,
        }
        made := []string{}
        tgr := filepath.Join(tmpdir, "testgoroot")
@@ -57,7 +64,7 @@ func SetupTestGoRoot(t *testing.T, tmpdir string, goroot string) string {
        replicateDir(filepath.Join(goroot, "bin"), made[0])
        replicateDir(filepath.Join(goroot, "src"), made[1])
        replicateDir(filepath.Join(goroot, "pkg", "include"), made[3])
-       replicateDir(filepath.Join(goroot, "pkg", toolsub), made[4])
+       replicateDir(filepath.Join(goroot, toolsub), made[4])
 
        return tgr
 }
@@ -66,7 +73,11 @@ func SetupTestGoRoot(t *testing.T, tmpdir string, goroot string) string {
 // an alternate executable newtoolpath within a test GOROOT directory
 // previously created by SetupTestGoRoot.
 func ReplaceGoToolInTestGoRoot(t *testing.T, testgoroot, toolname, newtoolpath string) {
-       toolsub := filepath.Join("pkg", "tool", runtime.GOOS+"_"+runtime.GOARCH)
+       findToolOnce.Do(func() { findToolSub(t) })
+       if toolsub == "" {
+               t.Fatal("failed to find toolsub")
+       }
+
        exename := toolname
        if runtime.GOOS == "windows" {
                exename += ".exe"
@@ -78,6 +89,24 @@ func ReplaceGoToolInTestGoRoot(t *testing.T, testgoroot, toolname, newtoolpath s
        linkOrCopy(t, newtoolpath, toolpath)
 }
 
+// toolsub is the tool subdirectory underneath GOROOT.
+var toolsub string
+
+// findToolOnce runs findToolSub only once.
+var findToolOnce sync.Once
+
+// findToolSub sets toolsub to the value used by the current go command.
+func findToolSub(t *testing.T) {
+       gocmd := testenv.Command(t, testenv.GoToolPath(t), "env", "GOHOSTARCH")
+       gocmd = testenv.CleanCmdEnv(gocmd)
+       goHostArchBytes, err := gocmd.CombinedOutput()
+       if err != nil {
+               t.Fatalf("%s failed: %v\n%s", gocmd, err, goHostArchBytes)
+       }
+       goHostArch := strings.TrimSpace(string(goHostArchBytes))
+       toolsub = filepath.Join("pkg", "tool", runtime.GOOS+"_"+goHostArch)
+}
+
 // linkOrCopy creates a link to src at dst, or if the symlink fails
 // (platform doesn't support) then copies src to dst.
 func linkOrCopy(t *testing.T, src, dst string) {