os.Pipe has issues on windows
@@ -3,6 +3,7 @@ package testing
import (
"io/ioutil"
"os"
+ "log"
"github.com/sensu/sensu-go/cli"
"github.com/sensu/sensu-go/cli/client"
@@ -37,9 +38,18 @@ func SimpleSensuCLI(apiClient client.APIClient) *cli.SensuCli {
func RunCmd(cmd *cobra.Command, args []string) (string, error) {
var err error
- // So that we can caputre output we reassign cmd.output
- reader, writer, _ := os.Pipe()
- cmd.SetOutput(writer)
+ // So that we can capture output we reassign cmd.output
+ tmpFile, err := ioutil.TempFile(os.TempDir(), "sensu-cli-")
+ if err != nil {
+ log.Panic("Error creating tmpFile: ", tmpFile.Name())
+ }
+
+ defer func() {
+ tmpFile.Close()
+ os.Remove(tmpFile.Name())
+ }()
+
+ cmd.SetOutput(tmpFile)
// Run given command
if cmd.Run != nil {
@@ -48,12 +58,11 @@ func RunCmd(cmd *cobra.Command, args []string) (string, error) {
err = cmd.RunE(cmd, args)
}
- // Close the writer so that we do not run into
- // a deadlock while reading
- writer.Close()
+ // Close the file so that we can read from it
+ tmpFile.Close()
// Store the contents of the reader as a string
- bytes, _ := ioutil.ReadAll(reader)
+ bytes, _ := ioutil.ReadFile(tmpFile.Name())
return string(bytes), err
}
