forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui_test.go
450 lines (383 loc) · 13 KB
/
ui_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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package ui_test
import (
"errors"
"time"
"code.cloudfoundry.org/cli/command/translatableerror/translatableerrorfakes"
"code.cloudfoundry.org/cli/util/configv3"
. "code.cloudfoundry.org/cli/util/ui"
"code.cloudfoundry.org/cli/util/ui/uifakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
)
var _ = Describe("UI", func() {
var (
ui *UI
fakeConfig *uifakes.FakeConfig
out *Buffer
errBuff *Buffer
)
BeforeEach(func() {
fakeConfig = new(uifakes.FakeConfig)
fakeConfig.ColorEnabledReturns(configv3.ColorEnabled)
var err error
ui, err = NewUI(fakeConfig)
Expect(err).NotTo(HaveOccurred())
out = NewBuffer()
ui.Out = out
ui.OutForInteration = out
errBuff = NewBuffer()
ui.Err = errBuff
})
Describe("DisplayDeprecationWarning", func() {
It("displays the deprecation warning to ui.Err", func() {
ui.DisplayDeprecationWarning()
Expect(ui.Err).To(Say("Deprecation warning: This command has been deprecated. This feature will be removed in the future.\n"))
})
When("the locale is not set to English", func() {
BeforeEach(func() {
fakeConfig.LocaleReturns("fr-FR")
var err error
ui, err = NewUI(fakeConfig)
Expect(err).NotTo(HaveOccurred())
ui.Err = NewBuffer()
})
PIt("displays the translated deprecation warning to ui.Err", func() {
// TODO: Test implementation awaits translated version of deprecation warning string literal #164098152.
})
})
})
Describe("DisplayFileDeprecationWarning", func() {
It("displays the `cf files` deprecation warning to ui.Err", func() {
ui.DisplayFileDeprecationWarning()
Expect(ui.Err).To(Say("Deprecation warning: This command has been deprecated and will be removed in the future. For similar functionality, please use the `cf ssh` command instead.\n"))
})
When("the locale is not set to English", func() {
BeforeEach(func() {
fakeConfig.LocaleReturns("fr-FR")
var err error
ui, err = NewUI(fakeConfig)
Expect(err).NotTo(HaveOccurred())
ui.Err = NewBuffer()
})
PIt("displays the translated deprecation warning to ui.Err", func() {
// TODO: Test implementation awaits translated version of deprecation warning string literal #164098103.
})
})
})
Describe("DisplayError", func() {
When("passed a TranslatableError", func() {
var fakeTranslateErr *translatableerrorfakes.FakeTranslatableError
BeforeEach(func() {
fakeTranslateErr = new(translatableerrorfakes.FakeTranslatableError)
fakeTranslateErr.TranslateReturns("I am an error")
ui.DisplayError(fakeTranslateErr)
})
It("displays the error to ui.Err and displays FAILED in bold red to ui.Out", func() {
Expect(ui.Err).To(Say("I am an error\n"))
Expect(out).To(Say("\x1b\\[31;1mFAILED\x1b\\[0m\n"))
})
When("the locale is not set to english", func() {
It("translates the error text", func() {
Expect(fakeTranslateErr.TranslateCallCount()).To(Equal(1))
Expect(fakeTranslateErr.TranslateArgsForCall(0)).NotTo(BeNil())
})
})
})
When("passed a generic error", func() {
It("displays the error text to ui.Err and displays FAILED in bold red to ui.Out", func() {
ui.DisplayError(errors.New("I am a BANANA!"))
Expect(ui.Err).To(Say("I am a BANANA!\n"))
Expect(out).To(Say("\x1b\\[31;1mFAILED\x1b\\[0m\n"))
})
})
})
Describe("DisplayHeader", func() {
It("displays the header colorized and bolded to ui.Out", func() {
ui.DisplayHeader("some-header")
Expect(out).To(Say("\x1b\\[1msome-header\x1b\\[0m"))
})
When("the locale is not set to English", func() {
BeforeEach(func() {
fakeConfig.LocaleReturns("fr-FR")
var err error
ui, err = NewUI(fakeConfig)
Expect(err).NotTo(HaveOccurred())
ui.Out = out
})
It("displays the translated header colorized and bolded to ui.Out", func() {
ui.DisplayHeader("FEATURE FLAGS")
Expect(out).To(Say("\x1b\\[1mINDICATEURS DE FONCTION\x1b\\[0m"))
})
})
})
Describe("DisplayNewline", func() {
It("displays a new line", func() {
ui.DisplayNewline()
Expect(out).To(Say("\n"))
})
})
Describe("DisplayOK", func() {
It("displays 'OK' in green and bold", func() {
ui.DisplayOK()
Expect(out).To(Say("\x1b\\[32;1mOK\x1b\\[0m"))
})
})
// Covers the happy paths, additional cases are tested in TranslateText
Describe("DisplayText", func() {
It("displays the template with map values substituted in to ui.Out with a newline", func() {
ui.DisplayText(
"template with {{.SomeMapValue}}",
map[string]interface{}{
"SomeMapValue": "map-value",
})
Expect(out).To(Say("template with map-value\n"))
})
When("the locale is not set to english", func() {
BeforeEach(func() {
fakeConfig.LocaleReturns("fr-FR")
var err error
ui, err = NewUI(fakeConfig)
Expect(err).NotTo(HaveOccurred())
ui.Out = out
})
It("displays the translated template with map values substituted in to ui.Out", func() {
ui.DisplayText(
"\nTIP: Use '{{.Command}}' to target new org",
map[string]interface{}{
"Command": "foo",
})
Expect(out).To(Say("\nASTUCE : utilisez 'foo' pour cibler une nouvelle organisation"))
})
})
})
Describe("Display JSON", func() {
It("displays the indented JSON object", func() {
obj := map[string]interface{}{
"str": "hello",
"bool": true,
"int": 42,
"pass": "abc>&gd!f",
"map": map[string]interface{}{"float": 123.03},
"arr": []string{"a", "b"},
}
ui.DisplayJSON("named_json", obj)
Expect(out).To(SatisfyAll(
Say("named_json: {\n"),
Say(" \"arr\": \\[\n"),
Say(" \"a\","),
Say(" \"b\"\n"),
Say(" \\],\n"),
Say(" \"bool\": true,\n"),
Say(" \"int\": 42,\n"),
Say(" \"map\": {\n"),
Say(" \"float\": 123.03\n"),
Say(" },\n"),
Say(" \"pass\": \"abc>&gd!f\",\n"),
Say(" \"str\": \"hello\"\n"),
Say("}\n"),
Say("\n"),
))
})
})
Describe("DeferText", func() {
It("defers the template with map values substituted in to ui.Out with a newline", func() {
ui.DeferText(
"template with {{.SomeMapValue}}",
map[string]interface{}{
"SomeMapValue": "map-value",
})
Expect(out).NotTo(Say("template with map-value\n"))
ui.FlushDeferred()
Expect(out).To(Say("template with map-value\n"))
})
When("the locale is not set to english", func() {
BeforeEach(func() {
fakeConfig.LocaleReturns("fr-FR")
var err error
ui, err = NewUI(fakeConfig)
Expect(err).NotTo(HaveOccurred())
ui.Out = out
})
It("defers the translated template with map values substituted in to ui.Out", func() {
ui.DeferText(
"\nTIP: Use '{{.Command}}' to target new org",
map[string]interface{}{
"Command": "foo",
})
Expect(out).NotTo(Say("\nASTUCE : utilisez 'foo' pour cibler une nouvelle organisation"))
ui.FlushDeferred()
Expect(out).To(Say("\nASTUCE : utilisez 'foo' pour cibler une nouvelle organisation"))
ui.FlushDeferred()
Expect(out).NotTo(Say("\nASTUCE : utilisez 'foo' pour cibler une nouvelle organisation"))
})
})
})
Describe("DisplayTextWithBold", func() {
It("displays the template to ui.Out", func() {
ui.DisplayTextWithBold("some-template")
Expect(out).To(Say("some-template"))
})
When("an optional map is passed in", func() {
It("displays the template with map values bolded and substituted in to ui.Out", func() {
ui.DisplayTextWithBold(
"template with {{.SomeMapValue}}",
map[string]interface{}{
"SomeMapValue": "map-value",
})
Expect(out).To(Say("template with \x1b\\[1mmap-value\x1b\\[0m"))
})
})
When("multiple optional maps are passed in", func() {
It("displays the template with only the first map values bolded and substituted in to ui.Out", func() {
ui.DisplayTextWithBold(
"template with {{.SomeMapValue}} and {{.SomeOtherMapValue}}",
map[string]interface{}{
"SomeMapValue": "map-value",
},
map[string]interface{}{
"SomeOtherMapValue": "other-map-value",
})
Expect(out).To(Say("template with \x1b\\[1mmap-value\x1b\\[0m and <no value>"))
})
})
When("the locale is not set to english", func() {
BeforeEach(func() {
fakeConfig.LocaleReturns("fr-FR")
var err error
ui, err = NewUI(fakeConfig)
Expect(err).NotTo(HaveOccurred())
ui.Out = out
})
It("displays the translated template with map values bolded and substituted in to ui.Out", func() {
ui.DisplayTextWithBold(
"App {{.AppName}} does not exist.",
map[string]interface{}{
"AppName": "some-app-name",
})
Expect(out).To(Say("L'application \x1b\\[1msome-app-name\x1b\\[0m n'existe pas.\n"))
})
})
})
Describe("DisplayTextWithFlavor", func() {
It("displays the template to ui.Out", func() {
ui.DisplayTextWithFlavor("some-template")
Expect(out).To(Say("some-template"))
})
When("an optional map is passed in", func() {
It("displays the template with map values colorized, bolded, and substituted in to ui.Out", func() {
ui.DisplayTextWithFlavor(
"template with {{.SomeMapValue}}",
map[string]interface{}{
"SomeMapValue": "map-value",
})
Expect(out).To(Say("template with \x1b\\[36;1mmap-value\x1b\\[0m"))
})
})
When("multiple optional maps are passed in", func() {
It("displays the template with only the first map values colorized, bolded, and substituted in to ui.Out", func() {
ui.DisplayTextWithFlavor(
"template with {{.SomeMapValue}} and {{.SomeOtherMapValue}}",
map[string]interface{}{
"SomeMapValue": "map-value",
},
map[string]interface{}{
"SomeOtherMapValue": "other-map-value",
})
Expect(out).To(Say("template with \x1b\\[36;1mmap-value\x1b\\[0m and <no value>"))
})
})
When("the locale is not set to english", func() {
BeforeEach(func() {
fakeConfig.LocaleReturns("fr-FR")
var err error
ui, err = NewUI(fakeConfig)
Expect(err).NotTo(HaveOccurred())
ui.Out = out
})
It("displays the translated template with map values colorized, bolded and substituted in to ui.Out", func() {
ui.DisplayTextWithFlavor(
"App {{.AppName}} does not exist.",
map[string]interface{}{
"AppName": "some-app-name",
})
Expect(out).To(Say("L'application \x1b\\[36;1msome-app-name\x1b\\[0m n'existe pas.\n"))
})
})
})
Describe("DisplayDiffAddition", func() {
It("displays a green indented line with a +", func() {
ui.DisplayDiffAddition("added", 3, false)
Expect(out).To(Say(`\x1b\[32m\+ added\x1b\[0m`))
})
It("displays a hyphen when the addHyphen is true", func() {
ui.DisplayDiffAddition("added", 3, true)
Expect(out).To(Say(`\x1b\[32m\+ - added\x1b\[0m`))
})
})
Describe("DisplayDiffRemoval", func() {
It("displays a red indented line with a -", func() {
ui.DisplayDiffRemoval("removed", 3, false)
Expect(out).To(Say(`\x1b\[31m\- removed\x1b\[0m`))
})
It("displays a a hyphen when addHyphen is true", func() {
ui.DisplayDiffRemoval("removed", 3, true)
Expect(out).To(Say(`\x1b\[31m\- - removed\x1b\[0m`))
})
})
Describe("DisplayDiffUnchanged", func() {
It("displays a plain indented line with no prefix", func() {
ui.DisplayDiffUnchanged("unchanged", 3, false)
Expect(out).To(Say(" unchanged"))
})
It("displays a a hyphen when addHyphen is true", func() {
ui.DisplayDiffUnchanged("unchanged", 3, true)
Expect(out).To(Say(" - unchanged"))
})
})
Describe("TranslateText", func() {
It("returns the template", func() {
Expect(ui.TranslateText("some-template")).To(Equal("some-template"))
})
When("an optional map is passed in", func() {
It("returns the template with map values substituted in", func() {
expected := ui.TranslateText(
"template {{.SomeMapValue}}",
map[string]interface{}{
"SomeMapValue": "map-value",
})
Expect(expected).To(Equal("template map-value"))
})
})
When("multiple optional maps are passed in", func() {
It("returns the template with only the first map values substituted in", func() {
expected := ui.TranslateText(
"template with {{.SomeMapValue}} and {{.SomeOtherMapValue}}",
map[string]interface{}{
"SomeMapValue": "map-value",
},
map[string]interface{}{
"SomeOtherMapValue": "other-map-value",
})
Expect(expected).To(Equal("template with map-value and <no value>"))
})
})
When("the locale is not set to english", func() {
BeforeEach(func() {
fakeConfig.LocaleReturns("fr-FR")
var err error
ui, err = NewUI(fakeConfig)
Expect(err).NotTo(HaveOccurred())
})
It("returns the translated template", func() {
expected := ui.TranslateText(" View allowable quotas with 'CF_NAME quotas'")
Expect(expected).To(Equal(" Affichez les quotas pouvant être alloués avec 'CF_NAME quotas'"))
})
})
})
Describe("UserFriendlyDate", func() {
It("formats a time into an ISO8601 string", func() {
Expect(ui.UserFriendlyDate(time.Unix(0, 0))).To(MatchRegexp(`\w{3} [0-3]\d \w{3} [0-2]\d:[0-5]\d:[0-5]\d \w+ \d{4}`))
})
})
})