github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/helpers/errors_test.go (about)

     1  package helpers_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/caos/orbos/internal/helpers"
     9  	"github.com/caos/orbos/mntr"
    10  )
    11  
    12  func TestConcatKeepsUserErrorType(t *testing.T) {
    13  	type args struct {
    14  		left  error
    15  		right error
    16  	}
    17  	tests := []struct {
    18  		name    string
    19  		args    args
    20  		wantErr bool
    21  	}{{
    22  		name: "When both errors are of type UserError, the returned error should also be of type UserError",
    23  		args: args{
    24  			left:  mntr.UserError{Err: errors.New("left")},
    25  			right: mntr.UserError{Err: errors.New("right")},
    26  		},
    27  		wantErr: true,
    28  	}, {
    29  		name: "When only left error is of type UserError, the returned error should not be of type UserError",
    30  		args: args{
    31  			left:  mntr.UserError{Err: errors.New("left")},
    32  			right: errors.New("right"),
    33  		},
    34  		wantErr: false,
    35  	}, {
    36  		name: "When only right error is of type UserError, the returned error should not be of type UserError",
    37  		args: args{
    38  			left:  errors.New("left"),
    39  			right: mntr.UserError{Err: errors.New("right")},
    40  		},
    41  		wantErr: false,
    42  	}, {
    43  		name: "When only left error is non-nil and of type UserError, the returned error should also be of type UserError",
    44  		args: args{
    45  			left:  mntr.UserError{Err: errors.New("left")},
    46  			right: nil,
    47  		},
    48  		wantErr: true,
    49  	}, {
    50  		name: "When only right error is non-nil and of type UserError, the returned error should also be of type UserError",
    51  		args: args{
    52  			left:  nil,
    53  			right: mntr.UserError{Err: errors.New("right")},
    54  		},
    55  		wantErr: true,
    56  	}, {
    57  		name: "When only left error is non-nil but not of type UserError, the returned error should also not be of type UserError",
    58  		args: args{
    59  			left:  errors.New("left"),
    60  			right: nil,
    61  		},
    62  		wantErr: false,
    63  	}, {
    64  		name: "When only right error is non-nil but not of type UserError, the returned error should also not be of type UserError",
    65  		args: args{
    66  			left:  nil,
    67  			right: errors.New("right"),
    68  		},
    69  		wantErr: false,
    70  	}, {
    71  		name: "When both errors have a nested type UserError, the returned error should also be of type UserError",
    72  		args: args{
    73  			left:  fmt.Errorf("some UserError: %w", mntr.UserError{Err: errors.New("left")}),
    74  			right: fmt.Errorf("second level error: %w", fmt.Errorf("first level error: %w", mntr.UserError{Err: errors.New("right")})),
    75  		},
    76  		wantErr: true,
    77  	}}
    78  	for _, tt := range tests {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			if err := helpers.Concat(tt.args.left, tt.args.right); errors.As(err, &mntr.UserError{}) != tt.wantErr {
    81  				t.Errorf("Concat() error = %v, wantUserError %v", err, tt.wantErr)
    82  			}
    83  		})
    84  	}
    85  }