internal/testenv: add MustHaveSource, rm HasSrc

All the users of HasSrc call t.Skip anyway, so let's move it to testenv.

Fix go/build to use MustHaveSource rather than MustHaveGoBuild where
appropriate.

Change-Id: I052bf96fd5a5780c1930da5b3a52b7a8dbebea46
Reviewed-on: https://go-review.googlesource.com/c/go/+/612057
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Tim King <taking@google.com>
Auto-Submit: Tim King <taking@google.com>
This commit is contained in:
Kir Kolyshkin 2024-09-09 11:04:13 -07:00 committed by Gopher Robot
parent f3c89e21a5
commit 4c0f098533
6 changed files with 19 additions and 39 deletions

View File

@ -30,9 +30,7 @@ func TestGZIPFilesHaveZeroMTimes(t *testing.T) {
if testenv.Builder() == "" { if testenv.Builder() == "" {
t.Skip("skipping test on non-builder") t.Skip("skipping test on non-builder")
} }
if !testenv.HasSrc() { testenv.MustHaveSource(t)
t.Skip("skipping; no GOROOT available")
}
goroot, err := filepath.EvalSymlinks(runtime.GOROOT()) goroot, err := filepath.EvalSymlinks(runtime.GOROOT())
if err != nil { if err != nil {

View File

@ -496,7 +496,7 @@ func TestShellSafety(t *testing.T) {
// Want to get a "cannot find package" error when directory for package does not exist. // Want to get a "cannot find package" error when directory for package does not exist.
// There should be valid partial information in the returned non-nil *Package. // There should be valid partial information in the returned non-nil *Package.
func TestImportDirNotExist(t *testing.T) { func TestImportDirNotExist(t *testing.T) {
testenv.MustHaveGoBuild(t) // really must just have source testenv.MustHaveGoBuild(t) // Need 'go list' internally.
ctxt := Default ctxt := Default
emptyDir := t.TempDir() emptyDir := t.TempDir()
@ -550,7 +550,7 @@ func TestImportDirNotExist(t *testing.T) {
} }
func TestImportVendor(t *testing.T) { func TestImportVendor(t *testing.T) {
testenv.MustHaveGoBuild(t) // really must just have source testenv.MustHaveSource(t)
t.Setenv("GO111MODULE", "off") t.Setenv("GO111MODULE", "off")
@ -571,7 +571,7 @@ func TestImportVendor(t *testing.T) {
} }
func BenchmarkImportVendor(b *testing.B) { func BenchmarkImportVendor(b *testing.B) {
testenv.MustHaveGoBuild(b) // really must just have source testenv.MustHaveSource(b)
b.Setenv("GO111MODULE", "off") b.Setenv("GO111MODULE", "off")
@ -592,7 +592,7 @@ func BenchmarkImportVendor(b *testing.B) {
} }
func TestImportVendorFailure(t *testing.T) { func TestImportVendorFailure(t *testing.T) {
testenv.MustHaveGoBuild(t) // really must just have source testenv.MustHaveSource(t)
t.Setenv("GO111MODULE", "off") t.Setenv("GO111MODULE", "off")
@ -614,7 +614,7 @@ func TestImportVendorFailure(t *testing.T) {
} }
func TestImportVendorParentFailure(t *testing.T) { func TestImportVendorParentFailure(t *testing.T) {
testenv.MustHaveGoBuild(t) // really must just have source testenv.MustHaveSource(t)
t.Setenv("GO111MODULE", "off") t.Setenv("GO111MODULE", "off")

View File

@ -16,7 +16,6 @@ import (
"io/fs" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"slices" "slices"
"strings" "strings"
"testing" "testing"
@ -751,11 +750,7 @@ func listStdPkgs(goroot string) ([]string, error) {
} }
func TestDependencies(t *testing.T) { func TestDependencies(t *testing.T) {
if !testenv.HasSrc() { testenv.MustHaveSource(t)
// Tests run in a limited file system and we do not
// provide access to every source file.
t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
}
ctxt := Default ctxt := Default
all, err := listStdPkgs(ctxt.GOROOT) all, err := listStdPkgs(ctxt.GOROOT)
@ -859,9 +854,7 @@ func depsPolicy(t *testing.T) *dag.Graph {
// TestStdlibLowercase tests that all standard library package names are // TestStdlibLowercase tests that all standard library package names are
// lowercase. See Issue 40065. // lowercase. See Issue 40065.
func TestStdlibLowercase(t *testing.T) { func TestStdlibLowercase(t *testing.T) {
if !testenv.HasSrc() { testenv.MustHaveSource(t)
t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
}
ctxt := Default ctxt := Default
all, err := listStdPkgs(ctxt.GOROOT) all, err := listStdPkgs(ctxt.GOROOT)

View File

@ -83,9 +83,7 @@ func walkDir(t *testing.T, path string, endTime time.Time) (int, bool) {
} }
func TestImportStdLib(t *testing.T) { func TestImportStdLib(t *testing.T) {
if !testenv.HasSrc() { testenv.MustHaveSource(t)
t.Skip("no source code available")
}
if testing.Short() && testenv.Builder() == "" { if testing.Short() && testenv.Builder() == "" {
t.Skip("skipping in -short mode") t.Skip("skipping in -short mode")
@ -109,9 +107,7 @@ var importedObjectTests = []struct {
} }
func TestImportedTypes(t *testing.T) { func TestImportedTypes(t *testing.T) {
if !testenv.HasSrc() { testenv.MustHaveSource(t)
t.Skip("no source code available")
}
for _, test := range importedObjectTests { for _, test := range importedObjectTests {
i := strings.LastIndex(test.name, ".") i := strings.LastIndex(test.name, ".")
@ -179,9 +175,7 @@ func verifyInterfaceMethodRecvs(t *testing.T, named *types.Named, level int) {
} }
func TestReimport(t *testing.T) { func TestReimport(t *testing.T) {
if !testenv.HasSrc() { testenv.MustHaveSource(t)
t.Skip("no source code available")
}
// Reimporting a partially imported (incomplete) package is not supported (see issue #19337). // Reimporting a partially imported (incomplete) package is not supported (see issue #19337).
// Make sure we recognize the situation and report an error. // Make sure we recognize the situation and report an error.
@ -195,9 +189,7 @@ func TestReimport(t *testing.T) {
} }
func TestIssue20855(t *testing.T) { func TestIssue20855(t *testing.T) {
if !testenv.HasSrc() { testenv.MustHaveSource(t)
t.Skip("no source code available")
}
pkg, err := importer.ImportFrom("go/internal/srcimporter/testdata/issue20855", ".", 0) pkg, err := importer.ImportFrom("go/internal/srcimporter/testdata/issue20855", ".", 0)
if err == nil || !strings.Contains(err.Error(), "missing function body") { if err == nil || !strings.Contains(err.Error(), "missing function body") {
@ -209,9 +201,7 @@ func TestIssue20855(t *testing.T) {
} }
func testImportPath(t *testing.T, pkgPath string) { func testImportPath(t *testing.T, pkgPath string) {
if !testenv.HasSrc() { testenv.MustHaveSource(t)
t.Skip("no source code available")
}
pkgName := path.Base(pkgPath) pkgName := path.Base(pkgPath)

View File

@ -268,13 +268,14 @@ var goTool = sync.OnceValues(func() (string, error) {
return exec.LookPath("go") return exec.LookPath("go")
}) })
// HasSrc reports whether the entire source tree is available under GOROOT. // MustHaveSource checks that the entire source tree is available under GOROOT.
func HasSrc() bool { // If not, it calls t.Skip with an explanation.
func MustHaveSource(t testing.TB) {
switch runtime.GOOS { switch runtime.GOOS {
case "ios": case "ios":
return false t.Helper()
t.Skip("skipping test: no source tree on " + runtime.GOOS)
} }
return true
} }
// HasExternalNetwork reports whether the current system can use // HasExternalNetwork reports whether the current system can use

View File

@ -151,9 +151,7 @@ var forbiddenStringsFunctions = map[string]bool{
// strings and bytes package functions. HTTP is mostly ASCII based, and doing // strings and bytes package functions. HTTP is mostly ASCII based, and doing
// Unicode-aware case folding or space stripping can introduce vulnerabilities. // Unicode-aware case folding or space stripping can introduce vulnerabilities.
func TestNoUnicodeStrings(t *testing.T) { func TestNoUnicodeStrings(t *testing.T) {
if !testenv.HasSrc() { testenv.MustHaveSource(t)
t.Skip("source code not available")
}
re := regexp.MustCompile(`(strings|bytes).([A-Za-z]+)`) re := regexp.MustCompile(`(strings|bytes).([A-Za-z]+)`)
if err := fs.WalkDir(os.DirFS("."), ".", func(path string, d fs.DirEntry, err error) error { if err := fs.WalkDir(os.DirFS("."), ".", func(path string, d fs.DirEntry, err error) error {