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 <matloob@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
This commit is contained in:
Michael Matloob 2020-01-27 12:41:35 -05:00
parent 91d59a9776
commit 73c4c875f0
2 changed files with 71 additions and 68 deletions

View File

@ -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 {

View File

@ -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)
}
})
}
}