kati_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 status
  15. import (
  16. "testing"
  17. )
  18. type lastOutput struct {
  19. counterOutput
  20. action *Action
  21. result ActionResult
  22. msgLevel MsgLevel
  23. msg string
  24. }
  25. func (l *lastOutput) StartAction(a *Action, c Counts) {
  26. l.action = a
  27. l.counterOutput.StartAction(a, c)
  28. }
  29. func (l *lastOutput) FinishAction(r ActionResult, c Counts) {
  30. l.result = r
  31. l.counterOutput.FinishAction(r, c)
  32. }
  33. func (l *lastOutput) Message(level MsgLevel, msg string) {
  34. l.msgLevel = level
  35. l.msg = msg
  36. }
  37. func (l *lastOutput) Flush() {}
  38. func TestKatiNormalCase(t *testing.T) {
  39. status := &Status{}
  40. output := &lastOutput{}
  41. status.AddOutput(output)
  42. parser := &katiOutputParser{
  43. st: status.StartTool(),
  44. }
  45. msg := "*kati*: verbose msg"
  46. parser.parseLine(msg)
  47. output.Expect(t, Counts{})
  48. if output.msgLevel != VerboseLvl {
  49. t.Errorf("Expected verbose message, but got %d", output.msgLevel)
  50. }
  51. if output.msg != msg {
  52. t.Errorf("unexpected message contents:\nwant: %q\n got: %q\n", msg, output.msg)
  53. }
  54. parser.parseLine("out/build-aosp_arm.ninja is missing, regenerating...")
  55. output.Expect(t, Counts{})
  56. parser.parseLine("[1/1] initializing legacy Make module parser ...")
  57. output.Expect(t, Counts{
  58. TotalActions: 1,
  59. RunningActions: 1,
  60. StartedActions: 1,
  61. FinishedActions: 0,
  62. })
  63. parser.parseLine("[2/5] including out/soong/Android-aosp_arm.mk ...")
  64. output.Expect(t, Counts{
  65. TotalActions: 5,
  66. RunningActions: 1,
  67. StartedActions: 2,
  68. FinishedActions: 1,
  69. })
  70. parser.parseLine("[3/5] including a ...")
  71. msg = "a random message"
  72. parser.parseLine(msg)
  73. // Start the next line to flush the previous result
  74. parser.parseLine("[4/5] finishing legacy Make module parsing ...")
  75. msg += "\n"
  76. if output.result.Output != msg {
  77. t.Errorf("output for action did not match:\nwant: %q\n got: %q\n", msg, output.result.Output)
  78. }
  79. parser.parseLine("[5/5] writing legacy Make module rules ...")
  80. parser.parseLine("*kati*: verbose msg")
  81. parser.flushAction()
  82. if output.result.Output != "" {
  83. t.Errorf("expected no output for last action, but got %q", output.result.Output)
  84. }
  85. output.Expect(t, Counts{
  86. TotalActions: 5,
  87. RunningActions: 0,
  88. StartedActions: 5,
  89. FinishedActions: 5,
  90. })
  91. }
  92. func TestKatiExtraIncludes(t *testing.T) {
  93. status := &Status{}
  94. output := &lastOutput{}
  95. status.AddOutput(output)
  96. parser := &katiOutputParser{
  97. st: status.StartTool(),
  98. }
  99. parser.parseLine("[1/1] initializing legacy Make module parser ...")
  100. parser.parseLine("[2/5] including out/soong/Android-aosp_arm.mk ...")
  101. output.Expect(t, Counts{
  102. TotalActions: 5,
  103. RunningActions: 1,
  104. StartedActions: 2,
  105. FinishedActions: 1,
  106. })
  107. parser.parseLine("including a ...")
  108. output.Expect(t, Counts{
  109. TotalActions: 6,
  110. RunningActions: 1,
  111. StartedActions: 3,
  112. FinishedActions: 2,
  113. })
  114. parser.parseLine("including b ...")
  115. output.Expect(t, Counts{
  116. TotalActions: 7,
  117. RunningActions: 1,
  118. StartedActions: 4,
  119. FinishedActions: 3,
  120. })
  121. parser.parseLine("[3/5] finishing legacy Make module parsing ...")
  122. output.Expect(t, Counts{
  123. TotalActions: 7,
  124. RunningActions: 1,
  125. StartedActions: 5,
  126. FinishedActions: 4,
  127. })
  128. }
  129. func TestKatiFailOnError(t *testing.T) {
  130. status := &Status{}
  131. output := &lastOutput{}
  132. status.AddOutput(output)
  133. parser := &katiOutputParser{
  134. st: status.StartTool(),
  135. }
  136. parser.parseLine("[1/1] initializing legacy Make module parser ...")
  137. parser.parseLine("[2/5] inclduing out/soong/Android-aosp_arm.mk ...")
  138. parser.parseLine("build/make/tools/Android.mk:19: error: testing")
  139. parser.flushAction()
  140. if output.result.Error == nil {
  141. t.Errorf("Expected the last action to be marked as an error")
  142. }
  143. }