Skip to content

Commit

Permalink
handle os pipe error in testing
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbloss committed Aug 8, 2024
1 parent 445bfbd commit 330aa12
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/cmd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,25 @@ func execCmd(command Operation, resource string, inputs ...string) ([]byte, erro
cliArgs := []string{string(command), resource}
cliArgs = append(cliArgs, inputs...)

r, oldStdout := redirectStdout()
r, oldStdout, err := redirectStdout()
defer r.Close()
if err != nil {
return nil, err
}

cmd.RootCmd.SetArgs(cliArgs)
err := cmd.RootCmd.Execute()
err = cmd.RootCmd.Execute()

output := captureOutput(r, oldStdout)
return output, err
}

// redirectStdout redirects os.Stdout to a pipe and returns the read and write ends of the pipe.
func redirectStdout() (*os.File, *os.File) {
r, w, _ := os.Pipe()
func redirectStdout() (*os.File, *os.File, error) {
r, w, err := os.Pipe()
oldStdout := os.Stdout
os.Stdout = w
return r, oldStdout
return r, oldStdout, err
}

// captureOutput reads from r until EOF and returns the result as a string.
Expand Down

0 comments on commit 330aa12

Please sign in to comment.