Skip to content

Commit 98f8936

Browse files
committed
go/internal/gcimporter: cleanup testenv functions
There was a lot of duplicated code throughout the gcimporter test. Change-Id: Ib232717783b2b4053fa41139dfad05f37662072a Reviewed-on: https://go-review.googlesource.com/c/tools/+/322290 Trust: Rebecca Stambler <[email protected]> Reviewed-by: Robert Findley <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 71e666b commit 98f8936

File tree

1 file changed

+25
-76
lines changed

1 file changed

+25
-76
lines changed

go/internal/gcimporter/gcimporter_test.go

+25-76
Original file line numberDiff line numberDiff line change
@@ -29,43 +29,6 @@ func TestMain(m *testing.M) {
2929
os.Exit(m.Run())
3030
}
3131

32-
// ----------------------------------------------------------------------------
33-
// The following three functions (Builder, HasGoBuild, MustHaveGoBuild) were
34-
// copied from $GOROOT/src/internal/testenv since that package is not available
35-
// in x/tools.
36-
37-
// Builder reports the name of the builder running this test
38-
// (for example, "linux-amd64" or "windows-386-gce").
39-
// If the test is not running on the build infrastructure,
40-
// Builder returns the empty string.
41-
func Builder() string {
42-
return os.Getenv("GO_BUILDER_NAME")
43-
}
44-
45-
// HasGoBuild reports whether the current system can build programs with ``go build''
46-
// and then run them with os.StartProcess or exec.Command.
47-
func HasGoBuild() bool {
48-
switch runtime.GOOS {
49-
case "android", "nacl":
50-
return false
51-
case "darwin":
52-
if strings.HasPrefix(runtime.GOARCH, "arm") {
53-
return false
54-
}
55-
}
56-
return true
57-
}
58-
59-
// MustHaveGoBuild checks that the current system can build programs with ``go build''
60-
// and then run them with os.StartProcess or exec.Command.
61-
// If not, MustHaveGoBuild calls t.Skip with an explanation.
62-
func MustHaveGoBuild(t *testing.T) {
63-
testenv.NeedsTool(t, "go")
64-
if !HasGoBuild() {
65-
t.Skipf("skipping test: 'go build' not available on %s/%s", runtime.GOOS, runtime.GOARCH)
66-
}
67-
}
68-
6932
// ----------------------------------------------------------------------------
7033

7134
// skipSpecialPlatforms causes the test to be skipped for platforms where
@@ -82,10 +45,21 @@ func skipSpecialPlatforms(t *testing.T) {
8245
}
8346
}
8447

48+
func needsCompiler(t *testing.T, compiler string) {
49+
if runtime.Compiler == compiler {
50+
return
51+
}
52+
switch compiler {
53+
case "gc":
54+
t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
55+
}
56+
}
57+
8558
// compile runs the compiler on filename, with dirname as the working directory,
8659
// and writes the output file to outdirname.
8760
func compile(t *testing.T, dirname, filename, outdirname string) string {
88-
/* testenv. */ MustHaveGoBuild(t)
61+
testenv.NeedsGoBuild(t)
62+
8963
// filename must end with ".go"
9064
if !strings.HasSuffix(filename, ".go") {
9165
t.Fatalf("filename doesn't end in .go: %s", filename)
@@ -159,10 +133,7 @@ func mktmpdir(t *testing.T) string {
159133
const testfile = "exports.go"
160134

161135
func TestImportTestdata(t *testing.T) {
162-
// This package only handles gc export data.
163-
if runtime.Compiler != "gc" {
164-
t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
165-
}
136+
needsCompiler(t, "gc")
166137

167138
tmpdir := mktmpdir(t)
168139
defer os.RemoveAll(tmpdir)
@@ -194,9 +165,7 @@ func TestVersionHandling(t *testing.T) {
194165
skipSpecialPlatforms(t) // we really only need to exclude nacl platforms, but this is fine
195166

196167
// This package only handles gc export data.
197-
if runtime.Compiler != "gc" {
198-
t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
199-
}
168+
needsCompiler(t, "gc")
200169

201170
const dir = "./testdata/versions"
202171
list, err := ioutil.ReadDir(dir)
@@ -275,12 +244,10 @@ func TestImportStdLib(t *testing.T) {
275244
skipSpecialPlatforms(t)
276245

277246
// This package only handles gc export data.
278-
if runtime.Compiler != "gc" {
279-
t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
280-
}
247+
needsCompiler(t, "gc")
281248

282249
dt := maxTime
283-
if testing.Short() && /* testenv. */ Builder() == "" {
250+
if testing.Short() && os.Getenv("GO_BUILDER_NAME") == "" {
284251
dt = 10 * time.Millisecond
285252
}
286253
nimports := testDir(t, "", time.Now().Add(dt)) // installed packages
@@ -316,9 +283,7 @@ func TestImportedTypes(t *testing.T) {
316283
skipSpecialPlatforms(t)
317284

318285
// This package only handles gc export data.
319-
if runtime.Compiler != "gc" {
320-
t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
321-
}
286+
needsCompiler(t, "gc")
322287

323288
for _, test := range importedObjectTests {
324289
obj := importObject(t, test.name)
@@ -424,9 +389,7 @@ func TestIssue5815(t *testing.T) {
424389
skipSpecialPlatforms(t)
425390

426391
// This package only handles gc export data.
427-
if runtime.Compiler != "gc" {
428-
t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
429-
}
392+
needsCompiler(t, "gc")
430393

431394
pkg := importPkg(t, "strings", ".")
432395

@@ -453,9 +416,7 @@ func TestCorrectMethodPackage(t *testing.T) {
453416
skipSpecialPlatforms(t)
454417

455418
// This package only handles gc export data.
456-
if runtime.Compiler != "gc" {
457-
t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
458-
}
419+
needsCompiler(t, "gc")
459420

460421
imports := make(map[string]*types.Package)
461422
_, err := Import(imports, "net/http", ".", nil)
@@ -476,9 +437,7 @@ func TestIssue13566(t *testing.T) {
476437
skipSpecialPlatforms(t)
477438

478439
// This package only handles gc export data.
479-
if runtime.Compiler != "gc" {
480-
t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
481-
}
440+
needsCompiler(t, "gc")
482441

483442
// On windows, we have to set the -D option for the compiler to avoid having a drive
484443
// letter and an illegal ':' in the import path - just skip it (see also issue #3483).
@@ -515,9 +474,7 @@ func TestIssue13898(t *testing.T) {
515474
skipSpecialPlatforms(t)
516475

517476
// This package only handles gc export data.
518-
if runtime.Compiler != "gc" {
519-
t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
520-
}
477+
needsCompiler(t, "gc")
521478

522479
// import go/internal/gcimporter which imports go/types partially
523480
imports := make(map[string]*types.Package)
@@ -561,9 +518,7 @@ func TestIssue15517(t *testing.T) {
561518
skipSpecialPlatforms(t)
562519

563520
// This package only handles gc export data.
564-
if runtime.Compiler != "gc" {
565-
t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
566-
}
521+
needsCompiler(t, "gc")
567522

568523
// On windows, we have to set the -D option for the compiler to avoid having a drive
569524
// letter and an illegal ':' in the import path - just skip it (see also issue #3483).
@@ -600,9 +555,7 @@ func TestIssue15920(t *testing.T) {
600555
skipSpecialPlatforms(t)
601556

602557
// This package only handles gc export data.
603-
if runtime.Compiler != "gc" {
604-
t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
605-
}
558+
needsCompiler(t, "gc")
606559

607560
// On windows, we have to set the -D option for the compiler to avoid having a drive
608561
// letter and an illegal ':' in the import path - just skip it (see also issue #3483).
@@ -617,9 +570,7 @@ func TestIssue20046(t *testing.T) {
617570
skipSpecialPlatforms(t)
618571

619572
// This package only handles gc export data.
620-
if runtime.Compiler != "gc" {
621-
t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
622-
}
573+
needsCompiler(t, "gc")
623574

624575
// On windows, we have to set the -D option for the compiler to avoid having a drive
625576
// letter and an illegal ':' in the import path - just skip it (see also issue #3483).
@@ -640,9 +591,7 @@ func TestIssue25301(t *testing.T) {
640591
skipSpecialPlatforms(t)
641592

642593
// This package only handles gc export data.
643-
if runtime.Compiler != "gc" {
644-
t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
645-
}
594+
needsCompiler(t, "gc")
646595

647596
// On windows, we have to set the -D option for the compiler to avoid having a drive
648597
// letter and an illegal ':' in the import path - just skip it (see also issue #3483).

0 commit comments

Comments
 (0)