| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 library unittest.runner.console_reporter; | 5 library unittest.runner.reporter.no_io_compact; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | |
| 9 | 8 |
| 10 import '../backend/live_test.dart'; | 9 import '../../backend/live_test.dart'; |
| 11 import '../backend/state.dart'; | 10 import '../../backend/state.dart'; |
| 12 import '../backend/suite.dart'; | 11 import '../../backend/suite.dart'; |
| 13 import '../utils.dart'; | 12 import '../../utils.dart'; |
| 14 import 'engine.dart'; | 13 import '../engine.dart'; |
| 15 | 14 |
| 16 /// The maximum console line length. | 15 /// The maximum console line length. |
| 17 /// | 16 /// |
| 18 /// Lines longer than this will be cropped. | 17 /// Lines longer than this will be cropped. |
| 19 const _lineLength = 100; | 18 const _lineLength = 100; |
| 20 | 19 |
| 21 /// A reporter that prints test results to the console in a single | 20 // TODO(nweiz): Get rid of this when issue 6943 is fixed. |
| 22 /// continuously-updating line. | 21 /// A reporter that doesn't import `dart:io`, even transitively. |
| 23 class ConsoleReporter { | 22 /// |
| 23 /// This is used in place of [CompactReporter] by `lib/unittest.dart`, which |
| 24 /// can't transitively import `dart:io` but still needs access to a runner so |
| 25 /// that test files can be run directly. |
| 26 class NoIoCompactReporter { |
| 24 /// The terminal escape for green text, or the empty string if this is Windows | 27 /// The terminal escape for green text, or the empty string if this is Windows |
| 25 /// or not outputting to a terminal. | 28 /// or not outputting to a terminal. |
| 26 final String _green; | 29 final String _green; |
| 27 | 30 |
| 28 /// The terminal escape for red text, or the empty string if this is Windows | 31 /// The terminal escape for red text, or the empty string if this is Windows |
| 29 /// or not outputting to a terminal. | 32 /// or not outputting to a terminal. |
| 30 final String _red; | 33 final String _red; |
| 31 | 34 |
| 32 /// The terminal escape for removing test coloring, or the empty string if | 35 /// The terminal escape for removing test coloring, or the empty string if |
| 33 /// this is Windows or not outputting to a terminal. | 36 /// this is Windows or not outputting to a terminal. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 50 | 53 |
| 51 /// The size of [_passed] last time a progress notification was printed. | 54 /// The size of [_passed] last time a progress notification was printed. |
| 52 int _lastProgressPassed; | 55 int _lastProgressPassed; |
| 53 | 56 |
| 54 /// The size of [_failed] last time a progress notification was printed. | 57 /// The size of [_failed] last time a progress notification was printed. |
| 55 int _lastProgressFailed; | 58 int _lastProgressFailed; |
| 56 | 59 |
| 57 /// The message printed for the last progress notification. | 60 /// The message printed for the last progress notification. |
| 58 String _lastProgressMessage; | 61 String _lastProgressMessage; |
| 59 | 62 |
| 60 /// Creates a [ConsoleReporter] that will run all tests in [suites]. | 63 /// Creates a [NoIoCompactReporter] that will run all tests in [suites]. |
| 61 /// | 64 /// |
| 62 /// If [color] is `true`, this will use terminal colors; if it's `false`, it | 65 /// If [color] is `true`, this will use terminal colors; if it's `false`, it |
| 63 /// won't. | 66 /// won't. |
| 64 ConsoleReporter(Iterable<Suite> suites, {bool color: true}) | 67 NoIoCompactReporter(Iterable<Suite> suites, {bool color: true}) |
| 65 : _multipleSuites = suites.length > 1, | 68 : _multipleSuites = suites.length > 1, |
| 66 _engine = new Engine(suites), | 69 _engine = new Engine(suites), |
| 67 _green = color ? '\u001b[32m' : '', | 70 _green = color ? '\u001b[32m' : '', |
| 68 _red = color ? '\u001b[31m' : '', | 71 _red = color ? '\u001b[31m' : '', |
| 69 _noColor = color ? '\u001b[0m' : '' { | 72 _noColor = color ? '\u001b[0m' : '' { |
| 70 _engine.onTestStarted.listen((liveTest) { | 73 _engine.onTestStarted.listen((liveTest) { |
| 71 _progressLine(_description(liveTest)); | |
| 72 liveTest.onStateChange.listen((state) { | 74 liveTest.onStateChange.listen((state) { |
| 73 if (state.status != Status.complete) return; | 75 if (state.status != Status.complete) return; |
| 74 if (state.result == Result.success) { | 76 if (state.result == Result.success) { |
| 75 _passed.add(liveTest); | 77 _passed.add(liveTest); |
| 76 } else { | 78 } else { |
| 77 _passed.remove(liveTest); | 79 _passed.remove(liveTest); |
| 78 _failed.add(liveTest); | 80 _failed.add(liveTest); |
| 79 } | 81 } |
| 80 _progressLine(_description(liveTest)); | 82 _progressLine(_description(liveTest)); |
| 81 }); | 83 }); |
| 82 | 84 |
| 83 liveTest.onError.listen((error) { | 85 liveTest.onError.listen((error) { |
| 84 if (liveTest.state.status != Status.complete) return; | 86 if (liveTest.state.status != Status.complete) return; |
| 85 | 87 |
| 86 _progressLine(_description(liveTest)); | 88 _progressLine(_description(liveTest)); |
| 87 print(''); | |
| 88 print(indent(error.error.toString())); | 89 print(indent(error.error.toString())); |
| 89 print(indent(terseChain(error.stackTrace).toString())); | 90 print(indent(terseChain(error.stackTrace).toString())); |
| 90 }); | 91 }); |
| 91 }); | 92 }); |
| 92 } | 93 } |
| 93 | 94 |
| 94 /// Runs all tests in all provided suites. | 95 /// Runs all tests in all provided suites. |
| 95 /// | 96 /// |
| 96 /// This returns `true` if all tests succeed, and `false` otherwise. It will | 97 /// This returns `true` if all tests succeed, and `false` otherwise. It will |
| 97 /// only return once all tests have finished running. | 98 /// only return once all tests have finished running. |
| 98 Future<bool> run() { | 99 Future<bool> run() { |
| 99 if (_stopwatch.isRunning) { | 100 if (_stopwatch.isRunning) { |
| 100 throw new StateError("ConsoleReporter.run() may not be called more than " | 101 throw new StateError("CompactReporter.run() may not be called more than " |
| 101 "once."); | 102 "once."); |
| 102 } | 103 } |
| 103 | 104 |
| 104 if (_engine.liveTests.isEmpty) { | 105 if (_engine.liveTests.isEmpty) { |
| 105 print("No tests ran."); | 106 print("No tests ran."); |
| 106 return new Future.value(true); | 107 return new Future.value(true); |
| 107 } | 108 } |
| 108 | 109 |
| 109 _stopwatch.start(); | 110 _stopwatch.start(); |
| 110 return _engine.run().then((success) { | 111 return _engine.run().then((success) { |
| 111 if (success) { | 112 if (success) { |
| 112 _progressLine("All tests passed!"); | 113 _progressLine("All tests passed!"); |
| 113 print(''); | |
| 114 } else { | 114 } else { |
| 115 _progressLine('Some tests failed.', color: _red); | 115 _progressLine('Some tests failed.', color: _red); |
| 116 print(''); | |
| 117 } | 116 } |
| 118 | 117 |
| 119 return success; | 118 return success; |
| 120 }); | 119 }); |
| 121 } | 120 } |
| 122 | 121 |
| 123 /// Signals that the caller is done with any test output and the reporter | 122 /// Signals that the caller is done with any test output and the reporter |
| 124 /// should release any resources it has allocated. | 123 /// should release any resources it has allocated. |
| 125 Future close() => _engine.close(); | 124 Future close() => _engine.close(); |
| 126 | 125 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 | 160 |
| 162 buffer.write(': '); | 161 buffer.write(': '); |
| 163 buffer.write(color); | 162 buffer.write(color); |
| 164 | 163 |
| 165 // Ensure the line fits within [_lineLength]. [buffer] includes the color | 164 // Ensure the line fits within [_lineLength]. [buffer] includes the color |
| 166 // escape sequences too. Because these sequences are not visible characters, | 165 // escape sequences too. Because these sequences are not visible characters, |
| 167 // we make sure they are not counted towards the limit. | 166 // we make sure they are not counted towards the limit. |
| 168 var nonVisible = 1 + _green.length + _noColor.length + color.length + | 167 var nonVisible = 1 + _green.length + _noColor.length + color.length + |
| 169 (_failed.isEmpty ? 0 : _red.length + _noColor.length); | 168 (_failed.isEmpty ? 0 : _red.length + _noColor.length); |
| 170 var length = buffer.length - nonVisible; | 169 var length = buffer.length - nonVisible; |
| 171 buffer.write(_truncate(message, _lineLength - length)); | 170 buffer.write(truncate(message, _lineLength - length)); |
| 172 buffer.write(_noColor); | 171 buffer.write(_noColor); |
| 173 | 172 |
| 174 // Pad the rest of the line so that it looks erased. | 173 print(buffer.toString()); |
| 175 length = buffer.length - nonVisible - _noColor.length; | |
| 176 buffer.write(' ' * (_lineLength - length)); | |
| 177 stdout.write(buffer.toString()); | |
| 178 } | 174 } |
| 179 | 175 |
| 180 /// Returns a representation of [duration] as `MM:SS`. | 176 /// Returns a representation of [duration] as `MM:SS`. |
| 181 String _timeString(Duration duration) { | 177 String _timeString(Duration duration) { |
| 182 return "${duration.inMinutes.toString().padLeft(2, '0')}:" | 178 return "${duration.inMinutes.toString().padLeft(2, '0')}:" |
| 183 "${(duration.inSeconds % 60).toString().padLeft(2, '0')}"; | 179 "${(duration.inSeconds % 60).toString().padLeft(2, '0')}"; |
| 184 } | 180 } |
| 185 | 181 |
| 186 /// Truncates [text] to fit within [maxLength]. | |
| 187 /// | |
| 188 /// This will try to truncate along word boundaries and preserve words both at | |
| 189 /// the beginning and the end of [text]. | |
| 190 String _truncate(String text, int maxLength) { | |
| 191 // Return the full message if it fits. | |
| 192 if (text.length <= maxLength) return text; | |
| 193 | |
| 194 // If we can fit the first and last three words, do so. | |
| 195 var words = text.split(' '); | |
| 196 if (words.length > 1) { | |
| 197 var i = words.length; | |
| 198 var length = words.first.length + 4; | |
| 199 do { | |
| 200 i--; | |
| 201 length += 1 + words[i].length; | |
| 202 } while (length <= maxLength && i > 0); | |
| 203 if (length > maxLength || i == 0) i++; | |
| 204 if (i < words.length - 4) { | |
| 205 // Require at least 3 words at the end. | |
| 206 var buffer = new StringBuffer(); | |
| 207 buffer.write(words.first); | |
| 208 buffer.write(' ...'); | |
| 209 for ( ; i < words.length; i++) { | |
| 210 buffer.write(' '); | |
| 211 buffer.write(words[i]); | |
| 212 } | |
| 213 return buffer.toString(); | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 // Otherwise truncate to return the trailing text, but attempt to start at | |
| 218 // the beginning of a word. | |
| 219 var result = text.substring(text.length - maxLength + 4); | |
| 220 var firstSpace = result.indexOf(' '); | |
| 221 if (firstSpace > 0) { | |
| 222 result = result.substring(firstSpace); | |
| 223 } | |
| 224 return '...$result'; | |
| 225 } | |
| 226 | |
| 227 /// Returns a description of [liveTest]. | 182 /// Returns a description of [liveTest]. |
| 228 /// | 183 /// |
| 229 /// This differs from the test's own description in that it may also include | 184 /// This differs from the test's own description in that it may also include |
| 230 /// the suite's name. | 185 /// the suite's name. |
| 231 String _description(LiveTest liveTest) { | 186 String _description(LiveTest liveTest) { |
| 232 if (_multipleSuites) return "${liveTest.suite.name}: ${liveTest.test.name}"; | 187 if (_multipleSuites) return "${liveTest.suite.name}: ${liveTest.test.name}"; |
| 233 return liveTest.test.name; | 188 return liveTest.test.name; |
| 234 } | 189 } |
| 235 } | 190 } |
| OLD | NEW |