Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * A test configuration that generates a compact 1-line progress bar. The bar is | 6 * A test configuration that generates a compact 1-line progress bar. The bar is |
| 7 * updated in-place before and after each test is executed. If all test pass, | 7 * updated in-place before and after each test is executed. If all test pass, |
| 8 * you should only see a couple lines in the terminal. If a test fails, the | 8 * you should only see a couple lines in the terminal. If a test fails, the |
| 9 * failure is shown and the progress bar continues to be updated below it. | 9 * failure is shown and the progress bar continues to be updated below it. |
| 10 */ | 10 */ |
| 11 library compact_vm_config; | 11 library compact_vm_config; |
| 12 | 12 |
| 13 import 'dart:io'; | 13 import 'dart:io'; |
| 14 import 'unittest.dart'; | 14 import 'unittest.dart'; |
| 15 import 'vm_config.dart'; | 15 import 'vm_config.dart'; |
| 16 | 16 |
| 17 const String _GREEN = '\u001b[32m'; | 17 const String _GREEN = '\u001b[32m'; |
| 18 const String _RED = '\u001b[31m'; | 18 const String _RED = '\u001b[31m'; |
| 19 const String _NONE = '\u001b[0m'; | 19 const String _NONE = '\u001b[0m'; |
| 20 | |
| 20 const int MAX_LINE = 80; | 21 const int MAX_LINE = 80; |
| 21 | 22 |
| 22 class CompactVMConfiguration extends VMConfiguration { | 23 class CompactVMConfiguration extends VMConfiguration { |
| 23 DateTime _start; | 24 DateTime _start; |
| 24 int _pass = 0; | 25 int _pass = 0; |
| 25 int _fail = 0; | 26 int _fail = 0; |
| 26 | 27 |
| 27 void onInit() { | 28 void onInit() { |
| 28 super.onInit(); | 29 // On the bots, we need to output the "unittest-suite-..." |
| 30 // boilerplate that the bots look for. On local machines, don't print it. | |
| 31 if (_runningOnBuildbot) super.onInit(); | |
| 29 } | 32 } |
| 30 | 33 |
| 31 void onStart() { | 34 void onStart() { |
| 32 super.onStart(); | |
| 33 _start = new DateTime.now(); | 35 _start = new DateTime.now(); |
| 34 } | 36 } |
| 35 | 37 |
| 36 void onTestStart(TestCase test) { | 38 void onTestStart(TestCase test) { |
| 37 super.onTestStart(test); | 39 super.onTestStart(test); |
| 38 _progressLine(_start, _pass, _fail, test.description); | 40 _progressLine(_start, _pass, _fail, test.description); |
| 39 } | 41 } |
| 40 | 42 |
| 41 void onTestResult(TestCase test) { | 43 void onTestResult(TestCase test) { |
| 42 super.onTestResult(test); | 44 super.onTestResult(test); |
| 43 if (test.result == PASS) { | 45 if (test.result == PASS) { |
| 44 _pass++; | 46 _pass++; |
| 45 _progressLine(_start, _pass, _fail, test.description); | 47 _progressLine(_start, _pass, _fail, test.description); |
| 46 } else { | 48 } else { |
| 47 _fail++; | 49 _fail++; |
| 48 _progressLine(_start, _pass, _fail, test.description); | 50 _progressLine(_start, _pass, _fail, test.description); |
| 49 print(''); | 51 print(''); |
| 50 if (test.message != '') { | 52 if (test.message != '') { |
| 51 print(_indent(test.message)); | 53 print(_indent(test.message)); |
| 52 } | 54 } |
| 53 | 55 |
| 54 if (test.stackTrace != null && test.stackTrace != '') { | 56 if (test.stackTrace != null && test.stackTrace != '') { |
| 55 print(_indent(test.stackTrace)); | 57 print(_indent(test.stackTrace)); |
| 56 } | 58 } |
| 57 } | 59 } |
| 58 } | 60 } |
| 59 | 61 |
| 62 void onDone(bool success) { | |
| 63 // On the bots, we need to output the "unittest-suite-..." | |
| 64 // boilerplate that the bots look for. On local machines, don't print it. | |
| 65 if (_runningOnBuildbot) super.onDone(success); | |
|
Siggi Cherem (dart-lang)
2013/07/11 00:26:31
we should at least exit with an appropriate exit c
Bob Nystrom
2013/07/11 16:42:15
Done.
| |
| 66 } | |
| 67 | |
| 60 String _indent(String str) { | 68 String _indent(String str) { |
| 61 return str.split("\n").map((line) => " $line").join("\n"); | 69 return str.split("\n").map((line) => " $line").join("\n"); |
| 62 } | 70 } |
| 63 | 71 |
| 72 /// Returns whether we're running on a Dart build bot. | |
| 73 bool get _runningOnBuildbot => | |
| 74 Platform.environment.containsKey('BUILDBOT_BUILDERNAME'); | |
| 75 | |
| 64 void onSummary(int passed, int failed, int errors, List<TestCase> results, | 76 void onSummary(int passed, int failed, int errors, List<TestCase> results, |
| 65 String uncaughtError) { | 77 String uncaughtError) { |
| 66 var success = false; | 78 var success = false; |
| 67 if (passed == 0 && failed == 0 && errors == 0 && uncaughtError == null) { | 79 if (passed == 0 && failed == 0 && errors == 0 && uncaughtError == null) { |
| 68 print('\nNo tests ran.'); | 80 print('\nNo tests ran.'); |
| 69 } else if (failed == 0 && errors == 0 && uncaughtError == null) { | 81 } else if (failed == 0 && errors == 0 && uncaughtError == null) { |
| 70 _progressLine(_start, _pass, _fail, 'All tests pass', _GREEN); | 82 _progressLine(_start, _pass, _fail, 'All tests passed!', _NONE); |
| 71 print('\nAll $passed tests passed.'); | 83 print(''); |
| 72 success = true; | 84 success = true; |
| 73 } else { | 85 } else { |
| 74 _progressLine(_start, _pass, _fail, 'Some tests fail', _RED); | 86 _progressLine(_start, _pass, _fail, 'Some tests failed.', _RED); |
| 75 print(''); | 87 print(''); |
| 76 if (uncaughtError != null) { | 88 if (uncaughtError != null) { |
| 77 print('Top-level uncaught error: $uncaughtError'); | 89 print('Top-level uncaught error: $uncaughtError'); |
| 78 } | 90 } |
| 79 print('$passed PASSED, $failed FAILED, $errors ERRORS'); | 91 print('$passed PASSED, $failed FAILED, $errors ERRORS'); |
| 80 } | 92 } |
| 81 } | 93 } |
| 82 | 94 |
| 83 int _lastLength = 0; | 95 int _lastLength = 0; |
| 84 | 96 |
| 85 final int _nonVisiblePrefix = 1 + _GREEN.length + _NONE.length; | 97 final int _nonVisiblePrefix = 1 + _GREEN.length + _NONE.length; |
| 86 | 98 |
| 87 void _progressLine(DateTime startTime, int passed, int failed, String message, | 99 void _progressLine(DateTime startTime, int passed, int failed, String message, |
| 88 [String color = _NONE]) { | 100 [String color = _NONE]) { |
| 89 var duration = (new DateTime.now()).difference(startTime); | 101 var duration = (new DateTime.now()).difference(startTime); |
| 90 var buffer = new StringBuffer(); | 102 var buffer = new StringBuffer(); |
| 91 // \r moves back to the beginning of the current line. | 103 // \r moves back to the beginning of the current line. |
| 92 buffer.write('\r${_timeString(duration)} '); | 104 buffer.write('\r${_timeString(duration)} '); |
| 93 buffer.write(_GREEN); | 105 buffer.write(_GREEN); |
| 94 buffer.write('+'); | 106 buffer.write('+'); |
| 95 buffer.write(passed); | 107 buffer.write(passed); |
| 96 buffer.write(_NONE); | 108 buffer.write(_NONE); |
| 97 if (failed != 0) buffer.write(_RED); | 109 if (failed != 0) { |
| 98 buffer.write(' -'); | 110 buffer.write(_RED); |
| 99 buffer.write(failed); | 111 buffer.write(' -'); |
| 100 if (failed != 0) buffer.write(_NONE); | 112 buffer.write(failed); |
| 113 buffer.write(_NONE); | |
| 114 } | |
| 101 buffer.write(': '); | 115 buffer.write(': '); |
| 102 buffer.write(color); | 116 buffer.write(color); |
| 103 | 117 |
| 104 // Ensure the line fits under MAX_LINE. [buffer] includes the color escape | 118 // Ensure the line fits under MAX_LINE. [buffer] includes the color escape |
| 105 // sequences too. Because these sequences are not visible characters, we | 119 // sequences too. Because these sequences are not visible characters, we |
| 106 // make sure they are not counted towards the limit. | 120 // make sure they are not counted towards the limit. |
| 107 int nonVisible = _nonVisiblePrefix + color.length + | 121 int nonVisible = _nonVisiblePrefix + color.length + |
| 108 (failed != 0 ? (_RED.length + _NONE.length) : 0); | 122 (failed != 0 ? (_RED.length + _NONE.length) : 0); |
| 109 int len = buffer.length - nonVisible; | 123 int len = buffer.length - nonVisible; |
| 110 var mx = MAX_LINE - len; | 124 var mx = MAX_LINE - len; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 var res = text.substring(text.length - maxLength + 4); | 178 var res = text.substring(text.length - maxLength + 4); |
| 165 var firstSpace = res.indexOf(' '); | 179 var firstSpace = res.indexOf(' '); |
| 166 if (firstSpace > 0) { | 180 if (firstSpace > 0) { |
| 167 res = res.substring(firstSpace); | 181 res = res.substring(firstSpace); |
| 168 } | 182 } |
| 169 return '...$res'; | 183 return '...$res'; |
| 170 } | 184 } |
| 171 } | 185 } |
| 172 | 186 |
| 173 void useCompactVMConfiguration() { | 187 void useCompactVMConfiguration() { |
| 188 // If the test is running on the Dart buildbots, we don't want to use this | |
| 189 // config since it's output may not be what the bots expect. | |
| 190 if (Platform.environment.containsKey('BUILDBOT_BUILDERNAME')) { | |
|
Siggi Cherem (dart-lang)
2013/07/11 00:26:31
if we don't set it altogether in the build bots, d
Bob Nystrom
2013/07/11 16:42:15
Oh, no. That was old stale code. Removed.
| |
| 191 return; | |
| 192 } | |
| 193 | |
| 174 unittestConfiguration = _singleton; | 194 unittestConfiguration = _singleton; |
| 175 } | 195 } |
| 176 | 196 |
| 177 final _singleton = new CompactVMConfiguration(); | 197 final _singleton = new CompactVMConfiguration(); |
| OLD | NEW |