-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathcli_helper_test.go
More file actions
233 lines (214 loc) · 5.19 KB
/
cli_helper_test.go
File metadata and controls
233 lines (214 loc) · 5.19 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package console
import (
"bytes"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/goravel/framework/console/console"
contractsconsole "github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/console/command"
"github.com/goravel/framework/support/color"
)
func TestShowCommandHelp_HelpPrinterCustom(t *testing.T) {
output := &bytes.Buffer{}
cliApp := &Application{
name: "artisan",
usage: "Goravel Framework",
usageText: "artisan command [options] [arguments...]",
useArtisan: true,
version: "v1.16.0",
writer: output,
}
cliApp.Register([]contractsconsole.Command{
&TestFooCommand{},
&TestBarCommand{},
console.NewListCommand(),
})
tests := []struct {
name string
call string
containsOutput []string
}{
{
name: "print commands list",
call: "list",
containsOutput: []string{
color.Yellow().Sprint("Usage:"),
color.Yellow().Sprint("Global options:"),
color.Yellow().Sprint("Available commands:"),
color.Yellow().Sprint("test"),
color.Green().Sprint("test:foo"),
color.Green().Sprint("test:bar"),
},
},
{
name: "print command help",
call: "test:foo --help",
containsOutput: []string{
color.Yellow().Sprint("Description:"),
color.Yellow().Sprint("Usage:"),
color.Yellow().Sprint("Global options:"),
color.Yellow().Sprint("Options:"),
color.Green().Sprint("-b, --bool"),
color.Green().Sprint("-i, --int"),
color.Blue().Sprint("int"),
color.Green().Sprint(" --no-ansi"),
color.Green().Sprint("-h, --help"),
},
},
{
name: "print command help(check flag sorted)",
call: "test:foo --help --no-ansi",
containsOutput: []string{
`Description:
Test command
Usage:
artisan test:foo [options] [arguments...]
Options:
-b, --bool Bool flag [default: false]
-h, --help Show help
-i, --int int flag [default: 0]
--no-ansi Force disable ANSI output`,
},
},
{
name: "print version",
call: "--version",
containsOutput: []string{
"Goravel Framework " + color.Green().Sprint("v1.16.0"),
},
},
{
name: "command not found",
call: "not-found",
containsOutput: []string{
color.New(color.FgLightRed).Sprint("Command 'not-found' is not defined."),
},
},
{
name: "command not found(suggest)",
call: "test",
containsOutput: []string{
color.New(color.FgLightRed).Sprint("Command 'test' is not defined. Did you mean one of these?"),
color.Gray().Sprint(" test:bar"),
color.Gray().Sprint(" test:foo"),
},
},
{
name: "command not found(suggest)",
call: "fo",
containsOutput: []string{
color.New(color.FgLightRed).Sprint("Command 'fo' is not defined. Did you mean this?"),
color.Gray().Sprint(" test:foo"),
},
},
{
name: "option not found",
call: "test:foo --not-found",
containsOutput: []string{
color.Red().Sprint("The 'not-found' option does not exist."),
},
},
{
name: "option needs a value",
call: "test:foo --int",
containsOutput: []string{
color.Red().Sprint("The '--int' option requires a value."),
},
},
{
name: "option value is not valid",
call: "test:foo --int not-a-number",
containsOutput: []string{
color.Red().Sprint("Invalid value 'not-a-number' for option 'int'."),
},
},
{
name: "no ansi color",
call: "--no-ansi",
containsOutput: []string{
`Goravel Framework v1.16.0
Usage:
artisan command [options] [arguments...]
Global options:
--no-ansi Force disable ANSI output
-v, --version Print the version
Available commands:
list List commands
test:
test:bar Test command
test:foo Test command`,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := color.CaptureOutput(func(io.Writer) {
assert.NoError(t, cliApp.Call(tt.call))
})
if len(got) == 0 {
got = output.String()
}
for _, contain := range tt.containsOutput {
assert.Contains(t, got, contain)
}
})
}
}
type TestFooCommand struct {
}
type TestBarCommand struct {
TestFooCommand
}
func (receiver *TestFooCommand) Signature() string {
return "test:foo"
}
func (receiver *TestFooCommand) Description() string {
return "Test command"
}
func (receiver *TestFooCommand) Extend() command.Extend {
return command.Extend{
Category: "test",
Flags: []command.Flag{
&command.BoolFlag{
Name: "bool",
Aliases: []string{"b"},
Usage: "bool flag",
},
&command.IntFlag{
Name: "int",
Aliases: []string{"i"},
Usage: "<fg=blue>int</> flag",
},
},
}
}
func (receiver *TestFooCommand) Handle(_ contractsconsole.Context) error {
return nil
}
func (receiver *TestBarCommand) Signature() string {
return "test:bar"
}
func TestLexicographicLess(t *testing.T) {
tests := []struct {
i string
j string
expected bool
}{
{"", "a", true},
{"a", "", false},
{"a", "a", false},
{"a", "A", false},
{"A", "a", true},
{"aa", "a", false},
{"a", "aa", true},
{"a", "b", true},
{"a", "B", true},
{"A", "b", true},
{"A", "B", true},
}
for _, tt := range tests {
actual := lexicographicLess(tt.i, tt.j)
assert.Equal(t, tt.expected, actual)
}
}