-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathroot_test.go
387 lines (303 loc) · 8.38 KB
/
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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package commands
import (
"bytes"
"fmt"
"io"
"kool-dev/kool/core/environment"
"kool-dev/kool/core/shell"
"os"
"strings"
"testing"
"github.com/creack/pty"
"github.com/spf13/cobra"
)
func assertServiceAfterExecutingDefaultRun(service *FakeKoolService) (errMessage string) {
if !service.shell.CalledSetOutStream {
errMessage = "did not call SetOutStream on kool service"
return
}
if !service.shell.CalledSetInStream {
errMessage = "did not call SetInStream on kool service"
return
}
if !service.shell.CalledSetErrStream {
errMessage = "did not call SetErrStream on kool service"
return
}
if !service.CalledExecute {
errMessage = "did not call Execute on kool service"
return
}
return
}
func TestNewRootCmd(t *testing.T) {
fakeEnv := environment.NewFakeEnvStorage()
cmd := NewRootCmd(fakeEnv)
if cmd.Name() != rootCmd.Name() {
t.Errorf("expecting RootCmd to return '%s', got '%s'", rootCmd.Name(), cmd.Name())
}
}
func TestRootCmd(t *testing.T) {
cmd := RootCmd()
if cmd.Name() != rootCmd.Name() {
t.Errorf("expecting RootCmd to return '%s', got '%s'", rootCmd.Name(), cmd.Name())
}
}
func TestRootCmdExecute(t *testing.T) {
_, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
originalOutput := os.Stdout
os.Stdout = w
defer func(originalOutput *os.File) {
os.Stdout = originalOutput
}(originalOutput)
if err := Execute(); err != nil {
t.Errorf("unexpected error executing root command; error: %v", err)
}
}
func TestVersionFlagCommand(t *testing.T) {
cmd := RootCmd()
cmd.SetArgs([]string{"--version"})
b := bytes.NewBufferString("")
cmd.SetOut(b)
if err := cmd.Execute(); err != nil {
t.Errorf("unexpected error executing root command; error: %v", err)
}
var (
out []byte
err error
)
if out, err = io.ReadAll(b); err != nil {
t.Fatal(err)
}
expected := fmt.Sprintf("kool version %s", version)
output := strings.TrimSpace(string(out))
if expected != output {
t.Errorf("expecting rootCmd with Version Flag to return '%s', got '%s'", expected, output)
}
}
func TestDefaultCommandRunFunction(t *testing.T) {
f := newFakeKoolService()
cmd := &cobra.Command{
Use: "fake-command",
Short: "fake - fake command",
RunE: DefaultCommandRunFunction(f),
}
if err := cmd.Execute(); err != nil {
t.Errorf("unexpected error executing root command; error: %v", err)
}
if errMessage := assertServiceAfterExecutingDefaultRun(f); errMessage != "" {
t.Error(errMessage)
}
}
func TestFailingDefaultCommandRunFunction(t *testing.T) {
f := newFakeKoolService()
f.MockExecuteErr = fmt.Errorf("execute error")
cmd := &cobra.Command{
Use: "fake-command",
Short: "fake - fake command",
RunE: DefaultCommandRunFunction(f),
}
assertExecGotError(t, cmd, "execute error")
}
func TestMultipleServicesDefaultCommandRunFunction(t *testing.T) {
var services []*FakeKoolService
services = append(services, newFakeKoolService())
services = append(services, newFakeKoolService())
cmd := &cobra.Command{
Use: "fake-command",
Short: "fake - fake command",
RunE: DefaultCommandRunFunction(services[0], services[1]),
}
if err := cmd.Execute(); err != nil {
t.Errorf("unexpected error executing root command; error: %v", err)
}
for _, service := range services {
if errMessage := assertServiceAfterExecutingDefaultRun(service); errMessage != "" {
t.Error(errMessage)
}
}
}
func TestMultipleServicesFailingDefaultCommandRunFunction(t *testing.T) {
failing := newFakeKoolService()
failing.MockExecuteErr = fmt.Errorf("execute error")
passing := newFakeKoolService()
cmd := &cobra.Command{
Use: "fake-command",
Short: "fake - fake command",
RunE: DefaultCommandRunFunction(failing, passing),
}
assertExecGotError(t, cmd, "execute error")
}
func TestVerboseFlagRootCommand(t *testing.T) {
fakeEnv := environment.NewFakeEnvStorage()
fInfo := fakeKoolInfo()
root := NewRootCmd(fakeEnv)
info := NewInfoCmd(fInfo)
root.AddCommand(info)
root.SetArgs([]string{"--verbose", "info"})
if err := root.Execute(); err != nil {
t.Errorf("unexpected error executing command; error: %v", err)
}
if verbose := fakeEnv.IsTrue("KOOL_VERBOSE"); !verbose {
t.Error("expecting 'KOOL_VERBOSE' to be true, got false")
}
}
func TestRecursiveCall(t *testing.T) {
recursive := &cobra.Command{
Use: "recursive",
Run: func(cmd *cobra.Command, args []string) {
_ = shell.Interactive("kool", "-v")
},
}
rootCmd.AddCommand(recursive)
rootCmd.SetArgs([]string{"recursive"})
err := Execute()
if err != nil {
t.Errorf("fail calling recursive command: %v", err)
}
}
func TestMultipleRecursiveCall(t *testing.T) {
recursive := &cobra.Command{
Use: "recursive",
RunE: func(cmd *cobra.Command, args []string) (err error) {
if err = shell.Interactive("kool", "-v"); err != nil {
return
}
err = shell.Interactive("kool", "-v")
return
},
}
rootCmd.AddCommand(recursive)
rootCmd.SetArgs([]string{"recursive"})
err := Execute()
if err != nil {
t.Errorf("fail calling recursive command: %v", err)
}
}
func TestAddCommands(t *testing.T) {
root := NewRootCmd(environment.NewFakeEnvStorage())
AddCommands(root)
var subcommands map[string]bool = map[string]bool{
"completion": false,
"create": false,
"cloud": false,
"docker": false,
"exec": false,
"info": false,
"logs": false,
"preset": false,
"restart": false,
"run": false,
"self-update": false,
"share": false,
"start": false,
"status": false,
"stop": false,
"recipe": false,
}
for _, subCmd := range root.Commands() {
name := subCmd.Name()
if _, ok := subcommands[name]; !ok {
t.Errorf("unexpected command was added: %s", name)
continue
}
subcommands[name] = true
}
for cmd, added := range subcommands {
if !added {
t.Errorf("expected command is missing: %s", cmd)
}
}
}
func TestDevelopmentVersionWarning(t *testing.T) {
fakeEnv := environment.NewFakeEnvStorage()
root := NewRootCmd(fakeEnv)
fakecmd := &cobra.Command{
Use: "fakecmd",
Run: func(cmd *cobra.Command, args []string) {},
}
root.AddCommand(fakecmd)
// default test NOT A TTY
b := bytes.NewBufferString("")
root.SetOut(b)
root.SetArgs([]string{"fakecmd"})
version = DEV_VERSION
if err := root.Execute(); err != nil {
t.Errorf("unexpected error executing command; error: %v", err)
}
var (
out []byte
err error
)
if out, err = io.ReadAll(b); err != nil {
t.Fatal(err)
}
expected := "you are executing a development version"
output := strings.TrimSpace(string(out))
if strings.Contains(output, expected) {
t.Errorf("bad warning under non-TTY: %s", output)
}
if hasWarnedDevelopmentVersion {
t.Error("bar warning under non-TTY")
}
if pty, tty, err := pty.Open(); err != nil {
t.Fatalf("failed creting PTY for testing: %v", err)
} else {
root.SetOut(tty)
defer pty.Close()
defer tty.Close()
}
version = DEV_VERSION
if err := root.Execute(); err != nil {
t.Errorf("unexpected error executing command; error: %v", err)
}
if !hasWarnedDevelopmentVersion {
t.Error("failed to warn about development version")
}
hasWarnedDevelopmentVersion = false
version = "100.100.100"
if err := root.Execute(); err != nil {
t.Errorf("unexpected error executing command; error: %v", err)
}
if hasWarnedDevelopmentVersion {
t.Error("should not have warned on non-dev version")
}
}
func TestPromptSelectInterruptedError(t *testing.T) {
failing := newFakeKoolService()
failing.MockExecuteErr = shell.ErrUserCancelled
cmd := &cobra.Command{
Use: "fake-command",
Short: "fake - fake command",
RunE: DefaultCommandRunFunction(failing),
}
b := bytes.NewBufferString("")
cmd.SetOut(b)
if err := cmd.Execute(); err != nil {
t.Errorf("unexpected error executing root command; error: %v", err)
}
var (
out []byte
err error
)
if out, err = io.ReadAll(b); err != nil {
t.Fatal(err)
}
expected := "Operation Cancelled"
output := strings.TrimSpace(string(out))
if strings.Contains(output, expected) {
t.Errorf("bad warning about cancelling operation: %s", output)
}
}
func assertExecGotError(t *testing.T, cmd *cobra.Command, partialErr string) {
cmd.SilenceErrors = true
cmd.SilenceUsage = true
if err := cmd.Execute(); err == nil {
t.Errorf("should have got an error - %s", partialErr)
} else if !strings.Contains(err.Error(), partialErr) {
t.Errorf("unexpected error executing command; '%s' but got error: %v", partialErr, err)
}
}