WaitDnoteCmd runs a dnote command and passes stdout to the callback.
(t *testing.T, opts RunDnoteCmdOptions, runFunc func(io.Reader, io.WriteCloser) error, binaryName string, arg ...string)
| 168 | |
| 169 | // WaitDnoteCmd runs a dnote command and passes stdout to the callback. |
| 170 | func WaitDnoteCmd(t *testing.T, opts RunDnoteCmdOptions, runFunc func(io.Reader, io.WriteCloser) error, binaryName string, arg ...string) (string, error) { |
| 171 | t.Logf("running: %s %s", binaryName, strings.Join(arg, " ")) |
| 172 | |
| 173 | binaryPath, err := filepath.Abs(binaryName) |
| 174 | if err != nil { |
| 175 | return "", errors.Wrap(err, "getting absolute path to test binary") |
| 176 | } |
| 177 | |
| 178 | cmd := exec.Command(binaryPath, arg...) |
| 179 | cmd.Env = opts.Env |
| 180 | |
| 181 | var stderr bytes.Buffer |
| 182 | cmd.Stderr = &stderr |
| 183 | |
| 184 | stdout, err := cmd.StdoutPipe() |
| 185 | if err != nil { |
| 186 | return "", errors.Wrap(err, "getting stdout pipe") |
| 187 | } |
| 188 | |
| 189 | stdin, err := cmd.StdinPipe() |
| 190 | if err != nil { |
| 191 | return "", errors.Wrap(err, "getting stdin") |
| 192 | } |
| 193 | defer stdin.Close() |
| 194 | |
| 195 | if err = cmd.Start(); err != nil { |
| 196 | return "", errors.Wrap(err, "starting command") |
| 197 | } |
| 198 | |
| 199 | var output bytes.Buffer |
| 200 | tee := io.TeeReader(stdout, &output) |
| 201 | |
| 202 | err = runFunc(tee, stdin) |
| 203 | if err != nil { |
| 204 | t.Logf("\n%s", output.String()) |
| 205 | return output.String(), errors.Wrap(err, "running callback") |
| 206 | } |
| 207 | |
| 208 | io.Copy(&output, stdout) |
| 209 | |
| 210 | if err := cmd.Wait(); err != nil { |
| 211 | t.Logf("\n%s", output.String()) |
| 212 | return output.String(), errors.Wrapf(err, "command failed: %s", stderr.String()) |
| 213 | } |
| 214 | |
| 215 | t.Logf("\n%s", output.String()) |
| 216 | return output.String(), nil |
| 217 | } |
| 218 | |
| 219 | func MustWaitDnoteCmd(t *testing.T, opts RunDnoteCmdOptions, runFunc func(io.Reader, io.WriteCloser) error, binaryName string, arg ...string) string { |
| 220 | output, err := WaitDnoteCmd(t, opts, runFunc, binaryName, arg...) |
no test coverage detected