"os"
"os/exec"
"path/filepath"
- "runtime"
"strings"
"syscall"
"testing"
// Program to run.
var bin []string
-// C compiler wiht args (from $(go env CC) $(go env GOGCCFLAGS)).
+// C compiler with args (from $(go env CC) $(go env GOGCCFLAGS)).
var cc []string
// An environment with GOPATH=$(pwd).
// ".exe" on Windows.
var exeSuffix string
+var GOOS, GOARCH string
+
func init() {
bin = []string{"./testp"}
- execScript := "go_" + runtime.GOOS + "_" + runtime.GOARCH + "_exec"
+ GOOS = goEnv("GOOS")
+ GOARCH = goEnv("GOARCH")
+ execScript := "go_" + GOOS + "_" + GOARCH + "_exec"
if executor, err := exec.LookPath(execScript); err == nil {
bin = []string{executor, "./testp"}
}
- out, err := exec.Command("go", "env", "CC").Output()
- if err != nil {
- fmt.Fprintf(os.Stderr, "go env CC failed:\n%s", err)
- fmt.Fprintf(os.Stderr, "%s", err.(*exec.ExitError).Stderr)
- os.Exit(2)
- }
- cc = []string{strings.TrimSpace(string(out))}
+ ccOut := goEnv("CC")
+ cc = []string{string(ccOut)}
- out, err = exec.Command("go", "env", "GOGCCFLAGS").Output()
- if err != nil {
- fmt.Fprintf(os.Stderr, "go env GOGCCFLAGS failed:\n%s", err)
- fmt.Fprintf(os.Stderr, "%s", err.(*exec.ExitError).Stderr)
- os.Exit(2)
- }
+ out := goEnv("GOGCCFLAGS")
quote := '\000'
start := 0
lastSpace := true
cc = append(cc, s[start:])
}
- if runtime.GOOS == "darwin" {
+ if GOOS == "darwin" {
cc = append(cc, "-Wl,-no_pie")
// For Darwin/ARM.
// TODO(crawshaw): can we do better?
cc = append(cc, []string{"-framework", "CoreFoundation", "-framework", "Foundation"}...)
}
- cc = append(cc, "-I", filepath.Join("pkg", runtime.GOOS+"_"+runtime.GOARCH))
+ cc = append(cc, "-I", filepath.Join("pkg", GOOS+"_"+GOARCH))
// Build an environment with GOPATH=$(pwd)
env := os.Environ()
n = append(n, "GOPATH="+dir)
gopathEnv = n
- if runtime.GOOS == "windows" {
+ if GOOS == "windows" {
exeSuffix = ".exe"
}
}
+func goEnv(key string) string {
+ out, err := exec.Command("go", "env", key).Output()
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "go env %s failed:\n%s", key, err)
+ fmt.Fprintf(os.Stderr, "%s", err.(*exec.ExitError).Stderr)
+ os.Exit(2)
+ }
+ return strings.TrimSpace(string(out))
+}
+
func TestInstall(t *testing.T) {
defer func() {
os.Remove("libgo.a")
t.Fatal(err)
}
- ccArgs := append(cc, "-o", "testp"+exeSuffix, "main.c", filepath.Join("pkg", runtime.GOOS+"_"+runtime.GOARCH, "libgo.a"))
+ ccArgs := append(cc, "-o", "testp"+exeSuffix, "main.c", filepath.Join("pkg", GOOS+"_"+GOARCH, "libgo.a"))
if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil {
t.Logf("%s", out)
t.Fatal(err)
}
func TestEarlySignalHandler(t *testing.T) {
- switch runtime.GOOS {
+ switch GOOS {
case "darwin":
- switch runtime.GOARCH {
+ switch GOARCH {
case "arm", "arm64":
- t.Skipf("skipping on %s/%s; see https://golang.org/issue/13701", runtime.GOOS, runtime.GOARCH)
+ t.Skipf("skipping on %s/%s; see https://golang.org/issue/13701", GOOS, GOARCH)
}
case "windows":
t.Skip("skipping signal test on Windows")
}
func TestOsSignal(t *testing.T) {
- switch runtime.GOOS {
+ switch GOOS {
case "windows":
t.Skip("skipping signal test on Windows")
}
}
func TestSigaltstack(t *testing.T) {
- switch runtime.GOOS {
+ switch GOOS {
case "windows":
t.Skip("skipping signal test on Windows")
}
`
func TestExtar(t *testing.T) {
- switch runtime.GOOS {
+ switch GOOS {
case "windows":
t.Skip("skipping signal test on Windows")
}
},
})
}
- if t.supportedBuildmode("c-archive") && t.goos != "android" && !t.iOS() {
- // TODO(elias.naur): reenable on android and iOS
- // golang.org/issue/8345
- t.registerTest("testcarchive", "../misc/cgo/testcarchive", "go", "test", "carchive_test.go")
+ if t.supportedBuildmode("c-archive") {
+ t.registerHostTest("testcarchive", "misc/cgo/testcarchive", "carchive_test.go")
}
if t.supportedBuildmode("c-shared") {
t.registerTest("testcshared", "../misc/cgo/testcshared", "./test.bash")
}
}
+func (t *tester) registerHostTest(name, dirBanner, pkg string) {
+ t.tests = append(t.tests, distTest{
+ name: name,
+ heading: dirBanner,
+ fn: func(dt *distTest) error {
+ return t.runHostTest(dirBanner, pkg)
+ },
+ })
+}
+
+func (t *tester) runHostTest(dirBanner, pkg string) error {
+ env := mergeEnvLists([]string{"GOARCH=" + t.gohostarch, "GOOS=" + t.gohostos}, os.Environ())
+ defer os.Remove(filepath.Join(t.goroot, dirBanner, "test.test"))
+ cmd := t.dirCmd(dirBanner, "go", "test", t.tags(), "-c", "-o", "test.test", pkg)
+ cmd.Env = env
+ if err := cmd.Run(); err != nil {
+ return err
+ }
+ return t.dirCmd(dirBanner, "./test.test").Run()
+}
+
func (t *tester) cgoTest(dt *distTest) error {
env := mergeEnvLists([]string{"GOTRACEBACK=2"}, os.Environ())