From 73c4c875f0a0c79588c30bc4ca5c698f6fe9068d Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Mon, 27 Jan 2020 12:41:35 -0500 Subject: [PATCH] go/packages: only run TestCgoNoSyntax when cgo is available Fixes golang/go#36799 Change-Id: I3f2013891465bef1ef9a15097b3d4614b9856035 Reviewed-on: https://go-review.googlesource.com/c/tools/+/216538 Run-TryBot: Michael Matloob Reviewed-by: Dmitri Shuralyov --- go/packages/packages_test.go | 68 ------------------------------- go/packages/packagescgo_test.go | 71 +++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 68 deletions(-) diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index 77ca60f8c5..9edce09c2b 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -2535,74 +2535,6 @@ func testForTestField(t *testing.T, exporter packagestest.Exporter) { } } -func TestCgoNoSyntax(t *testing.T) { - packagestest.TestAll(t, testCgoNoSyntax) -} - -// Stolen from internal/testenv package in core. -// hasGoBuild reports whether the current system can build programs with ``go build'' -// and then run them with os.StartProcess or exec.Command. -func hasGoBuild() bool { - if os.Getenv("GO_GCFLAGS") != "" { - // It's too much work to require every caller of the go command - // to pass along "-gcflags="+os.Getenv("GO_GCFLAGS"). - // For now, if $GO_GCFLAGS is set, report that we simply can't - // run go build. - return false - } - switch runtime.GOOS { - case "android", "js": - return false - case "darwin": - if strings.HasPrefix(runtime.GOARCH, "arm") { - return false - } - } - return true -} - -func testCgoNoSyntax(t *testing.T, exporter packagestest.Exporter) { - // The android builders have a complex setup which causes this test to fail. See discussion on - // golang.org/cl/214943 for more details. - if !hasGoBuild() { - t.Skip("this test can't run on platforms without go build. See discussion on golang.org/cl/214943 for more details.") - } - - exported := packagestest.Export(t, exporter, []packagestest.Module{{ - Name: "golang.org/fake", - Files: map[string]interface{}{ - "c/c.go": `package c; import "C"`, - }, - }}) - - // Explicitly enable cgo. - exported.Config.Env = append(exported.Config.Env, "CGO_ENABLED=1") - - modes := []packages.LoadMode{ - packages.NeedTypes, - packages.NeedName | packages.NeedTypes, - packages.NeedName | packages.NeedTypes | packages.NeedImports, - packages.NeedName | packages.NeedTypes | packages.NeedImports | packages.NeedDeps, - packages.NeedName | packages.NeedImports, - } - for _, mode := range modes { - t.Run(fmt.Sprint(mode), func(t *testing.T) { - exported.Config.Mode = mode - pkgs, err := packages.Load(exported.Config, "golang.org/fake/c") - if err != nil { - t.Fatal(err) - } - if len(pkgs) != 1 { - t.Fatalf("Expected 1 package, got %v", pkgs) - } - pkg := pkgs[0] - if len(pkg.Errors) != 0 { - t.Fatalf("Expected no errors in package, got %v", pkg.Errors) - } - }) - } -} - func errorMessages(errors []packages.Error) []string { var msgs []string for _, err := range errors { diff --git a/go/packages/packagescgo_test.go b/go/packages/packagescgo_test.go index 9858ba8524..ed51522e3e 100644 --- a/go/packages/packagescgo_test.go +++ b/go/packages/packagescgo_test.go @@ -7,11 +7,14 @@ package packages_test import ( + "fmt" + "os" "runtime" "strings" "testing" "golang.org/x/tools/go/packages" + "golang.org/x/tools/go/packages/packagestest" "golang.org/x/tools/internal/testenv" ) @@ -62,3 +65,71 @@ func TestLoadImportsC(t *testing.T) { } } } + +func TestCgoNoSyntax(t *testing.T) { + packagestest.TestAll(t, testCgoNoSyntax) +} + +// Stolen from internal/testenv package in core. +// hasGoBuild reports whether the current system can build programs with ``go build'' +// and then run them with os.StartProcess or exec.Command. +func hasGoBuild() bool { + if os.Getenv("GO_GCFLAGS") != "" { + // It's too much work to require every caller of the go command + // to pass along "-gcflags="+os.Getenv("GO_GCFLAGS"). + // For now, if $GO_GCFLAGS is set, report that we simply can't + // run go build. + return false + } + switch runtime.GOOS { + case "android", "js": + return false + case "darwin": + if strings.HasPrefix(runtime.GOARCH, "arm") { + return false + } + } + return true +} + +func testCgoNoSyntax(t *testing.T, exporter packagestest.Exporter) { + // The android builders have a complex setup which causes this test to fail. See discussion on + // golang.org/cl/214943 for more details. + if !hasGoBuild() { + t.Skip("this test can't run on platforms without go build. See discussion on golang.org/cl/214943 for more details.") + } + + exported := packagestest.Export(t, exporter, []packagestest.Module{{ + Name: "golang.org/fake", + Files: map[string]interface{}{ + "c/c.go": `package c; import "C"`, + }, + }}) + + // Explicitly enable cgo. + exported.Config.Env = append(exported.Config.Env, "CGO_ENABLED=1") + + modes := []packages.LoadMode{ + packages.NeedTypes, + packages.NeedName | packages.NeedTypes, + packages.NeedName | packages.NeedTypes | packages.NeedImports, + packages.NeedName | packages.NeedTypes | packages.NeedImports | packages.NeedDeps, + packages.NeedName | packages.NeedImports, + } + for _, mode := range modes { + t.Run(fmt.Sprint(mode), func(t *testing.T) { + exported.Config.Mode = mode + pkgs, err := packages.Load(exported.Config, "golang.org/fake/c") + if err != nil { + t.Fatal(err) + } + if len(pkgs) != 1 { + t.Fatalf("Expected 1 package, got %v", pkgs) + } + pkg := pkgs[0] + if len(pkg.Errors) != 0 { + t.Fatalf("Expected no errors in package, got %v", pkg.Errors) + } + }) + } +}