1
0
mirror of https://github.com/golang/go synced 2024-11-17 12:04:43 -07:00

cmd/go: use testenv.Command instead of exec.Command in tests

testenv.Command sets a default timeout based on the test's deadline
and sends SIGQUIT (where supported) in case of a hang.

Change-Id: Ic19172b6cd05526cf6776b904e2dfebdd8b055a8
Reviewed-on: https://go-review.googlesource.com/c/go/+/450697
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Bryan C. Mills 2022-11-15 10:08:11 -05:00 committed by Gopher Robot
parent 2cb103ff58
commit c9f7370a09
6 changed files with 13 additions and 17 deletions

View File

@ -487,7 +487,7 @@ func (tg *testgoData) doRun(args []string) error {
} }
tg.t.Logf("running testgo %v", args) tg.t.Logf("running testgo %v", args)
cmd := exec.Command(prog, args...) cmd := testenv.Command(tg.t, prog, args...)
tg.stdout.Reset() tg.stdout.Reset()
tg.stderr.Reset() tg.stderr.Reset()
cmd.Dir = tg.execDir cmd.Dir = tg.execDir
@ -530,7 +530,7 @@ func (tg *testgoData) runFail(args ...string) {
// runGit runs a git command, and expects it to succeed. // runGit runs a git command, and expects it to succeed.
func (tg *testgoData) runGit(dir string, args ...string) { func (tg *testgoData) runGit(dir string, args ...string) {
tg.t.Helper() tg.t.Helper()
cmd := exec.Command("git", args...) cmd := testenv.Command(tg.t, "git", args...)
tg.stdout.Reset() tg.stdout.Reset()
tg.stderr.Reset() tg.stderr.Reset()
cmd.Stdout = &tg.stdout cmd.Stdout = &tg.stdout
@ -1583,7 +1583,7 @@ func TestCgoPkgConfig(t *testing.T) {
tg.run("env", "PKG_CONFIG") tg.run("env", "PKG_CONFIG")
pkgConfig := strings.TrimSpace(tg.getStdout()) pkgConfig := strings.TrimSpace(tg.getStdout())
testenv.MustHaveExecPath(t, pkgConfig) testenv.MustHaveExecPath(t, pkgConfig)
if out, err := exec.Command(pkgConfig, "--atleast-pkgconfig-version", "0.24").CombinedOutput(); err != nil { if out, err := testenv.Command(t, pkgConfig, "--atleast-pkgconfig-version", "0.24").CombinedOutput(); err != nil {
t.Skipf("%s --atleast-pkgconfig-version 0.24: %v\n%s", pkgConfig, err, out) t.Skipf("%s --atleast-pkgconfig-version 0.24: %v\n%s", pkgConfig, err, out)
} }
@ -2281,7 +2281,7 @@ func testBuildmodePIE(t *testing.T, useCgo, setBuildmodeToPIE bool) {
panic("unreachable") panic("unreachable")
} }
out, err := exec.Command(obj).CombinedOutput() out, err := testenv.Command(t, obj).CombinedOutput()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -2298,7 +2298,7 @@ func TestUpxCompression(t *testing.T) {
} }
testenv.MustHaveExecPath(t, "upx") testenv.MustHaveExecPath(t, "upx")
out, err := exec.Command("upx", "--version").CombinedOutput() out, err := testenv.Command(t, "upx", "--version").CombinedOutput()
if err != nil { if err != nil {
t.Fatalf("upx --version failed: %v", err) t.Fatalf("upx --version failed: %v", err)
} }
@ -2332,13 +2332,13 @@ func TestUpxCompression(t *testing.T) {
obj := tg.path("main") obj := tg.path("main")
tg.run("build", "-o", obj, src) tg.run("build", "-o", obj, src)
out, err = exec.Command("upx", obj).CombinedOutput() out, err = testenv.Command(t, "upx", obj).CombinedOutput()
if err != nil { if err != nil {
t.Logf("executing upx\n%s\n", out) t.Logf("executing upx\n%s\n", out)
t.Fatalf("upx failed with %v", err) t.Fatalf("upx failed with %v", err)
} }
out, err = exec.Command(obj).CombinedOutput() out, err = testenv.Command(t, obj).CombinedOutput()
if err != nil { if err != nil {
t.Logf("%s", out) t.Logf("%s", out)
t.Fatalf("running compressed go binary failed with error %s", err) t.Fatalf("running compressed go binary failed with error %s", err)

View File

@ -5,8 +5,8 @@
package main_test package main_test
import ( import (
"internal/testenv"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
@ -38,7 +38,7 @@ func TestAbsolutePath(t *testing.T) {
noVolume := file[len(filepath.VolumeName(file)):] noVolume := file[len(filepath.VolumeName(file)):]
wrongPath := filepath.Join(dir, noVolume) wrongPath := filepath.Join(dir, noVolume)
cmd := exec.Command(tg.goTool(), "build", noVolume) cmd := testenv.Command(t, tg.goTool(), "build", noVolume)
cmd.Dir = dir cmd.Dir = dir
output, err := cmd.CombinedOutput() output, err := cmd.CombinedOutput()
if err == nil { if err == nil {

View File

@ -6,7 +6,6 @@ package main_test
import ( import (
"internal/testenv" "internal/testenv"
"os/exec"
"sync/atomic" "sync/atomic"
"testing" "testing"
) )
@ -27,7 +26,7 @@ func BenchmarkExecGoEnv(b *testing.B) {
b.ResetTimer() b.ResetTimer()
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
cmd := exec.Command(gotool, "env", "GOARCH") cmd := testenv.Command(b, gotool, "env", "GOARCH")
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
b.Fatal(err) b.Fatal(err)

View File

@ -10,7 +10,6 @@ import (
"fmt" "fmt"
"internal/testenv" "internal/testenv"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"testing" "testing"
@ -199,7 +198,7 @@ func TestLockNotDroppedByExecCommand(t *testing.T) {
// Some kinds of file locks are dropped when a duplicated or forked file // Some kinds of file locks are dropped when a duplicated or forked file
// descriptor is unlocked. Double-check that the approach used by os/exec does // descriptor is unlocked. Double-check that the approach used by os/exec does
// not accidentally drop locks. // not accidentally drop locks.
cmd := exec.Command(os.Args[0], "-test.run=^$") cmd := testenv.Command(t, os.Args[0], "-test.run=^$")
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
t.Fatalf("exec failed: %v", err) t.Fatalf("exec failed: %v", err)
} }

View File

@ -12,7 +12,6 @@ import (
"fmt" "fmt"
"internal/testenv" "internal/testenv"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"testing" "testing"
"time" "time"
@ -226,7 +225,7 @@ func TestSpuriousEDEADLK(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
cmd := exec.Command(os.Args[0], "-test.run="+t.Name()) cmd := testenv.Command(t, os.Args[0], "-test.run="+t.Name())
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s", dirVar, dir)) cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s", dirVar, dir))
qDone := make(chan struct{}) qDone := make(chan struct{})

View File

@ -11,7 +11,6 @@ import (
"internal/diff" "internal/diff"
"internal/testenv" "internal/testenv"
"os" "os"
"os/exec"
"strings" "strings"
"testing" "testing"
"text/template" "text/template"
@ -43,7 +42,7 @@ func checkScriptReadme(t *testing.T, engine *script.Engine, env []string) {
} }
doc := new(strings.Builder) doc := new(strings.Builder)
cmd := exec.Command(testGo, "doc", "cmd/go/internal/script") cmd := testenv.Command(t, testGo, "doc", "cmd/go/internal/script")
cmd.Env = env cmd.Env = env
cmd.Stdout = doc cmd.Stdout = doc
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {