| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library test.runner.reporter.no_io_compact; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import '../../backend/live_test.dart'; | |
| 10 import '../../backend/state.dart'; | |
| 11 import '../../backend/suite.dart'; | |
| 12 import '../../utils.dart'; | |
| 13 import '../engine.dart'; | |
| 14 | |
| 15 /// The maximum console line length. | |
| 16 /// | |
| 17 /// Lines longer than this will be cropped. | |
| 18 const _lineLength = 100; | |
| 19 | |
| 20 // TODO(nweiz): Get rid of this when issue 6943 is fixed. | |
| 21 /// A reporter that doesn't import `dart:io`, even transitively. | |
| 22 /// | |
| 23 /// This is used in place of [CompactReporter] by `lib/test.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 { | |
| 27 /// The terminal escape for green text, or the empty string if this is Windows | |
| 28 /// or not outputting to a terminal. | |
| 29 final String _green; | |
| 30 | |
| 31 /// The terminal escape for red text, or the empty string if this is Windows | |
| 32 /// or not outputting to a terminal. | |
| 33 final String _red; | |
| 34 | |
| 35 /// The terminal escape for yellow text, or the empty string if this is | |
| 36 /// Windows or not outputting to a terminal. | |
| 37 final String _yellow; | |
| 38 | |
| 39 /// The terminal escape for removing test coloring, or the empty string if | |
| 40 /// this is Windows or not outputting to a terminal. | |
| 41 final String _noColor; | |
| 42 | |
| 43 /// The engine used to run the tests. | |
| 44 final Engine _engine; | |
| 45 | |
| 46 /// Whether multiple test files are being run. | |
| 47 final bool _multiplePaths; | |
| 48 | |
| 49 /// Whether tests are being run on multiple platforms. | |
| 50 final bool _multiplePlatforms; | |
| 51 | |
| 52 /// A stopwatch that tracks the duration of the full run. | |
| 53 final _stopwatch = new Stopwatch(); | |
| 54 | |
| 55 /// The set of tests that have completed and been marked as passing. | |
| 56 final _passed = new Set<LiveTest>(); | |
| 57 | |
| 58 /// The set of tests that have completed and been marked as skipped. | |
| 59 final _skipped = new Set<LiveTest>(); | |
| 60 | |
| 61 /// The set of tests that have completed and been marked as failing or error. | |
| 62 final _failed = new Set<LiveTest>(); | |
| 63 | |
| 64 /// The size of [_passed] last time a progress notification was printed. | |
| 65 int _lastProgressPassed; | |
| 66 | |
| 67 /// The size of [_skipped] last time a progress notification was printed. | |
| 68 int _lastProgressSkipped; | |
| 69 | |
| 70 /// The size of [_failed] last time a progress notification was printed. | |
| 71 int _lastProgressFailed; | |
| 72 | |
| 73 /// The message printed for the last progress notification. | |
| 74 String _lastProgressMessage; | |
| 75 | |
| 76 /// Creates a [NoIoCompactReporter] that will run all tests in [suites]. | |
| 77 /// | |
| 78 /// If [color] is `true`, this will use terminal colors; if it's `false`, it | |
| 79 /// won't. | |
| 80 NoIoCompactReporter(Iterable<Suite> suites, {bool color: true}) | |
| 81 : _multiplePaths = suites.map((suite) => suite.path).toSet().length > 1, | |
| 82 _multiplePlatforms = | |
| 83 suites.map((suite) => suite.platform).toSet().length > 1, | |
| 84 _engine = new Engine(suites), | |
| 85 _green = color ? '\u001b[32m' : '', | |
| 86 _red = color ? '\u001b[31m' : '', | |
| 87 _yellow = color ? '\u001b[33m' : '', | |
| 88 _noColor = color ? '\u001b[0m' : '' { | |
| 89 _engine.onTestStarted.listen((liveTest) { | |
| 90 liveTest.onStateChange.listen((state) { | |
| 91 if (state.status != Status.complete) return; | |
| 92 | |
| 93 if (state.result != Result.success) { | |
| 94 _passed.remove(liveTest); | |
| 95 _failed.add(liveTest); | |
| 96 } else if (liveTest.test.metadata.skip) { | |
| 97 _skipped.add(liveTest); | |
| 98 } else { | |
| 99 _passed.add(liveTest); | |
| 100 } | |
| 101 | |
| 102 _progressLine(_description(liveTest)); | |
| 103 | |
| 104 if (liveTest.test.metadata.skip && | |
| 105 liveTest.test.metadata.skipReason != null) { | |
| 106 print(indent('${_yellow}Skip: ${liveTest.test.metadata.skipReason}' | |
| 107 '$_noColor')); | |
| 108 } | |
| 109 }); | |
| 110 | |
| 111 liveTest.onError.listen((error) { | |
| 112 if (liveTest.state.status != Status.complete) return; | |
| 113 | |
| 114 _progressLine(_description(liveTest)); | |
| 115 print(indent(error.error.toString())); | |
| 116 print(indent(terseChain(error.stackTrace).toString())); | |
| 117 }); | |
| 118 | |
| 119 liveTest.onPrint.listen((line) { | |
| 120 _progressLine(_description(liveTest)); | |
| 121 print(line); | |
| 122 }); | |
| 123 }); | |
| 124 } | |
| 125 | |
| 126 /// Runs all tests in all provided suites. | |
| 127 /// | |
| 128 /// This returns `true` if all tests succeed, and `false` otherwise. It will | |
| 129 /// only return once all tests have finished running. | |
| 130 Future<bool> run() { | |
| 131 if (_stopwatch.isRunning) { | |
| 132 throw new StateError("CompactReporter.run() may not be called more than " | |
| 133 "once."); | |
| 134 } | |
| 135 | |
| 136 if (_engine.liveTests.isEmpty) { | |
| 137 print("No tests ran."); | |
| 138 return new Future.value(true); | |
| 139 } | |
| 140 | |
| 141 _stopwatch.start(); | |
| 142 return _engine.run().then((success) { | |
| 143 if (!success) { | |
| 144 _progressLine('Some tests failed.', color: _red); | |
| 145 } else if (_passed.isEmpty) { | |
| 146 _progressLine("All tests skipped."); | |
| 147 } else { | |
| 148 _progressLine("All tests passed!"); | |
| 149 } | |
| 150 | |
| 151 return success; | |
| 152 }); | |
| 153 } | |
| 154 | |
| 155 /// Signals that the caller is done with any test output and the reporter | |
| 156 /// should release any resources it has allocated. | |
| 157 Future close() => _engine.close(); | |
| 158 | |
| 159 /// Prints a line representing the current state of the tests. | |
| 160 /// | |
| 161 /// [message] goes after the progress report, and may be truncated to fit the | |
| 162 /// entire line within [_lineLength]. If [color] is passed, it's used as the | |
| 163 /// color for [message]. | |
| 164 void _progressLine(String message, {String color}) { | |
| 165 // Print nothing if nothing has changed since the last progress line. | |
| 166 if (_passed.length == _lastProgressPassed && | |
| 167 _skipped.length == _lastProgressSkipped && | |
| 168 _failed.length == _lastProgressFailed && | |
| 169 message == _lastProgressMessage) { | |
| 170 return; | |
| 171 } | |
| 172 | |
| 173 _lastProgressPassed = _passed.length; | |
| 174 _lastProgressSkipped = _skipped.length; | |
| 175 _lastProgressFailed = _failed.length; | |
| 176 _lastProgressMessage = message; | |
| 177 | |
| 178 if (color == null) color = ''; | |
| 179 var duration = _stopwatch.elapsed; | |
| 180 var buffer = new StringBuffer(); | |
| 181 | |
| 182 // \r moves back to the beginning of the current line. | |
| 183 buffer.write('\r${_timeString(duration)} '); | |
| 184 buffer.write(_green); | |
| 185 buffer.write('+'); | |
| 186 buffer.write(_passed.length); | |
| 187 buffer.write(_noColor); | |
| 188 | |
| 189 if (_skipped.isNotEmpty) { | |
| 190 buffer.write(_yellow); | |
| 191 buffer.write(' ~'); | |
| 192 buffer.write(_skipped.length); | |
| 193 buffer.write(_noColor); | |
| 194 } | |
| 195 | |
| 196 if (_failed.isNotEmpty) { | |
| 197 buffer.write(_red); | |
| 198 buffer.write(' -'); | |
| 199 buffer.write(_failed.length); | |
| 200 buffer.write(_noColor); | |
| 201 } | |
| 202 | |
| 203 buffer.write(': '); | |
| 204 buffer.write(color); | |
| 205 | |
| 206 // Ensure the line fits within [_lineLength]. [buffer] includes the color | |
| 207 // escape sequences too. Because these sequences are not visible characters, | |
| 208 // we make sure they are not counted towards the limit. | |
| 209 var nonVisible = 1 + _green.length + _noColor.length + color.length + | |
| 210 (_failed.isEmpty ? 0 : _red.length + _noColor.length); | |
| 211 var length = buffer.length - nonVisible; | |
| 212 buffer.write(truncate(message, _lineLength - length)); | |
| 213 buffer.write(_noColor); | |
| 214 | |
| 215 print(buffer.toString()); | |
| 216 } | |
| 217 | |
| 218 /// Returns a representation of [duration] as `MM:SS`. | |
| 219 String _timeString(Duration duration) { | |
| 220 return "${duration.inMinutes.toString().padLeft(2, '0')}:" | |
| 221 "${(duration.inSeconds % 60).toString().padLeft(2, '0')}"; | |
| 222 } | |
| 223 | |
| 224 /// Returns a description of [liveTest]. | |
| 225 /// | |
| 226 /// This differs from the test's own description in that it may also include | |
| 227 /// the suite's name. | |
| 228 String _description(LiveTest liveTest) { | |
| 229 var name = liveTest.test.name; | |
| 230 | |
| 231 if (_multiplePaths && liveTest.suite.path != null) { | |
| 232 name = "${liveTest.suite.path}: $name"; | |
| 233 } | |
| 234 | |
| 235 if (_multiplePlatforms && liveTest.suite.platform != null) { | |
| 236 name = "[$liveTest.suite.platform] $name"; | |
| 237 } | |
| 238 | |
| 239 return name; | |
| 240 } | |
| 241 } | |
| OLD | NEW |