Run executes kopia with given arguments and returns the output lines.
(tb testing.TB, expectedError bool, args ...string)
| 293 | |
| 294 | // Run executes kopia with given arguments and returns the output lines. |
| 295 | func (e *CLITest) Run(tb testing.TB, expectedError bool, args ...string) (stdout, stderr []string, err error) { |
| 296 | tb.Helper() |
| 297 | |
| 298 | args = e.cmdArgs(args) |
| 299 | outputPrefix, logOutput := e.getLogOutputPrefix() |
| 300 | tb.Logf("%vrunning 'kopia %v' with %v", outputPrefix, strings.Join(args, " "), e.Environment) |
| 301 | |
| 302 | timer := timetrack.StartTimer() |
| 303 | |
| 304 | stdoutReader, stderrReader, wait, _ := e.Runner.Start(tb, e.RunContext, args, e.Environment) |
| 305 | |
| 306 | var wg sync.WaitGroup |
| 307 | |
| 308 | wg.Go(func() { |
| 309 | scanner := bufio.NewScanner(stdoutReader) |
| 310 | for scanner.Scan() { |
| 311 | if logOutput { |
| 312 | tb.Logf("[%vstdout] %v", outputPrefix, scanner.Text()) |
| 313 | } |
| 314 | |
| 315 | stdout = append(stdout, scanner.Text()) |
| 316 | } |
| 317 | }) |
| 318 | |
| 319 | wg.Go(func() { |
| 320 | scanner := bufio.NewScanner(stderrReader) |
| 321 | for scanner.Scan() { |
| 322 | if logOutput { |
| 323 | tb.Logf("[%vstderr] %v", outputPrefix, scanner.Text()) |
| 324 | } |
| 325 | |
| 326 | stderr = append(stderr, scanner.Text()) |
| 327 | } |
| 328 | }) |
| 329 | |
| 330 | wg.Wait() |
| 331 | |
| 332 | gotErr := wait() |
| 333 | |
| 334 | if expectedError { |
| 335 | require.Error(tb, gotErr, "unexpected success when running 'kopia %v' (stdout:\n%v\nstderr:\n%v", strings.Join(args, " "), strings.Join(stdout, "\n"), strings.Join(stderr, "\n")) |
| 336 | } else { |
| 337 | require.NoError(tb, gotErr, "unexpected error when running 'kopia %v' (stdout:\n%v\nstderr:\n%v", strings.Join(args, " "), strings.Join(stdout, "\n"), strings.Join(stderr, "\n")) |
| 338 | } |
| 339 | |
| 340 | //nolint:forbidigo |
| 341 | tb.Logf("%vfinished in %v: 'kopia %v'", outputPrefix, timer.Elapsed().Milliseconds(), strings.Join(args, " ")) |
| 342 | |
| 343 | return stdout, stderr, gotErr |
| 344 | } |