github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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 uint64s for values", func() {
   286  				When("the values are not equal", func() {
   287  					When("the originalValue is not empty", func() {
   288  						It("should display the header with differences", func() {
   289  							err := ui.DisplayChangeForPush("val", 2, false, uint64(1), uint64(2))
   290  							Expect(err).ToNot(HaveOccurred())
   291  							Expect(out).To(Say("\x1b\\[31m\\-\\s+val  1\x1b\\[0m"))
   292  							Expect(out).To(Say("\x1b\\[32m\\+\\s+val  2\x1b\\[0m"))
   293  						})
   294  					})
   295  
   296  					When("the originalValue is zero", func() {
   297  						It("should display the header with the new value only", func() {
   298  							err := ui.DisplayChangeForPush("val", 2, false, uint64(0), uint64(1))
   299  							Expect(err).ToNot(HaveOccurred())
   300  							Expect(out).To(Say("\x1b\\[32m\\+\\s+val  1\x1b\\[0m"))
   301  						})
   302  					})
   303  				})
   304  
   305  				When("the values are the equal", func() {
   306  					It("should display the header without differences", func() {
   307  						err := ui.DisplayChangeForPush("val", 2, false, uint64(3), uint64(3))
   308  						Expect(err).ToNot(HaveOccurred())
   309  						Expect(out).To(Say(`(?m)^\s+val  3$`))
   310  					})
   311  				})
   312  
   313  				When("the values are a different type", func() {
   314  					It("should return an ErrValueMissmatch", func() {
   315  						err := ui.DisplayChangeForPush("asdf", 2, false, uint64(7), "asdf")
   316  						Expect(err).To(MatchError(ErrValueMissmatch))
   317  					})
   318  				})
   319  			})
   320  
   321  			When("passed map[string]string for values", func() {
   322  				It("should display the header with sorted differences", func() {
   323  					old := map[string]string{"key2": "2", "key3": "2", "key4": "4"}
   324  					new := map[string]string{"key1": "1", "key3": "3", "key4": "4"}
   325  					err := ui.DisplayChangeForPush("maps", 2, false, old, new)
   326  					Expect(err).ToNot(HaveOccurred())
   327  					Expect(out).To(Say(`\s+maps`))
   328  					Expect(out).To(Say("\x1b\\[32m\\+\\s+key1\x1b\\[0m"))
   329  					Expect(out).To(Say("\x1b\\[31m\\-\\s+key2\x1b\\[0m"))
   330  					Expect(out).To(Say("\x1b\\[31m\\-\\s+key3\x1b\\[0m"))
   331  					Expect(out).To(Say("\x1b\\[32m\\+\\s+key3\x1b\\[0m"))
   332  					Expect(out).To(Say(`(?m)^\s+key4`))
   333  				})
   334  
   335  				When("the values are a different type", func() {
   336  					It("should return an ErrValueMissmatch", func() {
   337  						err := ui.DisplayChangeForPush("asdf", 2, false, map[string]string{}, map[string]int{})
   338  						Expect(err).To(MatchError(ErrValueMissmatch))
   339  					})
   340  				})
   341  
   342  				When("both sets are empty", func() {
   343  					It("does not display anything", func() {
   344  						var old map[string]string
   345  						new := map[string]string{}
   346  						err := ui.DisplayChangeForPush("maps", 2, false, old, new)
   347  						Expect(err).ToNot(HaveOccurred())
   348  						Expect(out).ToNot(Say(`\s+maps`))
   349  					})
   350  				})
   351  			})
   352  		})
   353  
   354  		Context("in a non-english language", func() {
   355  			BeforeEach(func() {
   356  				fakeConfig.LocaleReturns("fr-FR")
   357  			})
   358  
   359  			When("passed strings for values", func() {
   360  				When("the values are not equal", func() {
   361  					It("should display the differences", func() {
   362  						err := ui.DisplayChangeForPush("Name", 2, false, "old", "new")
   363  						Expect(err).ToNot(HaveOccurred())
   364  						Expect(out).To(Say("\x1b\\[31m\\-\\s+Nom  old\x1b\\[0m"))
   365  						Expect(out).To(Say("\x1b\\[32m\\+\\s+Nom  new\x1b\\[0m"))
   366  					})
   367  				})
   368  
   369  				When("the values are the equal", func() {
   370  					It("should display the header without differences", func() {
   371  						err := ui.DisplayChangeForPush("Name", 2, false, "old", "old")
   372  						Expect(err).ToNot(HaveOccurred())
   373  						Expect(out).To(Say(`(?m)^\s+Nom  old$`))
   374  					})
   375  				})
   376  			})
   377  
   378  			When("passed list of strings for values", func() {
   379  				It("should display the header with sorted differences", func() {
   380  					old := []string{"route2", "route1", "route4"}
   381  					new := []string{"route4", "route2", "route3"}
   382  					err := ui.DisplayChangeForPush("Name", 2, false, old, new)
   383  					Expect(err).ToNot(HaveOccurred())
   384  					Expect(out).To(Say(`\s+Nom`))
   385  					Expect(out).To(Say("\x1b\\[31m\\-\\s+route1\x1b\\[0m"))
   386  					Expect(out).To(Say(`(?m)^\s+route2$`))
   387  					Expect(out).To(Say("\x1b\\[32m\\+\\s+route3\x1b\\[0m"))
   388  					Expect(out).To(Say(`(?m)^\s+route4$`))
   389  				})
   390  			})
   391  		})
   392  	})
   393  })