forked from jawher/mow.cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_test.go
164 lines (125 loc) · 3.42 KB
/
cli_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package cli
import (
"flag"
"github.com/stretchr/testify/require"
"testing"
)
func TestTheCpCase(t *testing.T) {
app := App("cp", "")
app.Spec = "SRC... DST"
src := app.Strings(StringsArg{Name: "SRC", Value: nil, Desc: ""})
dst := app.String(StringArg{Name: "DST", Value: "", Desc: ""})
ex := false
app.Action = func() {
ex = true
}
app.Run([]string{"cp", "x", "y", "z"})
require.Equal(t, []string{"x", "y"}, *src)
require.Equal(t, "z", *dst)
require.True(t, ex, "Exec wasn't called")
}
func TestImplicitSpec(t *testing.T) {
app := App("test", "")
x := app.Bool(BoolOpt{Name: "x", Value: false, Desc: ""})
y := app.String(StringOpt{Name: "y", Value: "", Desc: ""})
called := false
app.Action = func() {
called = true
}
app.ErrorHandling = flag.ContinueOnError
err := app.Run([]string{"test", "-x", "-y", "hello"})
require.Nil(t, err)
require.True(t, *x)
require.Equal(t, "hello", *y)
require.True(t, called, "Exec wasn't called")
}
func TestHelpShortcut(t *testing.T) {
defer suppressOutput()()
exitCalled := false
defer exitShouldBeCalledWith(t, 2, &exitCalled)()
app := App("x", "")
app.Spec = "Y"
app.String(StringArg{Name: "Y", Value: "", Desc: ""})
actionCalled := false
app.Action = func() {
actionCalled = true
}
app.Run([]string{"x", "y", "-h", "z"})
require.False(t, actionCalled, "action should not have been called")
require.True(t, exitCalled, "exit should have been called")
}
func TestVersionShortcut(t *testing.T) {
defer suppressOutput()()
exitCalled := false
defer exitShouldBeCalledWith(t, 0, &exitCalled)()
app := App("cp", "")
app.Version("v version", "cp 1.2.3")
actionCalled := false
app.Action = func() {
actionCalled = true
}
app.Run([]string{"cp", "--version"})
require.False(t, actionCalled, "action should not have been called")
require.True(t, exitCalled, "exit should have been called")
}
func TestSubCommands(t *testing.T) {
app := App("say", "")
hi, bye := false, false
app.Command("hi", "", func(cmd *Cmd) {
cmd.Action = func() {
hi = true
}
})
app.Command("byte", "", func(cmd *Cmd) {
cmd.Action = func() {
bye = true
}
})
app.Run([]string{"say", "hi"})
require.True(t, hi, "hi should have been called")
require.False(t, bye, "byte should NOT have been called")
}
func TestContinueOnError(t *testing.T) {
defer exitShouldNotCalled(t)()
defer suppressOutput()()
app := App("say", "")
app.String(StringOpt{Name: "f", Value: "", Desc: ""})
app.Spec = "-f"
app.ErrorHandling = flag.ContinueOnError
called := false
app.Action = func() {
called = true
}
err := app.Run([]string{"say"})
require.NotNil(t, err)
require.False(t, called, "Exec should NOT have been called")
}
func TestExitOnError(t *testing.T) {
defer suppressOutput()()
exitCalled := false
defer exitShouldBeCalledWith(t, 2, &exitCalled)()
app := App("x", "")
app.Spec = "Y"
app.String(StringArg{Name: "Y", Value: "", Desc: ""})
app.Run([]string{"x", "y", "z"})
require.True(t, exitCalled, "exit should have been called")
}
func TestPanicOnError(t *testing.T) {
defer suppressOutput()()
app := App("say", "")
app.String(StringOpt{Name: "f", Value: "", Desc: ""})
app.Spec = "-f"
app.ErrorHandling = flag.PanicOnError
called := false
app.Action = func() {
called = true
}
defer func() {
if r := recover(); r != nil {
require.False(t, called, "Exec should NOT have been called")
} else {
}
}()
app.Run([]string{"say"})
t.Fatalf("wanted panic")
}