Skip to content

Commit

Permalink
Use Stdout.terminalColumns for line length (flutter#716)
Browse files Browse the repository at this point in the history
Use Stdout.terminalColumns for line length

Fall back on a 200-character default, which is less likely to obscure
test names when printing to a non-terminal destination.

Closes flutter#86
  • Loading branch information
nex3 authored Nov 2, 2017
1 parent 5652c6d commit 28dfb44
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 81 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.12.27

* When running in an interactive terminal, the test runner now prints status
lines as wide as the terminal and no wider.

## 0.12.26+1

* Fix lower bound on package `stack_trace`. Now 1.6.0.
Expand Down
16 changes: 6 additions & 10 deletions lib/src/runner/reporter/compact.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ import '../../backend/message.dart';
import '../../backend/state.dart';
import '../../utils.dart';
import '../../utils.dart' as utils;
import '../../util/io.dart';
import '../configuration.dart';
import '../engine.dart';
import '../load_exception.dart';
import '../load_suite.dart';
import '../reporter.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 CompactReporter implements Reporter {
Expand Down Expand Up @@ -251,7 +247,7 @@ class CompactReporter implements Reporter {
stdout.write(message);

// Add extra padding to overwrite any load messages.
if (!_printedNewline) stdout.write(" " * (_lineLength - message.length));
if (!_printedNewline) stdout.write(" " * (lineLength - message.length));
stdout.writeln();
} else if (!success) {
_progressLine('Some tests failed.', color: _red);
Expand All @@ -268,7 +264,7 @@ class CompactReporter implements Reporter {
/// Prints a line representing the current state of the tests.
///
/// [message] goes after the progress report, and may be truncated to fit the
/// entire line within [_lineLength]. If [color] is passed, it's used as the
/// entire line within [lineLength]. If [color] is passed, it's used as the
/// color for [message]. If [suffix] is passed, it's added to the end of
/// [message].
bool _progressLine(String message,
Expand Down Expand Up @@ -329,16 +325,16 @@ class CompactReporter implements Reporter {
buffer.write(': ');
buffer.write(color);

// Ensure the line fits within [_lineLength]. [buffer] includes the color
// Ensure the line fits within [lineLength]. [buffer] includes the color
// escape sequences too. Because these sequences are not visible characters,
// we make sure they are not counted towards the limit.
var length = withoutColors(buffer.toString()).length;
if (truncate) message = utils.truncate(message, _lineLength - length);
if (truncate) message = utils.truncate(message, lineLength - length);
buffer.write(message);
buffer.write(_noColor);

// Pad the rest of the line so that it looks erased.
buffer.write(' ' * (_lineLength - withoutColors(buffer.toString()).length));
buffer.write(' ' * (lineLength - withoutColors(buffer.toString()).length));
stdout.write(buffer.toString());

_printedNewline = false;
Expand Down
12 changes: 3 additions & 9 deletions lib/src/runner/reporter/expanded.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import '../load_exception.dart';
import '../load_suite.dart';
import '../reporter.dart';

/// The maximum console line length.
///
/// Lines longer than this will be cropped.
const _lineLength = 100;

/// A reporter that prints each test on its own line.
///
/// This is currently used in place of [CompactReporter] by `lib/test.dart`,
Expand Down Expand Up @@ -238,10 +233,9 @@ class ExpandedReporter implements Reporter {

/// Prints a line representing the current state of the tests.
///
/// [message] goes after the progress report, and may be truncated to fit the
/// entire line within [_lineLength]. If [color] is passed, it's used as the
/// color for [message]. If [suffix] is passed, it's added to the end of
/// [message].
/// [message] goes after the progress report. If [color] is passed, it's used
/// as the color for [message]. If [suffix] is passed, it's added to the end
/// of [message].
void _progressLine(String message, {String color, String suffix}) {
// Print nothing if nothing has changed since the last progress line.
if (_engine.passed.length == _lastProgressPassed &&
Expand Down
47 changes: 47 additions & 0 deletions lib/src/util/io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ const _newline = 0xA;
/// The ASCII code for a carriage return character.
const _carriageReturn = 0xD;

/// The default line length for output when there isn't a terminal attached to
/// stdout.
const _defaultLineLength = 200;

/// The maximum line length for output.
final int lineLength = () {
try {
return stdout.terminalColumns;
} on UnsupportedError {
// This can throw an [UnsupportedError] if we're running in a JS context
// where `dart:io` is unavaiable.
return _defaultLineLength;
} on StdoutException {
return _defaultLineLength;
}
}();

/// The root directory of the Dart SDK.
final String sdkDir = p.dirname(p.dirname(Platform.resolvedExecutable));

Expand Down Expand Up @@ -103,6 +120,36 @@ Stream<List<int>> sanitizeForWindows(Stream<List<int>> input) {
});
}

/// Wraps [text] so that it fits within [lineLength].
///
/// This preserves existing newlines and doesn't consider terminal color escapes
/// part of a word's length. It only splits words on spaces, not on other sorts
/// of whitespace.
String wordWrap(String text) {
return text.split("\n").map((originalLine) {
var buffer = new StringBuffer();
var lengthSoFar = 0;
for (var word in originalLine.split(" ")) {
var wordLength = withoutColors(word).length;
if (wordLength > lineLength) {
if (lengthSoFar != 0) buffer.writeln();
buffer.writeln(word);
} else if (lengthSoFar == 0) {
buffer.write(word);
lengthSoFar = wordLength;
} else if (lengthSoFar + 1 + wordLength > lineLength) {
buffer.writeln();
buffer.write(word);
lengthSoFar = wordLength;
} else {
buffer.write(" $word");
lengthSoFar += 1 + wordLength;
}
}
return buffer.toString();
}).join("\n");
}

/// Print a warning containing [message].
///
/// This automatically wraps lines if they get too long. If [color] is passed,
Expand Down
34 changes: 0 additions & 34 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import 'package:term_glyph/term_glyph.dart' as glyph;
import 'backend/invoker.dart';
import 'backend/operating_system.dart';

/// The maximum console line length.
const _lineLength = 100;

/// A typedef for a possibly-asynchronous function.
///
/// The return type should only ever by [Future] or void.
Expand Down Expand Up @@ -140,37 +137,6 @@ String pluralize(String name, int number, {String plural}) {
/// whether its first letter is a vowel.
String a(String noun) => noun.startsWith(_vowel) ? "an $noun" : "a $noun";

/// Wraps [text] so that it fits within [lineLength], which defaults to 100
/// characters.
///
/// This preserves existing newlines and doesn't consider terminal color escapes
/// part of a word's length.
String wordWrap(String text, {int lineLength}) {
if (lineLength == null) lineLength = _lineLength;
return text.split("\n").map((originalLine) {
var buffer = new StringBuffer();
var lengthSoFar = 0;
for (var word in originalLine.split(" ")) {
var wordLength = withoutColors(word).length;
if (wordLength > lineLength) {
if (lengthSoFar != 0) buffer.writeln();
buffer.writeln(word);
} else if (lengthSoFar == 0) {
buffer.write(word);
lengthSoFar = wordLength;
} else if (lengthSoFar + 1 + wordLength > lineLength) {
buffer.writeln();
buffer.write(word);
lengthSoFar = wordLength;
} else {
buffer.write(" $word");
lengthSoFar += 1 + wordLength;
}
}
return buffer.toString();
}).join("\n");
}

/// A regular expression matching terminal color codes.
final _colorCode = new RegExp('\u001b\\[[0-9;]+m');

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: test
version: 0.12.26+1
version: 0.12.27-dev
author: Dart Team <[email protected]>
description: A library for writing dart unit tests.
homepage: https://github.com/dart-lang/test
Expand Down
20 changes: 12 additions & 8 deletions test/runner/compact_reporter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,15 @@ void main() {
return _expectReport("""
test(
'really gosh dang long test name. Even longer than that. No, yet '
'longer. A little more... okay, that should do it.',
'longer. Even more. We have to get to at least 200 characters. '
'I know that seems like a lot, but I believe in you. A little '
'more... okay, that should do it.',
() => throw new TestFailure('oh no'));""", """
+0: loading test.dart
+0: really ... longer than that. No, yet longer. A little more... okay, that should do it.
+0 -1: really gosh dang long test name. Even longer than that. No, yet longer. A little more... okay, that should do it. [E]
+0: really ... than that. No, yet longer. Even more. We have to get to at least 200 characters. I know that seems like a lot, but I believe in you. A little more... okay, that should do it.
+0 -1: really gosh dang long test name. Even longer than that. No, yet longer. Even more. We have to get to at least 200 characters. I know that seems like a lot, but I believe in you. A little more... okay, that should do it. [E]
oh no
test.dart 9:18 main.<fn>
test.dart 11:18 main.<fn>
+0 -1: Some tests failed.""");
Expand Down Expand Up @@ -268,14 +270,16 @@ void main() {
return _expectReport("""
test(
'really gosh dang long test name. Even longer than that. No, yet '
'longer. A little more... okay, that should do it.',
'longer. Even more. We have to get to at least 200 '
'characters. I know that seems like a lot, but I believe in '
'you. A little more... okay, that should do it.',
() => print('hello'));""", """
+0: loading test.dart
+0: really ... longer than that. No, yet longer. A little more... okay, that should do it.
+0: really gosh dang long test name. Even longer than that. No, yet longer. A little more... okay, that should do it.
+0: really ... than that. No, yet longer. Even more. We have to get to at least 200 characters. I know that seems like a lot, but I believe in you. A little more... okay, that should do it.
+0: really gosh dang long test name. Even longer than that. No, yet longer. Even more. We have to get to at least 200 characters. I know that seems like a lot, but I believe in you. A little more... okay, that should do it.
hello
+1: really ... longer than that. No, yet longer. A little more... okay, that should do it.
+1: really ... than that. No, yet longer. Even more. We have to get to at least 200 characters. I know that seems like a lot, but I believe in you. A little more... okay, that should do it.
+1: All tests passed!""");
});

Expand Down
6 changes: 3 additions & 3 deletions test/runner/configuration/top_level_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ void main() {
"",
startsWith("Observatory URL: "),
startsWith("Remote debugger URL: "),
"The test runner is paused. Open the remote debugger or the Observatory "
"and set breakpoints. Once",
"you're finished, return to this terminal and press Enter."
"The test runner is paused. Open the remote debugger or the "
"Observatory and set breakpoints. Once you're finished, return "
"to this terminal and press Enter."
]));

var nextLineFired = false;
Expand Down
32 changes: 16 additions & 16 deletions test/runner/pause_after_load_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ void main() {
emitsThrough(emitsInOrder([
startsWith("Observatory URL: "),
"The test runner is paused. Open the dev console in Dartium or the "
"Observatory and set breakpoints.",
"Once you're finished, return to this terminal and press Enter."
"Observatory and set breakpoints. Once you're finished, return "
"to this terminal and press Enter."
])));

var nextLineFired = false;
Expand All @@ -67,8 +67,8 @@ void main() {
emitsThrough(emitsInOrder([
startsWith("Observatory URL: "),
"The test runner is paused. Open the dev console in Dartium or the "
"Observatory and set breakpoints.",
"Once you're finished, return to this terminal and press Enter."
"Observatory and set breakpoints. Once you're finished, return "
"to this terminal and press Enter."
])));

nextLineFired = false;
Expand Down Expand Up @@ -108,8 +108,8 @@ void main() {
emitsThrough(emitsInOrder([
startsWith("Observatory URL: "),
"The test runner is paused. Open the dev console in Dartium or the "
"Observatory and set breakpoints.",
"Once you're finished, return to this terminal and press Enter."
"Observatory and set breakpoints. Once you're finished, return "
"to this terminal and press Enter."
])));

var nextLineFired = false;
Expand All @@ -130,8 +130,8 @@ void main() {
test.stdout,
emitsThrough(emitsInOrder([
"The test runner is paused. Open the dev console in Chrome and set "
"breakpoints. Once you're finished,",
"return to this terminal and press Enter."
"breakpoints. Once you're finished, return to this terminal and "
"press Enter."
])));

nextLineFired = false;
Expand Down Expand Up @@ -191,8 +191,8 @@ void main() {
emitsThrough(emitsInOrder([
startsWith("Observatory URL: "),
"The test runner is paused. Open the dev console in Dartium or the "
"Observatory and set breakpoints.",
"Once you're finished, return to this terminal and press Enter."
"Observatory and set breakpoints. Once you're finished, return "
"to this terminal and press Enter."
])));

var nextLineFired = false;
Expand Down Expand Up @@ -234,8 +234,8 @@ void main() {
emitsThrough(emitsInOrder([
startsWith("Observatory URL: "),
"The test runner is paused. Open the dev console in Dartium or the "
"Observatory and set breakpoints.",
"Once you're finished, return to this terminal and press Enter."
"Observatory and set breakpoints. Once you're finished, return "
"to this terminal and press Enter."
])));

test.signal(ProcessSignal.SIGTERM);
Expand Down Expand Up @@ -266,8 +266,8 @@ void main() {
emitsThrough(emitsInOrder([
startsWith("Observatory URL: "),
"The test runner is paused. Open the dev console in Dartium or the "
"Observatory and set breakpoints.",
"Once you're finished, return to this terminal and press Enter."
"Observatory and set breakpoints. Once you're finished, return "
"to this terminal and press Enter."
])));

var nextLineFired = false;
Expand Down Expand Up @@ -309,8 +309,8 @@ void main() {
emitsThrough(emitsInOrder([
startsWith("Observatory URL: "),
"The test runner is paused. Open the dev console in Dartium or the "
"Observatory and set breakpoints.",
"Once you're finished, return to this terminal and press Enter."
"Observatory and set breakpoints. Once you're finished, return "
"to this terminal and press Enter."
])));

var nextLineFired = false;
Expand Down

0 comments on commit 28dfb44

Please sign in to comment.