-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathmain_test.go
50 lines (41 loc) · 1.11 KB
/
main_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
package main
import (
"bytes"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"
)
func TestExitCodesWorkProperly(t *testing.T) {
testcases := []struct {
arguments []string
expectedExitCode int
expectedText string
}{
{arguments: []string{}, expectedExitCode: 0, expectedText: "USAGE"},
{arguments: []string{"--config", "/tmp/doesnotexist123", "start"}, expectedExitCode: 1, expectedText: "no config files found"},
}
cli.OsExiter = func(int) {}
for idx, tc := range testcases {
t.Run(strconv.Itoa(idx), func(t *testing.T) {
tc := tc
c := buildCLI()
c.ExitErrHandler = func(_ *cli.Context, err error) {}
var buf bytes.Buffer
c.Writer = &buf
c.ErrWriter = &buf
args := append([]string{"./ui-server"}, tc.arguments...)
err := c.Run(args)
if tc.expectedExitCode != 0 {
assert.Error(t, err)
}
if err != nil {
ec := err.(cli.ExitCoder)
assert.Equal(t, tc.expectedExitCode, ec.ExitCode())
assert.Contains(t, ec.Error(), tc.expectedText)
} else {
assert.Contains(t, buf.String(), tc.expectedText)
}
})
}
}