@@ -436,28 +436,53 @@ func TestFunction(t *testing.T) {
436436
437437 dir := t .TempDir ()
438438
439- p := pipe .New (pipe .WithDir (dir ))
440- p .Add (
441- pipe .Print ("hello world" ),
442- pipe .Function (
443- "farewell" ,
444- func (_ context.Context , _ pipe.Env , stdin io.Reader , stdout io.Writer ) error {
445- buf , err := io .ReadAll (stdin )
446- if err != nil {
439+ t .Run ("successful function" , func (t * testing.T ) {
440+ p := pipe .New (pipe .WithDir (dir ))
441+ p .Add (
442+ pipe .Print ("hello world" ),
443+ pipe .Function (
444+ "farewell" ,
445+ func (_ context.Context , _ pipe.Env , stdin io.Reader , stdout io.Writer ) error {
446+ buf , err := io .ReadAll (stdin )
447+ if err != nil {
448+ return err
449+ }
450+ if string (buf ) != "hello world" {
451+ return fmt .Errorf ("expected \" hello world\" ; got %q" , string (buf ))
452+ }
453+ _ , err = stdout .Write ([]byte ("goodbye, cruel world" ))
447454 return err
448- }
449- if string (buf ) != "hello world" {
450- return fmt .Errorf ("expected \" hello world\" ; got %q" , string (buf ))
451- }
452- _ , err = stdout .Write ([]byte ("goodbye, cruel world" ))
455+ },
456+ ),
457+ )
458+
459+ out , err := p .Output (ctx )
460+ assert .NoError (t , err )
461+ assert .EqualValues (t , "goodbye, cruel world" , out )
462+ })
463+
464+ t .Run ("panic with handler" , func (t * testing.T ) {
465+ p := pipe .New (
466+ pipe .WithDir (dir ),
467+ pipe .WithStagePanicHandler (func (p any ) error {
468+ err := fmt .Errorf ("panic handled: %v" , p )
453469 return err
454- },
455- ),
456- )
470+ }),
471+ )
472+ p .Add (
473+ pipe .Print ("hello world" ),
474+ pipe .Function (
475+ "farewell" ,
476+ func (_ context.Context , _ pipe.Env , stdin io.Reader , stdout io.Writer ) error {
477+ panic ("this is a panic" )
478+ },
479+ ),
480+ )
457481
458- out , err := p .Output (ctx )
459- assert .NoError (t , err )
460- assert .EqualValues (t , "goodbye, cruel world" , out )
482+ out , err := p .Output (ctx )
483+ assert .ErrorContains (t , err , "panic handled" )
484+ assert .Empty (t , out )
485+ })
461486}
462487
463488func TestPipelineWithFunction (t * testing.T ) {
0 commit comments