github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/util/ui/ui_for_push_test.go (about) 1 package ui_test 2 3 import ( 4 "regexp" 5 6 "code.cloudfoundry.org/cli/types" 7 "code.cloudfoundry.org/cli/util/configv3" 8 . "code.cloudfoundry.org/cli/util/ui" 9 "code.cloudfoundry.org/cli/util/ui/uifakes" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 . "github.com/onsi/gomega/gbytes" 13 ) 14 15 var _ = Describe("UI", func() { 16 var ( 17 ui *UI 18 fakeConfig *uifakes.FakeConfig 19 out *Buffer 20 ) 21 22 BeforeEach(func() { 23 fakeConfig = new(uifakes.FakeConfig) 24 fakeConfig.ColorEnabledReturns(configv3.ColorEnabled) 25 }) 26 27 JustBeforeEach(func() { 28 var err error 29 ui, err = NewUI(fakeConfig) 30 Expect(err).NotTo(HaveOccurred()) 31 32 out = NewBuffer() 33 ui.Out = out 34 ui.Err = NewBuffer() 35 }) 36 37 Describe("DisplayChangesForPush", func() { 38 It("alings all the string types", func() { 39 changeSet := []Change{ 40 { 41 Header: "h1", 42 CurrentValue: "old", 43 NewValue: "new", 44 }, 45 { 46 Header: "header2", 47 CurrentValue: "old", 48 NewValue: "old", 49 }, 50 { 51 Header: "header3", 52 CurrentValue: []string{"route2", "route1", "route4"}, 53 NewValue: []string{"route4", "route2", "route3"}, 54 }, 55 } 56 57 err := ui.DisplayChangesForPush(changeSet) 58 Expect(err).ToNot(HaveOccurred()) 59 60 Expect(out).To(Say("\x1b\\[31m\\-\\s+h1 old\x1b\\[0m")) 61 Expect(out).To(Say("\x1b\\[32m\\+\\s+h1 new\x1b\\[0m")) 62 Expect(out).To(Say("(?m)^\\s+header2 old")) 63 Expect(out).To(Say("(?m)^\\s+header3$")) 64 Expect(out).To(Say("\x1b\\[31m\\-\\s+route1\x1b\\[0m")) 65 Expect(out).To(Say("(?m)^\\s+route2$")) 66 Expect(out).To(Say("\x1b\\[32m\\+\\s+route3\x1b\\[0m")) 67 Expect(out).To(Say("(?m)^\\s+route4$")) 68 }) 69 }) 70 71 Describe("DisplayChangeForPush", func() { 72 Context("in english", func() { 73 When("passed strings for values", func() { 74 When("the values are *not* hidden", func() { 75 When("the values are not equal", func() { 76 When("both values are not empty", func() { 77 It("should display the header with differences", func() { 78 err := ui.DisplayChangeForPush("val", 2, false, "old", "new") 79 Expect(err).ToNot(HaveOccurred()) 80 Expect(out).To(Say("\x1b\\[31m\\-\\s+val old\x1b\\[0m")) 81 Expect(out).To(Say("\x1b\\[32m\\+\\s+val new\x1b\\[0m")) 82 }) 83 }) 84 85 When("the originalValue is empty", func() { 86 It("should display the header with the new value only", func() { 87 err := ui.DisplayChangeForPush("val", 2, false, "", "new") 88 Expect(err).ToNot(HaveOccurred()) 89 Expect(out).To(Say("\x1b\\[32m\\+\\s+val new\x1b\\[0m")) 90 91 err = ui.DisplayChangeForPush("val", 2, false, "", "new") 92 Expect(err).ToNot(HaveOccurred()) 93 Expect(out).ToNot(Say("\x1b\\[31m\\-\\s+val old\x1b\\[0m")) 94 }) 95 }) 96 97 When("the newValue is empty", func() { 98 It("should display the header with the new value only", func() { 99 err := ui.DisplayChangeForPush("val", 2, false, "old", "") 100 Expect(err).ToNot(HaveOccurred()) 101 Expect(out).To(Say("\x1b\\[31m\\-\\s+val old\x1b\\[0m")) 102 Expect(out).ToNot(Say("\x1b\\[32m\\+\\s+val \x1b\\[0m")) 103 }) 104 }) 105 }) 106 107 When("the values are the equal", func() { 108 It("should display the header without differences", func() { 109 err := ui.DisplayChangeForPush("val", 2, false, "old", "old") 110 Expect(err).ToNot(HaveOccurred()) 111 Expect(out).To(Say(`(?m)^\s+val old$`)) 112 }) 113 }) 114 115 When("the values are a different type", func() { 116 It("should return an ErrValueMissmatch", func() { 117 err := ui.DisplayChangeForPush("asdf", 2, false, "asdf", 7) 118 Expect(err).To(MatchError(ErrValueMissmatch)) 119 }) 120 }) 121 }) 122 123 When("the values are hidden", func() { 124 When("the values are not equal", func() { 125 When("the originalValue is not empty", func() { 126 It("should display the header with differences", func() { 127 err := ui.DisplayChangeForPush("val", 2, true, "old", "new") 128 Expect(err).ToNot(HaveOccurred()) 129 Expect(out).To(Say("\x1b\\[31m\\-\\s+val %s\x1b\\[0m", regexp.QuoteMeta(RedactedValue))) 130 Expect(out).To(Say("\x1b\\[32m\\+\\s+val %s\x1b\\[0m", regexp.QuoteMeta(RedactedValue))) 131 }) 132 }) 133 134 When("the originalValue is empty", func() { 135 It("should display the header with the new value only", func() { 136 err := ui.DisplayChangeForPush("val", 2, true, "", "new") 137 Expect(err).ToNot(HaveOccurred()) 138 Expect(out).To(Say("\x1b\\[32m\\+\\s+val %s\x1b\\[0m", regexp.QuoteMeta(RedactedValue))) 139 140 err = ui.DisplayChangeForPush("val", 2, true, "", "new") 141 Expect(err).ToNot(HaveOccurred()) 142 Expect(out).ToNot(Say("\x1b\\[31m\\-\\s+val %s\x1b\\[0m", regexp.QuoteMeta(RedactedValue))) 143 }) 144 }) 145 }) 146 147 When("the values are the equal", func() { 148 It("should display the header without differences", func() { 149 err := ui.DisplayChangeForPush("val", 2, true, "old", "old") 150 Expect(err).ToNot(HaveOccurred()) 151 Expect(out).To(Say(`(?m)^\s+val %s`, regexp.QuoteMeta(RedactedValue))) 152 }) 153 }) 154 155 When("the values are a different type", func() { 156 It("should return an ErrValueMissmatch", func() { 157 err := ui.DisplayChangeForPush("asdf", 2, true, "asdf", 7) 158 Expect(err).To(MatchError(ErrValueMissmatch)) 159 }) 160 }) 161 }) 162 }) 163 164 When("passed list of strings for values", func() { 165 It("should display the header with sorted differences", func() { 166 old := []string{"route2", "route1", "route4"} 167 new := []string{"route4", "route2", "route3"} 168 err := ui.DisplayChangeForPush("val", 2, false, old, new) 169 Expect(err).ToNot(HaveOccurred()) 170 Expect(out).To(Say(`\s+val`)) 171 Expect(out).To(Say("\x1b\\[31m\\-\\s+route1\x1b\\[0m")) 172 Expect(out).To(Say(`(?m)^\s+route2$`)) 173 Expect(out).To(Say("\x1b\\[32m\\+\\s+route3\x1b\\[0m")) 174 Expect(out).To(Say(`(?m)^\s+route4$`)) 175 }) 176 177 When("the values are a different type", func() { 178 It("should return an ErrValueMissmatch", func() { 179 err := ui.DisplayChangeForPush("asdf", 2, false, []string{"route4", "route2", "route3"}, 7) 180 Expect(err).To(MatchError(ErrValueMissmatch)) 181 }) 182 }) 183 184 When("both sets are empty", func() { 185 It("does not display anything", func() { 186 var old []string 187 new := []string{} 188 err := ui.DisplayChangeForPush("val", 2, false, old, new) 189 Expect(err).ToNot(HaveOccurred()) 190 Expect(out).ToNot(Say(`\s+val`)) 191 }) 192 }) 193 }) 194 195 When("passed ints for values", func() { 196 When("the values are not equal", func() { 197 When("the originalValue is not empty", func() { 198 It("should display the header with differences", func() { 199 err := ui.DisplayChangeForPush("val", 2, false, 1, 2) 200 Expect(err).ToNot(HaveOccurred()) 201 Expect(out).To(Say("\x1b\\[31m\\-\\s+val 1\x1b\\[0m")) 202 Expect(out).To(Say("\x1b\\[32m\\+\\s+val 2\x1b\\[0m")) 203 }) 204 }) 205 206 When("the originalValue is zero", func() { 207 It("should display the header with the new value only", func() { 208 err := ui.DisplayChangeForPush("val", 2, false, 0, 1) 209 Expect(err).ToNot(HaveOccurred()) 210 Expect(out).To(Say("\x1b\\[32m\\+\\s+val 1\x1b\\[0m")) 211 }) 212 }) 213 }) 214 215 When("the values are the equal", func() { 216 It("should display the header without differences", func() { 217 err := ui.DisplayChangeForPush("val", 2, false, 3, 3) 218 Expect(err).ToNot(HaveOccurred()) 219 Expect(out).To(Say(`(?m)^\s+val 3$`)) 220 }) 221 }) 222 223 When("the values are a different type", func() { 224 It("should return an ErrValueMissmatch", func() { 225 err := ui.DisplayChangeForPush("asdf", 2, false, 7, "asdf") 226 Expect(err).To(MatchError(ErrValueMissmatch)) 227 }) 228 }) 229 }) 230 231 When("passed NullInt for values", func() { 232 When("the values are not equal", func() { 233 When("the originalValue is not empty", func() { 234 It("should display the header with differences", func() { 235 err := ui.DisplayChangeForPush("val", 2, false, types.NullInt{ 236 Value: 1, 237 IsSet: true, 238 }, types.NullInt{ 239 Value: 2, 240 IsSet: true, 241 }) 242 Expect(err).ToNot(HaveOccurred()) 243 Expect(out).To(Say("\x1b\\[31m\\-\\s+val 1\x1b\\[0m")) 244 Expect(out).To(Say("\x1b\\[32m\\+\\s+val 2\x1b\\[0m")) 245 }) 246 }) 247 248 When("the originalValue is not set", func() { 249 It("should display the header with the new value only", func() { 250 err := ui.DisplayChangeForPush("val", 2, false, types.NullInt{ 251 Value: 0, 252 IsSet: false, 253 }, types.NullInt{ 254 Value: 1, 255 IsSet: true, 256 }) 257 Expect(err).ToNot(HaveOccurred()) 258 Expect(out).To(Say("\x1b\\[32m\\+\\s+val 1\x1b\\[0m")) 259 }) 260 }) 261 }) 262 263 When("the values are the equal", func() { 264 It("should display the header without differences", func() { 265 err := ui.DisplayChangeForPush("val", 2, false, types.NullInt{ 266 Value: 3, 267 IsSet: true, 268 }, types.NullInt{ 269 Value: 3, 270 IsSet: true, 271 }) 272 Expect(err).ToNot(HaveOccurred()) 273 Expect(out).To(Say(`(?m)^\s+val 3$`)) 274 }) 275 }) 276 277 When("the values are a different type", func() { 278 It("should return an ErrValueMissmatch", func() { 279 err := ui.DisplayChangeForPush("asdf", 2, false, types.NullInt{}, "asdf") 280 Expect(err).To(MatchError(ErrValueMissmatch)) 281 }) 282 }) 283 }) 284 285 When("passed map[string]string for values", func() { 286 It("should display the header with sorted differences", func() { 287 old := map[string]string{"key2": "2", "key3": "2", "key4": "4"} 288 new := map[string]string{"key1": "1", "key3": "3", "key4": "4"} 289 err := ui.DisplayChangeForPush("maps", 2, false, old, new) 290 Expect(err).ToNot(HaveOccurred()) 291 Expect(out).To(Say(`\s+maps`)) 292 Expect(out).To(Say("\x1b\\[32m\\+\\s+key1\x1b\\[0m")) 293 Expect(out).To(Say("\x1b\\[31m\\-\\s+key2\x1b\\[0m")) 294 Expect(out).To(Say("\x1b\\[31m\\-\\s+key3\x1b\\[0m")) 295 Expect(out).To(Say("\x1b\\[32m\\+\\s+key3\x1b\\[0m")) 296 Expect(out).To(Say(`(?m)^\s+key4`)) 297 }) 298 299 When("the values are a different type", func() { 300 It("should return an ErrValueMissmatch", func() { 301 err := ui.DisplayChangeForPush("asdf", 2, false, map[string]string{}, map[string]int{}) 302 Expect(err).To(MatchError(ErrValueMissmatch)) 303 }) 304 }) 305 306 When("both sets are empty", func() { 307 It("does not display anything", func() { 308 var old map[string]string 309 new := map[string]string{} 310 err := ui.DisplayChangeForPush("maps", 2, false, old, new) 311 Expect(err).ToNot(HaveOccurred()) 312 Expect(out).ToNot(Say(`\s+maps`)) 313 }) 314 }) 315 }) 316 }) 317 318 Context("in a non-english language", func() { 319 BeforeEach(func() { 320 fakeConfig.LocaleReturns("fr-FR") 321 }) 322 323 When("passed strings for values", func() { 324 When("the values are not equal", func() { 325 It("should display the differences", func() { 326 err := ui.DisplayChangeForPush("Name", 2, false, "old", "new") 327 Expect(err).ToNot(HaveOccurred()) 328 Expect(out).To(Say("\x1b\\[31m\\-\\s+Nom old\x1b\\[0m")) 329 Expect(out).To(Say("\x1b\\[32m\\+\\s+Nom new\x1b\\[0m")) 330 }) 331 }) 332 333 When("the values are the equal", func() { 334 It("should display the header without differences", func() { 335 err := ui.DisplayChangeForPush("Name", 2, false, "old", "old") 336 Expect(err).ToNot(HaveOccurred()) 337 Expect(out).To(Say(`(?m)^\s+Nom old$`)) 338 }) 339 }) 340 }) 341 342 When("passed list of strings for values", func() { 343 It("should display the header with sorted differences", func() { 344 old := []string{"route2", "route1", "route4"} 345 new := []string{"route4", "route2", "route3"} 346 err := ui.DisplayChangeForPush("Name", 2, false, old, new) 347 Expect(err).ToNot(HaveOccurred()) 348 Expect(out).To(Say(`\s+Nom`)) 349 Expect(out).To(Say("\x1b\\[31m\\-\\s+route1\x1b\\[0m")) 350 Expect(out).To(Say(`(?m)^\s+route2$`)) 351 Expect(out).To(Say("\x1b\\[32m\\+\\s+route3\x1b\\[0m")) 352 Expect(out).To(Say(`(?m)^\s+route4$`)) 353 }) 354 }) 355 }) 356 }) 357 })