@@ -52,7 +52,6 @@ func init() {
52
52
53
53
var (
54
54
canRace = false // whether we can run the race detector
55
- canCgo = false // whether we can use cgo
56
55
canMSan = false // whether we can run the memory sanitizer
57
56
canASan = false // whether we can run the address sanitizer
58
57
)
@@ -236,11 +235,6 @@ func TestMain(m *testing.M) {
236
235
os .Setenv ("TESTGO_GOHOSTARCH" , goHostArch )
237
236
238
237
cgoEnabled = goEnv ("CGO_ENABLED" )
239
- canCgo , err = strconv .ParseBool (cgoEnabled )
240
- if err != nil {
241
- fmt .Fprintf (os .Stderr , "can't parse go env CGO_ENABLED output: %q\n " , strings .TrimSpace (cgoEnabled ))
242
- os .Exit (2 )
243
- }
244
238
245
239
// Duplicate the test executable into the path at testGo, for $PATH.
246
240
// If the OS supports symlinks, use them instead of copying bytes.
@@ -277,9 +271,9 @@ func TestMain(m *testing.M) {
277
271
}
278
272
testGOCACHE = strings .TrimSpace (string (out ))
279
273
280
- canMSan = canCgo && platform .MSanSupported (runtime .GOOS , runtime .GOARCH )
281
- canASan = canCgo && platform .ASanSupported (runtime .GOOS , runtime .GOARCH )
282
- canRace = canCgo && platform .RaceDetectorSupported (runtime .GOOS , runtime .GOARCH )
274
+ canMSan = testenv . HasCGO () && platform .MSanSupported (runtime .GOOS , runtime .GOARCH )
275
+ canASan = testenv . HasCGO () && platform .ASanSupported (runtime .GOOS , runtime .GOARCH )
276
+ canRace = testenv . HasCGO () && platform .RaceDetectorSupported (runtime .GOOS , runtime .GOARCH )
283
277
// The race detector doesn't work on Alpine Linux:
284
278
// golang.org/issue/14481
285
279
// gccgo does not support the race detector.
@@ -1082,6 +1076,7 @@ func TestPackageMainTestCompilerFlags(t *testing.T) {
1082
1076
// Issue 4104.
1083
1077
func TestGoTestWithPackageListedMultipleTimes (t * testing.T ) {
1084
1078
tooSlow (t , "links and runs a test" )
1079
+
1085
1080
tg := testgo (t )
1086
1081
defer tg .cleanup ()
1087
1082
tg .parallel ()
@@ -1093,6 +1088,7 @@ func TestGoTestWithPackageListedMultipleTimes(t *testing.T) {
1093
1088
1094
1089
func TestGoListHasAConsistentOrder (t * testing.T ) {
1095
1090
tooSlow (t , "walks all of GOROOT/src twice" )
1091
+
1096
1092
tg := testgo (t )
1097
1093
defer tg .cleanup ()
1098
1094
tg .parallel ()
@@ -1106,6 +1102,7 @@ func TestGoListHasAConsistentOrder(t *testing.T) {
1106
1102
1107
1103
func TestGoListStdDoesNotIncludeCommands (t * testing.T ) {
1108
1104
tooSlow (t , "walks all of GOROOT/src" )
1105
+
1109
1106
tg := testgo (t )
1110
1107
defer tg .cleanup ()
1111
1108
tg .parallel ()
@@ -1116,6 +1113,7 @@ func TestGoListStdDoesNotIncludeCommands(t *testing.T) {
1116
1113
func TestGoListCmdOnlyShowsCommands (t * testing.T ) {
1117
1114
skipIfGccgo (t , "gccgo does not have GOROOT" )
1118
1115
tooSlow (t , "walks all of GOROOT/src/cmd" )
1116
+
1119
1117
tg := testgo (t )
1120
1118
defer tg .cleanup ()
1121
1119
tg .parallel ()
@@ -1421,6 +1419,7 @@ func TestDefaultGOPATHPrintedSearchList(t *testing.T) {
1421
1419
func TestLdflagsArgumentsWithSpacesIssue3941 (t * testing.T ) {
1422
1420
skipIfGccgo (t , "gccgo does not support -ldflags -X" )
1423
1421
tooSlow (t , "compiles and links a binary" )
1422
+
1424
1423
tg := testgo (t )
1425
1424
defer tg .cleanup ()
1426
1425
tg .parallel ()
@@ -1438,6 +1437,7 @@ func TestLdFlagsLongArgumentsIssue42295(t *testing.T) {
1438
1437
// get encoded and passed correctly.
1439
1438
skipIfGccgo (t , "gccgo does not support -ldflags -X" )
1440
1439
tooSlow (t , "compiles and links a binary" )
1440
+
1441
1441
tg := testgo (t )
1442
1442
defer tg .cleanup ()
1443
1443
tg .parallel ()
@@ -1460,6 +1460,7 @@ func TestLdFlagsLongArgumentsIssue42295(t *testing.T) {
1460
1460
func TestGoTestDashCDashOControlsBinaryLocation (t * testing.T ) {
1461
1461
skipIfGccgo (t , "gccgo has no standard packages" )
1462
1462
tooSlow (t , "compiles and links a test binary" )
1463
+
1463
1464
tg := testgo (t )
1464
1465
defer tg .cleanup ()
1465
1466
tg .parallel ()
@@ -1471,6 +1472,7 @@ func TestGoTestDashCDashOControlsBinaryLocation(t *testing.T) {
1471
1472
func TestGoTestDashOWritesBinary (t * testing.T ) {
1472
1473
skipIfGccgo (t , "gccgo has no standard packages" )
1473
1474
tooSlow (t , "compiles and runs a test binary" )
1475
+
1474
1476
tg := testgo (t )
1475
1477
defer tg .cleanup ()
1476
1478
tg .parallel ()
@@ -1482,6 +1484,7 @@ func TestGoTestDashOWritesBinary(t *testing.T) {
1482
1484
// Issue 4515.
1483
1485
func TestInstallWithTags (t * testing.T ) {
1484
1486
tooSlow (t , "compiles and links binaries" )
1487
+
1485
1488
tg := testgo (t )
1486
1489
defer tg .cleanup ()
1487
1490
tg .parallel ()
@@ -1533,9 +1536,7 @@ func TestSymlinkWarning(t *testing.T) {
1533
1536
}
1534
1537
1535
1538
func TestCgoShowsFullPathNames (t * testing.T ) {
1536
- if ! canCgo {
1537
- t .Skip ("skipping because cgo not enabled" )
1538
- }
1539
+ testenv .MustHaveCGO (t )
1539
1540
1540
1541
tg := testgo (t )
1541
1542
defer tg .cleanup ()
@@ -1551,9 +1552,7 @@ func TestCgoShowsFullPathNames(t *testing.T) {
1551
1552
1552
1553
func TestCgoHandlesWlORIGIN (t * testing.T ) {
1553
1554
tooSlow (t , "compiles cgo files" )
1554
- if ! canCgo {
1555
- t .Skip ("skipping because cgo not enabled" )
1556
- }
1555
+ testenv .MustHaveCGO (t )
1557
1556
1558
1557
tg := testgo (t )
1559
1558
defer tg .cleanup ()
@@ -1569,9 +1568,8 @@ func TestCgoHandlesWlORIGIN(t *testing.T) {
1569
1568
1570
1569
func TestCgoPkgConfig (t * testing.T ) {
1571
1570
tooSlow (t , "compiles cgo files" )
1572
- if ! canCgo {
1573
- t .Skip ("skipping because cgo not enabled" )
1574
- }
1571
+ testenv .MustHaveCGO (t )
1572
+
1575
1573
tg := testgo (t )
1576
1574
defer tg .cleanup ()
1577
1575
tg .parallel ()
@@ -1821,13 +1819,11 @@ func TestImportLocal(t *testing.T) {
1821
1819
1822
1820
func TestGoInstallPkgdir (t * testing.T ) {
1823
1821
skipIfGccgo (t , "gccgo has no standard packages" )
1824
- if ! canCgo {
1825
- // Only the stdlib packages that use cgo have install
1826
- // targets, (we're using net below) so cgo is required
1827
- // for the install.
1828
- t .Skip ("skipping because cgo not enabled" )
1829
- }
1830
1822
tooSlow (t , "builds a package with cgo dependencies" )
1823
+ // Only the stdlib packages that use cgo have install
1824
+ // targets, (we're using net below) so cgo is required
1825
+ // for the install.
1826
+ testenv .MustHaveCGO (t )
1831
1827
1832
1828
tg := testgo (t )
1833
1829
tg .parallel ()
@@ -1843,6 +1839,7 @@ func TestGoInstallPkgdir(t *testing.T) {
1843
1839
// For issue 14337.
1844
1840
func TestParallelTest (t * testing.T ) {
1845
1841
tooSlow (t , "links and runs test binaries" )
1842
+
1846
1843
tg := testgo (t )
1847
1844
tg .parallel ()
1848
1845
defer tg .cleanup ()
@@ -2036,9 +2033,7 @@ GLOBL ·constants<>(SB),8,$8
2036
2033
2037
2034
// Issue 18975.
2038
2035
func TestFFLAGS (t * testing.T ) {
2039
- if ! canCgo {
2040
- t .Skip ("skipping because cgo not enabled" )
2041
- }
2036
+ testenv .MustHaveCGO (t )
2042
2037
2043
2038
tg := testgo (t )
2044
2039
defer tg .cleanup ()
@@ -2068,9 +2063,7 @@ func TestDuplicateGlobalAsmSymbols(t *testing.T) {
2068
2063
if runtime .GOARCH != "386" && runtime .GOARCH != "amd64" {
2069
2064
t .Skipf ("skipping test on %s" , runtime .GOARCH )
2070
2065
}
2071
- if ! canCgo {
2072
- t .Skip ("skipping because cgo not enabled" )
2073
- }
2066
+ testenv .MustHaveCGO (t )
2074
2067
2075
2068
tg := testgo (t )
2076
2069
defer tg .cleanup ()
@@ -2139,19 +2132,10 @@ func TestNeedVersion(t *testing.T) {
2139
2132
}
2140
2133
2141
2134
func TestBuildmodePIE (t * testing.T ) {
2142
- if testing .Short () && testenv .Builder () == "" {
2143
- t .Skipf ("skipping in -short mode on non-builder" )
2144
- }
2135
+ tooSlow (t , "links binaries" )
2145
2136
2146
- platform := fmt .Sprintf ("%s/%s" , runtime .GOOS , runtime .GOARCH )
2147
- switch platform {
2148
- case "linux/386" , "linux/amd64" , "linux/arm" , "linux/arm64" , "linux/ppc64le" , "linux/riscv64" , "linux/s390x" ,
2149
- "android/amd64" , "android/arm" , "android/arm64" , "android/386" ,
2150
- "freebsd/amd64" ,
2151
- "windows/386" , "windows/amd64" , "windows/arm" , "windows/arm64" :
2152
- case "darwin/amd64" :
2153
- default :
2154
- t .Skipf ("skipping test because buildmode=pie is not supported on %s" , platform )
2137
+ if ! platform .BuildModeSupported (runtime .Compiler , "pie" , runtime .GOOS , runtime .GOARCH ) {
2138
+ t .Skipf ("skipping test because buildmode=pie is not supported on %s/%s" , runtime .GOOS , runtime .GOARCH )
2155
2139
}
2156
2140
// Skip on alpine until https://go.dev/issues/54354 resolved.
2157
2141
if strings .HasSuffix (testenv .Builder (), "-alpine" ) {
@@ -2160,33 +2144,25 @@ func TestBuildmodePIE(t *testing.T) {
2160
2144
t .Run ("non-cgo" , func (t * testing.T ) {
2161
2145
testBuildmodePIE (t , false , true )
2162
2146
})
2163
- if canCgo {
2164
- switch runtime .GOOS {
2165
- case "darwin" , "freebsd" , "linux" , "windows" :
2166
- t .Run ("cgo" , func (t * testing.T ) {
2167
- testBuildmodePIE (t , true , true )
2168
- })
2169
- }
2170
- }
2147
+ t .Run ("cgo" , func (t * testing.T ) {
2148
+ testenv .MustHaveCGO (t )
2149
+ testBuildmodePIE (t , true , true )
2150
+ })
2171
2151
}
2172
2152
2173
2153
func TestWindowsDefaultBuildmodIsPIE (t * testing.T ) {
2174
- if testing .Short () && testenv .Builder () == "" {
2175
- t .Skipf ("skipping in -short mode on non-builder" )
2176
- }
2177
-
2178
2154
if runtime .GOOS != "windows" {
2179
2155
t .Skip ("skipping windows only test" )
2180
2156
}
2157
+ tooSlow (t , "links binaries" )
2181
2158
2182
2159
t .Run ("non-cgo" , func (t * testing.T ) {
2183
2160
testBuildmodePIE (t , false , false )
2184
2161
})
2185
- if canCgo {
2186
- t .Run ("cgo" , func (t * testing.T ) {
2187
- testBuildmodePIE (t , true , false )
2188
- })
2189
- }
2162
+ t .Run ("cgo" , func (t * testing.T ) {
2163
+ testenv .MustHaveCGO (t )
2164
+ testBuildmodePIE (t , true , false )
2165
+ })
2190
2166
}
2191
2167
2192
2168
func testBuildmodePIE (t * testing.T , useCgo , setBuildmodeToPIE bool ) {
@@ -2218,7 +2194,7 @@ func testBuildmodePIE(t *testing.T, useCgo, setBuildmodeToPIE bool) {
2218
2194
if f .Type != elf .ET_DYN {
2219
2195
t .Errorf ("PIE type must be ET_DYN, but %s" , f .Type )
2220
2196
}
2221
- case "darwin" :
2197
+ case "darwin" , "ios" :
2222
2198
f , err := macho .Open (obj )
2223
2199
if err != nil {
2224
2200
t .Fatal (err )
@@ -2290,7 +2266,9 @@ func testBuildmodePIE(t *testing.T, useCgo, setBuildmodeToPIE bool) {
2290
2266
}
2291
2267
}
2292
2268
default :
2293
- panic ("unreachable" )
2269
+ // testBuildmodePIE opens object files, so it needs to understand the object
2270
+ // file format.
2271
+ t .Skipf ("skipping test: test helper does not support %s" , runtime .GOOS )
2294
2272
}
2295
2273
2296
2274
out , err := testenv .Command (t , obj ).CombinedOutput ()
@@ -2481,10 +2459,10 @@ func TestIssue22596(t *testing.T) {
2481
2459
2482
2460
func TestTestCache (t * testing.T ) {
2483
2461
tooSlow (t , "links and runs test binaries" )
2484
-
2485
2462
if gocacheverify .Value () == "1" {
2486
2463
t .Skip ("GODEBUG gocacheverify" )
2487
2464
}
2465
+
2488
2466
tg := testgo (t )
2489
2467
defer tg .cleanup ()
2490
2468
tg .parallel ()
@@ -2756,9 +2734,7 @@ func TestBadCommandLines(t *testing.T) {
2756
2734
}
2757
2735
2758
2736
func TestTwoPkgConfigs (t * testing.T ) {
2759
- if ! canCgo {
2760
- t .Skip ("no cgo" )
2761
- }
2737
+ testenv .MustHaveCGO (t )
2762
2738
if runtime .GOOS == "windows" || runtime .GOOS == "plan9" {
2763
2739
t .Skipf ("no shell scripts on %s" , runtime .GOOS )
2764
2740
}
@@ -2791,9 +2767,7 @@ echo $* >>`+tg.path("pkg-config.out"))
2791
2767
}
2792
2768
2793
2769
func TestCgoCache (t * testing.T ) {
2794
- if ! canCgo {
2795
- t .Skip ("no cgo" )
2796
- }
2770
+ testenv .MustHaveCGO (t )
2797
2771
tooSlow (t , "builds a package with cgo dependencies" )
2798
2772
2799
2773
tg := testgo (t )
@@ -2844,9 +2818,7 @@ func TestDontReportRemoveOfEmptyDir(t *testing.T) {
2844
2818
// Issue 24704.
2845
2819
func TestLinkerTmpDirIsDeleted (t * testing.T ) {
2846
2820
skipIfGccgo (t , "gccgo does not use cmd/link" )
2847
- if ! canCgo {
2848
- t .Skip ("skipping because cgo not enabled" )
2849
- }
2821
+ testenv .MustHaveCGO (t )
2850
2822
tooSlow (t , "builds a package with cgo dependencies" )
2851
2823
2852
2824
tg := testgo (t )
0 commit comments