summaryrefslogtreecommitdiffstats
path: root/tpl/internal/go_templates/testenv/testenv.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-03-16 08:48:16 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-03-16 08:54:25 +0100
commit65a78cae1ecdcfcb0701d78fed812083a1a6e82b (patch)
treeb57295e36c91cbfb76bdd606657d0c1f6c7fdf2b /tpl/internal/go_templates/testenv/testenv.go
parent4d6d1d08dad5538f4710fc4b56e09419aa0d854b (diff)
tpl: Sync go_templates for Go 1.18
Using Go tag go1.18 4aa1efed4853ea067d665a952eee77c52faac774 Updates #9677
Diffstat (limited to 'tpl/internal/go_templates/testenv/testenv.go')
-rw-r--r--tpl/internal/go_templates/testenv/testenv.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/tpl/internal/go_templates/testenv/testenv.go b/tpl/internal/go_templates/testenv/testenv.go
index e0a0b4ec6..510b5406e 100644
--- a/tpl/internal/go_templates/testenv/testenv.go
+++ b/tpl/internal/go_templates/testenv/testenv.go
@@ -11,6 +11,7 @@
package testenv
import (
+ "bytes"
"errors"
"flag"
"github.com/gohugoio/hugo/tpl/internal/go_templates/cfg"
@@ -22,6 +23,7 @@ import (
"strings"
"sync"
"testing"
+ "time"
)
// Builder reports the name of the builder running this test
@@ -306,3 +308,59 @@ func SkipIfShortAndSlow(t testing.TB) {
t.Skipf("skipping test in -short mode on %s", runtime.GOARCH)
}
}
+
+// RunWithTimeout runs cmd and returns its combined output. If the
+// subprocess exits with a non-zero status, it will log that status
+// and return a non-nil error, but this is not considered fatal.
+func RunWithTimeout(t testing.TB, cmd *exec.Cmd) ([]byte, error) {
+ args := cmd.Args
+ if args == nil {
+ args = []string{cmd.Path}
+ }
+
+ var b bytes.Buffer
+ cmd.Stdout = &b
+ cmd.Stderr = &b
+ if err := cmd.Start(); err != nil {
+ t.Fatalf("starting %s: %v", args, err)
+ }
+
+ // If the process doesn't complete within 1 minute,
+ // assume it is hanging and kill it to get a stack trace.
+ p := cmd.Process
+ done := make(chan bool)
+ go func() {
+ scale := 1
+ // This GOARCH/GOOS test is copied from cmd/dist/test.go.
+ // TODO(iant): Have cmd/dist update the environment variable.
+ if runtime.GOARCH == "arm" || runtime.GOOS == "windows" {
+ scale = 2
+ }
+ if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
+ if sc, err := strconv.Atoi(s); err == nil {
+ scale = sc
+ }
+ }
+
+ select {
+ case <-done:
+ case <-time.After(time.Duration(scale) * time.Minute):
+ p.Signal(Sigquit)
+ // If SIGQUIT doesn't do it after a little
+ // while, kill the process.
+ select {
+ case <-done:
+ case <-time.After(time.Duration(scale) * 30 * time.Second):
+ p.Signal(os.Kill)
+ }
+ }
+ }()
+
+ err := cmd.Wait()
+ if err != nil {
+ t.Logf("%s exit status: %v", args, err)
+ }
+ close(done)
+
+ return b.Bytes(), err
+}