Skip to content

Commit

Permalink
cmd/stringer, go/loader: use testenv.NeedsTool(t, "cgo")
Browse files Browse the repository at this point in the history
Using go env CGO_ENABLED has an advantage over build.Default.CgoEnabled
in that it pays attention to whether a C compiler is available in $PATH,
so it correctly skips the test when cgo is not enabled in more contexts.
I haven't found better docs for this than https://go.dev/doc/go1.20#cgo.

Also add this in TestCgoCwdIssue46877, since it requires cgo.

Change-Id: Ib14a983cc179b725a98f154846662887a91edc19
Reviewed-on: https://go-review.googlesource.com/c/tools/+/560175
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
  • Loading branch information
dmitshur authored and gopherbot committed Feb 1, 2024
1 parent 5f90691 commit 1efbdde
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
9 changes: 6 additions & 3 deletions cmd/stringer/endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"bytes"
"flag"
"fmt"
"go/build"
"io"
"os"
"path"
Expand Down Expand Up @@ -50,6 +49,8 @@ func TestMain(m *testing.M) {
}

func TestEndToEnd(t *testing.T) {
testenv.NeedsTool(t, "go")

stringer := stringerPath(t)
// Read the testdata directory.
fd, err := os.Open("testdata")
Expand All @@ -76,8 +77,8 @@ func TestEndToEnd(t *testing.T) {
continue
}
t.Run(name, func(t *testing.T) {
if name == "cgo.go" && !build.Default.CgoEnabled {
t.Skipf("cgo is not enabled for %s", name)
if name == "cgo.go" {
testenv.NeedsTool(t, "cgo")
}
stringerCompileAndRun(t, t.TempDir(), stringer, typeName(name), name)
})
Expand Down Expand Up @@ -155,6 +156,8 @@ func TestTags(t *testing.T) {
// TestConstValueChange verifies that if a constant value changes and
// the stringer code is not regenerated, we'll get a compiler error.
func TestConstValueChange(t *testing.T) {
testenv.NeedsTool(t, "go")

stringer := stringerPath(t)
dir := t.TempDir()
source := filepath.Join(dir, "day.go")
Expand Down
1 change: 1 addition & 0 deletions go/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ func loadIO(t *testing.T) {

func TestCgoCwdIssue46877(t *testing.T) {
testenv.NeedsTool(t, "go")
testenv.NeedsTool(t, "cgo")
var conf loader.Config
conf.Import("golang.org/x/tools/go/loader/testdata/issue46877")
if _, err := conf.Load(); err != nil {
Expand Down
6 changes: 2 additions & 4 deletions go/loader/stdlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,11 @@ func TestCgoOption(t *testing.T) {
case "darwin":
t.Skipf("golang/go#58493: file locations in this test are stale on darwin")
}
testenv.NeedsTool(t, "go")
// In nocgo builds (e.g. linux-amd64-nocgo),
// there is no "runtime/cgo" package,
// so cgo-generated Go files will have a failing import.
if !build.Default.CgoEnabled {
return
}
testenv.NeedsTool(t, "go")
testenv.NeedsTool(t, "cgo")

// Test that we can load cgo-using packages with
// CGO_ENABLED=[01], which causes go/build to select pure
Expand Down

0 comments on commit 1efbdde

Please sign in to comment.