/
application_test.go
305 lines (239 loc) · 8.38 KB
/
application_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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package console_test
import (
"bytes"
"errors"
"fmt"
"io"
"math/rand"
"strings"
"testing"
"github.com/eidolon/console"
"github.com/eidolon/console/parameters"
"github.com/seeruk/assert"
)
func TestNewApplication(t *testing.T) {
application := console.NewApplication("eidolon/console", "1.2.3+testing")
assert.True(t, application != nil, "Application should not be nil")
}
func TestApplication(t *testing.T) {
createApplication := func(writer io.Writer) *console.Application {
application := console.NewApplication("eidolon/console", "1.2.3.+testing")
application.Writer = writer
return application
}
createTestCommand := func(a *string, b *int) *console.Command {
return &console.Command{
Name: "test",
Alias: "t",
Configure: func(definition *console.Definition) {
definition.AddArgument(console.ArgumentDefinition{
Value: parameters.NewStringValue(a),
Spec: "STRINGARG",
})
definition.AddOption(console.OptionDefinition{
Value: parameters.NewIntValue(b),
Spec: "--int-opt=VALUE",
})
},
Execute: func(input *console.Input, output *console.Output) error {
output.Printf("STRINGARG = %s", *a)
output.Printf("--int-opt = %v", *b)
return nil
},
}
}
t.Run("Run()", func(t *testing.T) {
t.Run("should return exit code 2 if no command was asked for", func(t *testing.T) {
writer := bytes.Buffer{}
application := createApplication(&writer)
code := application.Run([]string{}, []string{})
assert.Equal(t, 100, code)
})
t.Run("should return exit code 2 if no command was found", func(t *testing.T) {
writer := bytes.Buffer{}
application := createApplication(&writer)
code := application.Run([]string{"foo"}, []string{})
assert.Equal(t, 100, code)
})
t.Run("should return exit code 100 if the help flag is set", func(t *testing.T) {
writer := bytes.Buffer{}
application := createApplication(&writer)
code := application.Run([]string{"--help"}, []string{})
assert.Equal(t, 100, code)
})
t.Run("should show application help if the help flag is set", func(t *testing.T) {
writer := bytes.Buffer{}
application := createApplication(&writer)
application.Run([]string{"--help"}, []string{})
output := writer.String()
containsUsage := strings.Contains(output, "USAGE:")
containsArguments := strings.Contains(output, "ARGUMENTS:")
containsOptions := strings.Contains(output, "OPTIONS:")
containsHelp := containsUsage && containsOptions && !containsArguments
assert.True(t, containsHelp, "Expected help output.")
})
t.Run("should show command help if the help flag is set when running a command", func(t *testing.T) {
var a string
var b int
writer := bytes.Buffer{}
application := createApplication(&writer)
application.AddCommand(createTestCommand(&a, &b))
application.Run([]string{"test", "--help"}, []string{})
output := writer.String()
containsUsage := strings.Contains(output, "USAGE:")
containsArguments := strings.Contains(output, "ARGUMENTS:")
containsOptions := strings.Contains(output, "OPTIONS:")
containsHelp := containsUsage && containsOptions && containsArguments
assert.True(t, containsHelp, "Expected help output.")
})
t.Run("should return exit code 0 if a command was found, and ran OK", func(t *testing.T) {
var a string
var b int
writer := bytes.Buffer{}
application := createApplication(&writer)
application.AddCommand(createTestCommand(&a, &b))
code := application.Run([]string{"test", "aval", "--int-opt=384"}, []string{})
assert.Equal(t, 0, code)
})
t.Run("should return exit code 0 if a command with an alias was found, and ran OK", func(t *testing.T) {
var a string
var b int
writer := bytes.Buffer{}
application := createApplication(&writer)
application.AddCommand(createTestCommand(&a, &b))
code := application.Run([]string{"t", "aval", "--int-opt=384"}, []string{})
assert.Equal(t, 0, code)
})
t.Run("should return exit code 101 if mapping input fails", func(t *testing.T) {
var a string
var b int
writer := bytes.Buffer{}
application := createApplication(&writer)
application.AddCommand(createTestCommand(&a, &b))
code := application.Run([]string{"test", "aval", "--int-opt=hello"}, []string{})
assert.Equal(t, 101, code)
})
t.Run("should return exit code 1 if the command execution fails", func(t *testing.T) {
writer := bytes.Buffer{}
application := createApplication(&writer)
application.AddCommand(&console.Command{
Name: "test",
Execute: func(input *console.Input, output *console.Output) error {
return errors.New("Testing errors")
},
})
code := application.Run([]string{"test", "aval", "--int-opt=hello"}, []string{})
assert.Equal(t, 1, code)
})
t.Run("should return exit code set on output during execute if execution succeeds", func(t *testing.T) {
exitCode := 200
writer := bytes.Buffer{}
application := createApplication(&writer)
application.AddCommand(&console.Command{
Name: "test",
Execute: func(input *console.Input, output *console.Output) error {
output.SetExitCode(exitCode)
return nil
},
})
code := application.Run([]string{"test", "aval", "--int-opt=hello"}, []string{})
assert.Equal(t, exitCode, code)
})
t.Run("should return exit code 1 if command execution fails even if exitCode is set", func(t *testing.T) {
writer := bytes.Buffer{}
application := createApplication(&writer)
application.AddCommand(&console.Command{
Name: "test",
Execute: func(input *console.Input, output *console.Output) error {
output.SetExitCode(400)
return errors.New("Testing errors")
},
})
code := application.Run([]string{"test", "aval", "--int-opt=hello"}, []string{})
assert.Equal(t, 1, code)
})
t.Run("should configure the application definition", func(t *testing.T) {
// @TODO: Update with global options implementation.
//var a string
//var b int
//var foo string
//
//writer := bytes.Buffer{}
//application := createApplication(&writer)
//application.Configure = func(definition *console.Definition) {
// definition.AddOption(console.OptionDefinition{
// Value: parameters.NewStringValue(&foo),
// Spec: "--foo=FOO",
// })
//}
//
//application.AddCommand(createTestCommand(&a, &b))
//application.Run([]string{"test", "aval", "--foo=bar"}, []string{})
//
//assert.Equal(t, "bar", foo)
})
t.Run("should work with sub-commands", func(t *testing.T) {
message := fmt.Sprintf("sub-command: %d", rand.Int())
subCommand := console.Command{
Name: "subCommand",
Execute: func(input *console.Input, output *console.Output) error {
output.Println(message)
return nil
},
}
command := console.Command{Name: "command"}
command.AddCommand(&subCommand)
writer := bytes.Buffer{}
application := createApplication(&writer)
application.AddCommand(&command)
application.Run([]string{"command", "subCommand"}, []string{})
assert.True(t, strings.Contains(writer.String(), message), "Expected sub-command to run")
})
})
t.Run("AddCommands()", func(t *testing.T) {
t.Run("should work when adding 1 command", func(t *testing.T) {
writer := bytes.Buffer{}
application := createApplication(&writer)
assert.Equal(t, 0, len(application.Commands()))
application.AddCommands([]*console.Command{
{
Name: "test1",
},
})
assert.Equal(t, 1, len(application.Commands()))
})
t.Run("should work when adding no commands", func(t *testing.T) {
writer := bytes.Buffer{}
application := createApplication(&writer)
assert.Equal(t, 0, len(application.Commands()))
application.AddCommands([]*console.Command{})
assert.Equal(t, 0, len(application.Commands()))
})
t.Run("should work when adding more than 1 command", func(t *testing.T) {
writer := bytes.Buffer{}
application := createApplication(&writer)
assert.Equal(t, 0, len(application.Commands()))
application.AddCommands([]*console.Command{
{
Name: "test1",
},
{
Name: "test2",
},
{
Name: "test3",
},
})
assert.Equal(t, 3, len(application.Commands()))
})
})
t.Run("AddCommand()", func(t *testing.T) {
writer := bytes.Buffer{}
application := createApplication(&writer)
assert.Equal(t, 0, len(application.Commands()))
application.AddCommand(&console.Command{
Name: "test1",
})
assert.Equal(t, 1, len(application.Commands()))
})
}