cmd/dist: test: use existing globals rather than environment variables

Change-Id: Ief6bad2d15461d455e7230eadd9b42b27d04ec8b
Reviewed-on: https://go-review.googlesource.com/64630
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Hiroshi Ioka 2017-09-19 22:13:22 +09:00 committed by Ian Lance Taylor
parent 4a2391e7c9
commit c100a0f668
1 changed files with 52 additions and 70 deletions

122
src/cmd/dist/test.go vendored
View File

@ -57,11 +57,6 @@ type tester struct {
banner string // prefix, or "" for none banner string // prefix, or "" for none
lastHeading string // last dir heading printed lastHeading string // last dir heading printed
goroot string
goarch string
gohostarch string
goos string
gohostos string
cgoEnabled bool cgoEnabled bool
partial bool partial bool
haveTime bool // the 'time' binary is available haveTime bool // the 'time' binary is available
@ -89,20 +84,7 @@ type distTest struct {
fn func(*distTest) error fn func(*distTest) error
} }
func mustEnv(k string) string {
v := os.Getenv(k)
if v == "" {
log.Fatalf("Unset environment variable %v", k)
}
return v
}
func (t *tester) run() { func (t *tester) run() {
t.goroot = mustEnv("GOROOT")
t.goos = mustEnv("GOOS")
t.gohostos = mustEnv("GOHOSTOS")
t.goarch = mustEnv("GOARCH")
t.gohostarch = mustEnv("GOHOSTARCH")
slurp, err := exec.Command("go", "env", "CGO_ENABLED").Output() slurp, err := exec.Command("go", "env", "CGO_ENABLED").Output()
if err != nil { if err != nil {
log.Fatalf("Error running go env CGO_ENABLED: %v", err) log.Fatalf("Error running go env CGO_ENABLED: %v", err)
@ -111,6 +93,7 @@ func (t *tester) run() {
if flag.NArg() > 0 && t.runRxStr != "" { if flag.NArg() > 0 && t.runRxStr != "" {
log.Fatalf("the -run regular expression flag is mutually exclusive with test name arguments") log.Fatalf("the -run regular expression flag is mutually exclusive with test name arguments")
} }
t.runNames = flag.Args() t.runNames = flag.Args()
if t.hasBash() { if t.hasBash() {
@ -153,7 +136,7 @@ func (t *tester) run() {
} }
t.timeoutScale = 1 t.timeoutScale = 1
switch t.goarch { switch goarch {
case "arm": case "arm":
t.timeoutScale = 2 t.timeoutScale = 2
case "mips", "mipsle", "mips64", "mips64le": case "mips", "mipsle", "mips64", "mips64le":
@ -415,7 +398,7 @@ func (t *tester) registerTests() {
// This test needs its stdout/stderr to be terminals, so we don't run it from cmd/go's tests. // This test needs its stdout/stderr to be terminals, so we don't run it from cmd/go's tests.
// See issue 18153. // See issue 18153.
if t.goos == "linux" { if goos == "linux" {
t.tests = append(t.tests, distTest{ t.tests = append(t.tests, distTest{
name: "cmd_go_test_terminal", name: "cmd_go_test_terminal",
heading: "cmd/go terminal test", heading: "cmd/go terminal test",
@ -439,15 +422,15 @@ func (t *tester) registerTests() {
// in the unmoved GOROOT. // in the unmoved GOROOT.
// Fails on Android with an exec format error. // Fails on Android with an exec format error.
// Fails on plan9 with "cannot find GOROOT" (issue #21016). // Fails on plan9 with "cannot find GOROOT" (issue #21016).
if os.Getenv("GO_BUILDER_NAME") != "" && t.goos != "android" && !t.iOS() && t.goos != "plan9" { if os.Getenv("GO_BUILDER_NAME") != "" && goos != "android" && !t.iOS() && goos != "plan9" {
t.tests = append(t.tests, distTest{ t.tests = append(t.tests, distTest{
name: "moved_goroot", name: "moved_goroot",
heading: "moved GOROOT", heading: "moved GOROOT",
fn: func(dt *distTest) error { fn: func(dt *distTest) error {
t.runPending(dt) t.runPending(dt)
moved := t.goroot + "-moved" moved := goroot + "-moved"
if err := os.Rename(t.goroot, moved); err != nil { if err := os.Rename(goroot, moved); err != nil {
if t.goos == "windows" { if goos == "windows" {
// Fails on Windows (with "Access is denied") if a process // Fails on Windows (with "Access is denied") if a process
// or binary is in this directory. For instance, using all.bat // or binary is in this directory. For instance, using all.bat
// when run from c:\workdir\go\src fails here // when run from c:\workdir\go\src fails here
@ -473,7 +456,7 @@ func (t *tester) registerTests() {
} }
err := cmd.Run() err := cmd.Run()
if rerr := os.Rename(moved, t.goroot); rerr != nil { if rerr := os.Rename(moved, goroot); rerr != nil {
log.Fatalf("failed to restore GOROOT: %v", rerr) log.Fatalf("failed to restore GOROOT: %v", rerr)
} }
return err return err
@ -491,7 +474,7 @@ func (t *tester) registerTests() {
} }
// ARM libgcc may be Thumb, which internal linking does not support. // ARM libgcc may be Thumb, which internal linking does not support.
if t.goarch == "arm" { if goarch == "arm" {
break break
} }
@ -511,7 +494,7 @@ func (t *tester) registerTests() {
} }
// Test internal linking of PIE binaries where it is supported. // Test internal linking of PIE binaries where it is supported.
if t.goos == "linux" && t.goarch == "amd64" && !isAlpineLinux() { if goos == "linux" && goarch == "amd64" && !isAlpineLinux() {
// Issue 18243: We don't have a way to set the default // Issue 18243: We don't have a way to set the default
// dynamic linker used in internal linking mode. So // dynamic linker used in internal linking mode. So
// this test is skipped on Alpine. // this test is skipped on Alpine.
@ -584,7 +567,7 @@ func (t *tester) registerTests() {
}) })
} }
if t.hasBash() && t.cgoEnabled && t.goos != "android" && t.goos != "darwin" { if t.hasBash() && t.cgoEnabled && goos != "android" && goos != "darwin" {
t.registerTest("testgodefs", "../misc/cgo/testgodefs", "./test.bash") t.registerTest("testgodefs", "../misc/cgo/testgodefs", "./test.bash")
} }
if t.cgoEnabled { if t.cgoEnabled {
@ -616,32 +599,32 @@ func (t *tester) registerTests() {
if t.supportedBuildmode("plugin") { if t.supportedBuildmode("plugin") {
t.registerTest("testplugin", "../misc/cgo/testplugin", "./test.bash") t.registerTest("testplugin", "../misc/cgo/testplugin", "./test.bash")
} }
if t.gohostos == "linux" && t.goarch == "amd64" { if gohostos == "linux" && goarch == "amd64" {
t.registerTest("testasan", "../misc/cgo/testasan", "go", "run", "main.go") t.registerTest("testasan", "../misc/cgo/testasan", "go", "run", "main.go")
} }
if t.goos == "linux" && t.goarch == "amd64" { if goos == "linux" && goarch == "amd64" {
t.registerHostTest("testsanitizers/msan", "../misc/cgo/testsanitizers", "misc/cgo/testsanitizers", ".") t.registerHostTest("testsanitizers/msan", "../misc/cgo/testsanitizers", "misc/cgo/testsanitizers", ".")
} }
if t.hasBash() && t.goos != "android" && !t.iOS() && t.gohostos != "windows" { if t.hasBash() && goos != "android" && !t.iOS() && gohostos != "windows" {
t.registerHostTest("cgo_errors", "../misc/cgo/errors", "misc/cgo/errors", ".") t.registerHostTest("cgo_errors", "../misc/cgo/errors", "misc/cgo/errors", ".")
} }
if t.gohostos == "linux" && t.extLink() { if gohostos == "linux" && t.extLink() {
t.registerTest("testsigfwd", "../misc/cgo/testsigfwd", "go", "run", "main.go") t.registerTest("testsigfwd", "../misc/cgo/testsigfwd", "go", "run", "main.go")
} }
} }
// Doc tests only run on builders. // Doc tests only run on builders.
// They find problems approximately never. // They find problems approximately never.
if t.hasBash() && t.goos != "nacl" && t.goos != "android" && !t.iOS() && os.Getenv("GO_BUILDER_NAME") != "" { if t.hasBash() && goos != "nacl" && goos != "android" && !t.iOS() && os.Getenv("GO_BUILDER_NAME") != "" {
t.registerTest("doc_progs", "../doc/progs", "time", "go", "run", "run.go") t.registerTest("doc_progs", "../doc/progs", "time", "go", "run", "run.go")
t.registerTest("wiki", "../doc/articles/wiki", "./test.bash") t.registerTest("wiki", "../doc/articles/wiki", "./test.bash")
t.registerTest("codewalk", "../doc/codewalk", "time", "./run") t.registerTest("codewalk", "../doc/codewalk", "time", "./run")
} }
if t.goos != "android" && !t.iOS() { if goos != "android" && !t.iOS() {
t.registerTest("bench_go1", "../test/bench/go1", "go", "test", t.timeout(600), t.runFlag("")) t.registerTest("bench_go1", "../test/bench/go1", "go", "test", t.timeout(600), t.runFlag(""))
} }
if t.goos != "android" && !t.iOS() { if goos != "android" && !t.iOS() {
// Only start multiple test dir shards on builders, // Only start multiple test dir shards on builders,
// where they get distributed to multiple machines. // where they get distributed to multiple machines.
// See issue 20141. // See issue 20141.
@ -658,16 +641,16 @@ func (t *tester) registerTests() {
}) })
} }
} }
if t.goos != "nacl" && t.goos != "android" && !t.iOS() { if goos != "nacl" && goos != "android" && !t.iOS() {
t.tests = append(t.tests, distTest{ t.tests = append(t.tests, distTest{
name: "api", name: "api",
heading: "API check", heading: "API check",
fn: func(dt *distTest) error { fn: func(dt *distTest) error {
if t.compileOnly { if t.compileOnly {
t.addCmd(dt, "src", "go", "build", filepath.Join(t.goroot, "src/cmd/api/run.go")) t.addCmd(dt, "src", "go", "build", filepath.Join(goroot, "src/cmd/api/run.go"))
return nil return nil
} }
t.addCmd(dt, "src", "go", "run", filepath.Join(t.goroot, "src/cmd/api/run.go")) t.addCmd(dt, "src", "go", "run", filepath.Join(goroot, "src/cmd/api/run.go"))
return nil return nil
}, },
}) })
@ -698,9 +681,9 @@ func (t *tester) registerTest1(seq bool, name, dirBanner, bin string, args ...st
fn: func(dt *distTest) error { fn: func(dt *distTest) error {
if seq { if seq {
t.runPending(dt) t.runPending(dt)
return t.dirCmd(filepath.Join(t.goroot, "src", dirBanner), bin, args...).Run() return t.dirCmd(filepath.Join(goroot, "src", dirBanner), bin, args...).Run()
} }
t.addCmd(dt, filepath.Join(t.goroot, "src", dirBanner), bin, args...) t.addCmd(dt, filepath.Join(goroot, "src", dirBanner), bin, args...)
return nil return nil
}, },
}) })
@ -719,7 +702,7 @@ func (t *tester) bgDirCmd(dir, bin string, args ...string) *exec.Cmd {
if filepath.IsAbs(dir) { if filepath.IsAbs(dir) {
cmd.Dir = dir cmd.Dir = dir
} else { } else {
cmd.Dir = filepath.Join(t.goroot, dir) cmd.Dir = filepath.Join(goroot, dir)
} }
return cmd return cmd
} }
@ -744,7 +727,7 @@ func (t *tester) addCmd(dt *distTest, dir, bin string, args ...string) *exec.Cmd
} }
func (t *tester) iOS() bool { func (t *tester) iOS() bool {
return t.goos == "darwin" && (t.goarch == "arm" || t.goarch == "arm64") return goos == "darwin" && (goarch == "arm" || goarch == "arm64")
} }
func (t *tester) out(v string) { func (t *tester) out(v string) {
@ -755,7 +738,7 @@ func (t *tester) out(v string) {
} }
func (t *tester) extLink() bool { func (t *tester) extLink() bool {
pair := t.gohostos + "-" + t.goarch pair := gohostos + "-" + goarch
switch pair { switch pair {
case "android-arm", case "android-arm",
"darwin-arm", "darwin-arm64", "darwin-arm", "darwin-arm64",
@ -780,25 +763,25 @@ func (t *tester) extLink() bool {
} }
func (t *tester) internalLink() bool { func (t *tester) internalLink() bool {
if t.gohostos == "dragonfly" { if gohostos == "dragonfly" {
// linkmode=internal fails on dragonfly since errno is a TLS relocation. // linkmode=internal fails on dragonfly since errno is a TLS relocation.
return false return false
} }
if t.gohostarch == "ppc64le" { if gohostarch == "ppc64le" {
// linkmode=internal fails on ppc64le because cmd/link doesn't // linkmode=internal fails on ppc64le because cmd/link doesn't
// handle the TOC correctly (issue 15409). // handle the TOC correctly (issue 15409).
return false return false
} }
if t.goos == "android" { if goos == "android" {
return false return false
} }
if t.goos == "darwin" && (t.goarch == "arm" || t.goarch == "arm64") { if goos == "darwin" && (goarch == "arm" || goarch == "arm64") {
return false return false
} }
// Internally linking cgo is incomplete on some architectures. // Internally linking cgo is incomplete on some architectures.
// https://golang.org/issue/10373 // https://golang.org/issue/10373
// https://golang.org/issue/14449 // https://golang.org/issue/14449
if t.goarch == "arm64" || t.goarch == "mips64" || t.goarch == "mips64le" || t.goarch == "mips" || t.goarch == "mipsle" { if goarch == "arm64" || goarch == "mips64" || goarch == "mips64le" || goarch == "mips" || goarch == "mipsle" {
return false return false
} }
if isAlpineLinux() { if isAlpineLinux() {
@ -809,7 +792,7 @@ func (t *tester) internalLink() bool {
} }
func (t *tester) supportedBuildmode(mode string) bool { func (t *tester) supportedBuildmode(mode string) bool {
pair := t.goos + "-" + t.goarch pair := goos + "-" + goarch
switch mode { switch mode {
case "c-archive": case "c-archive":
if !t.extLink() { if !t.extLink() {
@ -864,9 +847,9 @@ func (t *tester) registerHostTest(name, heading, dir, pkg string) {
} }
func (t *tester) runHostTest(dir, pkg string) error { func (t *tester) runHostTest(dir, pkg string) error {
defer os.Remove(filepath.Join(t.goroot, dir, "test.test")) defer os.Remove(filepath.Join(goroot, dir, "test.test"))
cmd := t.dirCmd(dir, "go", "test", t.tags(), "-c", "-o", "test.test", pkg) cmd := t.dirCmd(dir, "go", "test", t.tags(), "-c", "-o", "test.test", pkg)
cmd.Env = append(os.Environ(), "GOARCH="+t.gohostarch, "GOOS="+t.gohostos) cmd.Env = append(os.Environ(), "GOARCH="+gohostarch, "GOOS="+gohostos)
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
return err return err
} }
@ -880,7 +863,7 @@ func (t *tester) cgoTest(dt *distTest) error {
t.addCmd(dt, "misc/cgo/test", "go", "test", "-ldflags", "-linkmode=internal", t.runFlag("")) t.addCmd(dt, "misc/cgo/test", "go", "test", "-ldflags", "-linkmode=internal", t.runFlag(""))
} }
pair := t.gohostos + "-" + t.goarch pair := gohostos + "-" + goarch
switch pair { switch pair {
case "darwin-386", "darwin-amd64", case "darwin-386", "darwin-amd64",
"openbsd-386", "openbsd-amd64", "openbsd-386", "openbsd-amd64",
@ -911,26 +894,25 @@ func (t *tester) cgoTest(dt *distTest) error {
// static linking on FreeBSD/ARM with clang. (cgo depends on // static linking on FreeBSD/ARM with clang. (cgo depends on
// -fPIC fundamentally.) // -fPIC fundamentally.)
default: default:
cc := mustEnv("CC")
cmd := t.dirCmd("misc/cgo/test", cmd := t.dirCmd("misc/cgo/test",
cc, "-xc", "-o", "/dev/null", "-static", "-") defaultcc, "-xc", "-o", "/dev/null", "-static", "-")
cmd.Stdin = strings.NewReader("int main() {}") cmd.Stdin = strings.NewReader("int main() {}")
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
fmt.Println("No support for static linking found (lacks libc.a?), skip cgo static linking test.") fmt.Println("No support for static linking found (lacks libc.a?), skip cgo static linking test.")
} else { } else {
if t.goos != "android" { if goos != "android" {
t.addCmd(dt, "misc/cgo/testtls", "go", "test", "-ldflags", `-linkmode=external -extldflags "-static -pthread"`) t.addCmd(dt, "misc/cgo/testtls", "go", "test", "-ldflags", `-linkmode=external -extldflags "-static -pthread"`)
} }
t.addCmd(dt, "misc/cgo/nocgo", "go", "test") t.addCmd(dt, "misc/cgo/nocgo", "go", "test")
t.addCmd(dt, "misc/cgo/nocgo", "go", "test", "-ldflags", `-linkmode=external`) t.addCmd(dt, "misc/cgo/nocgo", "go", "test", "-ldflags", `-linkmode=external`)
if t.goos != "android" { if goos != "android" {
t.addCmd(dt, "misc/cgo/nocgo", "go", "test", "-ldflags", `-linkmode=external -extldflags "-static -pthread"`) t.addCmd(dt, "misc/cgo/nocgo", "go", "test", "-ldflags", `-linkmode=external -extldflags "-static -pthread"`)
} }
} }
if pair != "freebsd-amd64" { // clang -pie fails to link misc/cgo/test if pair != "freebsd-amd64" { // clang -pie fails to link misc/cgo/test
cmd := t.dirCmd("misc/cgo/test", cmd := t.dirCmd("misc/cgo/test",
cc, "-xc", "-o", "/dev/null", "-pie", "-") defaultcc, "-xc", "-o", "/dev/null", "-pie", "-")
cmd.Stdin = strings.NewReader("int main() {}") cmd.Stdin = strings.NewReader("int main() {}")
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
fmt.Println("No support for -pie found, skip cgo PIE test.") fmt.Println("No support for -pie found, skip cgo PIE test.")
@ -1019,15 +1001,15 @@ func (t *tester) runPending(nextTest *distTest) {
} }
func (t *tester) cgoTestSOSupported() bool { func (t *tester) cgoTestSOSupported() bool {
if t.goos == "android" || t.iOS() { if goos == "android" || t.iOS() {
// No exec facility on Android or iOS. // No exec facility on Android or iOS.
return false return false
} }
if t.goarch == "ppc64" { if goarch == "ppc64" {
// External linking not implemented on ppc64 (issue #8912). // External linking not implemented on ppc64 (issue #8912).
return false return false
} }
if t.goarch == "mips64le" || t.goarch == "mips64" { if goarch == "mips64le" || goarch == "mips64" {
// External linking not implemented on mips64. // External linking not implemented on mips64.
return false return false
} }
@ -1037,7 +1019,7 @@ func (t *tester) cgoTestSOSupported() bool {
func (t *tester) cgoTestSO(dt *distTest, testpath string) error { func (t *tester) cgoTestSO(dt *distTest, testpath string) error {
t.runPending(dt) t.runPending(dt)
dir := filepath.Join(t.goroot, testpath) dir := filepath.Join(goroot, testpath)
// build shared object // build shared object
output, err := exec.Command("go", "env", "CC").Output() output, err := exec.Command("go", "env", "CC").Output()
@ -1056,7 +1038,7 @@ func (t *tester) cgoTestSO(dt *distTest, testpath string) error {
ext := "so" ext := "so"
args := append(gogccflags, "-shared") args := append(gogccflags, "-shared")
switch t.goos { switch goos {
case "darwin": case "darwin":
ext = "dylib" ext = "dylib"
args = append(args, "-undefined", "suppress", "-flat_namespace") args = append(args, "-undefined", "suppress", "-flat_namespace")
@ -1078,16 +1060,16 @@ func (t *tester) cgoTestSO(dt *distTest, testpath string) error {
defer os.Remove(filepath.Join(dir, "main.exe")) defer os.Remove(filepath.Join(dir, "main.exe"))
cmd := t.dirCmd(dir, "./main.exe") cmd := t.dirCmd(dir, "./main.exe")
if t.goos != "windows" { if goos != "windows" {
s := "LD_LIBRARY_PATH" s := "LD_LIBRARY_PATH"
if t.goos == "darwin" { if goos == "darwin" {
s = "DYLD_LIBRARY_PATH" s = "DYLD_LIBRARY_PATH"
} }
cmd.Env = append(os.Environ(), s+"=.") cmd.Env = append(os.Environ(), s+"=.")
// On FreeBSD 64-bit architectures, the 32-bit linker looks for // On FreeBSD 64-bit architectures, the 32-bit linker looks for
// different environment variables. // different environment variables.
if t.goos == "freebsd" && t.gohostarch == "386" { if goos == "freebsd" && gohostarch == "386" {
cmd.Env = append(cmd.Env, "LD_32_LIBRARY_PATH=.") cmd.Env = append(cmd.Env, "LD_32_LIBRARY_PATH=.")
} }
} }
@ -1095,7 +1077,7 @@ func (t *tester) cgoTestSO(dt *distTest, testpath string) error {
} }
func (t *tester) hasBash() bool { func (t *tester) hasBash() bool {
switch t.gohostos { switch gohostos {
case "windows", "plan9": case "windows", "plan9":
return false return false
} }
@ -1103,11 +1085,11 @@ func (t *tester) hasBash() bool {
} }
func (t *tester) raceDetectorSupported() bool { func (t *tester) raceDetectorSupported() bool {
switch t.gohostos { switch gohostos {
case "linux", "darwin", "freebsd", "windows": case "linux", "darwin", "freebsd", "windows":
// The race detector doesn't work on Alpine Linux: // The race detector doesn't work on Alpine Linux:
// golang.org/issue/14481 // golang.org/issue/14481
return t.cgoEnabled && t.goarch == "amd64" && t.gohostos == t.goos && !isAlpineLinux() return t.cgoEnabled && goarch == "amd64" && gohostos == goos && !isAlpineLinux()
} }
return false return false
} }
@ -1157,7 +1139,7 @@ func (t *tester) testDirTest(dt *distTest, shard, shards int) error {
runtest.Do(func() { runtest.Do(func() {
const exe = "runtest.exe" // named exe for Windows, but harmless elsewhere const exe = "runtest.exe" // named exe for Windows, but harmless elsewhere
cmd := t.dirCmd("test", "go", "build", "-o", exe, "run.go") cmd := t.dirCmd("test", "go", "build", "-o", exe, "run.go")
cmd.Env = append(os.Environ(), "GOOS="+t.gohostos, "GOARCH="+t.gohostarch) cmd.Env = append(os.Environ(), "GOOS="+gohostos, "GOARCH="+gohostarch)
runtest.exe = filepath.Join(cmd.Dir, exe) runtest.exe = filepath.Join(cmd.Dir, exe)
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
runtest.err = err runtest.err = err
@ -1197,7 +1179,7 @@ var funcBenchmark = []byte("\nfunc Benchmark")
// second or two per package, and this function returns false for // second or two per package, and this function returns false for
// about 100 packages. // about 100 packages.
func (t *tester) packageHasBenchmarks(pkg string) bool { func (t *tester) packageHasBenchmarks(pkg string) bool {
pkgDir := filepath.Join(t.goroot, "src", pkg) pkgDir := filepath.Join(goroot, "src", pkg)
d, err := os.Open(pkgDir) d, err := os.Open(pkgDir)
if err != nil { if err != nil {
return true // conservatively return true // conservatively