-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute_root_test.go
61 lines (57 loc) · 1.35 KB
/
execute_root_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
// Copyright © 2022 Patrik Neu
// SPDX-License-Identifier: Apache-2.0
package cobrax
import (
"errors"
"github.com/spf13/cobra"
"testing"
)
func TestExecuteRootIntegration(t *testing.T) {
lastExitCode := -1
osExit = func(code int) {
lastExitCode = code
}
for _, tc := range []struct {
desc string
expectedExitCode int
err error
}{
{
desc: "no error",
expectedExitCode: 0,
},
{
desc: "ErrNoPrintButFail",
err: ErrNoPrintButFail,
expectedExitCode: 1,
},
{
desc: "WithExitCode",
err: WithExitCode(errors.New("foo"), 2),
expectedExitCode: 2,
},
{
desc: "ErrNoPrintButFail with WithExitCode",
err: WithExitCode(ErrNoPrintButFail, 3),
expectedExitCode: 3,
},
{
desc: "WithExitCode 0",
err: WithExitCode(errors.New("foo"), 0),
expectedExitCode: 0,
},
{
desc: "other error",
err: errors.New("some error"),
expectedExitCode: 1,
},
} {
t.Run(tc.desc, func(t *testing.T) {
ExecuteRootCommand(&cobra.Command{RunE: func(*cobra.Command, []string) error { return tc.err }})
if lastExitCode != tc.expectedExitCode {
t.Errorf("Expected exit code %d, got %d", tc.expectedExitCode, lastExitCode)
t.FailNow()
}
})
}
}