| 352 | } |
| 353 | |
| 354 | func TestFunction(t *testing.T) { |
| 355 | t.Parallel() |
| 356 | ctx := context.Background() |
| 357 | |
| 358 | p := pipe.New() |
| 359 | p.Add( |
| 360 | pipe.Print("hello world"), |
| 361 | pipe.Function( |
| 362 | "farewell", |
| 363 | func(_ context.Context, _ pipe.Env, stdin io.Reader, stdout io.Writer) error { |
| 364 | buf, err := io.ReadAll(stdin) |
| 365 | if err != nil { |
| 366 | return err |
| 367 | } |
| 368 | if string(buf) != "hello world" { |
| 369 | return fmt.Errorf("expected \"hello world\"; got %q", string(buf)) |
| 370 | } |
| 371 | _, err = stdout.Write([]byte("goodbye, cruel world")) |
| 372 | return err |
| 373 | }, |
| 374 | ), |
| 375 | ) |
| 376 | |
| 377 | out, err := p.Output(ctx) |
| 378 | assert.NoError(t, err) |
| 379 | assert.EqualValues(t, "goodbye, cruel world", out) |
| 380 | } |
| 381 | |
| 382 | func TestPipelineWithFunction(t *testing.T) { |
| 383 | t.Parallel() |