flutter/packages/flutter_tools/test/base/terminal_test.dart
Greg Spencer 081d2a7a86
Re-land text wrapping/color PR (#22831)
This attempts to re-land #22656.

There are two changes from the original:

I turned off wrapping completely when not sending output to a terminal. Previously I had defaulted to wrapping at and arbitrary 100 chars in that case, just to keep long messages from being too long, but that turns out the be a bad idea because there are tests that are relying on the specific form of the output. It's also pretty arbitrary, and mostly people sending output to a non-terminal will want unwrapped text.

I found a better way to terminate ANSI color/bold sequences, so that they can be embedded within each other without needed quite as complex a dance with removing redundant sequences.

As part of these changes, I removed the Logger.supportsColor setter so that the one source of truth for color support is in AnsiTerminal.supportsColor.

*     Turn on line wrapping again in usage and status messages, adds ANSI color to doctor and analysis messages. (#22656)

    This turns on text wrapping for usage messages and status messages. When on a terminal, wraps to the width of the terminal. When writing to a non-terminal, wrap lines at a default column width (currently defined to be 100 chars). If --no-wrap is specified, then no wrapping occurs. If --wrap-column is specified, wraps to that column (if --wrap is on).

    Adds ANSI color to the doctor and analysis output on terminals. This is in this PR with the wrapping, since wrapping needs to know how to count visible characters in the presence of ANSI sequences. (This is just one more step towards re-implementing all of Curses for Flutter. :-)) Will not print ANSI sequences when sent to a non-terminal, or of --no-color is specified.

    Fixes ANSI color and bold sequences so that they can be combined (bold, colored text), and a small bug in indentation calculation for wrapping.

    Since wrapping is now turned on, also removed many redundant '\n's in the code.
2018-10-10 18:17:56 -07:00

172 lines
6.4 KiB
Dart

// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/terminal.dart';
import 'package:flutter_tools/src/globals.dart';
import '../src/common.dart';
import '../src/context.dart';
void main() {
group('output preferences', () {
testUsingContext('can wrap output', () async {
printStatus('0123456789' * 8);
expect(testLogger.statusText, equals(('0123456789' * 4 + '\n') * 2));
}, overrides: <Type, Generator>{
OutputPreferences: () => OutputPreferences(wrapText: true, wrapColumn: 40),
});
testUsingContext('can turn off wrapping', () async {
final String testString = '0123456789' * 20;
printStatus(testString);
expect(testLogger.statusText, equals('$testString\n'));
}, overrides: <Type, Generator>{
Platform: () => FakePlatform()..stdoutSupportsAnsi = true,
OutputPreferences: () => OutputPreferences(wrapText: false),
});
});
group('ANSI coloring and bold', () {
AnsiTerminal terminal;
setUp(() {
terminal = AnsiTerminal();
});
testUsingContext('adding colors works', () {
for (TerminalColor color in TerminalColor.values) {
expect(
terminal.color('output', color),
equals('${AnsiTerminal.colorCode(color)}output${AnsiTerminal.resetColor}'),
);
}
}, overrides: <Type, Generator> {
OutputPreferences: () => OutputPreferences(showColor: true),
Platform: () => FakePlatform()..stdoutSupportsAnsi = true,
});
testUsingContext('adding bold works', () {
expect(
terminal.bolden('output'),
equals('${AnsiTerminal.bold}output${AnsiTerminal.resetBold}'),
);
}, overrides: <Type, Generator> {
OutputPreferences: () => OutputPreferences(showColor: true),
Platform: () => FakePlatform()..stdoutSupportsAnsi = true,
});
testUsingContext('nesting bold within color works', () {
expect(
terminal.color(terminal.bolden('output'), TerminalColor.blue),
equals('${AnsiTerminal.blue}${AnsiTerminal.bold}output${AnsiTerminal.resetBold}${AnsiTerminal.resetColor}'),
);
expect(
terminal.color('non-bold ${terminal.bolden('output')} also non-bold', TerminalColor.blue),
equals('${AnsiTerminal.blue}non-bold ${AnsiTerminal.bold}output${AnsiTerminal.resetBold} also non-bold${AnsiTerminal.resetColor}'),
);
}, overrides: <Type, Generator> {
OutputPreferences: () => OutputPreferences(showColor: true),
Platform: () => FakePlatform()..stdoutSupportsAnsi = true,
});
testUsingContext('nesting color within bold works', () {
expect(
terminal.bolden(terminal.color('output', TerminalColor.blue)),
equals('${AnsiTerminal.bold}${AnsiTerminal.blue}output${AnsiTerminal.resetColor}${AnsiTerminal.resetBold}'),
);
expect(
terminal.bolden('non-color ${terminal.color('output', TerminalColor.blue)} also non-color'),
equals('${AnsiTerminal.bold}non-color ${AnsiTerminal.blue}output${AnsiTerminal.resetColor} also non-color${AnsiTerminal.resetBold}'),
);
}, overrides: <Type, Generator> {
OutputPreferences: () => OutputPreferences(showColor: true),
Platform: () => FakePlatform()..stdoutSupportsAnsi = true,
});
testUsingContext('nesting color within color works', () {
expect(
terminal.color(terminal.color('output', TerminalColor.blue), TerminalColor.magenta),
equals('${AnsiTerminal.magenta}${AnsiTerminal.blue}output${AnsiTerminal.resetColor}${AnsiTerminal.magenta}${AnsiTerminal.resetColor}'),
);
expect(
terminal.color('magenta ${terminal.color('output', TerminalColor.blue)} also magenta', TerminalColor.magenta),
equals('${AnsiTerminal.magenta}magenta ${AnsiTerminal.blue}output${AnsiTerminal.resetColor}${AnsiTerminal.magenta} also magenta${AnsiTerminal.resetColor}'),
);
}, overrides: <Type, Generator> {
OutputPreferences: () => OutputPreferences(showColor: true),
Platform: () => FakePlatform()..stdoutSupportsAnsi = true,
});
testUsingContext('nesting bold within bold works', () {
expect(
terminal.bolden(terminal.bolden('output')),
equals('${AnsiTerminal.bold}output${AnsiTerminal.resetBold}'),
);
expect(
terminal.bolden('bold ${terminal.bolden('output')} still bold'),
equals('${AnsiTerminal.bold}bold output still bold${AnsiTerminal.resetBold}'),
);
}, overrides: <Type, Generator> {
OutputPreferences: () => OutputPreferences(showColor: true),
Platform: () => FakePlatform()..stdoutSupportsAnsi = true,
});
});
group('character input prompt', () {
AnsiTerminal terminalUnderTest;
setUp(() {
terminalUnderTest = TestTerminal();
});
testUsingContext('character prompt', () async {
mockStdInStream = Stream<String>.fromFutures(<Future<String>>[
Future<String>.value('d'), // Not in accepted list.
Future<String>.value('\n'), // Not in accepted list
Future<String>.value('b'),
]).asBroadcastStream();
final String choice = await terminalUnderTest.promptForCharInput(
<String>['a', 'b', 'c'],
prompt: 'Please choose something',
);
expect(choice, 'b');
expect(
testLogger.statusText,
'Please choose something [a|b|c]: d\n'
'Please choose something [a|b|c]: \n'
'\n'
'Please choose something [a|b|c]: b\n');
});
testUsingContext('default character choice without displayAcceptedCharacters', () async {
mockStdInStream = Stream<String>.fromFutures(<Future<String>>[
Future<String>.value('\n'), // Not in accepted list
]).asBroadcastStream();
final String choice = await terminalUnderTest.promptForCharInput(
<String>['a', 'b', 'c'],
prompt: 'Please choose something',
displayAcceptedCharacters: false,
defaultChoiceIndex: 1, // which is b.
);
expect(choice, 'b');
expect(
testLogger.statusText,
'Please choose something: \n'
'\n');
});
});
}
Stream<String> mockStdInStream;
class TestTerminal extends AnsiTerminal {
@override
Stream<String> get onCharInput {
return mockStdInStream;
}
}