-
Notifications
You must be signed in to change notification settings - Fork 56
/
formatted_error_test.go
70 lines (67 loc) · 1.48 KB
/
formatted_error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"errors"
"fmt"
"testing"
)
func createWrappedError(reasons ...string) error {
var err error
for i := len(reasons) - 1; i >= 0; i-- {
reason := reasons[i]
if i == len(reasons)-1 {
err = errors.New(reason)
} else {
err = fmt.Errorf("%s: %w", reason, err)
}
}
return err
}
func Test_logError(t *testing.T) {
tests := []struct {
name string
err error
expected string
}{
{
name: "Simple error",
err: errors.New("step run failed"),
expected: "step run failed",
},
{
name: "Wrapped error",
err: createWrappedError("step run failed", "archiving the project failed", "command (xcodebuild archive) failed with exit status 65"),
expected: `step run failed:
archiving the project failed:
command (xcodebuild archive) failed with exit status 65`,
},
{
name: "Not wrapped error",
err: fmt.Errorf("error 1: %s", fmt.Errorf("error 2")),
expected: "error 1: error 2",
},
{
name: "Multi line error",
err: errors.New(`error: error 1
error: error 2
`),
expected: `error: error 1
error: error 2`,
},
{
name: "Multi line wrapped error",
err: createWrappedError("step run failed", `error: error 1
error: error 2`),
expected: `step run failed:
error: error 1
error: error 2`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := formattedError(tt.err)
if s != tt.expected {
t.Fatalf("expected (%s) != actual(%s)", tt.expected, s)
}
})
}
}