-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move handler into ConfigurableAction struct and add tests
- Loading branch information
1 parent
a95a07c
commit e65856c
Showing
12 changed files
with
284 additions
and
49 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,23 @@ | ||
package action | ||
|
||
import ( | ||
"github.com/deislabs/cnab-go/driver" | ||
) | ||
|
||
type OperationConfigFunc func(op *driver.Operation) error | ||
|
||
// ConfigurableAction is used to define actions which can configure operations before they are executed. | ||
type ConfigurableAction struct { | ||
// OperationConfig is an optional handler that applies additional | ||
// configuration to an operation before it is executed. | ||
OperationConfig OperationConfigFunc | ||
} | ||
|
||
// ApplyConfig safely applies the configuration function to the operation, if defined, | ||
// and returns any error. | ||
func (a ConfigurableAction) ApplyConfig(op *driver.Operation) error { | ||
if a.OperationConfig != nil { | ||
return a.OperationConfig(op) | ||
} | ||
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,46 @@ | ||
package action | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/deislabs/cnab-go/driver" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConfigurableAction_ApplyConfig(t *testing.T) { | ||
t.Run("no config defined", func(t *testing.T) { | ||
a := ConfigurableAction{} | ||
op := &driver.Operation{} | ||
err := a.ApplyConfig(op) | ||
assert.NoError(t, err, "ApplyConfig should not have returned an error") | ||
}) | ||
|
||
t.Run("config is persisted", func(t *testing.T) { | ||
a := ConfigurableAction{ | ||
OperationConfig: func(op *driver.Operation) error { | ||
if op.Files == nil { | ||
op.Files = make(map[string]string, 1) | ||
} | ||
op.Files["a"] = "b" | ||
return nil | ||
}, | ||
} | ||
op := &driver.Operation{} | ||
err := a.ApplyConfig(op) | ||
assert.NoError(t, err, "ApplyConfig should not have returned an error") | ||
assert.Contains(t, op.Files, "a", "Changes from the config function were not persisted") | ||
}) | ||
|
||
t.Run("error is returned", func(t *testing.T) { | ||
a := ConfigurableAction{ | ||
OperationConfig: func(op *driver.Operation) error { | ||
return errors.New("oops") | ||
}, | ||
} | ||
op := &driver.Operation{} | ||
err := a.ApplyConfig(op) | ||
require.EqualError(t, err, "oops") | ||
}) | ||
} |
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
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
Oops, something went wrong.