mirror of
https://github.com/gohugoio/hugo.git
synced 2025-09-01 22:42:45 +02:00
@@ -31,20 +31,17 @@ import (
|
||||
// If exec is not supported, testenv.SyscallIsNotSupported will return true
|
||||
// for the resulting error.
|
||||
func MustHaveExec(t testing.TB) {
|
||||
tryExecOnce.Do(func() {
|
||||
tryExecErr = tryExec()
|
||||
})
|
||||
if tryExecErr != nil {
|
||||
t.Skipf("skipping test: cannot exec subprocess on %s/%s: %v", runtime.GOOS, runtime.GOARCH, tryExecErr)
|
||||
if err := tryExec(); err != nil {
|
||||
msg := fmt.Sprintf("cannot exec subprocess on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
|
||||
if t == nil {
|
||||
panic(msg)
|
||||
}
|
||||
t.Helper()
|
||||
t.Skip("skipping test:", msg)
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
tryExecOnce sync.Once
|
||||
tryExecErr error
|
||||
)
|
||||
|
||||
func tryExec() error {
|
||||
var tryExec = sync.OnceValue(func() error {
|
||||
switch runtime.GOOS {
|
||||
case "wasip1", "js", "ios":
|
||||
default:
|
||||
@@ -70,15 +67,37 @@ func tryExec() error {
|
||||
|
||||
// We know that this is a test executable. We should be able to run it with a
|
||||
// no-op flag to check for overall exec support.
|
||||
exe, err := os.Executable()
|
||||
exe, err := exePath()
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't probe for exec support: %w", err)
|
||||
}
|
||||
cmd := exec.Command(exe, "-test.list=^$")
|
||||
cmd.Env = origEnv
|
||||
return cmd.Run()
|
||||
})
|
||||
|
||||
// Executable is a wrapper around [MustHaveExec] and [os.Executable].
|
||||
// It returns the path name for the executable that started the current process,
|
||||
// or skips the test if the current system can't start new processes,
|
||||
// or fails the test if the path can not be obtained.
|
||||
func Executable(t testing.TB) string {
|
||||
MustHaveExec(t)
|
||||
|
||||
exe, err := exePath()
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("os.Executable error: %v", err)
|
||||
if t == nil {
|
||||
panic(msg)
|
||||
}
|
||||
t.Fatal(msg)
|
||||
}
|
||||
return exe
|
||||
}
|
||||
|
||||
var exePath = sync.OnceValues(func() (string, error) {
|
||||
return os.Executable()
|
||||
})
|
||||
|
||||
var execPaths sync.Map // path -> error
|
||||
|
||||
// MustHaveExecPath checks that the current system can start the named executable
|
||||
@@ -93,6 +112,7 @@ func MustHaveExecPath(t testing.TB, path string) {
|
||||
err, _ = execPaths.LoadOrStore(path, err)
|
||||
}
|
||||
if err != nil {
|
||||
t.Helper()
|
||||
t.Skipf("skipping test: %s: %s", path, err)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user