get.porter.sh/porter@v1.3.0/pkg/exec/builder/errors_test.go (about)

     1  package builder
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  var _ ExitError = TestExitError{}
    11  
    12  type TestExitError struct {
    13  	exitCode int
    14  }
    15  
    16  func (t TestExitError) Error() string {
    17  	return "an error occurred"
    18  }
    19  
    20  func (t TestExitError) ExitCode() int {
    21  	return t.exitCode
    22  }
    23  
    24  func TestIgnoreErrorHandler_HandleError(t *testing.T) {
    25  	ctx := context.Background()
    26  
    27  	t.Run("no error", func(t *testing.T) {
    28  		h := IgnoreErrorHandler{}
    29  		err := h.HandleError(ctx, nil, "", "")
    30  		require.NoError(t, err)
    31  	})
    32  
    33  	t.Run("error - passthrough", func(t *testing.T) {
    34  		h := IgnoreErrorHandler{}
    35  		origErr := &TestExitError{1}
    36  		err := h.HandleError(ctx, origErr, "", "")
    37  		require.Same(t, origErr, err)
    38  	})
    39  
    40  	t.Run("all", func(t *testing.T) {
    41  		h := IgnoreErrorHandler{All: true}
    42  		err := h.HandleError(ctx, TestExitError{1}, "", "")
    43  		require.NoError(t, err)
    44  	})
    45  
    46  	t.Run("allowed exit code", func(t *testing.T) {
    47  		h := IgnoreErrorHandler{ExitCodes: []int{2, 1, 4}}
    48  		err := h.HandleError(ctx, TestExitError{1}, "", "")
    49  		require.NoError(t, err)
    50  	})
    51  
    52  	t.Run("disallowed exit code", func(t *testing.T) {
    53  		h := IgnoreErrorHandler{ExitCodes: []int{2, 1, 4}}
    54  		origErr := &TestExitError{10}
    55  		err := h.HandleError(ctx, origErr, "", "")
    56  		require.Same(t, origErr, err, "The original error should be preserved")
    57  	})
    58  
    59  	t.Run("stderr contains", func(t *testing.T) {
    60  		h := IgnoreErrorHandler{Output: IgnoreErrorWithOutput{Contains: []string{"already exists"}}}
    61  		origErr := &TestExitError{10}
    62  		err := h.HandleError(ctx, origErr, "", "The specified thing already exists")
    63  		require.NoError(t, err)
    64  	})
    65  
    66  	t.Run("stderr does not contain", func(t *testing.T) {
    67  		h := IgnoreErrorHandler{Output: IgnoreErrorWithOutput{Contains: []string{"already exists"}}}
    68  		origErr := &TestExitError{10}
    69  		err := h.HandleError(ctx, origErr, "", "Something went wrong")
    70  		require.Same(t, origErr, err)
    71  	})
    72  
    73  	t.Run("stderr matches regex", func(t *testing.T) {
    74  		h := IgnoreErrorHandler{Output: IgnoreErrorWithOutput{Regex: []string{"(exists|EXISTS)"}}}
    75  		origErr := &TestExitError{10}
    76  		err := h.HandleError(ctx, origErr, "", "something EXISTS")
    77  		require.NoError(t, err)
    78  	})
    79  
    80  	t.Run("stderr does not match regex", func(t *testing.T) {
    81  		h := IgnoreErrorHandler{Output: IgnoreErrorWithOutput{Regex: []string{"(exists|EXISTS)"}}}
    82  		origErr := &TestExitError{10}
    83  		err := h.HandleError(ctx, origErr, "", "something mumble mumble")
    84  		require.Same(t, origErr, err)
    85  	})
    86  }