@@ -35,13 +35,9 @@ var GOOS, GOARCH string
35
35
var libgodir string
36
36
37
37
func init () {
38
- bin = []string {"./testp" }
39
38
GOOS = goEnv ("GOOS" )
40
39
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" )
45
41
46
42
ccOut := goEnv ("CC" )
47
43
cc = []string {string (ccOut )}
@@ -126,81 +122,62 @@ func goEnv(key string) string {
126
122
return strings .TrimSpace (string (out ))
127
123
}
128
124
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 }
141
130
}
131
+ return []string {executor , name }
142
132
}
143
133
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 :]... )
153
136
cmd .Env = gopathEnv
154
137
if out , err := cmd .CombinedOutput (); err != nil {
155
138
t .Logf ("%s" , out )
156
139
t .Fatal (err )
157
140
}
141
+ defer func () {
142
+ os .Remove (libgoa )
143
+ os .Remove (libgoh )
144
+ }()
158
145
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 )
165
151
}
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 {
176
154
t .Logf ("%s" , out )
177
155
t .Fatal (err )
178
156
}
157
+ defer os .Remove (exe )
179
158
180
- compilemain (t , "libgo.a" )
181
-
159
+ binArgs := append (cmdToRun (exe ), "arg1" , "arg2" )
182
160
if out , err := exec .Command (binArgs [0 ], binArgs [1 :]... ).CombinedOutput (); err != nil {
183
161
t .Logf ("%s" , out )
184
162
t .Fatal (err )
185
163
}
164
+ }
186
165
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" )
190
168
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" )
197
173
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" ))
199
178
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" )
204
181
}
205
182
206
183
func TestEarlySignalHandler (t * testing.T ) {
0 commit comments