github.com/jdolitsky/cnab-go@v0.7.1-beta1/action/operation_configs_test.go (about)

     1  package action
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/deislabs/cnab-go/driver"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestOperationConfigs_ApplyConfig(t *testing.T) {
    14  	t.Run("no config defined", func(t *testing.T) {
    15  		a := OperationConfigs{}
    16  		op := &driver.Operation{}
    17  		err := a.ApplyConfig(op)
    18  		assert.NoError(t, err, "ApplyConfig should not have returned an error")
    19  	})
    20  
    21  	t.Run("all config is persisted", func(t *testing.T) {
    22  		a := OperationConfigs{
    23  			func(op *driver.Operation) error {
    24  				if op.Files == nil {
    25  					op.Files = make(map[string]string, 1)
    26  				}
    27  				op.Files["a"] = "b"
    28  				return nil
    29  			},
    30  			func(op *driver.Operation) error {
    31  				op.Out = os.Stdout
    32  				return nil
    33  			},
    34  		}
    35  		op := &driver.Operation{}
    36  		err := a.ApplyConfig(op)
    37  		require.NoError(t, err, "ApplyConfig should not have returned an error")
    38  		assert.Contains(t, op.Files, "a", "Changes from the first config function were not persisted")
    39  		assert.Equal(t, os.Stdout, op.Out, "Changes from the second config function were not persisted")
    40  	})
    41  
    42  	t.Run("error is returned immediately", func(t *testing.T) {
    43  		a := OperationConfigs{
    44  			func(op *driver.Operation) error {
    45  				return errors.New("oops")
    46  			},
    47  			func(op *driver.Operation) error {
    48  				op.Out = os.Stdout
    49  				return nil
    50  			},
    51  		}
    52  		op := &driver.Operation{}
    53  		err := a.ApplyConfig(op)
    54  		require.EqualError(t, err, "oops")
    55  		require.Nil(t, op.Out, "Changes from the second config function should not have been applied")
    56  	})
    57  }