Skip to content

Commit

Permalink
os/exec: use a TestMain to avoid hijacking stdout for helper commands
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Bryan C. Mills committed Apr 26, 2022
1 parent ad5eaa8 commit 7b31af0
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 252 deletions.
2 changes: 1 addition & 1 deletion src/os/exec/exec_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

func init() {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
if os.Getenv("GO_EXEC_TEST_PID") == "" {
return
}

Expand Down
45 changes: 31 additions & 14 deletions src/os/exec/exec_posix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
package exec_test

import (
"fmt"
"internal/testenv"
"os"
"os/exec"
"os/user"
"path/filepath"
"reflect"
Expand All @@ -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")
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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()
Expand Down
Loading

0 comments on commit 7b31af0

Please sign in to comment.