Skip to content

Commit d8cbc2c

Browse files
committed
misc/cgo/testcarchive: do not use same executable name in TestInstall
Fixes #17439 Change-Id: I7caa28519f38692f9ca306f0789cbb975fa1d7c4 Reviewed-on: https://go-review.googlesource.com/31112 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Run-TryBot: Alex Brainman <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 42f5ee4 commit d8cbc2c

File tree

1 file changed

+35
-58
lines changed

1 file changed

+35
-58
lines changed

misc/cgo/testcarchive/carchive_test.go

Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,9 @@ var GOOS, GOARCH string
3535
var libgodir string
3636

3737
func init() {
38-
bin = []string{"./testp"}
3938
GOOS = goEnv("GOOS")
4039
GOARCH = goEnv("GOARCH")
41-
execScript := "go_" + GOOS + "_" + GOARCH + "_exec"
42-
if executor, err := exec.LookPath(execScript); err == nil {
43-
bin = []string{executor, "./testp"}
44-
}
40+
bin = cmdToRun("./testp")
4541

4642
ccOut := goEnv("CC")
4743
cc = []string{string(ccOut)}
@@ -126,81 +122,62 @@ func goEnv(key string) string {
126122
return strings.TrimSpace(string(out))
127123
}
128124

129-
func compilemain(t *testing.T, libgo string) {
130-
ccArgs := append(cc, "-o", "testp"+exeSuffix, "main.c")
131-
if GOOS == "windows" {
132-
ccArgs = append(ccArgs, "main_windows.c", libgo, "-lntdll", "-lws2_32", "-lwinmm")
133-
} else {
134-
ccArgs = append(ccArgs, "main_unix.c", libgo)
135-
}
136-
t.Log(ccArgs)
137-
138-
if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil {
139-
t.Logf("%s", out)
140-
t.Fatal(err)
125+
func cmdToRun(name string) []string {
126+
execScript := "go_" + goEnv("GOOS") + "_" + goEnv("GOARCH") + "_exec"
127+
executor, err := exec.LookPath(execScript)
128+
if err != nil {
129+
return []string{name}
141130
}
131+
return []string{executor, name}
142132
}
143133

144-
func TestInstall(t *testing.T) {
145-
defer func() {
146-
os.Remove("libgo.a")
147-
os.Remove("libgo.h")
148-
os.Remove("testp")
149-
os.RemoveAll("pkg")
150-
}()
151-
152-
cmd := exec.Command("go", "install", "-buildmode=c-archive", "libgo")
134+
func testInstall(t *testing.T, exe, libgoa, libgoh string, buildcmd ...string) {
135+
cmd := exec.Command(buildcmd[0], buildcmd[1:]...)
153136
cmd.Env = gopathEnv
154137
if out, err := cmd.CombinedOutput(); err != nil {
155138
t.Logf("%s", out)
156139
t.Fatal(err)
157140
}
141+
defer func() {
142+
os.Remove(libgoa)
143+
os.Remove(libgoh)
144+
}()
158145

159-
compilemain(t, filepath.Join("pkg", libgodir, "libgo.a"))
160-
161-
binArgs := append(bin, "arg1", "arg2")
162-
if out, err := exec.Command(binArgs[0], binArgs[1:]...).CombinedOutput(); err != nil {
163-
t.Logf("%s", out)
164-
t.Fatal(err)
146+
ccArgs := append(cc, "-o", exe, "main.c")
147+
if GOOS == "windows" {
148+
ccArgs = append(ccArgs, "main_windows.c", libgoa, "-lntdll", "-lws2_32", "-lwinmm")
149+
} else {
150+
ccArgs = append(ccArgs, "main_unix.c", libgoa)
165151
}
166-
167-
os.Remove("libgo.a")
168-
os.Remove("libgo.h")
169-
os.Remove("testp")
170-
171-
// Test building libgo other than installing it.
172-
// Header files are now present.
173-
cmd = exec.Command("go", "build", "-buildmode=c-archive", filepath.Join("src", "libgo", "libgo.go"))
174-
cmd.Env = gopathEnv
175-
if out, err := cmd.CombinedOutput(); err != nil {
152+
t.Log(ccArgs)
153+
if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil {
176154
t.Logf("%s", out)
177155
t.Fatal(err)
178156
}
157+
defer os.Remove(exe)
179158

180-
compilemain(t, "libgo.a")
181-
159+
binArgs := append(cmdToRun(exe), "arg1", "arg2")
182160
if out, err := exec.Command(binArgs[0], binArgs[1:]...).CombinedOutput(); err != nil {
183161
t.Logf("%s", out)
184162
t.Fatal(err)
185163
}
164+
}
186165

187-
os.Remove("libgo.a")
188-
os.Remove("libgo.h")
189-
os.Remove("testp")
166+
func TestInstall(t *testing.T) {
167+
defer os.RemoveAll("pkg")
190168

191-
cmd = exec.Command("go", "build", "-buildmode=c-archive", "-o", "libgo.a", "libgo")
192-
cmd.Env = gopathEnv
193-
if out, err := cmd.CombinedOutput(); err != nil {
194-
t.Logf("%s", out)
195-
t.Fatal(err)
196-
}
169+
testInstall(t, "./testp1"+exeSuffix,
170+
filepath.Join("pkg", libgodir, "libgo.a"),
171+
filepath.Join("pkg", libgodir, "libgo.h"),
172+
"go", "install", "-buildmode=c-archive", "libgo")
197173

198-
compilemain(t, "libgo.a")
174+
// Test building libgo other than installing it.
175+
// Header files are now present.
176+
testInstall(t, "./testp2"+exeSuffix, "libgo.a", "libgo.h",
177+
"go", "build", "-buildmode=c-archive", filepath.Join("src", "libgo", "libgo.go"))
199178

200-
if out, err := exec.Command(binArgs[0], binArgs[1:]...).CombinedOutput(); err != nil {
201-
t.Logf("%s", out)
202-
t.Fatal(err)
203-
}
179+
testInstall(t, "./testp3"+exeSuffix, "libgo.a", "libgo.h",
180+
"go", "build", "-buildmode=c-archive", "-o", "libgo.a", "libgo")
204181
}
205182

206183
func TestEarlySignalHandler(t *testing.T) {

0 commit comments

Comments
 (0)