mirror of
https://github.com/flutter/packages.git
synced 2025-08-06 17:28:42 +08:00

Refactors colorization to a centralized utility file, and makes it all conditional on `stdout` having ANSI escape support. This makes the output more readable on LUCI, which unlike the Cirrus log display doesn't handle ANSI. Fixes https://github.com/flutter/flutter/issues/89392
101 lines
2.6 KiB
Dart
101 lines
2.6 KiB
Dart
// Copyright 2013 The Flutter 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 'dart:io';
|
|
|
|
import 'package:flutter_plugin_tools/src/common/output_utils.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
void main() {
|
|
group('with color support', () {
|
|
setUp(() {
|
|
useColorForOutput = true;
|
|
});
|
|
|
|
tearDown(() {
|
|
useColorForOutput = stdout.supportsAnsiEscapes;
|
|
});
|
|
|
|
test('colorize works', () async {
|
|
const String message = 'a message';
|
|
|
|
expect(
|
|
colorizeString(message, Styles.MAGENTA), '\x1B[35m$message\x1B[0m');
|
|
});
|
|
|
|
test('printSuccess is green', () async {
|
|
const String message = 'a message';
|
|
|
|
expect(await _capturePrint(() => printSuccess(message)),
|
|
'\x1B[32m$message\x1B[0m');
|
|
});
|
|
|
|
test('printWarning is yellow', () async {
|
|
const String message = 'a message';
|
|
|
|
expect(await _capturePrint(() => printWarning(message)),
|
|
'\x1B[33m$message\x1B[0m');
|
|
});
|
|
|
|
test('printError is red', () async {
|
|
const String message = 'a message';
|
|
|
|
expect(await _capturePrint(() => printError(message)),
|
|
'\x1B[31m$message\x1B[0m');
|
|
});
|
|
});
|
|
|
|
group('without color support', () {
|
|
setUp(() {
|
|
useColorForOutput = false;
|
|
});
|
|
|
|
tearDown(() {
|
|
useColorForOutput = stdout.supportsAnsiEscapes;
|
|
});
|
|
|
|
test('colorize no-ops', () async {
|
|
const String message = 'a message';
|
|
|
|
expect(colorizeString(message, Styles.MAGENTA), message);
|
|
});
|
|
|
|
test('printSuccess just prints', () async {
|
|
const String message = 'a message';
|
|
|
|
expect(await _capturePrint(() => printSuccess(message)), message);
|
|
});
|
|
|
|
test('printWarning just prints', () async {
|
|
const String message = 'a message';
|
|
|
|
expect(await _capturePrint(() => printWarning(message)), message);
|
|
});
|
|
|
|
test('printError just prints', () async {
|
|
const String message = 'a message';
|
|
|
|
expect(await _capturePrint(() => printError(message)), message);
|
|
});
|
|
});
|
|
}
|
|
|
|
/// Run the command [runner] with the given [args] and return
|
|
/// what was printed.
|
|
/// A custom [errorHandler] can be used to handle the runner error as desired without throwing.
|
|
Future<String> _capturePrint(void Function() printFunction) async {
|
|
final StringBuffer output = StringBuffer();
|
|
final ZoneSpecification spec = ZoneSpecification(
|
|
print: (_, __, ___, String message) {
|
|
output.write(message);
|
|
},
|
|
);
|
|
await Zone.current
|
|
.fork(specification: spec)
|
|
.run<Future<void>>(() async => printFunction());
|
|
|
|
return output.toString();
|
|
}
|