github.com/arunkumar7540/cli@v6.45.0+incompatible/util/ui/ui_test.go (about) 1 package ui_test 2 3 import ( 4 "errors" 5 "time" 6 7 "code.cloudfoundry.org/cli/command/translatableerror/translatableerrorfakes" 8 "code.cloudfoundry.org/cli/util/configv3" 9 . "code.cloudfoundry.org/cli/util/ui" 10 "code.cloudfoundry.org/cli/util/ui/uifakes" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 . "github.com/onsi/gomega/gbytes" 14 ) 15 16 var _ = Describe("UI", func() { 17 var ( 18 ui *UI 19 fakeConfig *uifakes.FakeConfig 20 out *Buffer 21 errBuff *Buffer 22 ) 23 24 BeforeEach(func() { 25 fakeConfig = new(uifakes.FakeConfig) 26 fakeConfig.ColorEnabledReturns(configv3.ColorEnabled) 27 28 var err error 29 ui, err = NewUI(fakeConfig) 30 Expect(err).NotTo(HaveOccurred()) 31 32 out = NewBuffer() 33 ui.Out = out 34 ui.OutForInteration = out 35 errBuff = NewBuffer() 36 ui.Err = errBuff 37 }) 38 39 Describe("DisplayDeprecationWarning", func() { 40 It("displays the deprecation warning to ui.Err", func() { 41 ui.DisplayDeprecationWarning() 42 Expect(ui.Err).To(Say("Deprecation warning: This command has been deprecated. This feature will be removed in the future.\n")) 43 }) 44 45 When("the locale is not set to English", func() { 46 BeforeEach(func() { 47 fakeConfig.LocaleReturns("fr-FR") 48 49 var err error 50 ui, err = NewUI(fakeConfig) 51 Expect(err).NotTo(HaveOccurred()) 52 53 ui.Err = NewBuffer() 54 }) 55 56 PIt("displays the translated deprecation warning to ui.Err", func() { 57 // TODO: Test implementation awaits translated version of deprecation warning string literal #164098152. 58 }) 59 }) 60 }) 61 62 Describe("DisplayFileDeprecationWarning", func() { 63 It("displays the `cf files` deprecation warning to ui.Err", func() { 64 ui.DisplayFileDeprecationWarning() 65 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")) 66 }) 67 68 When("the locale is not set to English", func() { 69 BeforeEach(func() { 70 fakeConfig.LocaleReturns("fr-FR") 71 72 var err error 73 ui, err = NewUI(fakeConfig) 74 Expect(err).NotTo(HaveOccurred()) 75 76 ui.Err = NewBuffer() 77 }) 78 79 PIt("displays the translated deprecation warning to ui.Err", func() { 80 // TODO: Test implementation awaits translated version of deprecation warning string literal #164098103. 81 }) 82 }) 83 }) 84 85 Describe("DisplayError", func() { 86 When("passed a TranslatableError", func() { 87 var fakeTranslateErr *translatableerrorfakes.FakeTranslatableError 88 89 BeforeEach(func() { 90 fakeTranslateErr = new(translatableerrorfakes.FakeTranslatableError) 91 fakeTranslateErr.TranslateReturns("I am an error") 92 93 ui.DisplayError(fakeTranslateErr) 94 }) 95 96 It("displays the error to ui.Err and displays FAILED in bold red to ui.Out", func() { 97 Expect(ui.Err).To(Say("I am an error\n")) 98 Expect(out).To(Say("\x1b\\[31;1mFAILED\x1b\\[0m\n")) 99 }) 100 101 When("the locale is not set to english", func() { 102 It("translates the error text", func() { 103 Expect(fakeTranslateErr.TranslateCallCount()).To(Equal(1)) 104 Expect(fakeTranslateErr.TranslateArgsForCall(0)).NotTo(BeNil()) 105 }) 106 }) 107 }) 108 109 When("passed a generic error", func() { 110 It("displays the error text to ui.Err and displays FAILED in bold red to ui.Out", func() { 111 ui.DisplayError(errors.New("I am a BANANA!")) 112 Expect(ui.Err).To(Say("I am a BANANA!\n")) 113 Expect(out).To(Say("\x1b\\[31;1mFAILED\x1b\\[0m\n")) 114 }) 115 }) 116 }) 117 118 Describe("DisplayHeader", func() { 119 It("displays the header colorized and bolded to ui.Out", func() { 120 ui.DisplayHeader("some-header") 121 Expect(out).To(Say("\x1b\\[1msome-header\x1b\\[0m")) 122 }) 123 124 When("the locale is not set to English", func() { 125 BeforeEach(func() { 126 fakeConfig.LocaleReturns("fr-FR") 127 128 var err error 129 ui, err = NewUI(fakeConfig) 130 Expect(err).NotTo(HaveOccurred()) 131 132 ui.Out = out 133 }) 134 135 It("displays the translated header colorized and bolded to ui.Out", func() { 136 ui.DisplayHeader("FEATURE FLAGS") 137 Expect(out).To(Say("\x1b\\[1mINDICATEURS DE FONCTION\x1b\\[0m")) 138 }) 139 }) 140 }) 141 142 Describe("DisplayNewline", func() { 143 It("displays a new line", func() { 144 ui.DisplayNewline() 145 Expect(out).To(Say("\n")) 146 }) 147 }) 148 149 Describe("DisplayOK", func() { 150 It("displays 'OK' in green and bold", func() { 151 ui.DisplayOK() 152 Expect(out).To(Say("\x1b\\[32;1mOK\x1b\\[0m")) 153 }) 154 }) 155 156 // Covers the happy paths, additional cases are tested in TranslateText 157 Describe("DisplayText", func() { 158 It("displays the template with map values substituted in to ui.Out with a newline", func() { 159 ui.DisplayText( 160 "template with {{.SomeMapValue}}", 161 map[string]interface{}{ 162 "SomeMapValue": "map-value", 163 }) 164 Expect(out).To(Say("template with map-value\n")) 165 }) 166 167 When("the locale is not set to english", func() { 168 BeforeEach(func() { 169 fakeConfig.LocaleReturns("fr-FR") 170 171 var err error 172 ui, err = NewUI(fakeConfig) 173 Expect(err).NotTo(HaveOccurred()) 174 175 ui.Out = out 176 }) 177 178 It("displays the translated template with map values substituted in to ui.Out", func() { 179 ui.DisplayText( 180 "\nTIP: Use '{{.Command}}' to target new org", 181 map[string]interface{}{ 182 "Command": "foo", 183 }) 184 Expect(out).To(Say("\nASTUCE : utilisez 'foo' pour cibler une nouvelle organisation")) 185 }) 186 }) 187 }) 188 189 Describe("DisplayTextWithBold", func() { 190 It("displays the template to ui.Out", func() { 191 ui.DisplayTextWithBold("some-template") 192 Expect(out).To(Say("some-template")) 193 }) 194 195 When("an optional map is passed in", func() { 196 It("displays the template with map values bolded and substituted in to ui.Out", func() { 197 ui.DisplayTextWithBold( 198 "template with {{.SomeMapValue}}", 199 map[string]interface{}{ 200 "SomeMapValue": "map-value", 201 }) 202 Expect(out).To(Say("template with \x1b\\[1mmap-value\x1b\\[0m")) 203 }) 204 }) 205 206 When("multiple optional maps are passed in", func() { 207 It("displays the template with only the first map values bolded and substituted in to ui.Out", func() { 208 ui.DisplayTextWithBold( 209 "template with {{.SomeMapValue}} and {{.SomeOtherMapValue}}", 210 map[string]interface{}{ 211 "SomeMapValue": "map-value", 212 }, 213 map[string]interface{}{ 214 "SomeOtherMapValue": "other-map-value", 215 }) 216 Expect(out).To(Say("template with \x1b\\[1mmap-value\x1b\\[0m and <no value>")) 217 }) 218 }) 219 220 When("the locale is not set to english", func() { 221 BeforeEach(func() { 222 fakeConfig.LocaleReturns("fr-FR") 223 224 var err error 225 ui, err = NewUI(fakeConfig) 226 Expect(err).NotTo(HaveOccurred()) 227 228 ui.Out = out 229 }) 230 231 It("displays the translated template with map values bolded and substituted in to ui.Out", func() { 232 ui.DisplayTextWithBold( 233 "App {{.AppName}} does not exist.", 234 map[string]interface{}{ 235 "AppName": "some-app-name", 236 }) 237 Expect(out).To(Say("L'application \x1b\\[1msome-app-name\x1b\\[0m n'existe pas.\n")) 238 }) 239 }) 240 }) 241 242 Describe("DisplayTextWithFlavor", func() { 243 It("displays the template to ui.Out", func() { 244 ui.DisplayTextWithFlavor("some-template") 245 Expect(out).To(Say("some-template")) 246 }) 247 248 When("an optional map is passed in", func() { 249 It("displays the template with map values colorized, bolded, and substituted in to ui.Out", func() { 250 ui.DisplayTextWithFlavor( 251 "template with {{.SomeMapValue}}", 252 map[string]interface{}{ 253 "SomeMapValue": "map-value", 254 }) 255 Expect(out).To(Say("template with \x1b\\[36;1mmap-value\x1b\\[0m")) 256 }) 257 }) 258 259 When("multiple optional maps are passed in", func() { 260 It("displays the template with only the first map values colorized, bolded, and substituted in to ui.Out", func() { 261 ui.DisplayTextWithFlavor( 262 "template with {{.SomeMapValue}} and {{.SomeOtherMapValue}}", 263 map[string]interface{}{ 264 "SomeMapValue": "map-value", 265 }, 266 map[string]interface{}{ 267 "SomeOtherMapValue": "other-map-value", 268 }) 269 Expect(out).To(Say("template with \x1b\\[36;1mmap-value\x1b\\[0m and <no value>")) 270 }) 271 }) 272 273 When("the locale is not set to english", func() { 274 BeforeEach(func() { 275 fakeConfig.LocaleReturns("fr-FR") 276 277 var err error 278 ui, err = NewUI(fakeConfig) 279 Expect(err).NotTo(HaveOccurred()) 280 281 ui.Out = out 282 }) 283 284 It("displays the translated template with map values colorized, bolded and substituted in to ui.Out", func() { 285 ui.DisplayTextWithFlavor( 286 "App {{.AppName}} does not exist.", 287 map[string]interface{}{ 288 "AppName": "some-app-name", 289 }) 290 Expect(out).To(Say("L'application \x1b\\[36;1msome-app-name\x1b\\[0m n'existe pas.\n")) 291 }) 292 }) 293 }) 294 295 // Covers the happy paths, additional cases are tested in TranslateText 296 Describe("DisplayWarning", func() { 297 It("displays the warning to ui.Err", func() { 298 ui.DisplayWarning( 299 "template with {{.SomeMapValue}}", 300 map[string]interface{}{ 301 "SomeMapValue": "map-value", 302 }) 303 Expect(ui.Err).To(Say("template with map-value\n\n")) 304 }) 305 306 When("the locale is not set to english", func() { 307 BeforeEach(func() { 308 fakeConfig.LocaleReturns("fr-FR") 309 310 var err error 311 ui, err = NewUI(fakeConfig) 312 Expect(err).NotTo(HaveOccurred()) 313 314 ui.Err = NewBuffer() 315 }) 316 317 It("displays the translated warning to ui.Err", func() { 318 ui.DisplayWarning( 319 "'{{.VersionShort}}' and '{{.VersionLong}}' are also accepted.", 320 map[string]interface{}{ 321 "VersionShort": "some-value", 322 "VersionLong": "some-other-value", 323 }) 324 Expect(ui.Err).To(Say("'some-value' et 'some-other-value' sont également acceptés.\n")) 325 }) 326 }) 327 }) 328 329 // Covers the happy paths, additional cases are tested in TranslateText 330 Describe("DisplayWarnings", func() { 331 It("displays the warnings to ui.Err", func() { 332 ui.DisplayWarnings([]string{"warning-1", "warning-2"}) 333 Expect(ui.Err).To(Say("warning-1\n")) 334 Expect(ui.Err).To(Say("warning-2\n")) 335 Expect(ui.Err).To(Say("\n")) 336 }) 337 338 When("the locale is not set to english", func() { 339 BeforeEach(func() { 340 fakeConfig.LocaleReturns("fr-FR") 341 342 var err error 343 ui, err = NewUI(fakeConfig) 344 Expect(err).NotTo(HaveOccurred()) 345 346 ui.Err = NewBuffer() 347 }) 348 349 It("displays the translated warnings to ui.Err", func() { 350 ui.DisplayWarnings([]string{"Also delete any mapped routes", "FEATURE FLAGS"}) 351 Expect(ui.Err).To(Say("Supprimer aussi les routes mappées\n")) 352 Expect(ui.Err).To(Say("INDICATEURS DE FONCTION\n")) 353 Expect(ui.Err).To(Say("\n")) 354 }) 355 }) 356 357 Context("does not display newline when warnings are empty", func() { 358 It("does not print out a new line", func() { 359 ui.DisplayWarnings(nil) 360 Expect(errBuff.Contents()).To(BeEmpty()) 361 }) 362 }) 363 }) 364 365 Describe("TranslateText", func() { 366 It("returns the template", func() { 367 Expect(ui.TranslateText("some-template")).To(Equal("some-template")) 368 }) 369 370 When("an optional map is passed in", func() { 371 It("returns the template with map values substituted in", func() { 372 expected := ui.TranslateText( 373 "template {{.SomeMapValue}}", 374 map[string]interface{}{ 375 "SomeMapValue": "map-value", 376 }) 377 Expect(expected).To(Equal("template map-value")) 378 }) 379 }) 380 381 When("multiple optional maps are passed in", func() { 382 It("returns the template with only the first map values substituted in", func() { 383 expected := ui.TranslateText( 384 "template with {{.SomeMapValue}} and {{.SomeOtherMapValue}}", 385 map[string]interface{}{ 386 "SomeMapValue": "map-value", 387 }, 388 map[string]interface{}{ 389 "SomeOtherMapValue": "other-map-value", 390 }) 391 Expect(expected).To(Equal("template with map-value and <no value>")) 392 }) 393 }) 394 395 When("the locale is not set to english", func() { 396 BeforeEach(func() { 397 fakeConfig.LocaleReturns("fr-FR") 398 399 var err error 400 ui, err = NewUI(fakeConfig) 401 Expect(err).NotTo(HaveOccurred()) 402 }) 403 404 It("returns the translated template", func() { 405 expected := ui.TranslateText(" View allowable quotas with 'CF_NAME quotas'") 406 Expect(expected).To(Equal(" Affichez les quotas pouvant être alloués avec 'CF_NAME quotas'")) 407 }) 408 }) 409 }) 410 411 Describe("UserFriendlyDate", func() { 412 It("formats a time into an ISO8601 string", func() { 413 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}`)) 414 }) 415 }) 416 })