]> Cypherpunks repositories - gostls13.git/commitdiff
misc/cgo/testcarchive: do not use same executable name in TestInstall
authorAlex Brainman <alex.brainman@gmail.com>
Fri, 14 Oct 2016 06:03:01 +0000 (17:03 +1100)
committerAlex Brainman <alex.brainman@gmail.com>
Mon, 17 Oct 2016 00:34:12 +0000 (00:34 +0000)
Fixes #17439

Change-Id: I7caa28519f38692f9ca306f0789cbb975fa1d7c4
Reviewed-on: https://go-review.googlesource.com/31112
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

misc/cgo/testcarchive/carchive_test.go

index 14de439bcee2a269cf5e0c57ea9f4f506e99ecd6..499992977517ea7aacdcf86aa437562950696fac 100644 (file)
@@ -35,13 +35,9 @@ var GOOS, GOARCH string
 var libgodir string
 
 func init() {
-       bin = []string{"./testp"}
        GOOS = goEnv("GOOS")
        GOARCH = goEnv("GOARCH")
-       execScript := "go_" + GOOS + "_" + GOARCH + "_exec"
-       if executor, err := exec.LookPath(execScript); err == nil {
-               bin = []string{executor, "./testp"}
-       }
+       bin = cmdToRun("./testp")
 
        ccOut := goEnv("CC")
        cc = []string{string(ccOut)}
@@ -126,81 +122,62 @@ func goEnv(key string) string {
        return strings.TrimSpace(string(out))
 }
 
-func compilemain(t *testing.T, libgo string) {
-       ccArgs := append(cc, "-o", "testp"+exeSuffix, "main.c")
-       if GOOS == "windows" {
-               ccArgs = append(ccArgs, "main_windows.c", libgo, "-lntdll", "-lws2_32", "-lwinmm")
-       } else {
-               ccArgs = append(ccArgs, "main_unix.c", libgo)
-       }
-       t.Log(ccArgs)
-
-       if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil {
-               t.Logf("%s", out)
-               t.Fatal(err)
+func cmdToRun(name string) []string {
+       execScript := "go_" + goEnv("GOOS") + "_" + goEnv("GOARCH") + "_exec"
+       executor, err := exec.LookPath(execScript)
+       if err != nil {
+               return []string{name}
        }
+       return []string{executor, name}
 }
 
-func TestInstall(t *testing.T) {
-       defer func() {
-               os.Remove("libgo.a")
-               os.Remove("libgo.h")
-               os.Remove("testp")
-               os.RemoveAll("pkg")
-       }()
-
-       cmd := exec.Command("go", "install", "-buildmode=c-archive", "libgo")
+func testInstall(t *testing.T, exe, libgoa, libgoh string, buildcmd ...string) {
+       cmd := exec.Command(buildcmd[0], buildcmd[1:]...)
        cmd.Env = gopathEnv
        if out, err := cmd.CombinedOutput(); err != nil {
                t.Logf("%s", out)
                t.Fatal(err)
        }
+       defer func() {
+               os.Remove(libgoa)
+               os.Remove(libgoh)
+       }()
 
-       compilemain(t, filepath.Join("pkg", libgodir, "libgo.a"))
-
-       binArgs := append(bin, "arg1", "arg2")
-       if out, err := exec.Command(binArgs[0], binArgs[1:]...).CombinedOutput(); err != nil {
-               t.Logf("%s", out)
-               t.Fatal(err)
+       ccArgs := append(cc, "-o", exe, "main.c")
+       if GOOS == "windows" {
+               ccArgs = append(ccArgs, "main_windows.c", libgoa, "-lntdll", "-lws2_32", "-lwinmm")
+       } else {
+               ccArgs = append(ccArgs, "main_unix.c", libgoa)
        }
-
-       os.Remove("libgo.a")
-       os.Remove("libgo.h")
-       os.Remove("testp")
-
-       // Test building libgo other than installing it.
-       // Header files are now present.
-       cmd = exec.Command("go", "build", "-buildmode=c-archive", filepath.Join("src", "libgo", "libgo.go"))
-       cmd.Env = gopathEnv
-       if out, err := cmd.CombinedOutput(); err != nil {
+       t.Log(ccArgs)
+       if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil {
                t.Logf("%s", out)
                t.Fatal(err)
        }
+       defer os.Remove(exe)
 
-       compilemain(t, "libgo.a")
-
+       binArgs := append(cmdToRun(exe), "arg1", "arg2")
        if out, err := exec.Command(binArgs[0], binArgs[1:]...).CombinedOutput(); err != nil {
                t.Logf("%s", out)
                t.Fatal(err)
        }
+}
 
-       os.Remove("libgo.a")
-       os.Remove("libgo.h")
-       os.Remove("testp")
+func TestInstall(t *testing.T) {
+       defer os.RemoveAll("pkg")
 
-       cmd = exec.Command("go", "build", "-buildmode=c-archive", "-o", "libgo.a", "libgo")
-       cmd.Env = gopathEnv
-       if out, err := cmd.CombinedOutput(); err != nil {
-               t.Logf("%s", out)
-               t.Fatal(err)
-       }
+       testInstall(t, "./testp1"+exeSuffix,
+               filepath.Join("pkg", libgodir, "libgo.a"),
+               filepath.Join("pkg", libgodir, "libgo.h"),
+               "go", "install", "-buildmode=c-archive", "libgo")
 
-       compilemain(t, "libgo.a")
+       // Test building libgo other than installing it.
+       // Header files are now present.
+       testInstall(t, "./testp2"+exeSuffix, "libgo.a", "libgo.h",
+               "go", "build", "-buildmode=c-archive", filepath.Join("src", "libgo", "libgo.go"))
 
-       if out, err := exec.Command(binArgs[0], binArgs[1:]...).CombinedOutput(); err != nil {
-               t.Logf("%s", out)
-               t.Fatal(err)
-       }
+       testInstall(t, "./testp3"+exeSuffix, "libgo.a", "libgo.h",
+               "go", "build", "-buildmode=c-archive", "-o", "libgo.a", "libgo")
 }
 
 func TestEarlySignalHandler(t *testing.T) {