| 380 | } |
| 381 | |
| 382 | func TestPipelineWithFunction(t *testing.T) { |
| 383 | t.Parallel() |
| 384 | ctx := context.Background() |
| 385 | |
| 386 | p := pipe.New() |
| 387 | p.Add( |
| 388 | pipe.Command("echo", "-n", "hello world"), |
| 389 | pipe.Function( |
| 390 | "farewell", |
| 391 | func(_ context.Context, _ pipe.Env, stdin io.Reader, stdout io.Writer) error { |
| 392 | buf, err := io.ReadAll(stdin) |
| 393 | if err != nil { |
| 394 | return err |
| 395 | } |
| 396 | if string(buf) != "hello world" { |
| 397 | return fmt.Errorf("expected \"hello world\"; got %q", string(buf)) |
| 398 | } |
| 399 | _, err = stdout.Write([]byte("goodbye, cruel world")) |
| 400 | return err |
| 401 | }, |
| 402 | ), |
| 403 | pipe.Command("tr", "a-z", "A-Z"), |
| 404 | ) |
| 405 | |
| 406 | out, err := p.Output(ctx) |
| 407 | assert.NoError(t, err) |
| 408 | assert.EqualValues(t, "GOODBYE, CRUEL WORLD", out) |
| 409 | } |
| 410 | |
| 411 | type ErrorStartingStage struct { |
| 412 | err error |