]> Cypherpunks repositories - gostls13.git/commitdiff
os: use testenv.Command instead of exec.Command in tests
authorcui fliter <imcusg@gmail.com>
Fri, 18 Nov 2022 06:06:56 +0000 (14:06 +0800)
committerGopher Robot <gobot@golang.org>
Tue, 21 Feb 2023 23:07:55 +0000 (23:07 +0000)
testenv.Command sets a default timeout based on the test's deadline
and sends SIGQUIT (where supported) in case of a hang.

Change-Id: I84d469d12ca1f39175b713ca41e7cd2202d11d96
Reviewed-on: https://go-review.googlesource.com/c/go/+/451895
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/os/exec/exec_test.go
src/os/exec/exec_windows_test.go
src/os/exec/lp_windows_test.go
src/os/signal/signal_test.go
src/os/signal/signal_windows_test.go

index 67e2d256b450276317d0dd82bf1c89e20620ed4c..c2f643a6454fcbcd7b1a01aaf7bca9acb76d0bf7 100644 (file)
@@ -752,7 +752,7 @@ func TestExtraFiles(t *testing.T) {
        tempdir := t.TempDir()
        exe := filepath.Join(tempdir, "read3.exe")
 
-       c := exec.Command(testenv.GoToolPath(t), "build", "-o", exe, "read3.go")
+       c := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", exe, "read3.go")
        // Build the test without cgo, so that C library functions don't
        // open descriptors unexpectedly. See issue 25628.
        c.Env = append(os.Environ(), "CGO_ENABLED=0")
index b39790d61a0fa328c1c424cf93461ea1a53f7ad4..efd37107edf54224b5c4e96272660e91f875d872 100644 (file)
@@ -8,6 +8,7 @@ package exec_test
 
 import (
        "fmt"
+       "internal/testenv"
        "io"
        "os"
        "os/exec"
@@ -69,7 +70,7 @@ func TestPipePassing(t *testing.T) {
 func TestNoInheritHandles(t *testing.T) {
        t.Parallel()
 
-       cmd := exec.Command("cmd", "/c exit 88")
+       cmd := testenv.Command(t, "cmd", "/c exit 88")
        cmd.SysProcAttr = &syscall.SysProcAttr{NoInheritHandles: true}
        err := cmd.Run()
        exitError, ok := err.(*exec.ExitError)
index d797b6c53c1c9760b46acb8ba7e02eb7747d901a..50d522948a7957df42576c9d5dd009d52f067920 100644 (file)
@@ -164,7 +164,7 @@ func (test lookPathTest) run(t *testing.T, tmpdir, printpathExe string) {
        // Run "cmd.exe /c test.searchFor" with new environment and
        // work directory set. All candidates are copies of printpath.exe.
        // These will output their program paths when run.
-       should, errCmd := test.runProg(t, env, exec.Command("cmd", "/c", test.searchFor))
+       should, errCmd := test.runProg(t, env, testenv.Command(t, "cmd", "/c", test.searchFor))
        // Run the lookpath program with new environment and work directory set.
        have, errLP := test.runProg(t, env, helperCommand(t, "lookpath", test.searchFor))
        // Compare results.
@@ -572,7 +572,7 @@ func buildPrintPathExe(t *testing.T, dir string) string {
                t.Fatalf("failed to execute template: %v", err)
        }
        outname := name + ".exe"
-       cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", outname, srcname)
+       cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", outname, srcname)
        cmd.Dir = dir
        out, err := cmd.CombinedOutput()
        if err != nil {
index fec6db72a933df899bee159754d3ecd71f88dab2..ddbd458a6da3cce5b52c8acc4e595ada4233e1a8 100644 (file)
@@ -304,7 +304,7 @@ func TestDetectNohup(t *testing.T) {
                // We have no intention of reading from c.
                c := make(chan os.Signal, 1)
                Notify(c, syscall.SIGHUP)
-               if out, err := exec.Command(os.Args[0], "-test.run=TestDetectNohup", "-check_sighup_ignored").CombinedOutput(); err == nil {
+               if out, err := testenv.Command(t, os.Args[0], "-test.run=TestDetectNohup", "-check_sighup_ignored").CombinedOutput(); err == nil {
                        t.Errorf("ran test with -check_sighup_ignored and it succeeded: expected failure.\nOutput:\n%s", out)
                }
                Stop(c)
@@ -315,7 +315,7 @@ func TestDetectNohup(t *testing.T) {
                }
                Ignore(syscall.SIGHUP)
                os.Remove("nohup.out")
-               out, err := exec.Command("/usr/bin/nohup", os.Args[0], "-test.run=TestDetectNohup", "-check_sighup_ignored").CombinedOutput()
+               out, err := testenv.Command(t, "/usr/bin/nohup", os.Args[0], "-test.run=TestDetectNohup", "-check_sighup_ignored").CombinedOutput()
 
                data, _ := os.ReadFile("nohup.out")
                os.Remove("nohup.out")
@@ -447,7 +447,7 @@ func TestNohup(t *testing.T) {
                        if subTimeout != 0 {
                                args = append(args, fmt.Sprintf("-test.timeout=%v", subTimeout))
                        }
-                       out, err := exec.Command(os.Args[0], args...).CombinedOutput()
+                       out, err := testenv.Command(t, os.Args[0], args...).CombinedOutput()
 
                        if err == nil {
                                t.Errorf("ran test with -send_uncaught_sighup=%d and it succeeded: expected failure.\nOutput:\n%s", i, out)
@@ -497,7 +497,7 @@ func TestNohup(t *testing.T) {
                        if subTimeout != 0 {
                                args = append(args, fmt.Sprintf("-test.timeout=%v", subTimeout))
                        }
-                       out, err := exec.Command("nohup", args...).CombinedOutput()
+                       out, err := testenv.Command(t, "nohup", args...).CombinedOutput()
 
                        if err != nil {
                                t.Errorf("ran test with -send_uncaught_sighup=%d under nohup and it failed: expected success.\nError: %v\nOutput:\n%s", i, err, out)
@@ -546,7 +546,7 @@ func TestAtomicStop(t *testing.T) {
                if deadline, ok := t.Deadline(); ok {
                        timeout = time.Until(deadline).String()
                }
-               cmd := exec.Command(os.Args[0], "-test.run=TestAtomicStop", "-test.timeout="+timeout)
+               cmd := testenv.Command(t, os.Args[0], "-test.run=TestAtomicStop", "-test.timeout="+timeout)
                cmd.Env = append(os.Environ(), "GO_TEST_ATOMIC_STOP=1")
                out, err := cmd.CombinedOutput()
                if err == nil {
@@ -749,7 +749,7 @@ func TestNotifyContextNotifications(t *testing.T) {
                        if subTimeout != 0 {
                                args = append(args, fmt.Sprintf("-test.timeout=%v", subTimeout))
                        }
-                       out, err := exec.Command(os.Args[0], args...).CombinedOutput()
+                       out, err := testenv.Command(t, os.Args[0], args...).CombinedOutput()
                        if err != nil {
                                t.Errorf("ran test with -check_notify_ctx_notification and it failed with %v.\nOutput:\n%s", err, out)
                        }
index 02803e5808091ea123d82f165174bfb7e34725fc..145a805c50b34d8b065e620d439958b5db10832f 100644 (file)
@@ -7,7 +7,6 @@ package signal
 import (
        "internal/testenv"
        "os"
-       "os/exec"
        "path/filepath"
        "strings"
        "syscall"
@@ -71,13 +70,13 @@ func main() {
        // compile it
        exe := name + ".exe"
        defer os.Remove(exe)
-       o, err := exec.Command(testenv.GoToolPath(t), "build", "-o", exe, src).CombinedOutput()
+       o, err := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", exe, src).CombinedOutput()
        if err != nil {
                t.Fatalf("Failed to compile: %v\n%v", err, string(o))
        }
 
        // run it
-       cmd := exec.Command(exe)
+       cmd := testenv.Command(t, exe)
        var buf strings.Builder
        cmd.Stdout = &buf
        cmd.Stderr = &buf