Skip to content

Commit b4ef80c

Browse files
Fix compact reporter crash when the terminal is too narrow (#2799)
Resolves the crash reported in flutter/flutter#194569 Truncate to just an ellipsis when the width is very small.
1 parent e3a7f2b commit b4ef80c

4 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎pkgs/test/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## 1.34.0-wip
22

33
* Enable asserts in the `cli` compiler if supported.
4+
* Fix a `RangeError` crash in the compact reporter when the terminal is too
5+
narrow to fit a test's description.
46
* Fix loading precompiled browser tests when the precompiled directory contains
57
symlinks to files outside of it, such as the output of `build_runner test`.
68
Requests whose path would resolve outside the precompiled directory are
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2026, 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+
import 'package:test/test.dart';
6+
import 'package:test_core/src/util/pretty_print.dart';
7+
8+
void main() {
9+
group('truncate', () {
10+
test('returns text that fits unchanged', () {
11+
expect(truncate('a short message', 15), 'a short message');
12+
});
13+
14+
test('keeps the first word and the last words when they fit', () {
15+
expect(truncate('first a b c d e f g h i j', 20), 'first ... f g h i j');
16+
});
17+
18+
test('keeps the end of text that has no spaces', () {
19+
expect(truncate('abcdefghijklmnop', 10), '...klmnop');
20+
});
21+
22+
// A narrow terminal can leave little or no room for the test description
23+
// once the compact reporter's progress prefix is written.
24+
group('when there is no room for the text', () {
25+
const description =
26+
'AppState Model and Discovery State Tests 3. Clearing search '
27+
'restores results';
28+
29+
// Returns as much of the three character `...` ellipsis as fits, and
30+
// never more than the ellipsis.
31+
for (var maxLength in [4, 3, 2, 1, 0, -10]) {
32+
test('maxLength $maxLength', () {
33+
expect(truncate(description, maxLength), '.' * maxLength.clamp(0, 3));
34+
});
35+
}
36+
});
37+
});
38+
}

‎pkgs/test_core/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## 0.6.22-wip
22

33
* Enable asserts in the `cli` compiler if supported.
4+
* Fix a `RangeError` crash in the compact reporter when the terminal is too
5+
narrow to fit a test's description.
46
* Add `PlatformPlugin.remainingLoadTime` so platforms can time out a step of
57
loading a suite before the suite load timeout cuts it off.
68

‎pkgs/test_core/lib/src/util/pretty_print.dart‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@ String truncate(String text, int maxLength) {
3737
// Return the full message if it fits.
3838
if (text.length <= maxLength) return text;
3939

40+
// The fallbacks below need at least `ellipsis.length` characters. With less
41+
// room, as in a very narrow terminal, return as much of the ellipsis as fits.
42+
const ellipsis = ' ...';
43+
if (maxLength < ellipsis.length) return '.' * maxLength;
44+
4045
// If we can fit the first and last three words, do so.
4146
var words = text.split(' ');
4247
if (words.length > 1) {
4348
var i = words.length;
44-
var length = words.first.length + 4;
49+
var length = words.first.length + ellipsis.length;
4550
do {
4651
i--;
4752
length += 1 + words[i].length;
@@ -51,7 +56,7 @@ String truncate(String text, int maxLength) {
5156
// Require at least 3 words at the end.
5257
var buffer = StringBuffer();
5358
buffer.write(words.first);
54-
buffer.write(' ...');
59+
buffer.write(ellipsis);
5560
for (; i < words.length; i++) {
5661
buffer.write(' ');
5762
buffer.write(words[i]);
@@ -62,7 +67,7 @@ String truncate(String text, int maxLength) {
6267

6368
// Otherwise truncate to return the trailing text, but attempt to start at
6469
// the beginning of a word.
65-
var result = text.substring(text.length - maxLength + 4);
70+
var result = text.substring(text.length - maxLength + ellipsis.length);
6671
var firstSpace = result.indexOf(' ');
6772
if (firstSpace > 0) {
6873
result = result.substring(firstSpace);

0 commit comments

Comments
 (0)