Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/abort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestExample_Abort(t *testing.T) {
)
result := p.Run()
assert.True(t, result.IsSuccessful())
assert.True(t, result.IsAborted())
}

func doNotExecute(_ pipeline.Context) error {
Expand Down
5 changes: 4 additions & 1 deletion pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ type (
// Result is the object that is returned after each step and after running a pipeline.
Result struct {
// Err contains the step's returned error, nil otherwise.
// In an aborted pipeline with ErrAbort it will still be nil.
Err error
// Name is an optional identifier for a result.
// ActionFunc may set this property before returning to help a ResultHandler with further processing.
Name string

aborted bool
}
// Step is an intermediary action and part of a Pipeline.
Step struct {
Expand Down Expand Up @@ -144,7 +147,7 @@ func (p *Pipeline) doRun() Result {
if err != nil {
if errors.Is(err, ErrAbort) {
// Abort pipeline without error
return Result{}
return Result{aborted: true}
}
if p.disableErrorWrapping {
return Result{Err: err}
Expand Down
17 changes: 12 additions & 5 deletions pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ func TestPipeline_Run(t *testing.T) {
callCount := 0
hook := &hook{}
tests := map[string]struct {
givenSteps []Step
givenBeforeHook Listener
givenFinalizer ResultHandler
expectErrorString string
expectedCalls int
givenSteps []Step
givenBeforeHook Listener
givenFinalizer ResultHandler
expectErrorString string
expectedCalls int
additionalAssertions func(t *testing.T, result Result)
}{
"GivenSingleStep_WhenRunning_ThenCallStep": {
givenSteps: []Step{
Expand Down Expand Up @@ -74,6 +75,9 @@ func TestPipeline_Run(t *testing.T) {
}),
},
expectedCalls: 1,
additionalAssertions: func(t *testing.T, result Result) {
assert.True(t, result.IsAborted())
},
},
"GivenSingleStepWithHandler_WhenRunningWithError_ThenAbortWithError": {
givenSteps: []Step{
Expand Down Expand Up @@ -153,6 +157,9 @@ func TestPipeline_Run(t *testing.T) {
assert.True(t, actualResult.IsSuccessful())
}
assert.Equal(t, tt.expectedCalls, callCount)
if tt.additionalAssertions != nil {
tt.additionalAssertions(t, actualResult)
}
})
}
}
Expand Down
7 changes: 7 additions & 0 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import "errors"
var ErrAbort = errors.New("abort")

// IsSuccessful returns true if the contained error is nil.
// Aborted pipelines (with ErrAbort) are still reported as success.
// To query if a pipeline is aborted early, use IsAborted.
func (r Result) IsSuccessful() bool {
return r.Err == nil
}
Expand All @@ -14,3 +16,8 @@ func (r Result) IsSuccessful() bool {
func (r Result) IsFailed() bool {
return r.Err != nil
}

// IsAborted returns true if the pipeline didn't stop with an error, but just aborted early with ErrAbort.
func (r Result) IsAborted() bool {
return r.aborted
}