blob: c81e837b0b267ad06002817ad1d6d53b6f4dfc2d [file] [log] [blame]
Colin Crossdde49cb2019-06-08 21:59:42 -07001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package terminal
16
17import (
18 "bytes"
19 "fmt"
20 "testing"
21
22 "android/soong/ui/status"
23)
24
25func TestStatusOutput(t *testing.T) {
26 tests := []struct {
27 name string
28 calls func(stat status.StatusOutput)
29 smart string
30 dumb string
31 }{
32 {
33 name: "two actions",
34 calls: twoActions,
35 smart: "\r[ 0% 0/2] action1\x1b[K\r[ 50% 1/2] action1\x1b[K\r[ 50% 1/2] action2\x1b[K\r[100% 2/2] action2\x1b[K\n",
36 dumb: "[ 50% 1/2] action1\n[100% 2/2] action2\n",
37 },
38 {
39 name: "two parallel actions",
40 calls: twoParallelActions,
41 smart: "\r[ 0% 0/2] action1\x1b[K\r[ 0% 0/2] action2\x1b[K\r[ 50% 1/2] action1\x1b[K\r[100% 2/2] action2\x1b[K\n",
42 dumb: "[ 50% 1/2] action1\n[100% 2/2] action2\n",
43 },
44 {
45 name: "action with output",
46 calls: actionsWithOutput,
47 smart: "\r[ 0% 0/3] action1\x1b[K\r[ 33% 1/3] action1\x1b[K\r[ 33% 1/3] action2\x1b[K\r[ 66% 2/3] action2\x1b[K\noutput1\noutput2\n\r[ 66% 2/3] action3\x1b[K\r[100% 3/3] action3\x1b[K\n",
48 dumb: "[ 33% 1/3] action1\n[ 66% 2/3] action2\noutput1\noutput2\n[100% 3/3] action3\n",
49 },
50 {
51 name: "action with output without newline",
52 calls: actionsWithOutputWithoutNewline,
53 smart: "\r[ 0% 0/3] action1\x1b[K\r[ 33% 1/3] action1\x1b[K\r[ 33% 1/3] action2\x1b[K\r[ 66% 2/3] action2\x1b[K\noutput1\noutput2\n\r[ 66% 2/3] action3\x1b[K\r[100% 3/3] action3\x1b[K\n",
54 dumb: "[ 33% 1/3] action1\n[ 66% 2/3] action2\noutput1\noutput2\n[100% 3/3] action3\n",
55 },
56 {
57 name: "action with error",
58 calls: actionsWithError,
59 smart: "\r[ 0% 0/3] action1\x1b[K\r[ 33% 1/3] action1\x1b[K\r[ 33% 1/3] action2\x1b[K\r[ 66% 2/3] action2\x1b[K\nFAILED: f1 f2\ntouch f1 f2\nerror1\nerror2\n\r[ 66% 2/3] action3\x1b[K\r[100% 3/3] action3\x1b[K\n",
60 dumb: "[ 33% 1/3] action1\n[ 66% 2/3] action2\nFAILED: f1 f2\ntouch f1 f2\nerror1\nerror2\n[100% 3/3] action3\n",
61 },
62 {
63 name: "action with empty description",
64 calls: actionWithEmptyDescription,
65 smart: "\r[ 0% 0/1] command1\x1b[K\r[100% 1/1] command1\x1b[K\n",
66 dumb: "[100% 1/1] command1\n",
67 },
68 {
69 name: "messages",
70 calls: actionsWithMessages,
71 smart: "\r[ 0% 0/2] action1\x1b[K\r[ 50% 1/2] action1\x1b[K\rstatus\x1b[K\r\x1b[Kprint\nFAILED: error\n\r[ 50% 1/2] action2\x1b[K\r[100% 2/2] action2\x1b[K\n",
72 dumb: "[ 50% 1/2] action1\nstatus\nprint\nFAILED: error\n[100% 2/2] action2\n",
73 },
74 {
75 name: "action with long description",
76 calls: actionWithLongDescription,
77 smart: "\r[ 0% 0/2] action with very long descrip\x1b[K\r[ 50% 1/2] action with very long descrip\x1b[K\n",
78 dumb: "[ 50% 1/2] action with very long description to test eliding\n",
79 },
80 {
81 name: "action with output with ansi codes",
82 calls: actionWithOuptutWithAnsiCodes,
83 smart: "\r[ 0% 0/1] action1\x1b[K\r[100% 1/1] action1\x1b[K\n\x1b[31mcolor\x1b[0m\n",
84 dumb: "[100% 1/1] action1\ncolor\n",
85 },
86 }
87
88 for _, tt := range tests {
89 t.Run(tt.name, func(t *testing.T) {
90 t.Run("smart", func(t *testing.T) {
91 smart := &fakeSmartTerminal{termWidth: 40}
Colin Cross097ed2a2019-06-08 21:48:58 -070092 stat := NewStatusOutput(smart, "", false)
Colin Crossdde49cb2019-06-08 21:59:42 -070093 tt.calls(stat)
94 stat.Flush()
Colin Crossdde49cb2019-06-08 21:59:42 -070095
96 if g, w := smart.String(), tt.smart; g != w {
97 t.Errorf("want:\n%q\ngot:\n%q", w, g)
98 }
99 })
100
101 t.Run("dumb", func(t *testing.T) {
102 dumb := &bytes.Buffer{}
Colin Cross097ed2a2019-06-08 21:48:58 -0700103 stat := NewStatusOutput(dumb, "", false)
Colin Crossdde49cb2019-06-08 21:59:42 -0700104 tt.calls(stat)
105 stat.Flush()
Colin Crossdde49cb2019-06-08 21:59:42 -0700106
107 if g, w := dumb.String(), tt.dumb; g != w {
108 t.Errorf("want:\n%q\ngot:\n%q", w, g)
109 }
110 })
111 })
112 }
113}
114
115type runner struct {
116 counts status.Counts
117 stat status.StatusOutput
118}
119
120func newRunner(stat status.StatusOutput, totalActions int) *runner {
121 return &runner{
122 counts: status.Counts{TotalActions: totalActions},
123 stat: stat,
124 }
125}
126
127func (r *runner) startAction(action *status.Action) {
128 r.counts.StartedActions++
129 r.counts.RunningActions++
130 r.stat.StartAction(action, r.counts)
131}
132
133func (r *runner) finishAction(result status.ActionResult) {
134 r.counts.FinishedActions++
135 r.counts.RunningActions--
136 r.stat.FinishAction(result, r.counts)
137}
138
139func (r *runner) finishAndStartAction(result status.ActionResult, action *status.Action) {
140 r.counts.FinishedActions++
141 r.stat.FinishAction(result, r.counts)
142
143 r.counts.StartedActions++
144 r.stat.StartAction(action, r.counts)
145}
146
147var (
148 action1 = &status.Action{Description: "action1"}
149 result1 = status.ActionResult{Action: action1}
150 action2 = &status.Action{Description: "action2"}
151 result2 = status.ActionResult{Action: action2}
152 action3 = &status.Action{Description: "action3"}
153 result3 = status.ActionResult{Action: action3}
154)
155
156func twoActions(stat status.StatusOutput) {
157 runner := newRunner(stat, 2)
158 runner.startAction(action1)
159 runner.finishAction(result1)
160 runner.startAction(action2)
161 runner.finishAction(result2)
162}
163
164func twoParallelActions(stat status.StatusOutput) {
165 runner := newRunner(stat, 2)
166 runner.startAction(action1)
167 runner.startAction(action2)
168 runner.finishAction(result1)
169 runner.finishAction(result2)
170}
171
172func actionsWithOutput(stat status.StatusOutput) {
173 result2WithOutput := status.ActionResult{Action: action2, Output: "output1\noutput2\n"}
174
175 runner := newRunner(stat, 3)
176 runner.startAction(action1)
177 runner.finishAction(result1)
178 runner.startAction(action2)
179 runner.finishAction(result2WithOutput)
180 runner.startAction(action3)
181 runner.finishAction(result3)
182}
183
184func actionsWithOutputWithoutNewline(stat status.StatusOutput) {
185 result2WithOutputWithoutNewline := status.ActionResult{Action: action2, Output: "output1\noutput2"}
186
187 runner := newRunner(stat, 3)
188 runner.startAction(action1)
189 runner.finishAction(result1)
190 runner.startAction(action2)
191 runner.finishAction(result2WithOutputWithoutNewline)
192 runner.startAction(action3)
193 runner.finishAction(result3)
194}
195
196func actionsWithError(stat status.StatusOutput) {
197 action2WithError := &status.Action{Description: "action2", Outputs: []string{"f1", "f2"}, Command: "touch f1 f2"}
198 result2WithError := status.ActionResult{Action: action2WithError, Output: "error1\nerror2\n", Error: fmt.Errorf("error1")}
199
200 runner := newRunner(stat, 3)
201 runner.startAction(action1)
202 runner.finishAction(result1)
203 runner.startAction(action2WithError)
204 runner.finishAction(result2WithError)
205 runner.startAction(action3)
206 runner.finishAction(result3)
207}
208
209func actionWithEmptyDescription(stat status.StatusOutput) {
210 action1 := &status.Action{Command: "command1"}
211 result1 := status.ActionResult{Action: action1}
212
213 runner := newRunner(stat, 1)
214 runner.startAction(action1)
215 runner.finishAction(result1)
216}
217
218func actionsWithMessages(stat status.StatusOutput) {
219 runner := newRunner(stat, 2)
220
221 runner.startAction(action1)
222 runner.finishAction(result1)
223
224 stat.Message(status.VerboseLvl, "verbose")
225 stat.Message(status.StatusLvl, "status")
226 stat.Message(status.PrintLvl, "print")
227 stat.Message(status.ErrorLvl, "error")
228
229 runner.startAction(action2)
230 runner.finishAction(result2)
231}
232
233func actionWithLongDescription(stat status.StatusOutput) {
234 action1 := &status.Action{Description: "action with very long description to test eliding"}
235 result1 := status.ActionResult{Action: action1}
236
237 runner := newRunner(stat, 2)
238
239 runner.startAction(action1)
240
241 runner.finishAction(result1)
242}
243
244func actionWithOuptutWithAnsiCodes(stat status.StatusOutput) {
245 result1WithOutputWithAnsiCodes := status.ActionResult{Action: action1, Output: "\x1b[31mcolor\x1b[0m"}
246
247 runner := newRunner(stat, 1)
248 runner.startAction(action1)
249 runner.finishAction(result1WithOutputWithAnsiCodes)
250}
251
252func TestSmartStatusOutputWidthChange(t *testing.T) {
253 smart := &fakeSmartTerminal{termWidth: 40}
Colin Cross097ed2a2019-06-08 21:48:58 -0700254 stat := NewStatusOutput(smart, "", false)
Colin Crossdde49cb2019-06-08 21:59:42 -0700255
256 runner := newRunner(stat, 2)
257
258 action := &status.Action{Description: "action with very long description to test eliding"}
259 result := status.ActionResult{Action: action}
260
261 runner.startAction(action)
262 smart.termWidth = 30
263 runner.finishAction(result)
264
265 stat.Flush()
Colin Crossdde49cb2019-06-08 21:59:42 -0700266
267 w := "\r[ 0% 0/2] action with very long descrip\x1b[K\r[ 50% 1/2] action with very lo\x1b[K\n"
268
269 if g := smart.String(); g != w {
270 t.Errorf("want:\n%q\ngot:\n%q", w, g)
271 }
272}