1
0
mirror of https://github.com/golang/go synced 2024-11-18 04:14:49 -07:00

all: consolidate cgo requirement checks

Many tools test check for the ability to compile cgo programs.
Consolidate them all into testenv.NeedsTool("cgo").

Change-Id: I62c96e7b4dc72df34b8fdbf10326c7d19e0613e8
Reviewed-on: https://go-review.googlesource.com/c/tools/+/234108
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Heschi Kreinick 2020-05-14 22:28:26 -04:00
parent d3bf790afa
commit c79c01b1c5
9 changed files with 28 additions and 74 deletions

View File

@ -1,11 +0,0 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build cgo
package main_test
func init() {
haveCGO = true
}

View File

@ -18,8 +18,6 @@ import (
"golang.org/x/tools/internal/testenv"
)
var haveCGO bool
type test struct {
offset, from, to string // specify the arguments
fileSpecified bool // true if the offset or from args specify a specific file
@ -31,10 +29,8 @@ type test struct {
// Test that renaming that would modify cgo files will produce an error and not modify the file.
func TestGeneratedFiles(t *testing.T) {
if !haveCGO {
t.Skipf("skipping test: no cgo")
}
testenv.NeedsTool(t, "go")
testenv.NeedsTool(t, "cgo")
tmp, bin, cleanup := buildGorename(t)
defer cleanup()

View File

@ -12,6 +12,7 @@ import (
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/packages/packagestest"
"golang.org/x/tools/internal/testenv"
)
// TestInvalidFilesInXTest checks the fix for golang/go#37971.
@ -48,11 +49,7 @@ func TestTypecheckCgo(t *testing.T) {
}
func testTypecheckCgo(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.")
}
testenv.NeedsTool(t, "cgo")
const cgo = `package cgo
import "C"

View File

@ -2768,25 +2768,3 @@ func copyAll(srcPath, dstPath string) error {
return nil
})
}
// 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
}

View File

@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build cgo
package packages_test
import (
@ -73,11 +71,7 @@ func TestCgoNoSyntax(t *testing.T) {
packagestest.TestAll(t, testCgoNoSyntax)
}
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.")
}
testenv.NeedsTool(t, "cgo")
exported := packagestest.Export(t, exporter, []packagestest.Module{{
Name: "golang.org/fake",
@ -118,9 +112,7 @@ func TestCgoBadPkgConfig(t *testing.T) {
packagestest.TestAll(t, testCgoBadPkgConfig)
}
func testCgoBadPkgConfig(t *testing.T, exporter packagestest.Exporter) {
if !hasGoBuild() {
t.Skip("this test can't run on platforms without go build. See discussion on golang.org/cl/214943 for more details.")
}
testenv.NeedsTool(t, "cgo")
exported := packagestest.Export(t, exporter, []packagestest.Module{{
Name: "golang.org/fake",

View File

@ -1,21 +1,18 @@
//+build go1.15,cgo
//+build go1.15
package regtest
import (
"runtime"
"testing"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/testenv"
)
func TestRegenerateCgo(t *testing.T) {
// The android builders have a complex setup which causes this test to fail. See discussion on
// golang.org/cl/214943 for more details.
if runtime.GOOS == "android" {
t.Skip("android not supported")
}
testenv.NeedsTool(t, "cgo")
const workspace = `
-- go.mod --
module example.com

View File

@ -16,7 +16,6 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
@ -30,6 +29,7 @@ import (
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/testenv"
"golang.org/x/tools/txtar"
)
@ -231,8 +231,7 @@ func DefaultOptions() source.Options {
}
var (
haveCgo = false
go115 = false
go115 = false
)
// Load creates the folder structure required when testing with modules.
@ -453,8 +452,8 @@ func Run(t *testing.T, tests Tests, data *Data) {
for i, e := range exp {
t.Run(SpanName(src)+"_"+strconv.Itoa(i), func(t *testing.T) {
t.Helper()
if (!haveCgo || runtime.GOOS == "android") && strings.Contains(t.Name(), "cgo") {
t.Skip("test requires cgo, not supported")
if strings.Contains(t.Name(), "cgo") {
testenv.NeedsTool(t, "cgo")
}
if !go115 && strings.Contains(t.Name(), "declarecgo") {
t.Skip("test requires Go 1.15")
@ -614,8 +613,8 @@ func Run(t *testing.T, tests Tests, data *Data) {
for spn, d := range data.Definitions {
t.Run(SpanName(spn), func(t *testing.T) {
t.Helper()
if (!haveCgo || runtime.GOOS == "android") && strings.Contains(t.Name(), "cgo") {
t.Skip("test requires cgo, not supported")
if strings.Contains(t.Name(), "cgo") {
testenv.NeedsTool(t, "cgo")
}
if !go115 && strings.Contains(t.Name(), "declarecgo") {
t.Skip("test requires Go 1.15")

View File

@ -1,7 +0,0 @@
// +build cgo
package tests
func init() {
haveCgo = true
}

View File

@ -40,6 +40,18 @@ var checkGoGoroot struct {
}
func hasTool(tool string) error {
if tool == "cgo" {
out, err := exec.Command("go", "env", "CGO_ENABLED").CombinedOutput()
if err != nil {
return err
}
enabled := strings.TrimSpace(string(out))
if enabled != "1" {
return fmt.Errorf("cgo not enabled: CGO_ENABLED=%q", enabled)
}
return nil
}
_, err := exec.LookPath(tool)
if err != nil {
return err
@ -125,6 +137,7 @@ func allowMissingTool(tool string) bool {
}
// NeedsTool skips t if the named tool is not present in the path.
// As a special case, "cgo" means "go" is present and can compile cgo programs.
func NeedsTool(t Testing, tool string) {
if t, ok := t.(helperer); ok {
t.Helper()