status_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // 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. package terminal
  15. import (
  16. "bytes"
  17. "fmt"
  18. "os"
  19. "syscall"
  20. "testing"
  21. "android/soong/ui/status"
  22. )
  23. func TestStatusOutput(t *testing.T) {
  24. tests := []struct {
  25. name string
  26. calls func(stat status.StatusOutput)
  27. smart string
  28. simple string
  29. }{
  30. {
  31. name: "two actions",
  32. calls: twoActions,
  33. smart: "\r\x1b[1m[ 0% 0/2] action1\x1b[0m\x1b[K\r\x1b[1m[ 50% 1/2] action1\x1b[0m\x1b[K\r\x1b[1m[ 50% 1/2] action2\x1b[0m\x1b[K\r\x1b[1m[100% 2/2] action2\x1b[0m\x1b[K\n",
  34. simple: "[ 50% 1/2] action1\n[100% 2/2] action2\n",
  35. },
  36. {
  37. name: "two parallel actions",
  38. calls: twoParallelActions,
  39. smart: "\r\x1b[1m[ 0% 0/2] action1\x1b[0m\x1b[K\r\x1b[1m[ 0% 0/2] action2\x1b[0m\x1b[K\r\x1b[1m[ 50% 1/2] action1\x1b[0m\x1b[K\r\x1b[1m[100% 2/2] action2\x1b[0m\x1b[K\n",
  40. simple: "[ 50% 1/2] action1\n[100% 2/2] action2\n",
  41. },
  42. {
  43. name: "action with output",
  44. calls: actionsWithOutput,
  45. smart: "\r\x1b[1m[ 0% 0/3] action1\x1b[0m\x1b[K\r\x1b[1m[ 33% 1/3] action1\x1b[0m\x1b[K\r\x1b[1m[ 33% 1/3] action2\x1b[0m\x1b[K\r\x1b[1m[ 66% 2/3] action2\x1b[0m\x1b[K\noutput1\noutput2\n\r\x1b[1m[ 66% 2/3] action3\x1b[0m\x1b[K\r\x1b[1m[100% 3/3] action3\x1b[0m\x1b[K\n",
  46. simple: "[ 33% 1/3] action1\n[ 66% 2/3] action2\noutput1\noutput2\n[100% 3/3] action3\n",
  47. },
  48. {
  49. name: "action with output without newline",
  50. calls: actionsWithOutputWithoutNewline,
  51. smart: "\r\x1b[1m[ 0% 0/3] action1\x1b[0m\x1b[K\r\x1b[1m[ 33% 1/3] action1\x1b[0m\x1b[K\r\x1b[1m[ 33% 1/3] action2\x1b[0m\x1b[K\r\x1b[1m[ 66% 2/3] action2\x1b[0m\x1b[K\noutput1\noutput2\n\r\x1b[1m[ 66% 2/3] action3\x1b[0m\x1b[K\r\x1b[1m[100% 3/3] action3\x1b[0m\x1b[K\n",
  52. simple: "[ 33% 1/3] action1\n[ 66% 2/3] action2\noutput1\noutput2\n[100% 3/3] action3\n",
  53. },
  54. {
  55. name: "action with error",
  56. calls: actionsWithError,
  57. smart: "\r\x1b[1m[ 0% 0/3] action1\x1b[0m\x1b[K\r\x1b[1m[ 33% 1/3] action1\x1b[0m\x1b[K\r\x1b[1m[ 33% 1/3] action2\x1b[0m\x1b[K\r\x1b[1m[ 66% 2/3] action2\x1b[0m\x1b[K\nFAILED: f1 f2\ntouch f1 f2\nerror1\nerror2\n\r\x1b[1m[ 66% 2/3] action3\x1b[0m\x1b[K\r\x1b[1m[100% 3/3] action3\x1b[0m\x1b[K\n",
  58. simple: "[ 33% 1/3] action1\n[ 66% 2/3] action2\nFAILED: f1 f2\ntouch f1 f2\nerror1\nerror2\n[100% 3/3] action3\n",
  59. },
  60. {
  61. name: "action with empty description",
  62. calls: actionWithEmptyDescription,
  63. smart: "\r\x1b[1m[ 0% 0/1] command1\x1b[0m\x1b[K\r\x1b[1m[100% 1/1] command1\x1b[0m\x1b[K\n",
  64. simple: "[100% 1/1] command1\n",
  65. },
  66. {
  67. name: "messages",
  68. calls: actionsWithMessages,
  69. smart: "\r\x1b[1m[ 0% 0/2] action1\x1b[0m\x1b[K\r\x1b[1m[ 50% 1/2] action1\x1b[0m\x1b[K\r\x1b[1mstatus\x1b[0m\x1b[K\r\x1b[Kprint\nFAILED: error\n\r\x1b[1m[ 50% 1/2] action2\x1b[0m\x1b[K\r\x1b[1m[100% 2/2] action2\x1b[0m\x1b[K\n",
  70. simple: "[ 50% 1/2] action1\nstatus\nprint\nFAILED: error\n[100% 2/2] action2\n",
  71. },
  72. {
  73. name: "action with long description",
  74. calls: actionWithLongDescription,
  75. smart: "\r\x1b[1m[ 0% 0/2] action with very long descrip\x1b[0m\x1b[K\r\x1b[1m[ 50% 1/2] action with very long descrip\x1b[0m\x1b[K\n",
  76. simple: "[ 50% 1/2] action with very long description to test eliding\n",
  77. },
  78. {
  79. name: "action with output with ansi codes",
  80. calls: actionWithOutputWithAnsiCodes,
  81. smart: "\r\x1b[1m[ 0% 0/1] action1\x1b[0m\x1b[K\r\x1b[1m[100% 1/1] action1\x1b[0m\x1b[K\n\x1b[31mcolor\x1b[0m\n\x1b[31mcolor message\x1b[0m\n",
  82. simple: "[100% 1/1] action1\ncolor\ncolor message\n",
  83. },
  84. }
  85. os.Setenv(tableHeightEnVar, "")
  86. for _, tt := range tests {
  87. t.Run(tt.name, func(t *testing.T) {
  88. t.Run("smart", func(t *testing.T) {
  89. smart := &fakeSmartTerminal{termWidth: 40}
  90. stat := NewStatusOutput(smart, "", false, false, false)
  91. tt.calls(stat)
  92. stat.Flush()
  93. if g, w := smart.String(), tt.smart; g != w {
  94. t.Errorf("want:\n%q\ngot:\n%q", w, g)
  95. }
  96. })
  97. t.Run("simple", func(t *testing.T) {
  98. simple := &bytes.Buffer{}
  99. stat := NewStatusOutput(simple, "", false, false, false)
  100. tt.calls(stat)
  101. stat.Flush()
  102. if g, w := simple.String(), tt.simple; g != w {
  103. t.Errorf("want:\n%q\ngot:\n%q", w, g)
  104. }
  105. })
  106. t.Run("force simple", func(t *testing.T) {
  107. smart := &fakeSmartTerminal{termWidth: 40}
  108. stat := NewStatusOutput(smart, "", true, false, false)
  109. tt.calls(stat)
  110. stat.Flush()
  111. if g, w := smart.String(), tt.simple; g != w {
  112. t.Errorf("want:\n%q\ngot:\n%q", w, g)
  113. }
  114. })
  115. })
  116. }
  117. }
  118. type runner struct {
  119. counts status.Counts
  120. stat status.StatusOutput
  121. }
  122. func newRunner(stat status.StatusOutput, totalActions int) *runner {
  123. return &runner{
  124. counts: status.Counts{TotalActions: totalActions},
  125. stat: stat,
  126. }
  127. }
  128. func (r *runner) startAction(action *status.Action) {
  129. r.counts.StartedActions++
  130. r.counts.RunningActions++
  131. r.stat.StartAction(action, r.counts)
  132. }
  133. func (r *runner) finishAction(result status.ActionResult) {
  134. r.counts.FinishedActions++
  135. r.counts.RunningActions--
  136. r.stat.FinishAction(result, r.counts)
  137. }
  138. func (r *runner) finishAndStartAction(result status.ActionResult, action *status.Action) {
  139. r.counts.FinishedActions++
  140. r.stat.FinishAction(result, r.counts)
  141. r.counts.StartedActions++
  142. r.stat.StartAction(action, r.counts)
  143. }
  144. var (
  145. action1 = &status.Action{Description: "action1"}
  146. result1 = status.ActionResult{Action: action1}
  147. action2 = &status.Action{Description: "action2"}
  148. result2 = status.ActionResult{Action: action2}
  149. action3 = &status.Action{Description: "action3"}
  150. result3 = status.ActionResult{Action: action3}
  151. )
  152. func twoActions(stat status.StatusOutput) {
  153. runner := newRunner(stat, 2)
  154. runner.startAction(action1)
  155. runner.finishAction(result1)
  156. runner.startAction(action2)
  157. runner.finishAction(result2)
  158. }
  159. func twoParallelActions(stat status.StatusOutput) {
  160. runner := newRunner(stat, 2)
  161. runner.startAction(action1)
  162. runner.startAction(action2)
  163. runner.finishAction(result1)
  164. runner.finishAction(result2)
  165. }
  166. func actionsWithOutput(stat status.StatusOutput) {
  167. result2WithOutput := status.ActionResult{Action: action2, Output: "output1\noutput2\n"}
  168. runner := newRunner(stat, 3)
  169. runner.startAction(action1)
  170. runner.finishAction(result1)
  171. runner.startAction(action2)
  172. runner.finishAction(result2WithOutput)
  173. runner.startAction(action3)
  174. runner.finishAction(result3)
  175. }
  176. func actionsWithOutputWithoutNewline(stat status.StatusOutput) {
  177. result2WithOutputWithoutNewline := status.ActionResult{Action: action2, Output: "output1\noutput2"}
  178. runner := newRunner(stat, 3)
  179. runner.startAction(action1)
  180. runner.finishAction(result1)
  181. runner.startAction(action2)
  182. runner.finishAction(result2WithOutputWithoutNewline)
  183. runner.startAction(action3)
  184. runner.finishAction(result3)
  185. }
  186. func actionsWithError(stat status.StatusOutput) {
  187. action2WithError := &status.Action{Description: "action2", Outputs: []string{"f1", "f2"}, Command: "touch f1 f2"}
  188. result2WithError := status.ActionResult{Action: action2WithError, Output: "error1\nerror2\n", Error: fmt.Errorf("error1")}
  189. runner := newRunner(stat, 3)
  190. runner.startAction(action1)
  191. runner.finishAction(result1)
  192. runner.startAction(action2WithError)
  193. runner.finishAction(result2WithError)
  194. runner.startAction(action3)
  195. runner.finishAction(result3)
  196. }
  197. func actionWithEmptyDescription(stat status.StatusOutput) {
  198. action1 := &status.Action{Command: "command1"}
  199. result1 := status.ActionResult{Action: action1}
  200. runner := newRunner(stat, 1)
  201. runner.startAction(action1)
  202. runner.finishAction(result1)
  203. }
  204. func actionsWithMessages(stat status.StatusOutput) {
  205. runner := newRunner(stat, 2)
  206. runner.startAction(action1)
  207. runner.finishAction(result1)
  208. stat.Message(status.VerboseLvl, "verbose")
  209. stat.Message(status.StatusLvl, "status")
  210. stat.Message(status.PrintLvl, "print")
  211. stat.Message(status.ErrorLvl, "error")
  212. runner.startAction(action2)
  213. runner.finishAction(result2)
  214. }
  215. func actionWithLongDescription(stat status.StatusOutput) {
  216. action1 := &status.Action{Description: "action with very long description to test eliding"}
  217. result1 := status.ActionResult{Action: action1}
  218. runner := newRunner(stat, 2)
  219. runner.startAction(action1)
  220. runner.finishAction(result1)
  221. }
  222. func actionWithOutputWithAnsiCodes(stat status.StatusOutput) {
  223. result1WithOutputWithAnsiCodes := status.ActionResult{Action: action1, Output: "\x1b[31mcolor\x1b[0m"}
  224. runner := newRunner(stat, 1)
  225. runner.startAction(action1)
  226. runner.finishAction(result1WithOutputWithAnsiCodes)
  227. stat.Message(status.PrintLvl, "\x1b[31mcolor message\x1b[0m")
  228. }
  229. func TestSmartStatusOutputWidthChange(t *testing.T) {
  230. os.Setenv(tableHeightEnVar, "")
  231. smart := &fakeSmartTerminal{termWidth: 40}
  232. stat := NewStatusOutput(smart, "", false, false, false)
  233. smartStat := stat.(*smartStatusOutput)
  234. smartStat.sigwinchHandled = make(chan bool)
  235. runner := newRunner(stat, 2)
  236. action := &status.Action{Description: "action with very long description to test eliding"}
  237. result := status.ActionResult{Action: action}
  238. runner.startAction(action)
  239. smart.termWidth = 30
  240. // Fake a SIGWINCH
  241. smartStat.sigwinch <- syscall.SIGWINCH
  242. <-smartStat.sigwinchHandled
  243. runner.finishAction(result)
  244. stat.Flush()
  245. w := "\r\x1b[1m[ 0% 0/2] action with very long descrip\x1b[0m\x1b[K\r\x1b[1m[ 50% 1/2] action with very lo\x1b[0m\x1b[K\n"
  246. if g := smart.String(); g != w {
  247. t.Errorf("want:\n%q\ngot:\n%q", w, g)
  248. }
  249. }
  250. func TestSmartStatusDoesntHideAfterSucecss(t *testing.T) {
  251. os.Setenv(tableHeightEnVar, "")
  252. smart := &fakeSmartTerminal{termWidth: 40}
  253. stat := NewStatusOutput(smart, "", false, false, false)
  254. smartStat := stat.(*smartStatusOutput)
  255. smartStat.sigwinchHandled = make(chan bool)
  256. runner := newRunner(stat, 2)
  257. action1 := &status.Action{Description: "action1"}
  258. result1 := status.ActionResult{
  259. Action: action1,
  260. Output: "Output1",
  261. }
  262. action2 := &status.Action{Description: "action2"}
  263. result2 := status.ActionResult{
  264. Action: action2,
  265. Output: "Output2",
  266. }
  267. runner.startAction(action1)
  268. runner.startAction(action2)
  269. runner.finishAction(result1)
  270. runner.finishAction(result2)
  271. stat.Flush()
  272. w := "\r\x1b[1m[ 0% 0/2] action1\x1b[0m\x1b[K\r\x1b[1m[ 0% 0/2] action2\x1b[0m\x1b[K\r\x1b[1m[ 50% 1/2] action1\x1b[0m\x1b[K\nOutput1\n\r\x1b[1m[100% 2/2] action2\x1b[0m\x1b[K\nOutput2\n"
  273. if g := smart.String(); g != w {
  274. t.Errorf("want:\n%q\ngot:\n%q", w, g)
  275. }
  276. }
  277. func TestSmartStatusHideAfterFailure(t *testing.T) {
  278. os.Setenv(tableHeightEnVar, "")
  279. smart := &fakeSmartTerminal{termWidth: 40}
  280. stat := NewStatusOutput(smart, "", false, false, false)
  281. smartStat := stat.(*smartStatusOutput)
  282. smartStat.sigwinchHandled = make(chan bool)
  283. runner := newRunner(stat, 2)
  284. action1 := &status.Action{Description: "action1"}
  285. result1 := status.ActionResult{
  286. Action: action1,
  287. Output: "Output1",
  288. Error: fmt.Errorf("Error1"),
  289. }
  290. action2 := &status.Action{Description: "action2"}
  291. result2 := status.ActionResult{
  292. Action: action2,
  293. Output: "Output2",
  294. }
  295. runner.startAction(action1)
  296. runner.startAction(action2)
  297. runner.finishAction(result1)
  298. runner.finishAction(result2)
  299. stat.Flush()
  300. w := "\r\x1b[1m[ 0% 0/2] action1\x1b[0m\x1b[K\r\x1b[1m[ 0% 0/2] action2\x1b[0m\x1b[K\r\x1b[1m[ 50% 1/2] action1\x1b[0m\x1b[K\nFAILED: \nOutput1\n\r\x1b[1m[100% 2/2] action2\x1b[0m\x1b[K\nThere was 1 action that completed after the action that failed. See verbose.log.gz for its output.\n"
  301. if g := smart.String(); g != w {
  302. t.Errorf("want:\n%q\ngot:\n%q", w, g)
  303. }
  304. }
  305. func TestSmartStatusHideAfterFailurePlural(t *testing.T) {
  306. os.Setenv(tableHeightEnVar, "")
  307. smart := &fakeSmartTerminal{termWidth: 40}
  308. stat := NewStatusOutput(smart, "", false, false, false)
  309. smartStat := stat.(*smartStatusOutput)
  310. smartStat.sigwinchHandled = make(chan bool)
  311. runner := newRunner(stat, 2)
  312. action1 := &status.Action{Description: "action1"}
  313. result1 := status.ActionResult{
  314. Action: action1,
  315. Output: "Output1",
  316. Error: fmt.Errorf("Error1"),
  317. }
  318. action2 := &status.Action{Description: "action2"}
  319. result2 := status.ActionResult{
  320. Action: action2,
  321. Output: "Output2",
  322. }
  323. action3 := &status.Action{Description: "action3"}
  324. result3 := status.ActionResult{
  325. Action: action3,
  326. Output: "Output3",
  327. }
  328. runner.startAction(action1)
  329. runner.startAction(action2)
  330. runner.startAction(action3)
  331. runner.finishAction(result1)
  332. runner.finishAction(result2)
  333. runner.finishAction(result3)
  334. stat.Flush()
  335. w := "\r\x1b[1m[ 0% 0/2] action1\x1b[0m\x1b[K\r\x1b[1m[ 0% 0/2] action2\x1b[0m\x1b[K\r\x1b[1m[ 0% 0/2] action3\x1b[0m\x1b[K\r\x1b[1m[ 50% 1/2] action1\x1b[0m\x1b[K\nFAILED: \nOutput1\n\r\x1b[1m[100% 2/2] action2\x1b[0m\x1b[K\r\x1b[1m[150% 3/2] action3\x1b[0m\x1b[K\nThere were 2 actions that completed after the action that failed. See verbose.log.gz for their output.\n"
  336. if g := smart.String(); g != w {
  337. t.Errorf("want:\n%q\ngot:\n%q", w, g)
  338. }
  339. }
  340. func TestSmartStatusDontHideErrorAfterFailure(t *testing.T) {
  341. os.Setenv(tableHeightEnVar, "")
  342. smart := &fakeSmartTerminal{termWidth: 40}
  343. stat := NewStatusOutput(smart, "", false, false, false)
  344. smartStat := stat.(*smartStatusOutput)
  345. smartStat.sigwinchHandled = make(chan bool)
  346. runner := newRunner(stat, 2)
  347. action1 := &status.Action{Description: "action1"}
  348. result1 := status.ActionResult{
  349. Action: action1,
  350. Output: "Output1",
  351. Error: fmt.Errorf("Error1"),
  352. }
  353. action2 := &status.Action{Description: "action2"}
  354. result2 := status.ActionResult{
  355. Action: action2,
  356. Output: "Output2",
  357. Error: fmt.Errorf("Error1"),
  358. }
  359. runner.startAction(action1)
  360. runner.startAction(action2)
  361. runner.finishAction(result1)
  362. runner.finishAction(result2)
  363. stat.Flush()
  364. w := "\r\x1b[1m[ 0% 0/2] action1\x1b[0m\x1b[K\r\x1b[1m[ 0% 0/2] action2\x1b[0m\x1b[K\r\x1b[1m[ 50% 1/2] action1\x1b[0m\x1b[K\nFAILED: \nOutput1\n\r\x1b[1m[100% 2/2] action2\x1b[0m\x1b[K\nFAILED: \nOutput2\n"
  365. if g := smart.String(); g != w {
  366. t.Errorf("want:\n%q\ngot:\n%q", w, g)
  367. }
  368. }