aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/internal/script/scripttest
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2025-03-11 15:00:23 -0700
committerGopher Robot <gobot@golang.org>2025-03-11 18:16:13 -0700
commit43648931492e45c78ba3e21e7191944a0bb7c10b (patch)
tree47e1107190f7b2adf9854ff9c43ca9d277bda819 /src/cmd/internal/script/scripttest
parent17b9c9f2ad6f7943a4a1861dfc000d190abca55b (diff)
downloadgo-43648931492e45c78ba3e21e7191944a0bb7c10b.tar.xz
cmd/internal/script/scripttest: use GOHOSTARCH to find tool directory
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>
Diffstat (limited to 'src/cmd/internal/script/scripttest')
-rw-r--r--src/cmd/internal/script/scripttest/setup.go37
1 files changed, 33 insertions, 4 deletions
diff --git a/src/cmd/internal/script/scripttest/setup.go b/src/cmd/internal/script/scripttest/setup.go
index d430367c12..2826b56e87 100644
--- a/src/cmd/internal/script/scripttest/setup.go
+++ b/src/cmd/internal/script/scripttest/setup.go
@@ -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) {