diff options
author | Bryan C. Mills <bcmills@google.com> | 2022-04-22 23:33:16 -0400 |
---|---|---|
committer | Bryan Mills <bcmills@google.com> | 2022-04-26 14:49:07 +0000 |
commit | 7b31af0eae8ce369d5ffd16be1de0b2f0121e7c2 (patch) | |
tree | 7b7097d33aa1c4b0fbc23c85b1f95d4124c4662d /src/os/exec/lp_windows_test.go | |
parent | ad5eaa8c4cd952df0d4894f11ee0158a9a33a0f3 (diff) | |
download | go-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/lp_windows_test.go')
-rw-r--r-- | src/os/exec/lp_windows_test.go | 50 |
1 files changed, 35 insertions, 15 deletions
diff --git a/src/os/exec/lp_windows_test.go b/src/os/exec/lp_windows_test.go index bbf6a9b7f1..34abe09d04 100644 --- a/src/os/exec/lp_windows_test.go +++ b/src/os/exec/lp_windows_test.go @@ -19,6 +19,31 @@ import ( "testing" ) +func init() { + registerHelperCommand("exec", cmdExec) + registerHelperCommand("lookpath", cmdLookPath) +} + +func cmdLookPath(args ...string) { + p, err := exec.LookPath(args[0]) + if err != nil { + fmt.Fprintf(os.Stderr, "LookPath failed: %v\n", err) + os.Exit(1) + } + fmt.Print(p) +} + +func cmdExec(args ...string) { + cmd := exec.Command(args[1]) + cmd.Dir = args[0] + output, err := cmd.CombinedOutput() + if err != nil { + fmt.Fprintf(os.Stderr, "Child: %s %s", err, string(output)) + os.Exit(1) + } + fmt.Printf("%s", string(output)) +} + func installExe(t *testing.T, dest, src string) { fsrc, err := os.Open(src) if err != nil { @@ -66,10 +91,10 @@ type lookPathTest struct { fails bool // test is expected to fail } -func (test lookPathTest) runProg(t *testing.T, env []string, args ...string) (string, error) { - cmd := exec.Command(args[0], args[1:]...) +func (test lookPathTest) runProg(t *testing.T, env []string, cmd *exec.Cmd) (string, error) { cmd.Env = env cmd.Dir = test.rootDir + args := append([]string(nil), cmd.Args...) args[0] = filepath.Base(args[0]) cmdText := fmt.Sprintf("%q command", strings.Join(args, " ")) out, err := cmd.CombinedOutput() @@ -135,10 +160,9 @@ 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, "cmd", "/c", test.searchFor) + should, errCmd := test.runProg(t, env, exec.Command("cmd", "/c", test.searchFor)) // Run the lookpath program with new environment and work directory set. - env = append(env, "GO_WANT_HELPER_PROCESS=1") - have, errLP := test.runProg(t, env, os.Args[0], "-test.run=TestHelperProcess", "--", "lookpath", test.searchFor) + have, errLP := test.runProg(t, env, helperCommand(t, "lookpath", test.searchFor)) // Compare results. if errCmd == nil && errLP == nil { // both succeeded @@ -346,30 +370,26 @@ func (test commandTest) isSuccess(rootDir, output string, err error) error { return nil } -func (test commandTest) runOne(rootDir string, env []string, dir, arg0 string) error { - cmd := exec.Command(os.Args[0], "-test.run=TestHelperProcess", "--", "exec", dir, arg0) +func (test commandTest) runOne(t *testing.T, rootDir string, env []string, dir, arg0 string) { + cmd := helperCommand(t, "exec", dir, arg0) cmd.Dir = rootDir cmd.Env = env output, err := cmd.CombinedOutput() err = test.isSuccess(rootDir, string(output), err) if (err != nil) != test.fails { if test.fails { - return fmt.Errorf("test=%+v: succeeded, but expected to fail", test) + t.Errorf("test=%+v: succeeded, but expected to fail", test) + } else { + t.Error(err) } - return err } - return nil } func (test commandTest) run(t *testing.T, rootDir, printpathExe string) { createFiles(t, rootDir, test.files, printpathExe) PATHEXT := `.COM;.EXE;.BAT` env := createEnv(rootDir, test.PATH, PATHEXT) - env = append(env, "GO_WANT_HELPER_PROCESS=1") - err := test.runOne(rootDir, env, test.dir, test.arg0) - if err != nil { - t.Error(err) - } + test.runOne(t, rootDir, env, test.dir, test.arg0) } var commandTests = []commandTest{ |