| 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 test.runner.reporter.compact; | 5 library test.runner.reporter.compact; |
| 6 | 6 |
| 7 import 'dart:async'; | |
| 8 import 'dart:io'; | 7 import 'dart:io'; |
| 9 import 'dart:isolate'; | 8 import 'dart:isolate'; |
| 10 | 9 |
| 11 import '../../backend/live_test.dart'; | 10 import '../../backend/live_test.dart'; |
| 12 import '../../backend/state.dart'; | 11 import '../../backend/state.dart'; |
| 13 import '../../backend/suite.dart'; | |
| 14 import '../../utils.dart'; | 12 import '../../utils.dart'; |
| 15 import '../engine.dart'; | 13 import '../engine.dart'; |
| 16 import '../load_exception.dart'; | 14 import '../load_exception.dart'; |
| 17 | 15 |
| 18 /// The maximum console line length. | 16 /// The maximum console line length. |
| 19 /// | 17 /// |
| 20 /// Lines longer than this will be cropped. | 18 /// Lines longer than this will be cropped. |
| 21 const _lineLength = 100; | 19 const _lineLength = 100; |
| 22 | 20 |
| 23 /// A reporter that prints test results to the console in a single | 21 /// A reporter that prints test results to the console in a single |
| (...skipping 17 matching lines...) Expand all Loading... |
| 41 /// The terminal escape for removing test coloring, or the empty string if | 39 /// The terminal escape for removing test coloring, or the empty string if |
| 42 /// this is Windows or not outputting to a terminal. | 40 /// this is Windows or not outputting to a terminal. |
| 43 final String _noColor; | 41 final String _noColor; |
| 44 | 42 |
| 45 /// Whether to use verbose stack traces. | 43 /// Whether to use verbose stack traces. |
| 46 final bool _verboseTrace; | 44 final bool _verboseTrace; |
| 47 | 45 |
| 48 /// The engine used to run the tests. | 46 /// The engine used to run the tests. |
| 49 final Engine _engine; | 47 final Engine _engine; |
| 50 | 48 |
| 51 /// Whether multiple test files are being run. | 49 /// Whether the path to each test's suite should be printed. |
| 52 final bool _multiplePaths; | 50 final bool _printPath; |
| 53 | 51 |
| 54 /// Whether tests are being run on multiple platforms. | 52 /// Whether the platform each test is running on should be printed. |
| 55 final bool _multiplePlatforms; | 53 final bool _printPlatform; |
| 56 | 54 |
| 57 /// A stopwatch that tracks the duration of the full run. | 55 /// A stopwatch that tracks the duration of the full run. |
| 58 final _stopwatch = new Stopwatch(); | 56 final _stopwatch = new Stopwatch(); |
| 59 | 57 |
| 60 /// Whether [close] has been called. | |
| 61 bool _closed = false; | |
| 62 | |
| 63 /// The size of `_engine.passed` last time a progress notification was | 58 /// The size of `_engine.passed` last time a progress notification was |
| 64 /// printed. | 59 /// printed. |
| 65 int _lastProgressPassed; | 60 int _lastProgressPassed; |
| 66 | 61 |
| 67 /// The size of `_engine.skipped` last time a progress notification was printe
d. | 62 /// The size of `_engine.skipped` last time a progress notification was printe
d. |
| 68 int _lastProgressSkipped; | 63 int _lastProgressSkipped; |
| 69 | 64 |
| 70 /// The size of `_engine.failed` last time a progress notification was | 65 /// The size of `_engine.failed` last time a progress notification was |
| 71 /// printed. | 66 /// printed. |
| 72 int _lastProgressFailed; | 67 int _lastProgressFailed; |
| 73 | 68 |
| 74 /// The message printed for the last progress notification. | 69 /// The message printed for the last progress notification. |
| 75 String _lastProgressMessage; | 70 String _lastProgressMessage; |
| 76 | 71 |
| 77 // Whether a newline has been printed since the last progress line. | 72 // Whether a newline has been printed since the last progress line. |
| 78 var _printedNewline = true; | 73 var _printedNewline = true; |
| 79 | 74 |
| 80 /// Creates a [ConsoleReporter] that will run all tests in [suites]. | 75 /// Watches the tests run by [engine] and prints their results to the |
| 76 /// terminal. |
| 81 /// | 77 /// |
| 82 /// [concurrency] controls how many suites are run at once. If [color] is | 78 /// If [color] is `true`, this will use terminal colors; if it's `false`, it |
| 83 /// `true`, this will use terminal colors; if it's `false`, it won't. If | 79 /// won't. If [verboseTrace] is `true`, this will print core library frames. |
| 84 /// [verboseTrace] is `true`, this will print core library frames. | 80 /// If [printPath] is `true`, this will print the path name as part of the |
| 85 CompactReporter(Iterable<Suite> suites, {int concurrency, bool color: true, | 81 /// test description. Likewise, if [printPlatform] is `true`, this will print |
| 86 bool verboseTrace: false}) | 82 /// the platform as part of the test description. |
| 87 : _multiplePaths = suites.map((suite) => suite.path).toSet().length > 1, | 83 static void watch(Engine engine, {bool color: true, bool verboseTrace: false, |
| 88 _multiplePlatforms = | 84 bool printPath: true, bool printPlatform: true}) { |
| 89 suites.map((suite) => suite.platform).toSet().length > 1, | 85 new CompactReporter._( |
| 90 _engine = new Engine(suites, concurrency: concurrency), | 86 engine, |
| 91 _verboseTrace = verboseTrace, | 87 color: color, |
| 88 verboseTrace: verboseTrace, |
| 89 printPath: printPath, |
| 90 printPlatform: printPlatform); |
| 91 } |
| 92 |
| 93 CompactReporter._(this._engine, {bool color: true, bool verboseTrace: false, |
| 94 bool printPath: true, bool printPlatform: true}) |
| 95 : _verboseTrace = verboseTrace, |
| 96 _printPath = printPath, |
| 97 _printPlatform = printPlatform, |
| 92 _color = color, | 98 _color = color, |
| 93 _green = color ? '\u001b[32m' : '', | 99 _green = color ? '\u001b[32m' : '', |
| 94 _red = color ? '\u001b[31m' : '', | 100 _red = color ? '\u001b[31m' : '', |
| 95 _yellow = color ? '\u001b[33m' : '', | 101 _yellow = color ? '\u001b[33m' : '', |
| 96 _noColor = color ? '\u001b[0m' : '' { | 102 _noColor = color ? '\u001b[0m' : '' { |
| 97 _engine.onTestStarted.listen((liveTest) { | 103 _engine.onTestStarted.listen(_onTestStarted); |
| 98 // If this is the first test to start, print a progress line so the user | 104 _engine.success.then(_onDone); |
| 99 // knows what's running. | 105 } |
| 100 if (_engine.active.length == 1) _progressLine(_description(liveTest)); | |
| 101 _printedNewline = false; | |
| 102 | 106 |
| 103 liveTest.onStateChange.listen((state) { | 107 /// A callback called when the engine begins running [liveTest]. |
| 104 if (state.status != Status.complete) return; | 108 void _onTestStarted(LiveTest liveTest) { |
| 109 if (!_stopwatch.isRunning) _stopwatch.start(); |
| 105 | 110 |
| 106 if (liveTest.test.metadata.skip && | 111 // If this is the first test to start, print a progress line so the user |
| 107 liveTest.test.metadata.skipReason != null) { | 112 // knows what's running. |
| 108 _progressLine(_description(liveTest)); | 113 if (_engine.active.length == 1) _progressLine(_description(liveTest)); |
| 109 print(''); | 114 _printedNewline = false; |
| 110 print(indent('${_yellow}Skip: ${liveTest.test.metadata.skipReason}' | |
| 111 '$_noColor')); | |
| 112 } else { | |
| 113 // Always display the name of the oldest active test, unless testing | |
| 114 // is finished in which case display the last test to complete. | |
| 115 if (_engine.active.isEmpty) { | |
| 116 _progressLine(_description(liveTest)); | |
| 117 } else { | |
| 118 _progressLine(_description(_engine.active.first)); | |
| 119 } | |
| 120 | 115 |
| 121 _printedNewline = false; | 116 liveTest.onStateChange.listen((state) => _onStateChange(liveTest, state)); |
| 122 } | |
| 123 }); | |
| 124 | 117 |
| 125 liveTest.onError.listen((error) { | 118 liveTest.onError.listen((error) => |
| 126 if (liveTest.state.status != Status.complete) return; | 119 _onError(liveTest, error.error, error.stackTrace)); |
| 127 | 120 |
| 128 _progressLine(_description(liveTest)); | 121 liveTest.onPrint.listen((line) { |
| 129 if (!_printedNewline) print(''); | 122 _progressLine(_description(liveTest)); |
| 130 _printedNewline = true; | 123 if (!_printedNewline) print(''); |
| 124 _printedNewline = true; |
| 131 | 125 |
| 132 if (error.error is! LoadException) { | 126 print(line); |
| 133 print(indent(error.error.toString())); | |
| 134 var chain = terseChain(error.stackTrace, verbose: _verboseTrace); | |
| 135 print(indent(chain.toString())); | |
| 136 return; | |
| 137 } | |
| 138 | |
| 139 print(indent(error.error.toString(color: _color))); | |
| 140 | |
| 141 // Only print stack traces for load errors that come from the user's cod
e. | |
| 142 if (error.error.innerError is! IOException && | |
| 143 error.error.innerError is! IsolateSpawnException && | |
| 144 error.error.innerError is! FormatException && | |
| 145 error.error.innerError is! String) { | |
| 146 print(indent(terseChain(error.stackTrace).toString())); | |
| 147 } | |
| 148 }); | |
| 149 | |
| 150 liveTest.onPrint.listen((line) { | |
| 151 _progressLine(_description(liveTest)); | |
| 152 if (!_printedNewline) print(''); | |
| 153 _printedNewline = true; | |
| 154 | |
| 155 print(line); | |
| 156 }); | |
| 157 }); | 127 }); |
| 158 } | 128 } |
| 159 | 129 |
| 160 /// Runs all tests in all provided suites. | 130 /// A callback called when [liveTest]'s state becomes [state]. |
| 131 void _onStateChange(LiveTest liveTest, State state) { |
| 132 if (state.status != Status.complete) return; |
| 133 |
| 134 if (liveTest.test.metadata.skip && |
| 135 liveTest.test.metadata.skipReason != null) { |
| 136 _progressLine(_description(liveTest)); |
| 137 print(''); |
| 138 print(indent('${_yellow}Skip: ${liveTest.test.metadata.skipReason}' |
| 139 '$_noColor')); |
| 140 } else { |
| 141 // Always display the name of the oldest active test, unless testing |
| 142 // is finished in which case display the last test to complete. |
| 143 if (_engine.active.isEmpty) { |
| 144 _progressLine(_description(liveTest)); |
| 145 } else { |
| 146 _progressLine(_description(_engine.active.first)); |
| 147 } |
| 148 |
| 149 _printedNewline = false; |
| 150 } |
| 151 } |
| 152 |
| 153 /// A callback called when [liveTest] throws [error]. |
| 154 void _onError(LiveTest liveTest, error, StackTrace stackTrace) { |
| 155 if (liveTest.state.status != Status.complete) return; |
| 156 |
| 157 _progressLine(_description(liveTest)); |
| 158 if (!_printedNewline) print(''); |
| 159 _printedNewline = true; |
| 160 |
| 161 if (error is! LoadException) { |
| 162 print(indent(error.toString())); |
| 163 var chain = terseChain(stackTrace, verbose: _verboseTrace); |
| 164 print(indent(chain.toString())); |
| 165 return; |
| 166 } |
| 167 |
| 168 print(indent(error.toString(color: _color))); |
| 169 |
| 170 // Only print stack traces for load errors that come from the user's code. |
| 171 if (error.innerError is! IOException && |
| 172 error.innerError is! IsolateSpawnException && |
| 173 error.innerError is! FormatException && |
| 174 error.innerError is! String) { |
| 175 print(indent(terseChain(stackTrace).toString())); |
| 176 } |
| 177 } |
| 178 |
| 179 /// A callback called when the engine is finished running tests. |
| 161 /// | 180 /// |
| 162 /// This returns `true` if all tests succeed, and `false` otherwise. It will | 181 /// [success] will be `true` if all tests passed, `false` if some tests |
| 163 /// only return once all tests have finished running. | 182 /// failed, and `null` if the engine was closed prematurely. |
| 164 Future<bool> run() async { | 183 void _onDone(bool success) { |
| 165 if (_stopwatch.isRunning) { | 184 // A null success value indicates that the engine was closed before the |
| 166 throw new StateError("CompactReporter.run() may not be called more than " | 185 // tests finished running, probably because of a signal from the user. We |
| 167 "once."); | 186 // shouldn't print summary information, we should just make sure the |
| 187 // terminal cursor is on its own line. |
| 188 if (success == null) { |
| 189 if (!_printedNewline) print(""); |
| 190 _printedNewline = true; |
| 191 return; |
| 168 } | 192 } |
| 169 | 193 |
| 170 if (_engine.liveTests.isEmpty) { | 194 if (_engine.liveTests.isEmpty) { |
| 195 if (!_printedNewline) print(""); |
| 171 print("No tests ran."); | 196 print("No tests ran."); |
| 172 return true; | 197 } else if (!success) { |
| 173 } | |
| 174 | |
| 175 _stopwatch.start(); | |
| 176 var success = await _engine.run(); | |
| 177 if (_closed) return false; | |
| 178 | |
| 179 if (!success) { | |
| 180 _progressLine('Some tests failed.', color: _red); | 198 _progressLine('Some tests failed.', color: _red); |
| 181 print(''); | 199 print(''); |
| 182 } else if (_engine.passed.isEmpty) { | 200 } else if (_engine.passed.isEmpty) { |
| 183 _progressLine("All tests skipped."); | 201 _progressLine("All tests skipped."); |
| 184 print(''); | 202 print(''); |
| 185 } else { | 203 } else { |
| 186 _progressLine("All tests passed!"); | 204 _progressLine("All tests passed!"); |
| 187 print(''); | 205 print(''); |
| 188 } | 206 } |
| 189 | |
| 190 return success; | |
| 191 } | |
| 192 | |
| 193 /// Signals that the caller is done with any test output and the reporter | |
| 194 /// should release any resources it has allocated. | |
| 195 Future close() { | |
| 196 if (!_printedNewline) print(""); | |
| 197 _printedNewline = true; | |
| 198 _closed = true; | |
| 199 return _engine.close(); | |
| 200 } | 207 } |
| 201 | 208 |
| 202 /// Prints a line representing the current state of the tests. | 209 /// Prints a line representing the current state of the tests. |
| 203 /// | 210 /// |
| 204 /// [message] goes after the progress report, and may be truncated to fit the | 211 /// [message] goes after the progress report, and may be truncated to fit the |
| 205 /// entire line within [_lineLength]. If [color] is passed, it's used as the | 212 /// entire line within [_lineLength]. If [color] is passed, it's used as the |
| 206 /// color for [message]. | 213 /// color for [message]. |
| 207 bool _progressLine(String message, {String color}) { | 214 bool _progressLine(String message, {String color}) { |
| 208 // Print nothing if nothing has changed since the last progress line. | 215 // Print nothing if nothing has changed since the last progress line. |
| 209 if (_engine.passed.length == _lastProgressPassed && | 216 if (_engine.passed.length == _lastProgressPassed && |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 "${(duration.inSeconds % 60).toString().padLeft(2, '0')}"; | 275 "${(duration.inSeconds % 60).toString().padLeft(2, '0')}"; |
| 269 } | 276 } |
| 270 | 277 |
| 271 /// Returns a description of [liveTest]. | 278 /// Returns a description of [liveTest]. |
| 272 /// | 279 /// |
| 273 /// This differs from the test's own description in that it may also include | 280 /// This differs from the test's own description in that it may also include |
| 274 /// the suite's name. | 281 /// the suite's name. |
| 275 String _description(LiveTest liveTest) { | 282 String _description(LiveTest liveTest) { |
| 276 var name = liveTest.test.name; | 283 var name = liveTest.test.name; |
| 277 | 284 |
| 278 if (_multiplePaths && liveTest.suite.path != null) { | 285 if (_printPath && liveTest.suite.path != null) { |
| 279 name = "${liveTest.suite.path}: $name"; | 286 name = "${liveTest.suite.path}: $name"; |
| 280 } | 287 } |
| 281 | 288 |
| 282 if (_multiplePlatforms && liveTest.suite.platform != null) { | 289 if (_printPlatform && liveTest.suite.platform != null) { |
| 283 name = "[${liveTest.suite.platform}] $name"; | 290 name = "[${liveTest.suite.platform}] $name"; |
| 284 } | 291 } |
| 285 | 292 |
| 286 return name; | 293 return name; |
| 287 } | 294 } |
| 288 } | 295 } |
| OLD | NEW |