Skip to content

Commit 73ca5c0

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
cmd/go: simplify cgo and buildmode checks in tests
Change-Id: I0d6e49226a8708cc5f6ed3bea7658bec202a7ae7 Reviewed-on: https://go-review.googlesource.com/c/go/+/474138 Auto-Submit: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 9f532dd commit 73ca5c0

File tree

2 files changed

+45
-73
lines changed

2 files changed

+45
-73
lines changed

src/cmd/go/go_test.go

Lines changed: 43 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func init() {
5252

5353
var (
5454
canRace = false // whether we can run the race detector
55-
canCgo = false // whether we can use cgo
5655
canMSan = false // whether we can run the memory sanitizer
5756
canASan = false // whether we can run the address sanitizer
5857
)
@@ -236,11 +235,6 @@ func TestMain(m *testing.M) {
236235
os.Setenv("TESTGO_GOHOSTARCH", goHostArch)
237236

238237
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-
}
244238

245239
// Duplicate the test executable into the path at testGo, for $PATH.
246240
// If the OS supports symlinks, use them instead of copying bytes.
@@ -277,9 +271,9 @@ func TestMain(m *testing.M) {
277271
}
278272
testGOCACHE = strings.TrimSpace(string(out))
279273

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)
283277
// The race detector doesn't work on Alpine Linux:
284278
// golang.org/issue/14481
285279
// gccgo does not support the race detector.
@@ -1082,6 +1076,7 @@ func TestPackageMainTestCompilerFlags(t *testing.T) {
10821076
// Issue 4104.
10831077
func TestGoTestWithPackageListedMultipleTimes(t *testing.T) {
10841078
tooSlow(t, "links and runs a test")
1079+
10851080
tg := testgo(t)
10861081
defer tg.cleanup()
10871082
tg.parallel()
@@ -1093,6 +1088,7 @@ func TestGoTestWithPackageListedMultipleTimes(t *testing.T) {
10931088

10941089
func TestGoListHasAConsistentOrder(t *testing.T) {
10951090
tooSlow(t, "walks all of GOROOT/src twice")
1091+
10961092
tg := testgo(t)
10971093
defer tg.cleanup()
10981094
tg.parallel()
@@ -1106,6 +1102,7 @@ func TestGoListHasAConsistentOrder(t *testing.T) {
11061102

11071103
func TestGoListStdDoesNotIncludeCommands(t *testing.T) {
11081104
tooSlow(t, "walks all of GOROOT/src")
1105+
11091106
tg := testgo(t)
11101107
defer tg.cleanup()
11111108
tg.parallel()
@@ -1116,6 +1113,7 @@ func TestGoListStdDoesNotIncludeCommands(t *testing.T) {
11161113
func TestGoListCmdOnlyShowsCommands(t *testing.T) {
11171114
skipIfGccgo(t, "gccgo does not have GOROOT")
11181115
tooSlow(t, "walks all of GOROOT/src/cmd")
1116+
11191117
tg := testgo(t)
11201118
defer tg.cleanup()
11211119
tg.parallel()
@@ -1421,6 +1419,7 @@ func TestDefaultGOPATHPrintedSearchList(t *testing.T) {
14211419
func TestLdflagsArgumentsWithSpacesIssue3941(t *testing.T) {
14221420
skipIfGccgo(t, "gccgo does not support -ldflags -X")
14231421
tooSlow(t, "compiles and links a binary")
1422+
14241423
tg := testgo(t)
14251424
defer tg.cleanup()
14261425
tg.parallel()
@@ -1438,6 +1437,7 @@ func TestLdFlagsLongArgumentsIssue42295(t *testing.T) {
14381437
// get encoded and passed correctly.
14391438
skipIfGccgo(t, "gccgo does not support -ldflags -X")
14401439
tooSlow(t, "compiles and links a binary")
1440+
14411441
tg := testgo(t)
14421442
defer tg.cleanup()
14431443
tg.parallel()
@@ -1460,6 +1460,7 @@ func TestLdFlagsLongArgumentsIssue42295(t *testing.T) {
14601460
func TestGoTestDashCDashOControlsBinaryLocation(t *testing.T) {
14611461
skipIfGccgo(t, "gccgo has no standard packages")
14621462
tooSlow(t, "compiles and links a test binary")
1463+
14631464
tg := testgo(t)
14641465
defer tg.cleanup()
14651466
tg.parallel()
@@ -1471,6 +1472,7 @@ func TestGoTestDashCDashOControlsBinaryLocation(t *testing.T) {
14711472
func TestGoTestDashOWritesBinary(t *testing.T) {
14721473
skipIfGccgo(t, "gccgo has no standard packages")
14731474
tooSlow(t, "compiles and runs a test binary")
1475+
14741476
tg := testgo(t)
14751477
defer tg.cleanup()
14761478
tg.parallel()
@@ -1482,6 +1484,7 @@ func TestGoTestDashOWritesBinary(t *testing.T) {
14821484
// Issue 4515.
14831485
func TestInstallWithTags(t *testing.T) {
14841486
tooSlow(t, "compiles and links binaries")
1487+
14851488
tg := testgo(t)
14861489
defer tg.cleanup()
14871490
tg.parallel()
@@ -1533,9 +1536,7 @@ func TestSymlinkWarning(t *testing.T) {
15331536
}
15341537

15351538
func TestCgoShowsFullPathNames(t *testing.T) {
1536-
if !canCgo {
1537-
t.Skip("skipping because cgo not enabled")
1538-
}
1539+
testenv.MustHaveCGO(t)
15391540

15401541
tg := testgo(t)
15411542
defer tg.cleanup()
@@ -1551,9 +1552,7 @@ func TestCgoShowsFullPathNames(t *testing.T) {
15511552

15521553
func TestCgoHandlesWlORIGIN(t *testing.T) {
15531554
tooSlow(t, "compiles cgo files")
1554-
if !canCgo {
1555-
t.Skip("skipping because cgo not enabled")
1556-
}
1555+
testenv.MustHaveCGO(t)
15571556

15581557
tg := testgo(t)
15591558
defer tg.cleanup()
@@ -1569,9 +1568,8 @@ func TestCgoHandlesWlORIGIN(t *testing.T) {
15691568

15701569
func TestCgoPkgConfig(t *testing.T) {
15711570
tooSlow(t, "compiles cgo files")
1572-
if !canCgo {
1573-
t.Skip("skipping because cgo not enabled")
1574-
}
1571+
testenv.MustHaveCGO(t)
1572+
15751573
tg := testgo(t)
15761574
defer tg.cleanup()
15771575
tg.parallel()
@@ -1821,13 +1819,11 @@ func TestImportLocal(t *testing.T) {
18211819

18221820
func TestGoInstallPkgdir(t *testing.T) {
18231821
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-
}
18301822
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)
18311827

18321828
tg := testgo(t)
18331829
tg.parallel()
@@ -1843,6 +1839,7 @@ func TestGoInstallPkgdir(t *testing.T) {
18431839
// For issue 14337.
18441840
func TestParallelTest(t *testing.T) {
18451841
tooSlow(t, "links and runs test binaries")
1842+
18461843
tg := testgo(t)
18471844
tg.parallel()
18481845
defer tg.cleanup()
@@ -2036,9 +2033,7 @@ GLOBL ·constants<>(SB),8,$8
20362033

20372034
// Issue 18975.
20382035
func TestFFLAGS(t *testing.T) {
2039-
if !canCgo {
2040-
t.Skip("skipping because cgo not enabled")
2041-
}
2036+
testenv.MustHaveCGO(t)
20422037

20432038
tg := testgo(t)
20442039
defer tg.cleanup()
@@ -2068,9 +2063,7 @@ func TestDuplicateGlobalAsmSymbols(t *testing.T) {
20682063
if runtime.GOARCH != "386" && runtime.GOARCH != "amd64" {
20692064
t.Skipf("skipping test on %s", runtime.GOARCH)
20702065
}
2071-
if !canCgo {
2072-
t.Skip("skipping because cgo not enabled")
2073-
}
2066+
testenv.MustHaveCGO(t)
20742067

20752068
tg := testgo(t)
20762069
defer tg.cleanup()
@@ -2139,19 +2132,10 @@ func TestNeedVersion(t *testing.T) {
21392132
}
21402133

21412134
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")
21452136

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)
21552139
}
21562140
// Skip on alpine until https://go.dev/issues/54354 resolved.
21572141
if strings.HasSuffix(testenv.Builder(), "-alpine") {
@@ -2160,33 +2144,25 @@ func TestBuildmodePIE(t *testing.T) {
21602144
t.Run("non-cgo", func(t *testing.T) {
21612145
testBuildmodePIE(t, false, true)
21622146
})
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+
})
21712151
}
21722152

21732153
func TestWindowsDefaultBuildmodIsPIE(t *testing.T) {
2174-
if testing.Short() && testenv.Builder() == "" {
2175-
t.Skipf("skipping in -short mode on non-builder")
2176-
}
2177-
21782154
if runtime.GOOS != "windows" {
21792155
t.Skip("skipping windows only test")
21802156
}
2157+
tooSlow(t, "links binaries")
21812158

21822159
t.Run("non-cgo", func(t *testing.T) {
21832160
testBuildmodePIE(t, false, false)
21842161
})
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+
})
21902166
}
21912167

21922168
func testBuildmodePIE(t *testing.T, useCgo, setBuildmodeToPIE bool) {
@@ -2218,7 +2194,7 @@ func testBuildmodePIE(t *testing.T, useCgo, setBuildmodeToPIE bool) {
22182194
if f.Type != elf.ET_DYN {
22192195
t.Errorf("PIE type must be ET_DYN, but %s", f.Type)
22202196
}
2221-
case "darwin":
2197+
case "darwin", "ios":
22222198
f, err := macho.Open(obj)
22232199
if err != nil {
22242200
t.Fatal(err)
@@ -2290,7 +2266,9 @@ func testBuildmodePIE(t *testing.T, useCgo, setBuildmodeToPIE bool) {
22902266
}
22912267
}
22922268
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)
22942272
}
22952273

22962274
out, err := testenv.Command(t, obj).CombinedOutput()
@@ -2481,10 +2459,10 @@ func TestIssue22596(t *testing.T) {
24812459

24822460
func TestTestCache(t *testing.T) {
24832461
tooSlow(t, "links and runs test binaries")
2484-
24852462
if gocacheverify.Value() == "1" {
24862463
t.Skip("GODEBUG gocacheverify")
24872464
}
2465+
24882466
tg := testgo(t)
24892467
defer tg.cleanup()
24902468
tg.parallel()
@@ -2756,9 +2734,7 @@ func TestBadCommandLines(t *testing.T) {
27562734
}
27572735

27582736
func TestTwoPkgConfigs(t *testing.T) {
2759-
if !canCgo {
2760-
t.Skip("no cgo")
2761-
}
2737+
testenv.MustHaveCGO(t)
27622738
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
27632739
t.Skipf("no shell scripts on %s", runtime.GOOS)
27642740
}
@@ -2791,9 +2767,7 @@ echo $* >>`+tg.path("pkg-config.out"))
27912767
}
27922768

27932769
func TestCgoCache(t *testing.T) {
2794-
if !canCgo {
2795-
t.Skip("no cgo")
2796-
}
2770+
testenv.MustHaveCGO(t)
27972771
tooSlow(t, "builds a package with cgo dependencies")
27982772

27992773
tg := testgo(t)
@@ -2844,9 +2818,7 @@ func TestDontReportRemoveOfEmptyDir(t *testing.T) {
28442818
// Issue 24704.
28452819
func TestLinkerTmpDirIsDeleted(t *testing.T) {
28462820
skipIfGccgo(t, "gccgo does not use cmd/link")
2847-
if !canCgo {
2848-
t.Skip("skipping because cgo not enabled")
2849-
}
2821+
testenv.MustHaveCGO(t)
28502822
tooSlow(t, "builds a package with cgo dependencies")
28512823

28522824
tg := testgo(t)

src/cmd/go/scriptconds_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func scriptConditions() map[string]script.Cond {
3939
add("asan", sysCondition("-asan", platform.ASanSupported, true))
4040
add("buildmode", script.PrefixCondition("go supports -buildmode=<suffix>", hasBuildmode))
4141
add("case-sensitive", script.OnceCondition("$WORK filesystem is case-sensitive", isCaseSensitive))
42-
add("cgo", script.BoolCondition("host CGO_ENABLED", canCgo))
42+
add("cgo", script.BoolCondition("host CGO_ENABLED", testenv.HasCGO()))
4343
add("cross", script.BoolCondition("cmd/go GOOS/GOARCH != GOHOSTOS/GOHOSTARCH", goHostOS != runtime.GOOS || goHostArch != runtime.GOARCH))
4444
add("fuzz", sysCondition("-fuzz", platform.FuzzSupported, false))
4545
add("fuzz-instrumented", sysCondition("-fuzz with instrumentation", platform.FuzzInstrumented, false))
@@ -84,7 +84,7 @@ func sysCondition(flag string, f func(goos, goarch string) bool, needsCgo bool)
8484
GOOS, _ := s.LookupEnv("GOOS")
8585
GOARCH, _ := s.LookupEnv("GOARCH")
8686
cross := goHostOS != GOOS || goHostArch != GOARCH
87-
return (!needsCgo || (canCgo && !cross)) && f(GOOS, GOARCH), nil
87+
return (!needsCgo || (testenv.HasCGO() && !cross)) && f(GOOS, GOARCH), nil
8888
})
8989
}
9090

0 commit comments

Comments
 (0)