From 258104ae329801b9194f9a76eeba73e87f596f27 Mon Sep 17 00:00:00 2001 From: Aaron Jacobs Date: Tue, 24 Mar 2015 09:22:39 +1100 Subject: [PATCH] Include stderr in exit errors. --- samples/subprocess.go | 46 ++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/samples/subprocess.go b/samples/subprocess.go index e4b313a..b9830d4 100644 --- a/samples/subprocess.go +++ b/samples/subprocess.go @@ -15,6 +15,7 @@ package samples import ( + "bytes" "fmt" "io" "io/ioutil" @@ -48,7 +49,9 @@ type SubprocessTest struct { // fail if closing fails. ToClose []io.Closer - mountCmd *exec.Cmd + mountCmd *exec.Cmd + mountStdout bytes.Buffer + mountStderr bytes.Buffer } // Mount the file system and initialize the other exported fields of the @@ -109,17 +112,6 @@ func buildMountSample() (toolPath string, err error) { return } -// Invoke mount_sample, returning a running command. -func invokeMountSample(path string, args []string) (cmd *exec.Cmd, err error) { - cmd = exec.Command(path, args...) - if err = cmd.Start(); err != nil { - err = fmt.Errorf("Start: %v", err) - return - } - - return -} - // Like SetUp, but doens't panic. func (t *SubprocessTest) initialize() (err error) { // Initialize the context. @@ -139,13 +131,22 @@ func (t *SubprocessTest) initialize() (err error) { return } - // Invoke it. - args := []string{"--type", t.MountType} + // Set up a command. + args := []string{ + toolPath, + "--type", + t.MountType, + } + args = append(args, t.MountFlags...) - t.mountCmd, err = invokeMountSample(toolPath, args) - if err != nil { - err = fmt.Errorf("invokeMountSample: %v", err) + t.mountCmd = exec.Command(toolPath, args...) + t.mountCmd.Stdout = &t.mountStdout + t.mountCmd.Stderr = &t.mountStderr + + // Start it. + if err = t.mountCmd.Start(); err != nil { + err = fmt.Errorf("mountCmd.Start: %v", err) return } @@ -201,7 +202,16 @@ func (t *SubprocessTest) destroy() (err error) { // Wait for the subprocess. if err = t.mountCmd.Wait(); err != nil { - err = fmt.Errorf("Cmd.Wait: %v", err) + if exitErr, ok := err.(*exec.ExitError); ok { + err = fmt.Errorf( + "mount_sample exited with %v. Stderr:\n%s", + exitErr, + t.mountStderr.String()) + + return + } + + err = fmt.Errorf("mountCmd.Wait: %v", err) return }