aboutsummaryrefslogtreecommitdiff
path: root/src/cmd
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2022-03-09 17:19:23 -0500
committerBryan Mills <bcmills@google.com>2022-03-18 21:55:52 +0000
commit58631ba54f45506f2f178bb01d22273e7dfba674 (patch)
tree6833d66fdc88efd536d43d0a71f3e61aec77778e /src/cmd
parent9ac75d39514402d9b314e758524dcc28612b8937 (diff)
downloadgo-58631ba54f45506f2f178bb01d22273e7dfba674.tar.xz
internal/testenv: add GOROOT and use it to fix tests broken with -trimpath
This fixes many (but not all) of the tests that currently fail (due to a bogus path reported by runtime.GOROOT) when run with 'go test -trimpath std cmd'. Updates #51461 Change-Id: Ia2cc05705529c4859e7928f32eeceed647f2e986 Reviewed-on: https://go-review.googlesource.com/c/go/+/391806 Trust: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/cmd')
-rw-r--r--src/cmd/api/goapi.go12
-rw-r--r--src/cmd/api/goapi_test.go2
-rw-r--r--src/cmd/compile/internal/importer/gcimporter_test.go8
-rw-r--r--src/cmd/compile/internal/syntax/parser_test.go7
-rw-r--r--src/cmd/compile/internal/types2/main_test.go17
-rw-r--r--src/cmd/compile/internal/types2/stdlib_test.go26
-rw-r--r--src/cmd/doc/doc_test.go8
-rw-r--r--src/cmd/go/internal/imports/scan_test.go3
-rw-r--r--src/cmd/gofmt/long_test.go7
-rw-r--r--src/cmd/internal/moddeps/moddeps_test.go23
-rw-r--r--src/cmd/link/internal/ld/nooptcgolink_test.go3
-rw-r--r--src/cmd/nm/nm_test.go2
12 files changed, 79 insertions, 39 deletions
diff --git a/src/cmd/api/goapi.go b/src/cmd/api/goapi.go
index 2a0e109575..b2a023a9b7 100644
--- a/src/cmd/api/goapi.go
+++ b/src/cmd/api/goapi.go
@@ -34,9 +34,11 @@ func goCmd() string {
if runtime.GOOS == "windows" {
exeSuffix = ".exe"
}
- path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
- if _, err := os.Stat(path); err == nil {
- return path
+ if goroot := build.Default.GOROOT; goroot != "" {
+ path := filepath.Join(goroot, "bin", "go"+exeSuffix)
+ if _, err := os.Stat(path); err == nil {
+ return path
+ }
}
return "go"
}
@@ -127,6 +129,10 @@ var internalPkg = regexp.MustCompile(`(^|/)internal($|/)`)
func main() {
flag.Parse()
+ if build.Default.GOROOT == "" {
+ log.Fatalf("GOROOT not found. (If binary was built with -trimpath, $GOROOT must be set.)")
+ }
+
if !strings.Contains(runtime.Version(), "weekly") && !strings.Contains(runtime.Version(), "devel") {
if *nextFiles != "" {
fmt.Printf("Go version is %q, ignoring -next %s\n", runtime.Version(), *nextFiles)
diff --git a/src/cmd/api/goapi_test.go b/src/cmd/api/goapi_test.go
index 16e0058e5e..862ab183b2 100644
--- a/src/cmd/api/goapi_test.go
+++ b/src/cmd/api/goapi_test.go
@@ -9,6 +9,7 @@ import (
"flag"
"fmt"
"go/build"
+ "internal/testenv"
"os"
"path/filepath"
"sort"
@@ -22,6 +23,7 @@ func TestMain(m *testing.M) {
for _, c := range contexts {
c.Compiler = build.Default.Compiler
}
+ build.Default.GOROOT = testenv.GOROOT(nil)
// Warm up the import cache in parallel.
var wg sync.WaitGroup
diff --git a/src/cmd/compile/internal/importer/gcimporter_test.go b/src/cmd/compile/internal/importer/gcimporter_test.go
index cc804aabbc..9fecf742fb 100644
--- a/src/cmd/compile/internal/importer/gcimporter_test.go
+++ b/src/cmd/compile/internal/importer/gcimporter_test.go
@@ -8,6 +8,7 @@ import (
"bytes"
"cmd/compile/internal/types2"
"fmt"
+ "go/build"
"internal/goexperiment"
"internal/testenv"
"os"
@@ -19,6 +20,11 @@ import (
"time"
)
+func TestMain(m *testing.M) {
+ build.Default.GOROOT = testenv.GOROOT(nil)
+ os.Exit(m.Run())
+}
+
// skipSpecialPlatforms causes the test to be skipped for platforms where
// builders (build.golang.org) don't have access to compiled packages for
// import.
@@ -62,7 +68,7 @@ func testPath(t *testing.T, path, srcDir string) *types2.Package {
const maxTime = 30 * time.Second
func testDir(t *testing.T, dir string, endTime time.Time) (nimports int) {
- dirname := filepath.Join(runtime.GOROOT(), "pkg", runtime.GOOS+"_"+runtime.GOARCH, dir)
+ dirname := filepath.Join(testenv.GOROOT(t), "pkg", runtime.GOOS+"_"+runtime.GOARCH, dir)
list, err := os.ReadDir(dirname)
if err != nil {
t.Fatalf("testDir(%s): %s", dirname, err)
diff --git a/src/cmd/compile/internal/syntax/parser_test.go b/src/cmd/compile/internal/syntax/parser_test.go
index ecb21e070b..66690a527a 100644
--- a/src/cmd/compile/internal/syntax/parser_test.go
+++ b/src/cmd/compile/internal/syntax/parser_test.go
@@ -8,6 +8,7 @@ import (
"bytes"
"flag"
"fmt"
+ "internal/testenv"
"io/ioutil"
"path/filepath"
"regexp"
@@ -74,12 +75,14 @@ func TestStdLib(t *testing.T) {
lines uint
}
+ goroot := testenv.GOROOT(t)
+
results := make(chan parseResult)
go func() {
defer close(results)
for _, dir := range []string{
- filepath.Join(runtime.GOROOT(), "src"),
- filepath.Join(runtime.GOROOT(), "misc"),
+ filepath.Join(goroot, "src"),
+ filepath.Join(goroot, "misc"),
} {
walkDirs(t, dir, func(filename string) {
if skipRx != nil && skipRx.MatchString(filename) {
diff --git a/src/cmd/compile/internal/types2/main_test.go b/src/cmd/compile/internal/types2/main_test.go
new file mode 100644
index 0000000000..42d26943c4
--- /dev/null
+++ b/src/cmd/compile/internal/types2/main_test.go
@@ -0,0 +1,17 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package types2_test
+
+import (
+ "go/build"
+ "internal/testenv"
+ "os"
+ "testing"
+)
+
+func TestMain(m *testing.M) {
+ build.Default.GOROOT = testenv.GOROOT(nil)
+ os.Exit(m.Run())
+}
diff --git a/src/cmd/compile/internal/types2/stdlib_test.go b/src/cmd/compile/internal/types2/stdlib_test.go
index 551611da55..fda78e20d1 100644
--- a/src/cmd/compile/internal/types2/stdlib_test.go
+++ b/src/cmd/compile/internal/types2/stdlib_test.go
@@ -15,7 +15,6 @@ import (
"internal/testenv"
"os"
"path/filepath"
- "runtime"
"strings"
"testing"
"time"
@@ -29,7 +28,7 @@ func TestStdlib(t *testing.T) {
testenv.MustHaveGoBuild(t)
pkgCount := 0
- duration := walkPkgDirs(filepath.Join(runtime.GOROOT(), "src"), func(dir string, filenames []string) {
+ duration := walkPkgDirs(filepath.Join(testenv.GOROOT(t), "src"), func(dir string, filenames []string) {
typecheck(t, dir, filenames)
pkgCount++
}, t.Error)
@@ -162,7 +161,7 @@ func TestStdTest(t *testing.T) {
t.Skip("skipping in short mode")
}
- testTestDir(t, filepath.Join(runtime.GOROOT(), "test"),
+ testTestDir(t, filepath.Join(testenv.GOROOT(t), "test"),
"cmplxdivide.go", // also needs file cmplxdivide1.go - ignore
"directive.go", // tests compiler rejection of bad directive placement - ignore
"directive2.go", // tests compiler rejection of bad directive placement - ignore
@@ -180,7 +179,7 @@ func TestStdFixed(t *testing.T) {
t.Skip("skipping in short mode")
}
- testTestDir(t, filepath.Join(runtime.GOROOT(), "test", "fixedbugs"),
+ testTestDir(t, filepath.Join(testenv.GOROOT(t), "test", "fixedbugs"),
"bug248.go", "bug302.go", "bug369.go", // complex test instructions - ignore
"issue6889.go", // gc-specific test
"issue11362.go", // canonical import path check
@@ -204,7 +203,7 @@ func TestStdFixed(t *testing.T) {
func TestStdKen(t *testing.T) {
testenv.MustHaveGoBuild(t)
- testTestDir(t, filepath.Join(runtime.GOROOT(), "test", "ken"))
+ testTestDir(t, filepath.Join(testenv.GOROOT(t), "test", "ken"))
}
// Package paths of excluded packages.
@@ -311,16 +310,13 @@ func (w *walker) walk(dir string) {
}
// apply pkgh to the files in directory dir
- // but ignore files directly under $GOROOT/src (might be temporary test files).
- if dir != filepath.Join(runtime.GOROOT(), "src") {
- files, err := pkgFilenames(dir)
- if err != nil {
- w.errh(err)
- return
- }
- if files != nil {
- w.pkgh(dir, files)
- }
+ pkgFiles, err := pkgFilenames(dir)
+ if err != nil {
+ w.errh(err)
+ return
+ }
+ if pkgFiles != nil {
+ w.pkgh(dir, pkgFiles)
}
// traverse subdirectories, but don't walk into testdata
diff --git a/src/cmd/doc/doc_test.go b/src/cmd/doc/doc_test.go
index 0ff9edcde3..ead4f722f6 100644
--- a/src/cmd/doc/doc_test.go
+++ b/src/cmd/doc/doc_test.go
@@ -7,6 +7,8 @@ package main
import (
"bytes"
"flag"
+ "go/build"
+ "internal/testenv"
"log"
"os"
"path/filepath"
@@ -21,6 +23,12 @@ func TestMain(m *testing.M) {
buildCtx.GOPATH = ""
testGOPATH = true // force GOPATH mode; module test is in cmd/go/testdata/script/mod_doc.txt
+ // Set GOROOT in case runtime.GOROOT is wrong (for example, if the test was
+ // built with -trimpath). dirsInit would identify it using 'go env GOROOT',
+ // but we can't be sure that the 'go' in $PATH is the right one either.
+ buildCtx.GOROOT = testenv.GOROOT(nil)
+ build.Default.GOROOT = testenv.GOROOT(nil)
+
// Add $GOROOT/src/cmd/doc/testdata explicitly so we can access its contents in the test.
// Normally testdata directories are ignored, but sending it to dirs.scan directly is
// a hack that works around the check.
diff --git a/src/cmd/go/internal/imports/scan_test.go b/src/cmd/go/internal/imports/scan_test.go
index 7e69c56513..56efa9023f 100644
--- a/src/cmd/go/internal/imports/scan_test.go
+++ b/src/cmd/go/internal/imports/scan_test.go
@@ -10,7 +10,6 @@ import (
"os"
"path"
"path/filepath"
- "runtime"
"strings"
"testing"
)
@@ -18,7 +17,7 @@ import (
func TestScan(t *testing.T) {
testenv.MustHaveGoBuild(t)
- imports, testImports, err := ScanDir(filepath.Join(runtime.GOROOT(), "src/encoding/json"), Tags())
+ imports, testImports, err := ScanDir(filepath.Join(testenv.GOROOT(t), "src/encoding/json"), Tags())
if err != nil {
t.Fatal(err)
}
diff --git a/src/cmd/gofmt/long_test.go b/src/cmd/gofmt/long_test.go
index 4a821705f1..a130874048 100644
--- a/src/cmd/gofmt/long_test.go
+++ b/src/cmd/gofmt/long_test.go
@@ -15,6 +15,7 @@ import (
"go/ast"
"go/printer"
"go/token"
+ "internal/testenv"
"io"
"io/fs"
"os"
@@ -130,7 +131,11 @@ func genFilenames(t *testing.T, filenames chan<- string) {
}
// otherwise, test all Go files under *root
- filepath.WalkDir(*root, handleFile)
+ goroot := *root
+ if goroot == "" {
+ goroot = testenv.GOROOT(t)
+ }
+ filepath.WalkDir(goroot, handleFile)
}
func TestAll(t *testing.T) {
diff --git a/src/cmd/internal/moddeps/moddeps_test.go b/src/cmd/internal/moddeps/moddeps_test.go
index 56c3b2585c..a63ac71a16 100644
--- a/src/cmd/internal/moddeps/moddeps_test.go
+++ b/src/cmd/internal/moddeps/moddeps_test.go
@@ -15,7 +15,6 @@ import (
"os"
"os/exec"
"path/filepath"
- "runtime"
"strings"
"sync"
"testing"
@@ -153,7 +152,7 @@ func TestAllDependencies(t *testing.T) {
// module version specified in GOROOT/src/cmd/go.mod.
bundleDir := t.TempDir()
r := runner{
- Dir: filepath.Join(runtime.GOROOT(), "src/cmd"),
+ Dir: filepath.Join(testenv.GOROOT(t), "src/cmd"),
Env: append(os.Environ(), modcacheEnv...),
}
r.run(t, goBin, "build", "-mod=readonly", "-o", bundleDir, "golang.org/x/tools/cmd/bundle")
@@ -183,9 +182,9 @@ func TestAllDependencies(t *testing.T) {
}
}()
- rel, err := filepath.Rel(runtime.GOROOT(), m.Dir)
+ rel, err := filepath.Rel(testenv.GOROOT(t), m.Dir)
if err != nil {
- t.Fatalf("filepath.Rel(%q, %q): %v", runtime.GOROOT(), m.Dir, err)
+ t.Fatalf("filepath.Rel(%q, %q): %v", testenv.GOROOT(t), m.Dir, err)
}
r := runner{
Dir: filepath.Join(gorootCopyDir, rel),
@@ -252,22 +251,22 @@ func packagePattern(modulePath string) string {
func makeGOROOTCopy(t *testing.T) string {
t.Helper()
gorootCopyDir := t.TempDir()
- err := filepath.Walk(runtime.GOROOT(), func(src string, info os.FileInfo, err error) error {
+ err := filepath.Walk(testenv.GOROOT(t), func(src string, info os.FileInfo, err error) error {
if err != nil {
return err
}
- if info.IsDir() && src == filepath.Join(runtime.GOROOT(), ".git") {
+ if info.IsDir() && src == filepath.Join(testenv.GOROOT(t), ".git") {
return filepath.SkipDir
}
- rel, err := filepath.Rel(runtime.GOROOT(), src)
+ rel, err := filepath.Rel(testenv.GOROOT(t), src)
if err != nil {
- return fmt.Errorf("filepath.Rel(%q, %q): %v", runtime.GOROOT(), src, err)
+ return fmt.Errorf("filepath.Rel(%q, %q): %v", testenv.GOROOT(t), src, err)
}
dst := filepath.Join(gorootCopyDir, rel)
- if info.IsDir() && (src == filepath.Join(runtime.GOROOT(), "bin") ||
- src == filepath.Join(runtime.GOROOT(), "pkg")) {
+ if info.IsDir() && (src == filepath.Join(testenv.GOROOT(t), "bin") ||
+ src == filepath.Join(testenv.GOROOT(t), "pkg")) {
// If the OS supports symlinks, use them instead
// of copying the bin and pkg directories.
if err := os.Symlink(src, dst); err == nil {
@@ -435,14 +434,14 @@ func findGorootModules(t *testing.T) []gorootModule {
goBin := testenv.GoToolPath(t)
goroot.once.Do(func() {
- goroot.err = filepath.WalkDir(runtime.GOROOT(), func(path string, info fs.DirEntry, err error) error {
+ goroot.err = filepath.WalkDir(testenv.GOROOT(t), func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if info.IsDir() && (info.Name() == "vendor" || info.Name() == "testdata") {
return filepath.SkipDir
}
- if info.IsDir() && path == filepath.Join(runtime.GOROOT(), "pkg") {
+ if info.IsDir() && path == filepath.Join(testenv.GOROOT(t), "pkg") {
// GOROOT/pkg contains generated artifacts, not source code.
//
// In https://golang.org/issue/37929 it was observed to somehow contain
diff --git a/src/cmd/link/internal/ld/nooptcgolink_test.go b/src/cmd/link/internal/ld/nooptcgolink_test.go
index 73548dabd4..0b76ecaecb 100644
--- a/src/cmd/link/internal/ld/nooptcgolink_test.go
+++ b/src/cmd/link/internal/ld/nooptcgolink_test.go
@@ -8,7 +8,6 @@ import (
"internal/testenv"
"os/exec"
"path/filepath"
- "runtime"
"testing"
)
@@ -22,7 +21,7 @@ func TestNooptCgoBuild(t *testing.T) {
testenv.MustHaveCGO(t)
dir := t.TempDir()
cmd := exec.Command(testenv.GoToolPath(t), "build", "-gcflags=-N -l", "-o", filepath.Join(dir, "a.out"))
- cmd.Dir = filepath.Join(runtime.GOROOT(), "src", "runtime", "testdata", "testprogcgo")
+ cmd.Dir = filepath.Join(testenv.GOROOT(t), "src", "runtime", "testdata", "testprogcgo")
out, err := cmd.CombinedOutput()
if err != nil {
t.Logf("go build output: %s", out)
diff --git a/src/cmd/nm/nm_test.go b/src/cmd/nm/nm_test.go
index 0d51b07a44..226c2c3bcd 100644
--- a/src/cmd/nm/nm_test.go
+++ b/src/cmd/nm/nm_test.go
@@ -66,7 +66,7 @@ func TestNonGoExecs(t *testing.T) {
"internal/xcoff/testdata/gcc-ppc64-aix-dwarf2-exec",
}
for _, f := range testfiles {
- exepath := filepath.Join(runtime.GOROOT(), "src", f)
+ exepath := filepath.Join(testenv.GOROOT(t), "src", f)
if strings.HasSuffix(f, ".base64") {
tf, err := obscuretestdata.DecodeToTempFile(exepath)
if err != nil {