Index: lib/src/runner/reporter/no_io_compact.dart |
diff --git a/lib/src/runner/console_reporter.dart b/lib/src/runner/reporter/no_io_compact.dart |
similarity index 71% |
rename from lib/src/runner/console_reporter.dart |
rename to lib/src/runner/reporter/no_io_compact.dart |
index 4680146b2dbe922d058740e25d5f603422f13703..a947730eb8c50fc4c32cd7d2f4d956b4fad9150a 100644 |
--- a/lib/src/runner/console_reporter.dart |
+++ b/lib/src/runner/reporter/no_io_compact.dart |
@@ -2,25 +2,28 @@ |
// for details. All rights reserved. Use of this source code is governed by a |
// BSD-style license that can be found in the LICENSE file. |
-library unittest.runner.console_reporter; |
+library unittest.runner.reporter.no_io_compact; |
import 'dart:async'; |
-import 'dart:io'; |
-import '../backend/live_test.dart'; |
-import '../backend/state.dart'; |
-import '../backend/suite.dart'; |
-import '../utils.dart'; |
-import 'engine.dart'; |
+import '../../backend/live_test.dart'; |
+import '../../backend/state.dart'; |
+import '../../backend/suite.dart'; |
+import '../../utils.dart'; |
+import '../engine.dart'; |
/// The maximum console line length. |
/// |
/// Lines longer than this will be cropped. |
const _lineLength = 100; |
-/// A reporter that prints test results to the console in a single |
-/// continuously-updating line. |
-class ConsoleReporter { |
+// TODO(nweiz): Get rid of this when issue 6943 is fixed. |
+/// A reporter that doesn't import `dart:io`, even transitively. |
+/// |
+/// This is used in place of [CompactReporter] by `lib/unittest.dart`, which |
+/// can't transitively import `dart:io` but still needs access to a runner so |
+/// that test files can be run directly. |
+class NoIoCompactReporter { |
/// The terminal escape for green text, or the empty string if this is Windows |
/// or not outputting to a terminal. |
final String _green; |
@@ -57,18 +60,17 @@ class ConsoleReporter { |
/// The message printed for the last progress notification. |
String _lastProgressMessage; |
- /// Creates a [ConsoleReporter] that will run all tests in [suites]. |
+ /// Creates a [NoIoCompactReporter] that will run all tests in [suites]. |
/// |
/// If [color] is `true`, this will use terminal colors; if it's `false`, it |
/// won't. |
- ConsoleReporter(Iterable<Suite> suites, {bool color: true}) |
+ NoIoCompactReporter(Iterable<Suite> suites, {bool color: true}) |
: _multipleSuites = suites.length > 1, |
_engine = new Engine(suites), |
_green = color ? '\u001b[32m' : '', |
_red = color ? '\u001b[31m' : '', |
_noColor = color ? '\u001b[0m' : '' { |
_engine.onTestStarted.listen((liveTest) { |
- _progressLine(_description(liveTest)); |
liveTest.onStateChange.listen((state) { |
if (state.status != Status.complete) return; |
if (state.result == Result.success) { |
@@ -84,7 +86,6 @@ class ConsoleReporter { |
if (liveTest.state.status != Status.complete) return; |
_progressLine(_description(liveTest)); |
- print(''); |
print(indent(error.error.toString())); |
print(indent(terseChain(error.stackTrace).toString())); |
}); |
@@ -97,7 +98,7 @@ class ConsoleReporter { |
/// only return once all tests have finished running. |
Future<bool> run() { |
if (_stopwatch.isRunning) { |
- throw new StateError("ConsoleReporter.run() may not be called more than " |
+ throw new StateError("CompactReporter.run() may not be called more than " |
"once."); |
} |
@@ -110,10 +111,8 @@ class ConsoleReporter { |
return _engine.run().then((success) { |
if (success) { |
_progressLine("All tests passed!"); |
- print(''); |
} else { |
_progressLine('Some tests failed.', color: _red); |
- print(''); |
} |
return success; |
@@ -168,13 +167,10 @@ class ConsoleReporter { |
var nonVisible = 1 + _green.length + _noColor.length + color.length + |
(_failed.isEmpty ? 0 : _red.length + _noColor.length); |
var length = buffer.length - nonVisible; |
- buffer.write(_truncate(message, _lineLength - length)); |
+ buffer.write(truncate(message, _lineLength - length)); |
buffer.write(_noColor); |
- // Pad the rest of the line so that it looks erased. |
- length = buffer.length - nonVisible - _noColor.length; |
- buffer.write(' ' * (_lineLength - length)); |
- stdout.write(buffer.toString()); |
+ print(buffer.toString()); |
} |
/// Returns a representation of [duration] as `MM:SS`. |
@@ -183,47 +179,6 @@ class ConsoleReporter { |
"${(duration.inSeconds % 60).toString().padLeft(2, '0')}"; |
} |
- /// Truncates [text] to fit within [maxLength]. |
- /// |
- /// This will try to truncate along word boundaries and preserve words both at |
- /// the beginning and the end of [text]. |
- String _truncate(String text, int maxLength) { |
- // Return the full message if it fits. |
- if (text.length <= maxLength) return text; |
- |
- // If we can fit the first and last three words, do so. |
- var words = text.split(' '); |
- if (words.length > 1) { |
- var i = words.length; |
- var length = words.first.length + 4; |
- do { |
- i--; |
- length += 1 + words[i].length; |
- } while (length <= maxLength && i > 0); |
- if (length > maxLength || i == 0) i++; |
- if (i < words.length - 4) { |
- // Require at least 3 words at the end. |
- var buffer = new StringBuffer(); |
- buffer.write(words.first); |
- buffer.write(' ...'); |
- for ( ; i < words.length; i++) { |
- buffer.write(' '); |
- buffer.write(words[i]); |
- } |
- return buffer.toString(); |
- } |
- } |
- |
- // Otherwise truncate to return the trailing text, but attempt to start at |
- // the beginning of a word. |
- var result = text.substring(text.length - maxLength + 4); |
- var firstSpace = result.indexOf(' '); |
- if (firstSpace > 0) { |
- result = result.substring(firstSpace); |
- } |
- return '...$result'; |
- } |
- |
/// Returns a description of [liveTest]. |
/// |
/// This differs from the test's own description in that it may also include |