-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exitcode_test.go
71 lines (63 loc) · 1.45 KB
/
exitcode_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
71
// Copyright (c) 2020, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package errors
import (
stderrors "errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestWithExitCode(t *testing.T) {
for name, wantErr := range provideErrors(true) {
t.Run(name, func(t *testing.T) {
haveErr := WithExitCode(wantErr, 123)
assert.Exactly(t, 123, GetExitCode(haveErr))
assert.ErrorIs(t, haveErr, wantErr)
// update existing exitcode
t.Run("update", func(t *testing.T) {
haveErr2 := WithExitCode(haveErr, 987)
assert.Exactly(t, 987, GetExitCode(haveErr2))
assert.Same(t, haveErr, haveErr2)
})
})
}
t.Run("nil", func(t *testing.T) {
assert.Exactly(t, nil, WithExitCode(nil, 666))
})
}
func TestGetExitCodeOr(t *testing.T) {
tests := map[string]struct {
err error
or int
want int
}{
"nil": {
err: nil,
or: 12,
want: 12,
},
"std error": {
err: stderrors.New("std err"),
or: 23,
want: 23,
},
"std error with exit code": {
err: WithExitCode(stderrors.New("std err"), 12),
want: 12,
},
"error": {
err: New("some error without exit code"),
or: 99,
want: 99,
},
"error with exit code": {
err: WithExitCode(New("bar"), 34),
want: 34,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
assert.Exactly(t, tc.want, GetExitCodeOr(tc.err, tc.or))
})
}
}