github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/util/ui/ui_for_app_test.go (about) 1 package ui_test 2 3 import ( 4 "code.cloudfoundry.org/cli/util/configv3" 5 . "code.cloudfoundry.org/cli/util/ui" 6 "code.cloudfoundry.org/cli/util/ui/uifakes" 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 . "github.com/onsi/gomega/gbytes" 10 ) 11 12 var _ = Describe("UI", func() { 13 var ( 14 ui *UI 15 fakeConfig *uifakes.FakeConfig 16 out *Buffer 17 ) 18 19 BeforeEach(func() { 20 fakeConfig = new(uifakes.FakeConfig) 21 fakeConfig.ColorEnabledReturns(configv3.ColorEnabled) 22 23 var err error 24 ui, err = NewUI(fakeConfig) 25 Expect(err).NotTo(HaveOccurred()) 26 27 out = NewBuffer() 28 ui.Out = out 29 ui.Err = NewBuffer() 30 }) 31 32 Describe("DisplayKeyValueTableForV3App", func() { 33 Context("when the app is running properly", func() { 34 BeforeEach(func() { 35 ui.DisplayKeyValueTableForV3App([][]string{ 36 {"name:", "dora"}, 37 {"requested state:", "started"}, 38 {"processes:", "web:1/1,worker:2/2"}}, 39 []string{}, 40 ) 41 }) 42 43 It("displays a table with the no change in coloring", func() { 44 Expect(ui.Out).To(Say("name: dora\n")) 45 Expect(ui.Out).To(Say("requested state: started\n")) 46 Expect(ui.Out).To(Say("processes: web:1/1,worker:2/2\n")) 47 }) 48 }) 49 50 Context("when the app is stopped and has 0 instances", func() { 51 BeforeEach(func() { 52 ui.DisplayKeyValueTableForV3App([][]string{ 53 {"name:", "dora"}, 54 {"requested state:", "stopped"}, 55 {"processes:", "web:1/1,worker:2/2"}}, 56 []string{}) 57 }) 58 59 It("displays a table with the no change in coloring", func() { 60 Expect(ui.Out).To(Say("name: dora\n")) 61 Expect(ui.Out).To(Say("requested state: stopped\n")) 62 Expect(ui.Out).To(Say("processes: web:1/1,worker:2/2\n")) 63 }) 64 }) 65 66 Context("when the app is started and has 1 crashed process", func() { 67 BeforeEach(func() { 68 ui.DisplayKeyValueTableForV3App([][]string{ 69 {"name:", "dora"}, 70 {"requested state:", "started"}, 71 {"processes:", "web:0/1,worker:2/2"}}, 72 []string{"web"}) 73 }) 74 75 It("displays a table with requested state and crashed instance count in red", func() { 76 Expect(ui.Out).To(Say("name: dora\n")) 77 Expect(ui.Out).To(Say("requested state: \x1b\\[31;1mstarted\x1b\\[0m\n")) 78 Expect(ui.Out).To(Say("processes: \x1b\\[31;1mweb:0/1\x1b\\[0m,worker:2/2\n")) 79 }) 80 }) 81 }) 82 83 Describe("DisplayInstancesTableForApp", func() { 84 Context("in english", func() { 85 It("displays a table with red coloring for down and crashed", func() { 86 ui.DisplayInstancesTableForApp([][]string{ 87 {"", "header1", "header2", "header3"}, 88 {"#0", "starting", "val1", "val2"}, 89 {"#1", "down", "val1", "val2"}, 90 {"#2", "crashed", "val1", "val2"}, 91 }) 92 93 Expect(ui.Out).To(Say("\x1b\\[1mheader1\x1b\\[0m\\s+\x1b\\[1mheader2\x1b\\[0m\\s+\x1b\\[1mheader3\x1b\\[0m")) // Makes sure empty values are not bolded 94 Expect(ui.Out).To(Say("#0\\s+starting\\s+val1\\s+val2")) 95 Expect(ui.Out).To(Say("#1\\s+\x1b\\[31;1mdown\x1b\\[0m\\s+val1\\s+val2")) 96 Expect(ui.Out).To(Say("#2\\s+\x1b\\[31;1mcrashed\x1b\\[0m\\s+val1\\s+val2")) 97 }) 98 }) 99 100 Context("in a non-english language", func() { 101 BeforeEach(func() { 102 fakeConfig.LocaleReturns("fr-FR") 103 104 var err error 105 ui, err = NewUI(fakeConfig) 106 Expect(err).NotTo(HaveOccurred()) 107 ui.Out = NewBuffer() 108 ui.Err = NewBuffer() 109 }) 110 111 It("displays a table with red coloring for down and crashed", func() { 112 ui.DisplayInstancesTableForApp([][]string{ 113 {"", "header1", "header2", "header3"}, 114 {"#0", ui.TranslateText("starting"), "val1", "val2"}, 115 {"#1", ui.TranslateText("down"), "val1", "val2"}, 116 {"#2", ui.TranslateText("crashed"), "val1", "val2"}, 117 }) 118 119 Expect(ui.Out).To(Say("\x1b\\[1mheader1\x1b\\[0m\\s+\x1b\\[1mheader2\x1b\\[0m\\s+\x1b\\[1mheader3\x1b\\[0m")) // Makes sure empty values are not bolded 120 Expect(ui.Out).To(Say("#0\\s+%s\\s+val1\\s+val2", ui.TranslateText("starting"))) 121 Expect(ui.Out).To(Say("#1\\s+\x1b\\[31;1m%s\x1b\\[0m\\s+val1\\s+val2", ui.TranslateText("down"))) 122 Expect(ui.Out).To(Say("#2\\s+\x1b\\[31;1m%s\x1b\\[0m\\s+val1\\s+val2", ui.TranslateText("crashed"))) 123 }) 124 }) 125 }) 126 127 Describe("DisplayKeyValueTableForApp", func() { 128 Context("when the app is running properly", func() { 129 BeforeEach(func() { 130 ui.DisplayKeyValueTableForApp([][]string{ 131 {"name:", "dora"}, 132 {"requested state:", "started"}, 133 {"instances:", "1/1"}, 134 }) 135 }) 136 137 It("displays a table with the no change in coloring", func() { 138 Expect(ui.Out).To(Say("name: dora\n")) 139 Expect(ui.Out).To(Say("requested state: started\n")) 140 Expect(ui.Out).To(Say("instances: 1/1\n")) 141 }) 142 }) 143 144 Context("when the app is stopped and has 0 instances", func() { 145 Context("in english", func() { 146 BeforeEach(func() { 147 ui.DisplayKeyValueTableForApp([][]string{ 148 {"name:", "dora"}, 149 {"requested state:", "stopped"}, 150 {"instances:", "0/1"}, 151 }) 152 }) 153 154 It("displays a table with the no change in coloring", func() { 155 Expect(ui.Out).To(Say("name: dora\n")) 156 Expect(ui.Out).To(Say("requested state: stopped\n")) 157 Expect(ui.Out).To(Say("instances: 0/1\n")) 158 }) 159 }) 160 161 Context("in a non-english language", func() { 162 BeforeEach(func() { 163 fakeConfig.LocaleReturns("fr-FR") 164 165 var err error 166 ui, err = NewUI(fakeConfig) 167 Expect(err).NotTo(HaveOccurred()) 168 ui.Out = NewBuffer() 169 ui.Err = NewBuffer() 170 171 ui.DisplayKeyValueTableForApp([][]string{ 172 {"name:", "dora"}, 173 {"requested state:", ui.TranslateText("stopped")}, 174 {"instances:", "0/1"}, 175 }) 176 }) 177 178 It("displays a table with the no change in coloring", func() { 179 Expect(ui.Out).To(Say("name: dora\n")) 180 Expect(ui.Out).To(Say("requested state: %s\n", ui.TranslateText("stopped"))) 181 Expect(ui.Out).To(Say("instances: 0/1\n")) 182 }) 183 }) 184 }) 185 186 Context("when the app is not stopped and has 0 instances", func() { 187 Context("in english", func() { 188 BeforeEach(func() { 189 ui.DisplayKeyValueTableForApp([][]string{ 190 {"name:", "dora"}, 191 {"requested state:", "running"}, 192 {"instances:", "0/1"}, 193 }) 194 }) 195 196 It("displays a table with requested state and instances in red", func() { 197 Expect(ui.Out).To(Say("name: dora\n")) 198 Expect(ui.Out).To(Say("requested state: \x1b\\[31;1mrunning\x1b\\[0m\n")) 199 Expect(ui.Out).To(Say("instances: \x1b\\[31;1m0/1\x1b\\[0m\n")) 200 }) 201 }) 202 203 Context("in a non-english language", func() { 204 BeforeEach(func() { 205 fakeConfig.LocaleReturns("fr-FR") 206 207 var err error 208 ui, err = NewUI(fakeConfig) 209 Expect(err).NotTo(HaveOccurred()) 210 ui.Out = NewBuffer() 211 ui.Err = NewBuffer() 212 213 ui.DisplayKeyValueTableForApp([][]string{ 214 {"name:", "dora"}, 215 {"requested state:", ui.TranslateText("running")}, 216 {"instances:", "0/1"}, 217 }) 218 }) 219 220 It("displays a table with requested state and instances in red", func() { 221 Expect(ui.Out).To(Say("name: dora\n")) 222 Expect(ui.Out).To(Say("requested state: \x1b\\[31;1m%s\x1b\\[0m\n", ui.TranslateText("running"))) 223 Expect(ui.Out).To(Say("instances: \x1b\\[31;1m0/1\x1b\\[0m\n")) 224 }) 225 }) 226 }) 227 }) 228 })