test_helpers: use an intermediate pipe for subprocess stdout

To Go test logic waits for stderr and stdout to close, so
when we share it with a subprocess, it will wait for it to
exit as well.

We don't want the tests to hang when the unmount fails.

Seen on MacOS as reported at
https://github.com/rfjakob/gocryptfs/issues/213
This commit is contained in:
Jakob Unterwurzacher 2018-02-28 20:03:54 +01:00
parent b96e3ee271
commit 48d5f10c79
1 changed files with 12 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
@ -157,8 +158,17 @@ func Mount(c string, p string, showOutput bool, extraArgs ...string) error {
cmd := exec.Command(GocryptfsBinary, args...)
if showOutput {
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
// The Go test logic waits for our stdout to close, and when we share
// it with the subprocess, it will wait for it to close it as well.
// Use an intermediate pipe so the tests do not hang when unmouting
// fails.
pr, pw, err := os.Pipe()
if err != nil {
return err
}
cmd.Stderr = pw
cmd.Stdout = pw
go func() { io.Copy(os.Stdout, pr) }()
}
return cmd.Run()