-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pipe package, streamline error handling in vcs
- Loading branch information
1 parent
dcd027f
commit aa28182
Showing
4 changed files
with
158 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package pipe | ||
|
||
// Op is the common pipe operation. Can be composed into Ops and run as a single unit | ||
type Op interface { | ||
Do() error | ||
} | ||
|
||
// OpFunc can easily wrap an anonymous function into an Op | ||
type OpFunc func() error | ||
|
||
// Do implements the Op interface | ||
func (o OpFunc) Do() error { | ||
return o() | ||
} | ||
|
||
// Ops can run a slice of Op's in series, stopping on the first error | ||
type Ops []Op | ||
|
||
// Do implements the Op interface | ||
func (ops Ops) Do() error { | ||
for _, op := range ops { | ||
if err := op.Do(); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package pipe | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestOpFuncDo(t *testing.T) { | ||
t.Run("no error", func(t *testing.T) { | ||
op := OpFunc(func() error { | ||
return nil | ||
}) | ||
assert.NoError(t, op.Do()) | ||
}) | ||
|
||
t.Run("error", func(t *testing.T) { | ||
someErr := errors.New("some error") | ||
op := OpFunc(func() error { | ||
return someErr | ||
}) | ||
assert.Equal(t, someErr, op.Do()) | ||
}) | ||
} | ||
|
||
func TestOpsDo(t *testing.T) { | ||
nilOp := OpFunc(func() error { | ||
return nil | ||
}) | ||
someErr := errors.New("some error") | ||
errOp := OpFunc(func() error { | ||
return someErr | ||
}) | ||
|
||
detectOp := func(ran *bool) Op { | ||
return OpFunc(func() error { | ||
*ran = true | ||
return nil | ||
}) | ||
} | ||
|
||
t.Run("no errors", func(t *testing.T) { | ||
var ranLast bool | ||
assert.NoError(t, Ops{nilOp, nilOp, nilOp, detectOp(&ranLast)}.Do()) | ||
assert.True(t, ranLast) | ||
}) | ||
|
||
t.Run("stops on first error", func(t *testing.T) { | ||
var ranAfterError bool | ||
assert.Equal(t, someErr, Ops{nilOp, errOp, detectOp(&ranAfterError), nilOp}.Do()) | ||
assert.False(t, ranAfterError) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters