aboutsummaryrefslogtreecommitdiff
path: root/src/os/exec/exec_posix_test.go
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2022-04-22 23:33:16 -0400
committerBryan Mills <bcmills@google.com>2022-04-26 14:49:07 +0000
commit7b31af0eae8ce369d5ffd16be1de0b2f0121e7c2 (patch)
tree7b7097d33aa1c4b0fbc23c85b1f95d4124c4662d /src/os/exec/exec_posix_test.go
parentad5eaa8c4cd952df0d4894f11ee0158a9a33a0f3 (diff)
downloadgo-7b31af0eae8ce369d5ffd16be1de0b2f0121e7c2.tar.gz
go-7b31af0eae8ce369d5ffd16be1de0b2f0121e7c2.zip
os/exec: use a TestMain to avoid hijacking stdout for helper commands
The previous implementation of helperCommand relied on running a well-known Test function which implemented all known commands. That not only added Skip noise in the test's output, but also (and more importantly) meant that the commands could not write directly to stdout in the usual way, since the testing package hijacks os.Stdout for its own use. The new implementation addresses the above issues, and also ensures that all registered commands are actually used, reducing the risk of an unused command sticking around after refactoring. It also sets the subprocess environment variable directly in the test process, instead of on each individual helper command's Env field, allowing helper commands to be used without an explicit Env. Updates #50599. (Also for #50436.) Change-Id: I189c7bed9a07cfe47a084b657b88575b1ee370b9 Reviewed-on: https://go-review.googlesource.com/c/go/+/401934 Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/os/exec/exec_posix_test.go')
-rw-r--r--src/os/exec/exec_posix_test.go45
1 files changed, 31 insertions, 14 deletions
diff --git a/src/os/exec/exec_posix_test.go b/src/os/exec/exec_posix_test.go
index e583039453..f0401377e8 100644
--- a/src/os/exec/exec_posix_test.go
+++ b/src/os/exec/exec_posix_test.go
@@ -7,9 +7,9 @@
package exec_test
import (
+ "fmt"
"internal/testenv"
"os"
- "os/exec"
"os/user"
"path/filepath"
"reflect"
@@ -21,8 +21,32 @@ import (
"time"
)
+func init() {
+ registerHelperCommand("pwd", cmdPwd)
+ registerHelperCommand("sleep", cmdSleep)
+}
+
+func cmdPwd(...string) {
+ pwd, err := os.Getwd()
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(1)
+ }
+ fmt.Println(pwd)
+}
+
+func cmdSleep(args ...string) {
+ n, err := strconv.Atoi(args[0])
+ if err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+ time.Sleep(time.Duration(n) * time.Second)
+}
+
func TestCredentialNoSetGroups(t *testing.T) {
if runtime.GOOS == "android" {
+ maySkipHelperCommand("echo")
t.Skip("unsupported on Android")
}
@@ -61,7 +85,7 @@ func TestCredentialNoSetGroups(t *testing.T) {
func TestWaitid(t *testing.T) {
t.Parallel()
- cmd := helperCommand(t, "sleep")
+ cmd := helperCommand(t, "sleep", "3")
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
@@ -97,9 +121,6 @@ func TestWaitid(t *testing.T) {
// implicitly update PWD to the correct path, and Environ should list the
// updated value.
func TestImplicitPWD(t *testing.T) {
- testenv.MustHaveExec(t)
- _, pwdErr := exec.LookPath("pwd")
-
t.Parallel()
cwd, err := os.Getwd()
@@ -124,12 +145,10 @@ func TestImplicitPWD(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
- // Note: we're using the actual "pwd" command here (instead of helperCommand)
- // because the implementation of helperCommand requires a non-empty Env.
- // (We could perhaps refactor helperCommand to use a flag or switch on the
- // value of argv[0] instead, but that doesn't seem worth the trouble at
- // the moment.)
- cmd := exec.Command("pwd", "-L")
+ cmd := helperCommand(t, "pwd")
+ if cmd.Env != nil {
+ t.Fatalf("test requires helperCommand not to set Env field")
+ }
cmd.Dir = tc.dir
var pwds []string
@@ -149,9 +168,6 @@ func TestImplicitPWD(t *testing.T) {
t.Errorf("PWD entries in cmd.Environ():\n\t%s\nwant:\n\t%s", strings.Join(pwds, "\n\t"), strings.Join(wantPWDs, "\n\t"))
}
- if pwdErr != nil {
- t.Skipf("not running `pwd` because it was not found: %v", pwdErr)
- }
cmd.Stderr = new(strings.Builder)
out, err := cmd.Output()
if err != nil {
@@ -170,6 +186,7 @@ func TestImplicitPWD(t *testing.T) {
// (This checks that the implementation for https://go.dev/issue/50599 doesn't
// break existing users who may have explicitly mismatched the PWD variable.)
func TestExplicitPWD(t *testing.T) {
+ maySkipHelperCommand("pwd")
testenv.MustHaveSymlink(t)
cwd, err := os.Getwd()