-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from carolynvs/op-config-func
Support tweaking the operation before running it
- Loading branch information
Showing
14 changed files
with
353 additions
and
47 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package action | ||
|
||
import ( | ||
"github.com/deislabs/cnab-go/driver" | ||
) | ||
|
||
// OperationConfigFunc is a configuration function that can be applied to an | ||
// operation. | ||
type OperationConfigFunc func(op *driver.Operation) error | ||
|
||
// OperationConfigs is a set of configuration functions that can be applied as a | ||
// unit to an operation. | ||
type OperationConfigs []OperationConfigFunc | ||
|
||
// ApplyConfig safely applies the configuration function to the operation, if | ||
// defined, and stops immediately upon the first error. | ||
func (cfgs OperationConfigs) ApplyConfig(op *driver.Operation) error { | ||
var err error | ||
for _, cfg := range cfgs { | ||
err = cfg(op) | ||
if 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,57 @@ | ||
package action | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"testing" | ||
|
||
"github.com/deislabs/cnab-go/driver" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestOperationConfigs_ApplyConfig(t *testing.T) { | ||
t.Run("no config defined", func(t *testing.T) { | ||
a := OperationConfigs{} | ||
op := &driver.Operation{} | ||
err := a.ApplyConfig(op) | ||
assert.NoError(t, err, "ApplyConfig should not have returned an error") | ||
}) | ||
|
||
t.Run("all config is persisted", func(t *testing.T) { | ||
a := OperationConfigs{ | ||
func(op *driver.Operation) error { | ||
if op.Files == nil { | ||
op.Files = make(map[string]string, 1) | ||
} | ||
op.Files["a"] = "b" | ||
return nil | ||
}, | ||
func(op *driver.Operation) error { | ||
op.Out = os.Stdout | ||
return nil | ||
}, | ||
} | ||
op := &driver.Operation{} | ||
err := a.ApplyConfig(op) | ||
require.NoError(t, err, "ApplyConfig should not have returned an error") | ||
assert.Contains(t, op.Files, "a", "Changes from the first config function were not persisted") | ||
assert.Equal(t, os.Stdout, op.Out, "Changes from the second config function were not persisted") | ||
}) | ||
|
||
t.Run("error is returned immediately", func(t *testing.T) { | ||
a := OperationConfigs{ | ||
func(op *driver.Operation) error { | ||
return errors.New("oops") | ||
}, | ||
func(op *driver.Operation) error { | ||
op.Out = os.Stdout | ||
return nil | ||
}, | ||
} | ||
op := &driver.Operation{} | ||
err := a.ApplyConfig(op) | ||
require.EqualError(t, err, "oops") | ||
require.Nil(t, op.Out, "Changes from the second config function should not have been applied") | ||
}) | ||
} |
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
Oops, something went wrong.