-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy patherror_test.go
77 lines (72 loc) · 1.31 KB
/
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
71
72
73
74
75
76
77
package main_test
import (
"bytes"
"fmt"
"log"
"os"
"testing"
"bou.ke/monkey"
"github.com/codegangsta/cli"
"github.com/corvus-ch/rabbitmq-cli-consumer"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
var exitErrHandlerTests = []struct {
name string
err error
out string
exit string
}{
{
"empty",
nil,
"",
"",
},
{
"exitCode",
cli.NewExitError("", 42),
"",
"os.Exit called with: 42",
},
{
"output",
fmt.Errorf("normal error"),
"normal error\n",
"os.Exit called with: 1",
},
{
"exitCodeOutput",
cli.NewExitError("exit code error", 42),
"exit code error\n",
"os.Exit called with: 42",
},
{
"outputFormatted",
errors.WithMessage(fmt.Errorf("error"), "nested"),
"error\nnested\n",
"os.Exit called with: 1",
},
}
func TestExitErrHandler(t *testing.T) {
log.SetFlags(0)
patch := monkey.Patch(os.Exit, func(code int) {
panic(fmt.Sprintf("os.Exit called with: %v", code))
})
defer patch.Unpatch()
for _, test := range exitErrHandlerTests {
t.Run(test.name, func(t *testing.T) {
buf := &bytes.Buffer{}
log.SetOutput(buf)
h := func() {
main.ExitErrHandler(nil, test.err)
}
if test.exit == "" {
h()
} else {
assert.PanicsWithValue(t, test.exit, h, "os.Exit was not called")
}
assert.Equal(t, test.out, buf.String())
})
}
}