testenv.go (hugo-0.80.0) | : | testenv.go (hugo-0.81.0) | ||
---|---|---|---|---|
skipping to change at line 16 | skipping to change at line 16 | |||
// is available in different testing environments run by the Go team. | // is available in different testing environments run by the Go team. | |||
// | // | |||
// It is an internal package because these details are specific | // It is an internal package because these details are specific | |||
// to the Go team's test setup (on build.golang.org) and not | // to the Go team's test setup (on build.golang.org) and not | |||
// fundamental to tests in general. | // fundamental to tests in general. | |||
package testenv | package testenv | |||
import ( | import ( | |||
"errors" | "errors" | |||
"flag" | "flag" | |||
"github.com/gohugoio/hugo/tpl/internal/go_templates/cfg" | ||||
"os" | "os" | |||
"os/exec" | "os/exec" | |||
"path/filepath" | "path/filepath" | |||
"runtime" | "runtime" | |||
"strconv" | "strconv" | |||
"strings" | "strings" | |||
"sync" | "sync" | |||
"testing" | "testing" | |||
"github.com/cli/safeexec" | ||||
"github.com/gohugoio/hugo/tpl/internal/go_templates/cfg" | ||||
) | ) | |||
// Builder reports the name of the builder running this test | // Builder reports the name of the builder running this test | |||
// (for example, "linux-amd64" or "windows-386-gce"). | // (for example, "linux-amd64" or "windows-386-gce"). | |||
// If the test is not running on the build infrastructure, | // If the test is not running on the build infrastructure, | |||
// Builder returns the empty string. | // Builder returns the empty string. | |||
func Builder() string { | func Builder() string { | |||
return os.Getenv("GO_BUILDER_NAME") | return os.Getenv("GO_BUILDER_NAME") | |||
} | } | |||
skipping to change at line 48 | skipping to change at line 46 | |||
// and then run them with os.StartProcess or exec.Command. | // and then run them with os.StartProcess or exec.Command. | |||
func HasGoBuild() bool { | func HasGoBuild() bool { | |||
if os.Getenv("GO_GCFLAGS") != "" { | if os.Getenv("GO_GCFLAGS") != "" { | |||
// It's too much work to require every caller of the go command | // It's too much work to require every caller of the go command | |||
// to pass along "-gcflags="+os.Getenv("GO_GCFLAGS"). | // to pass along "-gcflags="+os.Getenv("GO_GCFLAGS"). | |||
// For now, if $GO_GCFLAGS is set, report that we simply can't | // For now, if $GO_GCFLAGS is set, report that we simply can't | |||
// run go build. | // run go build. | |||
return false | return false | |||
} | } | |||
switch runtime.GOOS { | switch runtime.GOOS { | |||
case "android", "js": | case "android", "js", "ios": | |||
return false | return false | |||
case "darwin": | ||||
if runtime.GOARCH == "arm64" { | ||||
return false | ||||
} | ||||
} | } | |||
return true | return true | |||
} | } | |||
// MustHaveGoBuild checks that the current system can build programs with ``go b uild'' | // MustHaveGoBuild checks that the current system can build programs with ``go b uild'' | |||
// and then run them with os.StartProcess or exec.Command. | // and then run them with os.StartProcess or exec.Command. | |||
// If not, MustHaveGoBuild calls t.Skip with an explanation. | // If not, MustHaveGoBuild calls t.Skip with an explanation. | |||
func MustHaveGoBuild(t testing.TB) { | func MustHaveGoBuild(t testing.TB) { | |||
if os.Getenv("GO_GCFLAGS") != "" { | if os.Getenv("GO_GCFLAGS") != "" { | |||
t.Skipf("skipping test: 'go build' not compatible with setting $G O_GCFLAGS") | t.Skipf("skipping test: 'go build' not compatible with setting $G O_GCFLAGS") | |||
skipping to change at line 116 | skipping to change at line 110 | |||
return "", errors.New("platform cannot run go tool") | return "", errors.New("platform cannot run go tool") | |||
} | } | |||
var exeSuffix string | var exeSuffix string | |||
if runtime.GOOS == "windows" { | if runtime.GOOS == "windows" { | |||
exeSuffix = ".exe" | exeSuffix = ".exe" | |||
} | } | |||
path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix) | path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix) | |||
if _, err := os.Stat(path); err == nil { | if _, err := os.Stat(path); err == nil { | |||
return path, nil | return path, nil | |||
} | } | |||
goBin, err := safeexec.LookPath("go" + exeSuffix) | goBin, err := exec.LookPath("go" + exeSuffix) | |||
if err != nil { | if err != nil { | |||
return "", errors.New("cannot find go tool: " + err.Error()) | return "", errors.New("cannot find go tool: " + err.Error()) | |||
} | } | |||
return goBin, nil | return goBin, nil | |||
} | } | |||
// HasExec reports whether the current system can start new processes | // HasExec reports whether the current system can start new processes | |||
// using os.StartProcess or (more commonly) exec.Command. | // using os.StartProcess or (more commonly) exec.Command. | |||
func HasExec() bool { | func HasExec() bool { | |||
switch runtime.GOOS { | switch runtime.GOOS { | |||
case "js": | case "js", "ios": | |||
return false | return false | |||
case "darwin": | ||||
if runtime.GOARCH == "arm64" { | ||||
return false | ||||
} | ||||
} | } | |||
return true | return true | |||
} | } | |||
// HasSrc reports whether the entire source tree is available under GOROOT. | // HasSrc reports whether the entire source tree is available under GOROOT. | |||
func HasSrc() bool { | func HasSrc() bool { | |||
switch runtime.GOOS { | switch runtime.GOOS { | |||
case "darwin": | case "ios": | |||
if runtime.GOARCH == "arm64" { | return false | |||
return false | ||||
} | ||||
} | } | |||
return true | return true | |||
} | } | |||
// MustHaveExec checks that the current system can start new processes | // MustHaveExec checks that the current system can start new processes | |||
// using os.StartProcess or (more commonly) exec.Command. | // using os.StartProcess or (more commonly) exec.Command. | |||
// If not, MustHaveExec calls t.Skip with an explanation. | // If not, MustHaveExec calls t.Skip with an explanation. | |||
func MustHaveExec(t testing.TB) { | func MustHaveExec(t testing.TB) { | |||
if !HasExec() { | if !HasExec() { | |||
t.Skipf("skipping test: cannot exec subprocess on %s/%s", runtime .GOOS, runtime.GOARCH) | t.Skipf("skipping test: cannot exec subprocess on %s/%s", runtime .GOOS, runtime.GOARCH) | |||
skipping to change at line 167 | skipping to change at line 155 | |||
var execPaths sync.Map // path -> error | var execPaths sync.Map // path -> error | |||
// MustHaveExecPath checks that the current system can start the named executabl e | // MustHaveExecPath checks that the current system can start the named executabl e | |||
// using os.StartProcess or (more commonly) exec.Command. | // using os.StartProcess or (more commonly) exec.Command. | |||
// If not, MustHaveExecPath calls t.Skip with an explanation. | // If not, MustHaveExecPath calls t.Skip with an explanation. | |||
func MustHaveExecPath(t testing.TB, path string) { | func MustHaveExecPath(t testing.TB, path string) { | |||
MustHaveExec(t) | MustHaveExec(t) | |||
err, found := execPaths.Load(path) | err, found := execPaths.Load(path) | |||
if !found { | if !found { | |||
_, err = safeexec.LookPath(path) | _, err = exec.LookPath(path) | |||
err, _ = execPaths.LoadOrStore(path, err) | err, _ = execPaths.LoadOrStore(path, err) | |||
} | } | |||
if err != nil { | if err != nil { | |||
t.Skipf("skipping test: %s: %s", path, err) | t.Skipf("skipping test: %s: %s", path, err) | |||
} | } | |||
} | } | |||
// HasExternalNetwork reports whether the current system can use | // HasExternalNetwork reports whether the current system can use | |||
// external (non-localhost) networks. | // external (non-localhost) networks. | |||
func HasExternalNetwork() bool { | func HasExternalNetwork() bool { | |||
skipping to change at line 207 | skipping to change at line 195 | |||
return haveCGO | return haveCGO | |||
} | } | |||
// MustHaveCGO calls t.Skip if cgo is not available. | // MustHaveCGO calls t.Skip if cgo is not available. | |||
func MustHaveCGO(t testing.TB) { | func MustHaveCGO(t testing.TB) { | |||
if !haveCGO { | if !haveCGO { | |||
t.Skipf("skipping test: no cgo") | t.Skipf("skipping test: no cgo") | |||
} | } | |||
} | } | |||
// CanInternalLink reports whether the current system can link programs with | ||||
// internal linking. | ||||
// (This is the opposite of cmd/internal/sys.MustLinkExternal. Keep them in sync | ||||
.) | ||||
func CanInternalLink() bool { | ||||
switch runtime.GOOS { | ||||
case "android": | ||||
if runtime.GOARCH != "arm64" { | ||||
return false | ||||
} | ||||
case "ios": | ||||
if runtime.GOARCH == "arm64" { | ||||
return false | ||||
} | ||||
} | ||||
return true | ||||
} | ||||
// MustInternalLink checks that the current system can link programs with intern | ||||
al | ||||
// linking. | ||||
// If not, MustInternalLink calls t.Skip with an explanation. | ||||
func MustInternalLink(t testing.TB) { | ||||
if !CanInternalLink() { | ||||
t.Skipf("skipping test: internal linking on %s/%s is not supporte | ||||
d", runtime.GOOS, runtime.GOARCH) | ||||
} | ||||
} | ||||
// HasSymlink reports whether the current system can use os.Symlink. | // HasSymlink reports whether the current system can use os.Symlink. | |||
func HasSymlink() bool { | func HasSymlink() bool { | |||
ok, _ := hasSymlink() | ok, _ := hasSymlink() | |||
return ok | return ok | |||
} | } | |||
// MustHaveSymlink reports whether the current system can use os.Symlink. | // MustHaveSymlink reports whether the current system can use os.Symlink. | |||
// If not, MustHaveSymlink calls t.Skip with an explanation. | // If not, MustHaveSymlink calls t.Skip with an explanation. | |||
func MustHaveSymlink(t testing.TB) { | func MustHaveSymlink(t testing.TB) { | |||
ok, reason := hasSymlink() | ok, reason := hasSymlink() | |||
skipping to change at line 275 | skipping to change at line 289 | |||
continue | continue | |||
} | } | |||
// Exclude GOTRACEBACK for the same reason. | // Exclude GOTRACEBACK for the same reason. | |||
if strings.HasPrefix(env, "GOTRACEBACK=") { | if strings.HasPrefix(env, "GOTRACEBACK=") { | |||
continue | continue | |||
} | } | |||
cmd.Env = append(cmd.Env, env) | cmd.Env = append(cmd.Env, env) | |||
} | } | |||
return cmd | return cmd | |||
} | } | |||
// CPUIsSlow reports whether the CPU running the test is suspected to be slow. | ||||
func CPUIsSlow() bool { | ||||
switch runtime.GOARCH { | ||||
case "arm", "mips", "mipsle", "mips64", "mips64le": | ||||
return true | ||||
} | ||||
return false | ||||
} | ||||
// SkipIfShortAndSlow skips t if -short is set and the CPU running the test is | ||||
// suspected to be slow. | ||||
// | ||||
// (This is useful for CPU-intensive tests that otherwise complete quickly.) | ||||
func SkipIfShortAndSlow(t testing.TB) { | ||||
if testing.Short() && CPUIsSlow() { | ||||
t.Helper() | ||||
t.Skipf("skipping test in -short mode on %s", runtime.GOARCH) | ||||
} | ||||
} | ||||
End of changes. 11 change blocks. | ||||
19 lines changed or deleted | 36 lines changed or added |